Skip to content

Commit

Permalink
[TH2-5226] corrected after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Smirnov-Exactpro committed Sep 16, 2024
1 parent 0187cd0 commit 20ef110
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static String extractHashedName(Th2CustomResource customResource) {
return hashNameIfNeeded(extractName(customResource));
}

public static String hashNameIfNeeded(String resName) {
private static String hashNameIfNeeded(String resName) {
if (resName.length() >= HelmRelease.NAME_LENGTH_LIMIT) {
String result = digest(resName);
logger.debug("Resource '{}' name has been hashed to '{}'", resName, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import com.exactpro.th2.infraoperator.spec.strategy.linkresolver.createMstoreQue
import com.exactpro.th2.infraoperator.spec.strategy.linkresolver.mq.RabbitMQContext.DIRECT
import com.exactpro.th2.infraoperator.spec.strategy.linkresolver.mq.RabbitMQContext.TOPIC
import com.exactpro.th2.infraoperator.spec.strategy.linkresolver.mq.RabbitMQContext.toExchangeName
import com.exactpro.th2.infraoperator.util.CustomResourceUtils.hashNameIfNeeded
import com.exactpro.th2.infraoperator.util.CustomResourceUtils.extractHashedName
import com.exactpro.th2.infraoperator.util.JsonUtils.JSON_MAPPER
import com.exactpro.th2.infraoperator.util.JsonUtils.YAML_MAPPER
import com.exactpro.th2.infraoperator.util.createKubernetesClient
Expand Down Expand Up @@ -185,11 +185,11 @@ class IntegrationTest {
kubeClient.createRabbitMQAppConfigCfgMap(
TH2_NAMESPACE,
gitHash,
createRabbitMQConfig(rabbitMQContainer, TH2_NAMESPACE)
createRabbitMQConfig(rabbitMQContainer)
)

rabbitMQClient.assertUser(TH2_NAMESPACE, RABBIT_MQ_V_HOST, RABBIT_MQ_NAMESPACE_PERMISSIONS)
rabbitMQClient.assertExchange(toExchangeName(TH2_NAMESPACE), DIRECT, RABBIT_MQ_V_HOST)
rabbitMQClient.assertExchange(RABBIT_MQ_TH2_EXCHANGE, DIRECT, RABBIT_MQ_V_HOST)
rabbitMQClient.assertQueue(createMstoreQueue(TH2_NAMESPACE), RABBIT_MQ_QUEUE_CLASSIC_TYPE, RABBIT_MQ_V_HOST)
rabbitMQClient.assertQueue(createEstoreQueue(TH2_NAMESPACE), RABBIT_MQ_QUEUE_CLASSIC_TYPE, RABBIT_MQ_V_HOST)

Expand Down Expand Up @@ -252,9 +252,9 @@ class IntegrationTest {
type: $specType
""".trimIndent()

kubeClient.createTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, this::createResources)
val resource = kubeClient.createTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, this::createResources)
kubeClient.awaitPhase(TH2_NAMESPACE, name, SUCCEEDED, resourceClass)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, hashNameIfNeeded(name)).assertMinCfg(name, runAsJob)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, extractHashedName(resource)).assertMinCfg(name, runAsJob)
rabbitMQClient.assertBindings(
createEstoreQueue(TH2_NAMESPACE),
RABBIT_MQ_V_HOST,
Expand All @@ -274,9 +274,9 @@ class IntegrationTest {
disabled: false
""".trimIndent()

kubeClient.createTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, this::createResources)
val resource = kubeClient.createTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, this::createResources)
kubeClient.awaitPhase(TH2_NAMESPACE, name, SUCCEEDED, resourceClass)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, hashNameIfNeeded(name)).assertMinCfg(name, runAsJob)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, extractHashedName(resource)).assertMinCfg(name, runAsJob)
rabbitMQClient.assertBindings(
createEstoreQueue(TH2_NAMESPACE),
RABBIT_MQ_V_HOST,
Expand Down Expand Up @@ -333,9 +333,9 @@ class IntegrationTest {
disabled: false
""".trimIndent()

kubeClient.modifyTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, resourceClass)
val resource = kubeClient.modifyTh2CustomResource(TH2_NAMESPACE, name, gitHash, spec, resourceClass)
kubeClient.awaitPhase(TH2_NAMESPACE, name, SUCCEEDED, resourceClass)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, hashNameIfNeeded(name)).assertMinCfg(name, runAsJob)
kubeClient.awaitResource<HelmRelease>(TH2_NAMESPACE, extractHashedName(resource)).assertMinCfg(name, runAsJob)
rabbitMQClient.assertBindings(
createEstoreQueue(TH2_NAMESPACE),
RABBIT_MQ_V_HOST,
Expand Down Expand Up @@ -396,7 +396,7 @@ class IntegrationTest {
queues = mapOf(
PUBLISH_PIN to mapOf(
"attributes" to listOf("publish"),
"exchange" to toExchangeName(TH2_NAMESPACE),
"exchange" to RABBIT_MQ_TH2_EXCHANGE,
"filters" to emptyList<String>(),
"name" to routingKey,
"queue" to "",
Expand All @@ -409,7 +409,7 @@ class IntegrationTest {
queues = mapOf(
SUBSCRIBE_PIN to mapOf(
"attributes" to listOf("subscribe"),
"exchange" to toExchangeName(TH2_NAMESPACE),
"exchange" to RABBIT_MQ_TH2_EXCHANGE,
"filters" to emptyList<String>(),
"name" to "",
"queue" to queueName,
Expand Down Expand Up @@ -742,23 +742,27 @@ class IntegrationTest {
"th2-mstore-crd.yaml",
)

private const val TH2_PREFIX = "th2-"
private const val TH2_NAMESPACE = "${TH2_PREFIX}test"
private const val TH2_BOOK = "test_book"

private val RABBIT_MQ_NAMESPACE_PERMISSIONS = RabbitMQNamespacePermissions()
private const val RABBIT_MQ_QUEUE_CLASSIC_TYPE = "classic"
private const val RABBIT_MQ_V_HOST = "/"
private const val RABBIT_MQ_TOPIC_EXCHANGE = "test-global-exchange"
private val RABBIT_MQ_TH2_EXCHANGE = toExchangeName(TH2_NAMESPACE)

private const val TH2_PREFIX = "th2-"
private const val TH2_NAMESPACE = "${TH2_PREFIX}test"
private const val TH2_BOOK = "test_book"
private const val PUBLISH_PIN = "test-publish-pin"
private const val SUBSCRIBE_PIN = "test-subscribe-pin"

private const val SERVER_PIN = "test-server-pin"
private const val CLIENT_PIN = "test-client-pin"
private const val GRPC_SERVICE = "com.exactpro.th2.test.grpc.TestService"

private const val IMAGE = "ghcr.io/th2-net/th2-estore"
private const val VERSION = "0.0.0"
private const val DICTIONARY_CONTENT = "test-dictionary-content"

private const val DICTIONARY_CONTENT = "test-dictionary-content"
private const val TEST_CONTENT = "test-content"

@JvmStatic
Expand Down Expand Up @@ -794,13 +798,12 @@ class IntegrationTest {

private fun createRabbitMQConfig(
rabbitMQ: RabbitMQContainer,
namespace: String,
) = RabbitMQConfig(
rabbitMQ.amqpPort,
rabbitMQ.host,
RABBIT_MQ_V_HOST,
toExchangeName(namespace),
namespace,
RABBIT_MQ_TH2_EXCHANGE,
TH2_NAMESPACE,
"${'$'}{RABBITMQ_PASS}",
)

Expand Down Expand Up @@ -982,7 +985,7 @@ class IntegrationTest {
getValue(EVENT_STORAGE_PIN_ALIAS).isA<Map<String, Any?>>().and {
hasSize(5)
getValue("attributes").isA<List<String>>() isEqualTo listOf("publish", "event")
getValue("exchange") isEqualTo toExchangeName(TH2_NAMESPACE)
getValue("exchange") isEqualTo RABBIT_MQ_TH2_EXCHANGE
getValue("filters").isA<List<String>>().isEmpty() // FIXME
getValue("name") isEqualTo "key[$TH2_NAMESPACE:$name:$EVENT_STORAGE_PIN_ALIAS]"
getValue("queue").isA<String>().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ fun KubernetesClient.createSecret(
).create()
}

fun KubernetesClient.deleteSecret(
namespace: String,
name: String,
timeout: Long = 200,
unit: TimeUnit = TimeUnit.MILLISECONDS,
) {
secrets()
.inNamespace(namespace)
?.withName(name)
?.awaitDeleteResource("deleteSecret('$namespace/$name')", timeout, unit)
}

fun KubernetesClient.createConfigMap(
namespace: String,
name: String,
Expand Down Expand Up @@ -147,18 +135,6 @@ inline fun <reified T: HasMetadata> KubernetesClient.awaitNoResource(
.until { resources(T::class.java).inNamespace(namespace).withName(name).get() == null }
}

fun KubernetesClient.deleteConfigMap(
namespace: String,
name: String,
timeout: Long = 200,
unit: TimeUnit = TimeUnit.MILLISECONDS,
) {
configMaps()
.inNamespace(namespace)
?.withName(name)
?.awaitDeleteResource("deleteConfigMap('$namespace/$name')", timeout, unit)
}

inline fun <reified T: Th2CustomResource> KubernetesClient.awaitPhase(
namespace: String,
name: String,
Expand All @@ -180,44 +156,33 @@ fun KubernetesClient.awaitPhase(
.until { resources(resourceType)?.inNamespace(namespace)?.withName(name)?.get()?.status?.phase == phase }
}

fun KubernetesClient.createTh2CustomResource(
fun <T: Th2CustomResource> KubernetesClient.createTh2CustomResource(
namespace: String,
name: String,
gitHash: String,
spec: String,
create: () -> Th2CustomResource,
) {
resource(
create().apply {
this.metadata = createMeta(name, namespace, createAnnotations(gitHash, spec.hashCode().toString()))
this.spec = YAML_MAPPER.readValue(spec, Th2Spec::class.java)
}
).create()
create: () -> T,
): T = create().apply {
this.metadata = createMeta(name, namespace, createAnnotations(gitHash, spec.hashCode().toString()))
this.spec = YAML_MAPPER.readValue(spec, Th2Spec::class.java)
}.also {
resource(it).create()
}

fun KubernetesClient.modifyTh2CustomResource(
fun <T: Th2CustomResource> KubernetesClient.modifyTh2CustomResource(
namespace: String,
name: String,
gitHash: String,
spec: String,
resourceType: Class<out Th2CustomResource>,
) {
resource(
resources(resourceType).inNamespace(namespace).withName(name).get().apply {
this.metadata.annotations.putAll(createAnnotations(gitHash, spec.hashCode().toString()))
this.metadata.generation += 1
this.spec = YAML_MAPPER.readValue(spec, Th2Spec::class.java)
}
).update()
resourceType: Class<T>,
): T = resources(resourceType).inNamespace(namespace).withName(name).get().apply {
this.metadata.annotations.putAll(createAnnotations(gitHash, spec.hashCode().toString()))
this.metadata.generation += 1
this.spec = YAML_MAPPER.readValue(spec, Th2Spec::class.java)
}.also {
resource(it).update()
}

inline fun <reified T: Th2CustomResource> KubernetesClient.modifyTh2CustomResource(
namespace: String,
name: String,
gitHash: String,
spec: String
) = modifyTh2CustomResource(namespace, name, gitHash, spec, T::class.java)

fun KubernetesClient.createTh2Dictionary(
namespace: String,
name: String,
Expand Down

0 comments on commit 20ef110

Please sign in to comment.