Search in sources :

Example 1 with ServiceAccountOperator

use of io.strimzi.operator.common.operator.resource.ServiceAccountOperator in project strimzi by strimzi.

the class KafkaConnectBuildAssemblyOperatorKubeTest method testBuildOnKube.

@SuppressWarnings({ "checkstyle:MethodLength" })
@Test
public void testBuildOnKube(VertxTestContext context) {
    Plugin plugin1 = new PluginBuilder().withName("plugin1").withArtifacts(new JarArtifactBuilder().withUrl("https://my-domain.tld/my.jar").build()).build();
    KafkaConnect kc = new KafkaConnectBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withReplicas(1).withBootstrapServers("my-cluster-kafka-bootstrap:9092").withNewBuild().withNewDockerOutput().withImage(OUTPUT_IMAGE).withPushSecret("my-docker-credentials").endDockerOutput().withPlugins(plugin1).endBuild().endSpec().build();
    KafkaConnectBuild build = KafkaConnectBuild.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kc, VERSIONS);
    // Prepare and get mocks
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
    CrdOperator mockConnectOps = supplier.connectOperator;
    DeploymentOperator mockDepOps = supplier.deploymentOperations;
    PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
    PodDisruptionBudgetV1Beta1Operator mockPdbOpsV1Beta1 = supplier.podDisruptionBudgetV1Beta1Operator;
    ConfigMapOperator mockCmOps = supplier.configMapOperations;
    ServiceOperator mockServiceOps = supplier.serviceOperations;
    NetworkPolicyOperator mockNetPolOps = supplier.networkPolicyOperator;
    PodOperator mockPodOps = supplier.podOperations;
    BuildConfigOperator mockBcOps = supplier.buildConfigOperations;
    SecretOperator mockSecretOps = supplier.secretOperations;
    ServiceAccountOperator mockSaOps = supplier.serviceAccountOperations;
    CrdOperator<KubernetesClient, KafkaConnector, KafkaConnectorList> mockConnectorOps = supplier.kafkaConnectorOperator;
    // Mock KafkaConnector ops
    when(mockConnectorOps.listAsync(anyString(), any(Optional.class))).thenReturn(Future.succeededFuture(emptyList()));
    // Mock KafkaConnect ops
    when(mockConnectOps.get(NAMESPACE, NAME)).thenReturn(kc);
    when(mockConnectOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(kc));
    // Mock and capture service ops
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(any(), anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock and capture deployment ops
    ArgumentCaptor<Deployment> depCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDepOps.reconcile(any(), anyString(), anyString(), depCaptor.capture())).thenReturn(Future.succeededFuture());
    when(mockDepOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockDepOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockDepOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockDepOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(mockDepOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    // Mock and capture CM ops
    when(mockCmOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
    ArgumentCaptor<ConfigMap> dockerfileCaptor = ArgumentCaptor.forClass(ConfigMap.class);
    when(mockCmOps.reconcile(any(), anyString(), eq(KafkaConnectResources.dockerFileConfigMapName(NAME)), dockerfileCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
    // Mock and capture SA ops
    ArgumentCaptor<ServiceAccount> saCaptor = ArgumentCaptor.forClass(ServiceAccount.class);
    when(mockSaOps.reconcile(any(), anyString(), anyString(), saCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ServiceAccount())));
    // Mock and capture Pod ops
    ArgumentCaptor<Pod> builderPodCaptor = ArgumentCaptor.forClass(Pod.class);
    when(mockPodOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)), builderPodCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.noop(null)));
    Pod terminatedPod = new PodBuilder().withNewMetadata().withName(KafkaConnectResources.buildPodName(NAME)).withNamespace(NAMESPACE).endMetadata().withNewSpec().endSpec().withNewStatus().withContainerStatuses(new ContainerStatusBuilder().withNewState().withNewTerminated().withExitCode(0).withMessage("my-connect-build@sha256:blablabla").endTerminated().endState().build()).endStatus().build();
    when(mockPodOps.waitFor(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)), anyString(), anyLong(), anyLong(), any(BiPredicate.class))).thenReturn(Future.succeededFuture((Void) null));
    when(mockPodOps.getAsync(eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)))).thenReturn(Future.succeededFuture(null), Future.succeededFuture(terminatedPod));
    // Mock and capture BuildConfig ops
    when(mockBcOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildConfigName(NAME)), eq(null))).thenReturn(Future.succeededFuture(ReconcileResult.noop(null)));
    // Mock and capture NP ops
    when(mockNetPolOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.deploymentName(NAME)), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new NetworkPolicy())));
    // Mock and capture PDB ops
    when(mockPdbOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
    when(mockPdbOpsV1Beta1.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
    // Mock and capture KafkaConnect ops for status update
    ArgumentCaptor<KafkaConnect> connectCaptor = ArgumentCaptor.forClass(KafkaConnect.class);
    when(mockConnectOps.updateStatusAsync(any(), connectCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock KafkaConnect API client
    KafkaConnectApi mockConnectClient = mock(KafkaConnectApi.class);
    // Prepare and run reconciliation
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS), x -> mockConnectClient);
    KafkaConnectCluster connect = KafkaConnectCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kc, VERSIONS);
    Checkpoint async = context.checkpoint();
    ops.reconcile(new Reconciliation("test-trigger", KafkaConnect.RESOURCE_KIND, NAMESPACE, NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
        // Verify Deployment
        List<Deployment> capturedDeps = depCaptor.getAllValues();
        assertThat(capturedDeps, hasSize(1));
        Deployment dep = capturedDeps.get(0);
        assertThat(dep.getMetadata().getName(), is(connect.getName()));
        assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(0).getImage(), is("my-connect-build@sha256:blablabla"));
        assertThat(Annotations.stringAnnotation(dep.getSpec().getTemplate(), Annotations.STRIMZI_IO_CONNECT_BUILD_REVISION, null), is(build.generateDockerfile().hashStub() + OUTPUT_IMAGE_HASH_STUB));
        // Verify ConfigMap
        List<ConfigMap> capturedCms = dockerfileCaptor.getAllValues();
        assertThat(capturedCms, hasSize(1));
        ConfigMap dockerfileCm = capturedCms.get(0);
        assertThat(dockerfileCm.getData().containsKey("Dockerfile"), is(true));
        assertThat(dockerfileCm.getData().get("Dockerfile"), is(build.generateDockerfile().getDockerfile()));
        // Verify Service Account
        List<ServiceAccount> capturedSas = saCaptor.getAllValues();
        assertThat(capturedSas, hasSize(2));
        ServiceAccount sa = capturedSas.get(0);
        assertThat(sa.getMetadata().getName(), is(KafkaConnectResources.serviceAccountName(NAME)));
        sa = capturedSas.get(1);
        assertThat(sa.getMetadata().getName(), is(KafkaConnectResources.buildServiceAccountName(NAME)));
        // Verify builder Pod
        List<Pod> capturedBuilderPods = builderPodCaptor.getAllValues();
        assertThat(capturedBuilderPods, hasSize(2));
        assertThat(capturedBuilderPods.stream().filter(pod -> pod != null).collect(Collectors.toList()), hasSize(1));
        // Verify status
        List<KafkaConnect> capturedConnects = connectCaptor.getAllValues();
        assertThat(capturedConnects, hasSize(1));
        KafkaConnectStatus connectStatus = capturedConnects.get(0).getStatus();
        assertThat(connectStatus.getConditions().get(0).getStatus(), is("True"));
        assertThat(connectStatus.getConditions().get(0).getType(), is("Ready"));
        async.flag();
    })));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Annotations(io.strimzi.operator.common.Annotations) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) PodDisruptionBudgetV1Beta1Operator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetV1Beta1Operator) BeforeAll(org.junit.jupiter.api.BeforeAll) KafkaConnectStatus(io.strimzi.api.kafka.model.status.KafkaConnectStatus) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) Collections.emptyList(java.util.Collections.emptyList) KafkaConnectBuild(io.strimzi.operator.cluster.model.KafkaConnectBuild) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) KafkaConnectBuilder(io.strimzi.api.kafka.model.KafkaConnectBuilder) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) KafkaConnectCluster(io.strimzi.operator.cluster.model.KafkaConnectCluster) List(java.util.List) Plugin(io.strimzi.api.kafka.model.connect.build.Plugin) Optional(java.util.Optional) Checkpoint(io.vertx.junit5.Checkpoint) PodDisruptionBudgetOperator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetOperator) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) NetworkPolicyOperator(io.strimzi.operator.common.operator.resource.NetworkPolicyOperator) PluginBuilder(io.strimzi.api.kafka.model.connect.build.PluginBuilder) ServiceOperator(io.strimzi.operator.common.operator.resource.ServiceOperator) BiPredicate(java.util.function.BiPredicate) ArgumentCaptor(org.mockito.ArgumentCaptor) ServiceAccountOperator(io.strimzi.operator.common.operator.resource.ServiceAccountOperator) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) BuildConfigOperator(io.strimzi.operator.common.operator.resource.BuildConfigOperator) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Collections.emptyMap(java.util.Collections.emptyMap) ContainerStatusBuilder(io.fabric8.kubernetes.api.model.ContainerStatusBuilder) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) JarArtifactBuilder(io.strimzi.api.kafka.model.connect.build.JarArtifactBuilder) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Reconciliation(io.strimzi.operator.common.Reconciliation) Util(io.strimzi.operator.common.Util) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KafkaConnectResources(io.strimzi.api.kafka.model.KafkaConnectResources) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PluginBuilder(io.strimzi.api.kafka.model.connect.build.PluginBuilder) KafkaConnectBuilder(io.strimzi.api.kafka.model.KafkaConnectBuilder) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) PodDisruptionBudgetV1Beta1Operator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetV1Beta1Operator) KafkaConnectStatus(io.strimzi.api.kafka.model.status.KafkaConnectStatus) Optional(java.util.Optional) Pod(io.fabric8.kubernetes.api.model.Pod) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) PodDisruptionBudgetOperator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetOperator) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) ContainerStatusBuilder(io.fabric8.kubernetes.api.model.ContainerStatusBuilder) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ServiceAccountOperator(io.strimzi.operator.common.operator.resource.ServiceAccountOperator) BiPredicate(java.util.function.BiPredicate) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) KafkaConnectCluster(io.strimzi.operator.cluster.model.KafkaConnectCluster) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) ServiceOperator(io.strimzi.operator.common.operator.resource.ServiceOperator) JarArtifactBuilder(io.strimzi.api.kafka.model.connect.build.JarArtifactBuilder) NetworkPolicyOperator(io.strimzi.operator.common.operator.resource.NetworkPolicyOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) KafkaConnectBuild(io.strimzi.operator.cluster.model.KafkaConnectBuild) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Service(io.fabric8.kubernetes.api.model.Service) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) Checkpoint(io.vertx.junit5.Checkpoint) BuildConfigOperator(io.strimzi.operator.common.operator.resource.BuildConfigOperator) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) Plugin(io.strimzi.api.kafka.model.connect.build.Plugin) Test(org.junit.jupiter.api.Test)

Example 2 with ServiceAccountOperator

use of io.strimzi.operator.common.operator.resource.ServiceAccountOperator in project strimzi-kafka-operator by strimzi.

the class KafkaConnectBuildAssemblyOperatorKubeTest method testBuildOnKube.

@SuppressWarnings({ "checkstyle:MethodLength" })
@Test
public void testBuildOnKube(VertxTestContext context) {
    Plugin plugin1 = new PluginBuilder().withName("plugin1").withArtifacts(new JarArtifactBuilder().withUrl("https://my-domain.tld/my.jar").build()).build();
    KafkaConnect kc = new KafkaConnectBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withReplicas(1).withBootstrapServers("my-cluster-kafka-bootstrap:9092").withNewBuild().withNewDockerOutput().withImage(OUTPUT_IMAGE).withPushSecret("my-docker-credentials").endDockerOutput().withPlugins(plugin1).endBuild().endSpec().build();
    KafkaConnectBuild build = KafkaConnectBuild.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kc, VERSIONS);
    // Prepare and get mocks
    ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(true);
    CrdOperator mockConnectOps = supplier.connectOperator;
    DeploymentOperator mockDepOps = supplier.deploymentOperations;
    PodDisruptionBudgetOperator mockPdbOps = supplier.podDisruptionBudgetOperator;
    PodDisruptionBudgetV1Beta1Operator mockPdbOpsV1Beta1 = supplier.podDisruptionBudgetV1Beta1Operator;
    ConfigMapOperator mockCmOps = supplier.configMapOperations;
    ServiceOperator mockServiceOps = supplier.serviceOperations;
    NetworkPolicyOperator mockNetPolOps = supplier.networkPolicyOperator;
    PodOperator mockPodOps = supplier.podOperations;
    BuildConfigOperator mockBcOps = supplier.buildConfigOperations;
    SecretOperator mockSecretOps = supplier.secretOperations;
    ServiceAccountOperator mockSaOps = supplier.serviceAccountOperations;
    CrdOperator<KubernetesClient, KafkaConnector, KafkaConnectorList> mockConnectorOps = supplier.kafkaConnectorOperator;
    // Mock KafkaConnector ops
    when(mockConnectorOps.listAsync(anyString(), any(Optional.class))).thenReturn(Future.succeededFuture(emptyList()));
    // Mock KafkaConnect ops
    when(mockConnectOps.get(NAMESPACE, NAME)).thenReturn(kc);
    when(mockConnectOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture(kc));
    // Mock and capture service ops
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(any(), anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock and capture deployment ops
    ArgumentCaptor<Deployment> depCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDepOps.reconcile(any(), anyString(), anyString(), depCaptor.capture())).thenReturn(Future.succeededFuture());
    when(mockDepOps.getAsync(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockDepOps.scaleUp(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockDepOps.scaleDown(any(), anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockDepOps.readiness(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(mockDepOps.waitForObserved(any(), anyString(), anyString(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    when(mockSecretOps.reconcile(any(), anyString(), anyString(), any())).thenReturn(Future.succeededFuture());
    // Mock and capture CM ops
    when(mockCmOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
    ArgumentCaptor<ConfigMap> dockerfileCaptor = ArgumentCaptor.forClass(ConfigMap.class);
    when(mockCmOps.reconcile(any(), anyString(), eq(KafkaConnectResources.dockerFileConfigMapName(NAME)), dockerfileCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ConfigMap())));
    // Mock and capture SA ops
    ArgumentCaptor<ServiceAccount> saCaptor = ArgumentCaptor.forClass(ServiceAccount.class);
    when(mockSaOps.reconcile(any(), anyString(), anyString(), saCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new ServiceAccount())));
    // Mock and capture Pod ops
    ArgumentCaptor<Pod> builderPodCaptor = ArgumentCaptor.forClass(Pod.class);
    when(mockPodOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)), builderPodCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.noop(null)));
    Pod terminatedPod = new PodBuilder().withNewMetadata().withName(KafkaConnectResources.buildPodName(NAME)).withNamespace(NAMESPACE).endMetadata().withNewSpec().endSpec().withNewStatus().withContainerStatuses(new ContainerStatusBuilder().withNewState().withNewTerminated().withExitCode(0).withMessage("my-connect-build@sha256:blablabla").endTerminated().endState().build()).endStatus().build();
    when(mockPodOps.waitFor(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)), anyString(), anyLong(), anyLong(), any(BiPredicate.class))).thenReturn(Future.succeededFuture((Void) null));
    when(mockPodOps.getAsync(eq(NAMESPACE), eq(KafkaConnectResources.buildPodName(NAME)))).thenReturn(Future.succeededFuture(null), Future.succeededFuture(terminatedPod));
    // Mock and capture BuildConfig ops
    when(mockBcOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.buildConfigName(NAME)), eq(null))).thenReturn(Future.succeededFuture(ReconcileResult.noop(null)));
    // Mock and capture NP ops
    when(mockNetPolOps.reconcile(any(), eq(NAMESPACE), eq(KafkaConnectResources.deploymentName(NAME)), any())).thenReturn(Future.succeededFuture(ReconcileResult.created(new NetworkPolicy())));
    // Mock and capture PDB ops
    when(mockPdbOps.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
    when(mockPdbOpsV1Beta1.reconcile(any(), anyString(), any(), any())).thenReturn(Future.succeededFuture());
    // Mock and capture KafkaConnect ops for status update
    ArgumentCaptor<KafkaConnect> connectCaptor = ArgumentCaptor.forClass(KafkaConnect.class);
    when(mockConnectOps.updateStatusAsync(any(), connectCaptor.capture())).thenReturn(Future.succeededFuture());
    // Mock KafkaConnect API client
    KafkaConnectApi mockConnectClient = mock(KafkaConnectApi.class);
    // Prepare and run reconciliation
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), supplier, ResourceUtils.dummyClusterOperatorConfig(VERSIONS), x -> mockConnectClient);
    KafkaConnectCluster connect = KafkaConnectCluster.fromCrd(Reconciliation.DUMMY_RECONCILIATION, kc, VERSIONS);
    Checkpoint async = context.checkpoint();
    ops.reconcile(new Reconciliation("test-trigger", KafkaConnect.RESOURCE_KIND, NAMESPACE, NAME)).onComplete(context.succeeding(v -> context.verify(() -> {
        // Verify Deployment
        List<Deployment> capturedDeps = depCaptor.getAllValues();
        assertThat(capturedDeps, hasSize(1));
        Deployment dep = capturedDeps.get(0);
        assertThat(dep.getMetadata().getName(), is(connect.getName()));
        assertThat(dep.getSpec().getTemplate().getSpec().getContainers().get(0).getImage(), is("my-connect-build@sha256:blablabla"));
        assertThat(Annotations.stringAnnotation(dep.getSpec().getTemplate(), Annotations.STRIMZI_IO_CONNECT_BUILD_REVISION, null), is(build.generateDockerfile().hashStub() + OUTPUT_IMAGE_HASH_STUB));
        // Verify ConfigMap
        List<ConfigMap> capturedCms = dockerfileCaptor.getAllValues();
        assertThat(capturedCms, hasSize(1));
        ConfigMap dockerfileCm = capturedCms.get(0);
        assertThat(dockerfileCm.getData().containsKey("Dockerfile"), is(true));
        assertThat(dockerfileCm.getData().get("Dockerfile"), is(build.generateDockerfile().getDockerfile()));
        // Verify Service Account
        List<ServiceAccount> capturedSas = saCaptor.getAllValues();
        assertThat(capturedSas, hasSize(2));
        ServiceAccount sa = capturedSas.get(0);
        assertThat(sa.getMetadata().getName(), is(KafkaConnectResources.serviceAccountName(NAME)));
        sa = capturedSas.get(1);
        assertThat(sa.getMetadata().getName(), is(KafkaConnectResources.buildServiceAccountName(NAME)));
        // Verify builder Pod
        List<Pod> capturedBuilderPods = builderPodCaptor.getAllValues();
        assertThat(capturedBuilderPods, hasSize(2));
        assertThat(capturedBuilderPods.stream().filter(pod -> pod != null).collect(Collectors.toList()), hasSize(1));
        // Verify status
        List<KafkaConnect> capturedConnects = connectCaptor.getAllValues();
        assertThat(capturedConnects, hasSize(1));
        KafkaConnectStatus connectStatus = capturedConnects.get(0).getStatus();
        assertThat(connectStatus.getConditions().get(0).getStatus(), is("True"));
        assertThat(connectStatus.getConditions().get(0).getType(), is("Ready"));
        async.flag();
    })));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Annotations(io.strimzi.operator.common.Annotations) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) AfterAll(org.junit.jupiter.api.AfterAll) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) PodDisruptionBudgetV1Beta1Operator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetV1Beta1Operator) BeforeAll(org.junit.jupiter.api.BeforeAll) KafkaConnectStatus(io.strimzi.api.kafka.model.status.KafkaConnectStatus) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) Collections.emptyList(java.util.Collections.emptyList) KafkaConnectBuild(io.strimzi.operator.cluster.model.KafkaConnectBuild) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) KafkaConnectBuilder(io.strimzi.api.kafka.model.KafkaConnectBuilder) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) KafkaConnectCluster(io.strimzi.operator.cluster.model.KafkaConnectCluster) List(java.util.List) Plugin(io.strimzi.api.kafka.model.connect.build.Plugin) Optional(java.util.Optional) Checkpoint(io.vertx.junit5.Checkpoint) PodDisruptionBudgetOperator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetOperator) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) Mockito.mock(org.mockito.Mockito.mock) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) NetworkPolicyOperator(io.strimzi.operator.common.operator.resource.NetworkPolicyOperator) PluginBuilder(io.strimzi.api.kafka.model.connect.build.PluginBuilder) ServiceOperator(io.strimzi.operator.common.operator.resource.ServiceOperator) BiPredicate(java.util.function.BiPredicate) ArgumentCaptor(org.mockito.ArgumentCaptor) ServiceAccountOperator(io.strimzi.operator.common.operator.resource.ServiceAccountOperator) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) BuildConfigOperator(io.strimzi.operator.common.operator.resource.BuildConfigOperator) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ReconcileResult(io.strimzi.operator.common.operator.resource.ReconcileResult) Collections.singletonMap(java.util.Collections.singletonMap) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Collections.emptyMap(java.util.Collections.emptyMap) ContainerStatusBuilder(io.fabric8.kubernetes.api.model.ContainerStatusBuilder) KubernetesVersion(io.strimzi.operator.KubernetesVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) JarArtifactBuilder(io.strimzi.api.kafka.model.connect.build.JarArtifactBuilder) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Reconciliation(io.strimzi.operator.common.Reconciliation) Util(io.strimzi.operator.common.Util) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) KafkaConnectResources(io.strimzi.api.kafka.model.KafkaConnectResources) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PluginBuilder(io.strimzi.api.kafka.model.connect.build.PluginBuilder) KafkaConnectBuilder(io.strimzi.api.kafka.model.KafkaConnectBuilder) DeploymentOperator(io.strimzi.operator.common.operator.resource.DeploymentOperator) PodDisruptionBudgetV1Beta1Operator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetV1Beta1Operator) KafkaConnectStatus(io.strimzi.api.kafka.model.status.KafkaConnectStatus) Optional(java.util.Optional) Pod(io.fabric8.kubernetes.api.model.Pod) NetworkPolicy(io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) PodDisruptionBudgetOperator(io.strimzi.operator.common.operator.resource.PodDisruptionBudgetOperator) SecretOperator(io.strimzi.operator.common.operator.resource.SecretOperator) ContainerStatusBuilder(io.fabric8.kubernetes.api.model.ContainerStatusBuilder) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) CrdOperator(io.strimzi.operator.common.operator.resource.CrdOperator) ServiceAccountOperator(io.strimzi.operator.common.operator.resource.ServiceAccountOperator) BiPredicate(java.util.function.BiPredicate) ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) KafkaConnectCluster(io.strimzi.operator.cluster.model.KafkaConnectCluster) KafkaConnect(io.strimzi.api.kafka.model.KafkaConnect) ServiceOperator(io.strimzi.operator.common.operator.resource.ServiceOperator) JarArtifactBuilder(io.strimzi.api.kafka.model.connect.build.JarArtifactBuilder) NetworkPolicyOperator(io.strimzi.operator.common.operator.resource.NetworkPolicyOperator) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConnector(io.strimzi.api.kafka.model.KafkaConnector) KafkaConnectBuild(io.strimzi.operator.cluster.model.KafkaConnectBuild) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) PodOperator(io.strimzi.operator.common.operator.resource.PodOperator) Service(io.fabric8.kubernetes.api.model.Service) KafkaConnectorList(io.strimzi.api.kafka.KafkaConnectorList) Checkpoint(io.vertx.junit5.Checkpoint) BuildConfigOperator(io.strimzi.operator.common.operator.resource.BuildConfigOperator) ConfigMapOperator(io.strimzi.operator.common.operator.resource.ConfigMapOperator) Plugin(io.strimzi.api.kafka.model.connect.build.Plugin) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ContainerStatusBuilder (io.fabric8.kubernetes.api.model.ContainerStatusBuilder)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Service (io.fabric8.kubernetes.api.model.Service)2 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)2 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)2 NetworkPolicy (io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy)2 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 KafkaConnectorList (io.strimzi.api.kafka.KafkaConnectorList)2 KafkaConnect (io.strimzi.api.kafka.model.KafkaConnect)2 KafkaConnectBuilder (io.strimzi.api.kafka.model.KafkaConnectBuilder)2 KafkaConnectResources (io.strimzi.api.kafka.model.KafkaConnectResources)2 KafkaConnector (io.strimzi.api.kafka.model.KafkaConnector)2 JarArtifactBuilder (io.strimzi.api.kafka.model.connect.build.JarArtifactBuilder)2 Plugin (io.strimzi.api.kafka.model.connect.build.Plugin)2 PluginBuilder (io.strimzi.api.kafka.model.connect.build.PluginBuilder)2 KafkaConnectStatus (io.strimzi.api.kafka.model.status.KafkaConnectStatus)2 KubernetesVersion (io.strimzi.operator.KubernetesVersion)2 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)2