Search in sources :

Example 91 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method updateCluster.

private void updateCluster(TestContext context, ConfigMap originalCm, ConfigMap clusterCm, boolean kafkaRolling, boolean zkRolling) {
    KafkaCluster originalKafkaCluster = KafkaCluster.fromConfigMap(originalCm);
    KafkaCluster updatedKafkaCluster = KafkaCluster.fromConfigMap(clusterCm);
    ZookeeperCluster originalZookeeperCluster = ZookeeperCluster.fromConfigMap(originalCm);
    ZookeeperCluster updatedZookeeperCluster = ZookeeperCluster.fromConfigMap(clusterCm);
    TopicController originalTopicController = TopicController.fromConfigMap(originalCm);
    // create CM, Service, headless service, statefulset and so on
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    ZookeeperSetOperator mockZsOps = mock(ZookeeperSetOperator.class);
    KafkaSetOperator mockKsOps = mock(KafkaSetOperator.class);
    PvcOperator mockPvcOps = mock(PvcOperator.class);
    DeploymentOperator mockDepOps = mock(DeploymentOperator.class);
    String clusterCmName = clusterCm.getMetadata().getName();
    String clusterCmNamespace = clusterCm.getMetadata().getNamespace();
    // Mock CM get
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ConfigMap metricsCm = new ConfigMapBuilder().withNewMetadata().withName(KafkaCluster.metricConfigsName(clusterCmName)).withNamespace(clusterCmNamespace).endMetadata().withData(Collections.singletonMap(AbstractModel.METRICS_CONFIG_FILE, METRICS_CONFIG)).build();
    when(mockCmOps.get(clusterCmNamespace, KafkaCluster.metricConfigsName(clusterCmName))).thenReturn(metricsCm);
    ConfigMap zkMetricsCm = new ConfigMapBuilder().withNewMetadata().withName(ZookeeperCluster.zookeeperMetricsName(clusterCmName)).withNamespace(clusterCmNamespace).endMetadata().withData(Collections.singletonMap(AbstractModel.METRICS_CONFIG_FILE, METRICS_CONFIG)).build();
    when(mockCmOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperMetricsName(clusterCmName))).thenReturn(zkMetricsCm);
    // Mock Service gets
    when(mockServiceOps.get(clusterCmNamespace, KafkaCluster.kafkaClusterName(clusterCmName))).thenReturn(originalKafkaCluster.generateService());
    when(mockServiceOps.get(clusterCmNamespace, KafkaCluster.headlessName(clusterCmName))).thenReturn(originalKafkaCluster.generateHeadlessService());
    when(mockServiceOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperClusterName(clusterCmName))).thenReturn(originalKafkaCluster.generateService());
    when(mockServiceOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperHeadlessName(clusterCmName))).thenReturn(originalZookeeperCluster.generateHeadlessService());
    when(mockServiceOps.endpointReadiness(eq(clusterCmNamespace), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    // Mock StatefulSet get
    when(mockKsOps.get(clusterCmNamespace, KafkaCluster.kafkaClusterName(clusterCmName))).thenReturn(originalKafkaCluster.generateStatefulSet(openShift));
    when(mockZsOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperClusterName(clusterCmName))).thenReturn(originalZookeeperCluster.generateStatefulSet(openShift));
    // Mock Deployment get
    if (originalTopicController != null) {
        when(mockDepOps.get(clusterCmNamespace, TopicController.topicControllerName(clusterCmName))).thenReturn(originalTopicController.generateDeployment());
    }
    // Mock CM patch
    Set<String> metricsCms = set();
    doAnswer(invocation -> {
        metricsCms.add(invocation.getArgument(1));
        return Future.succeededFuture();
    }).when(mockCmOps).reconcile(eq(clusterCmNamespace), anyString(), any());
    // Mock Service patch (both service and headless service
    ArgumentCaptor<String> patchedServicesCaptor = ArgumentCaptor.forClass(String.class);
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), patchedServicesCaptor.capture(), any())).thenReturn(Future.succeededFuture());
    // Mock StatefulSet patch
    when(mockZsOps.reconcile(anyString(), anyString(), any())).thenReturn(Future.succeededFuture(ReconcileResult.patched(zkRolling)));
    when(mockKsOps.reconcile(anyString(), anyString(), any())).thenReturn(Future.succeededFuture(ReconcileResult.patched(kafkaRolling)));
    // Mock StatefulSet rollingUpdate
    Set<String> rollingRestarts = set();
    // Mock StatefulSet scaleUp
    ArgumentCaptor<String> scaledUpCaptor = ArgumentCaptor.forClass(String.class);
    when(mockZsOps.scaleUp(anyString(), scaledUpCaptor.capture(), anyInt())).thenReturn(Future.succeededFuture(42));
    // Mock StatefulSet scaleDown
    ArgumentCaptor<String> scaledDownCaptor = ArgumentCaptor.forClass(String.class);
    when(mockZsOps.scaleDown(anyString(), scaledDownCaptor.capture(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockZsOps.rollingUpdate(anyString(), anyString())).thenAnswer(i -> {
        if (!zkRolling) {
            context.fail("Unexpected rolling update");
        }
        return Future.succeededFuture();
    });
    // ArgumentCaptor<String> scaledUpCaptor = ArgumentCaptor.forClass(String.class);
    when(mockKsOps.scaleUp(anyString(), scaledUpCaptor.capture(), anyInt())).thenReturn(Future.succeededFuture(42));
    // Mock StatefulSet scaleDown
    // ArgumentCaptor<String> scaledDownCaptor = ArgumentCaptor.forClass(String.class);
    when(mockKsOps.scaleDown(anyString(), scaledDownCaptor.capture(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockKsOps.rollingUpdate(anyString(), anyString())).thenAnswer(i -> {
        if (!kafkaRolling) {
            context.fail("Unexpected rolling update");
        }
        return Future.succeededFuture();
    });
    // Mock Deployment patch
    ArgumentCaptor<String> depCaptor = ArgumentCaptor.forClass(String.class);
    when(mockDepOps.reconcile(anyString(), depCaptor.capture(), any())).thenReturn(Future.succeededFuture());
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps);
    // Now try to update a KafkaCluster based on this CM
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.KAFKA, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        if (createResult.failed())
            createResult.cause().printStackTrace();
        context.assertTrue(createResult.succeeded());
        // rolling restart
        Set<String> expectedRollingRestarts = set();
        if (KafkaSetOperator.needsRollingUpdate(new StatefulSetDiff(originalKafkaCluster.generateStatefulSet(openShift), updatedKafkaCluster.generateStatefulSet(openShift)))) {
            expectedRollingRestarts.add(originalKafkaCluster.getName());
        }
        if (ZookeeperSetOperator.needsRollingUpdate(new StatefulSetDiff(originalZookeeperCluster.generateStatefulSet(openShift), updatedZookeeperCluster.generateStatefulSet(openShift)))) {
            expectedRollingRestarts.add(originalZookeeperCluster.getName());
        }
        // No metrics config  => no CMs created
        verify(mockCmOps, never()).createOrUpdate(any());
        verifyNoMoreInteractions(mockPvcOps);
        async.complete();
    });
}
Also used : KafkaCluster(io.strimzi.controller.cluster.model.KafkaCluster) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) ZookeeperCluster(io.strimzi.controller.cluster.model.ZookeeperCluster) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) TopicController(io.strimzi.controller.cluster.model.TopicController) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Async(io.vertx.ext.unit.Async) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) Reconciliation(io.strimzi.controller.cluster.Reconciliation) StatefulSetDiff(io.strimzi.controller.cluster.operator.resource.StatefulSetDiff) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator)

Example 92 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method deleteCluster.

private void deleteCluster(TestContext context, ConfigMap clusterCm) {
    ZookeeperCluster zookeeperCluster = ZookeeperCluster.fromConfigMap(clusterCm);
    KafkaCluster kafkaCluster = KafkaCluster.fromConfigMap(clusterCm);
    TopicController topicController = TopicController.fromConfigMap(clusterCm);
    // create CM, Service, headless service, statefulset
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    ZookeeperSetOperator mockZsOps = mock(ZookeeperSetOperator.class);
    KafkaSetOperator mockKsOps = mock(KafkaSetOperator.class);
    PvcOperator mockPvcOps = mock(PvcOperator.class);
    DeploymentOperator mockDepOps = mock(DeploymentOperator.class);
    String clusterCmName = clusterCm.getMetadata().getName();
    String clusterCmNamespace = clusterCm.getMetadata().getNamespace();
    StatefulSet kafkaSs = kafkaCluster.generateStatefulSet(true);
    StatefulSet zkSs = zookeeperCluster.generateStatefulSet(true);
    when(mockKsOps.get(clusterCmNamespace, KafkaCluster.kafkaClusterName(clusterCmName))).thenReturn(kafkaSs);
    when(mockZsOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperClusterName(clusterCmName))).thenReturn(zkSs);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ArgumentCaptor<String> serviceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> ssCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> metricsCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.reconcile(eq(clusterCmNamespace), metricsCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), serviceCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockKsOps.reconcile(anyString(), ssCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockZsOps.reconcile(anyString(), ssCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> pvcCaptor = ArgumentCaptor.forClass(String.class);
    when(mockPvcOps.reconcile(eq(clusterCmNamespace), pvcCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> depCaptor = ArgumentCaptor.forClass(String.class);
    when(mockDepOps.reconcile(eq(clusterCmNamespace), depCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    if (topicController != null) {
        Deployment tcDep = topicController.generateDeployment();
        when(mockDepOps.get(clusterCmNamespace, TopicController.topicControllerName(clusterCmName))).thenReturn(tcDep);
    }
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps);
    // Now try to delete a KafkaCluster based on this CM
    Async async = context.async();
    ops.delete(new Reconciliation("test-trigger", AssemblyType.KAFKA, clusterCmNamespace, clusterCmName), createResult -> {
        context.assertTrue(createResult.succeeded());
        /*Set<String> metricsNames = new HashSet<>();
            if (kafkaCluster.isMetricsEnabled()) {
                metricsNames.add(KafkaCluster.metricConfigsName(clusterCmName));
            }
            if (zookeeperCluster.isMetricsEnabled()) {
                metricsNames.add(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
            }
            context.assertEquals(metricsNames, captured(metricsCaptor));*/
        verify(mockZsOps).reconcile(eq(clusterCmNamespace), eq(ZookeeperCluster.zookeeperClusterName(clusterCmName)), isNull());
        context.assertEquals(set(ZookeeperCluster.zookeeperHeadlessName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName), KafkaCluster.kafkaClusterName(clusterCmName), KafkaCluster.headlessName(clusterCmName)), captured(serviceCaptor));
        // verify deleted Statefulsets
        context.assertEquals(set(zookeeperCluster.getName(), kafkaCluster.getName()), captured(ssCaptor));
        // PvcOperations only used for deletion
        Set<String> expectedPvcDeletions = new HashSet<>();
        for (int i = 0; deleteClaim && i < kafkaCluster.getReplicas(); i++) {
            expectedPvcDeletions.add("data-" + clusterCmName + "-kafka-" + i);
        }
        for (int i = 0; deleteClaim && i < zookeeperCluster.getReplicas(); i++) {
            expectedPvcDeletions.add("data-" + clusterCmName + "-zookeeper-" + i);
        }
        context.assertEquals(expectedPvcDeletions, captured(pvcCaptor));
        // if topic controller configuration was defined in the CM
        if (topicController != null) {
            Set<String> expectedDepNames = new HashSet<>();
            expectedDepNames.add(TopicController.topicControllerName(clusterCmName));
            context.assertEquals(expectedDepNames, captured(depCaptor));
        }
        async.complete();
    });
}
Also used : KafkaCluster(io.strimzi.controller.cluster.model.KafkaCluster) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ZookeeperCluster(io.strimzi.controller.cluster.model.ZookeeperCluster) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) TopicController(io.strimzi.controller.cluster.model.TopicController) Async(io.vertx.ext.unit.Async) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) HashSet(java.util.HashSet)

Example 93 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaConnectS2IAssemblyOperatorTest method testUpdateClusterNoDiff.

@Test
public void testUpdateClusterNoDiff(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentConfigOperator mockDcOps = mock(DeploymentConfigOperator.class);
    BuildConfigOperator mockBcOps = mock(BuildConfigOperator.class);
    ImageStreamOperator mockIsOps = mock(ImageStreamOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectS2IClusterConfigMap(clusterCmNamespace, clusterCmName);
    KafkaConnectS2ICluster connect = KafkaConnectS2ICluster.fromConfigMap(clusterCm);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
    when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeploymentConfig());
    when(mockIsOps.get(clusterCmNamespace, connect.getSourceImageStreamName())).thenReturn(connect.generateSourceImageStream());
    when(mockIsOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateTargetImageStream());
    when(mockBcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateBuildConfig());
    ArgumentCaptor<String> serviceNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> serviceNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(serviceNamespaceCaptor.capture(), serviceNameCaptor.capture(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<DeploymentConfig> dcCaptor = ArgumentCaptor.forClass(DeploymentConfig.class);
    when(mockDcOps.reconcile(dcNamespaceCaptor.capture(), dcNameCaptor.capture(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleUpNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcScaleUpNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleUpReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleUp(dcScaleUpNamespaceCaptor.capture(), dcScaleUpNameCaptor.capture(), dcScaleUpReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleDownNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcScaleDownNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleDownReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleDown(dcScaleDownNamespaceCaptor.capture(), dcScaleDownNameCaptor.capture(), dcScaleDownReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> isNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> isNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<ImageStream> isCaptor = ArgumentCaptor.forClass(ImageStream.class);
    when(mockIsOps.reconcile(isNamespaceCaptor.capture(), isNameCaptor.capture(), isCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> bcNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> bcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<BuildConfig> bcCaptor = ArgumentCaptor.forClass(BuildConfig.class);
    when(mockBcOps.reconcile(bcNamespaceCaptor.capture(), bcNameCaptor.capture(), bcCaptor.capture())).thenReturn(Future.succeededFuture());
    KafkaConnectS2IAssemblyOperator ops = new KafkaConnectS2IAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps, mockIsOps, mockBcOps);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT_S2I, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        // Vertify service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(1, capturedServices.size());
        // Verify Deployment Config
        List<DeploymentConfig> capturedDc = dcCaptor.getAllValues();
        context.assertEquals(1, capturedDc.size());
        // Verify Build Config
        List<BuildConfig> capturedBc = bcCaptor.getAllValues();
        context.assertEquals(1, capturedBc.size());
        // Verify Image Streams
        List<ImageStream> capturedIs = isCaptor.getAllValues();
        context.assertEquals(2, capturedIs.size());
        // Verify scaleDown / scaleUp were not called
        context.assertEquals(1, dcScaleDownNameCaptor.getAllValues().size());
        context.assertEquals(1, dcScaleUpNameCaptor.getAllValues().size());
        async.complete();
    });
}
Also used : ImageStream(io.fabric8.openshift.api.model.ImageStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) KafkaConnectS2ICluster(io.strimzi.controller.cluster.model.KafkaConnectS2ICluster) BuildConfigOperator(io.strimzi.controller.cluster.operator.resource.BuildConfigOperator) ImageStreamOperator(io.strimzi.controller.cluster.operator.resource.ImageStreamOperator) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentConfigOperator(io.strimzi.controller.cluster.operator.resource.DeploymentConfigOperator) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Test(org.junit.Test)

Example 94 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaConnectS2IAssemblyOperatorTest method testCreateCluster.

@Test
public void testCreateCluster(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentConfigOperator mockDcOps = mock(DeploymentConfigOperator.class);
    BuildConfigOperator mockBcOps = mock(BuildConfigOperator.class);
    ImageStreamOperator mockIsOps = mock(ImageStreamOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectS2IClusterConfigMap(clusterCmNamespace, clusterCmName);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<DeploymentConfig> dcCaptor = ArgumentCaptor.forClass(DeploymentConfig.class);
    when(mockDcOps.reconcile(anyString(), anyString(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
    when(mockDcOps.scaleDown(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockDcOps.scaleUp(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    ArgumentCaptor<ImageStream> isCaptor = ArgumentCaptor.forClass(ImageStream.class);
    when(mockIsOps.reconcile(anyString(), anyString(), isCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<BuildConfig> bcCaptor = ArgumentCaptor.forClass(BuildConfig.class);
    when(mockBcOps.reconcile(anyString(), anyString(), bcCaptor.capture())).thenReturn(Future.succeededFuture());
    KafkaConnectS2IAssemblyOperator ops = new KafkaConnectS2IAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps, mockIsOps, mockBcOps);
    KafkaConnectS2ICluster connect = KafkaConnectS2ICluster.fromConfigMap(clusterCm);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT_S2I, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        // Vertify service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(1, capturedServices.size());
        Service service = capturedServices.get(0);
        context.assertEquals(connect.getName(), service.getMetadata().getName());
        context.assertEquals(connect.generateService(), service, "Services are not equal");
        // Verify Deployment Config
        List<DeploymentConfig> capturedDc = dcCaptor.getAllValues();
        context.assertEquals(1, capturedDc.size());
        DeploymentConfig dc = capturedDc.get(0);
        context.assertEquals(connect.getName(), dc.getMetadata().getName());
        context.assertEquals(connect.generateDeploymentConfig(), dc, "Deployment Configs are not equal");
        // Verify Build Config
        List<BuildConfig> capturedBc = bcCaptor.getAllValues();
        context.assertEquals(1, capturedBc.size());
        BuildConfig bc = capturedBc.get(0);
        context.assertEquals(connect.getName(), dc.getMetadata().getName());
        context.assertEquals(connect.generateBuildConfig(), bc, "Build Configs are not equal");
        // Verify Image Streams
        List<ImageStream> capturedIs = isCaptor.getAllValues();
        context.assertEquals(2, capturedIs.size());
        int sisIndex = (connect.getSourceImageStreamName()).equals(capturedIs.get(0).getMetadata().getName()) ? 0 : 1;
        int tisIndex = (connect.getName()).equals(capturedIs.get(0).getMetadata().getName()) ? 0 : 1;
        ImageStream sis = capturedIs.get(sisIndex);
        context.assertEquals(connect.getSourceImageStreamName(), sis.getMetadata().getName());
        context.assertEquals(connect.generateSourceImageStream(), sis, "Source Image Streams are not equal");
        ImageStream tis = capturedIs.get(tisIndex);
        context.assertEquals(connect.getName(), tis.getMetadata().getName());
        context.assertEquals(connect.generateTargetImageStream(), tis, "Target Image Streams are not equal");
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) ImageStream(io.fabric8.openshift.api.model.ImageStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) KafkaConnectS2ICluster(io.strimzi.controller.cluster.model.KafkaConnectS2ICluster) BuildConfigOperator(io.strimzi.controller.cluster.operator.resource.BuildConfigOperator) ImageStreamOperator(io.strimzi.controller.cluster.operator.resource.ImageStreamOperator) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) DeploymentConfigOperator(io.strimzi.controller.cluster.operator.resource.DeploymentConfigOperator) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Test(org.junit.Test)

Example 95 with Service

use of io.fabric8.patch.Service in project strimzi by strimzi.

the class KafkaConnectS2IAssemblyOperatorTest method testUpdateCluster.

@Test
public void testUpdateCluster(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentConfigOperator mockDcOps = mock(DeploymentConfigOperator.class);
    BuildConfigOperator mockBcOps = mock(BuildConfigOperator.class);
    ImageStreamOperator mockIsOps = mock(ImageStreamOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectS2IClusterConfigMap(clusterCmNamespace, clusterCmName);
    KafkaConnectS2ICluster connect = KafkaConnectS2ICluster.fromConfigMap(clusterCm);
    // Change the image to generate some diff
    clusterCm.getData().put("image", "some/different:image");
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
    when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeploymentConfig());
    when(mockIsOps.get(clusterCmNamespace, connect.getSourceImageStreamName())).thenReturn(connect.generateSourceImageStream());
    when(mockIsOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateTargetImageStream());
    when(mockBcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateBuildConfig());
    ArgumentCaptor<String> serviceNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> serviceNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(serviceNamespaceCaptor.capture(), serviceNameCaptor.capture(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<DeploymentConfig> dcCaptor = ArgumentCaptor.forClass(DeploymentConfig.class);
    when(mockDcOps.reconcile(dcNamespaceCaptor.capture(), dcNameCaptor.capture(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleUpNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcScaleUpNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleUpReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleUp(dcScaleUpNamespaceCaptor.capture(), dcScaleUpNameCaptor.capture(), dcScaleUpReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleDownNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> dcScaleDownNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleDownReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleDown(dcScaleDownNamespaceCaptor.capture(), dcScaleDownNameCaptor.capture(), dcScaleDownReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> isNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> isNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<ImageStream> isCaptor = ArgumentCaptor.forClass(ImageStream.class);
    when(mockIsOps.reconcile(isNamespaceCaptor.capture(), isNameCaptor.capture(), isCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> bcNamespaceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> bcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<BuildConfig> bcCaptor = ArgumentCaptor.forClass(BuildConfig.class);
    when(mockBcOps.reconcile(bcNamespaceCaptor.capture(), bcNameCaptor.capture(), bcCaptor.capture())).thenReturn(Future.succeededFuture());
    KafkaConnectS2IAssemblyOperator ops = new KafkaConnectS2IAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps, mockIsOps, mockBcOps);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT_S2I, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        KafkaConnectS2ICluster compareTo = KafkaConnectS2ICluster.fromConfigMap(clusterCm);
        // Vertify service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(1, capturedServices.size());
        Service service = capturedServices.get(0);
        context.assertEquals(compareTo.getName(), service.getMetadata().getName());
        context.assertEquals(compareTo.generateService(), service, "Services are not equal");
        // Verify Deployment Config
        List<DeploymentConfig> capturedDc = dcCaptor.getAllValues();
        context.assertEquals(1, capturedDc.size());
        DeploymentConfig dc = capturedDc.get(0);
        context.assertEquals(compareTo.getName(), dc.getMetadata().getName());
        context.assertEquals(compareTo.generateDeploymentConfig(), dc, "Deployment Configs are not equal");
        // Verify Build Config
        List<BuildConfig> capturedBc = bcCaptor.getAllValues();
        context.assertEquals(1, capturedBc.size());
        BuildConfig bc = capturedBc.get(0);
        context.assertEquals(compareTo.getName(), dc.getMetadata().getName());
        context.assertEquals(compareTo.generateBuildConfig(), bc, "Build Configs are not equal");
        // Verify Image Streams
        List<ImageStream> capturedIs = isCaptor.getAllValues();
        context.assertEquals(2, capturedIs.size());
        int sisIndex = (compareTo.getSourceImageStreamName()).equals(capturedIs.get(0).getMetadata().getName()) ? 0 : 1;
        int tisIndex = (compareTo.getName()).equals(capturedIs.get(0).getMetadata().getName()) ? 0 : 1;
        ImageStream sis = capturedIs.get(sisIndex);
        context.assertEquals(compareTo.getSourceImageStreamName(), sis.getMetadata().getName());
        context.assertEquals(compareTo.generateSourceImageStream(), sis, "Source Image Streams are not equal");
        ImageStream tis = capturedIs.get(tisIndex);
        context.assertEquals(compareTo.getName(), tis.getMetadata().getName());
        context.assertEquals(compareTo.generateTargetImageStream(), tis, "Target Image Streams are not equal");
        async.complete();
    });
}
Also used : ImageStream(io.fabric8.openshift.api.model.ImageStream) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) KafkaConnectS2ICluster(io.strimzi.controller.cluster.model.KafkaConnectS2ICluster) BuildConfigOperator(io.strimzi.controller.cluster.operator.resource.BuildConfigOperator) ImageStreamOperator(io.strimzi.controller.cluster.operator.resource.ImageStreamOperator) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentConfigOperator(io.strimzi.controller.cluster.operator.resource.DeploymentConfigOperator) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Test(org.junit.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)100 Test (org.junit.Test)78 IOException (java.io.IOException)35 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)33 File (java.io.File)28 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)26 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)24 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)22 Map (java.util.Map)19 Pod (io.fabric8.kubernetes.api.model.Pod)18 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)18 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)18 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)17 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 List (java.util.List)17 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)15 Reconciliation (io.strimzi.controller.cluster.Reconciliation)15 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)15 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)15