Search in sources :

Example 1 with Assertions

use of io.fabric8.kubernetes.assertions.Assertions in project strimzi by strimzi.

the class OpenShiftTemplatesTest method testStrimziPersistentWithCustomParameters.

@Test
public void testStrimziPersistentWithCustomParameters() throws IOException {
    String clusterName = "test-persistent-with-custom-parameters";
    oc.newApp("strimzi-persistent", map("CLUSTER_NAME", clusterName, "ZOOKEEPER_HEALTHCHECK_DELAY", "30", "ZOOKEEPER_HEALTHCHECK_TIMEOUT", "10", "KAFKA_HEALTHCHECK_DELAY", "30", "KAFKA_HEALTHCHECK_TIMEOUT", "10", "KAFKA_DEFAULT_REPLICATION_FACTOR", "2", "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "5", "KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "5", "ZOOKEEPER_VOLUME_CAPACITY", "2Gi", "KAFKA_VOLUME_CAPACITY", "2Gi"));
    // TODO Add assertions to check that Kafka brokers have a custom configuration
    ConfigMap cm = client.configMaps().inNamespace(NAMESPACE).withName(clusterName).get();
    assertNotNull(cm);
    Map<String, String> cmData = cm.getData();
    assertEquals("30", cmData.get("zookeeper-healthcheck-delay"));
    assertEquals("10", cmData.get("zookeeper-healthcheck-timeout"));
    assertEquals("30", cmData.get("kafka-healthcheck-delay"));
    assertEquals("10", cmData.get("kafka-healthcheck-timeout"));
    assertEquals("2", cmData.get("KAFKA_DEFAULT_REPLICATION_FACTOR"));
    assertEquals("5", cmData.get("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR"));
    assertEquals("5", cmData.get("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR"));
    assertEquals("2Gi", mapper.readTree(cmData.get("kafka-storage")).get("size").asText());
    assertEquals("2Gi", mapper.readTree(cmData.get("zookeeper-storage")).get("size").asText());
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Test(org.junit.Test)

Example 2 with Assertions

use of io.fabric8.kubernetes.assertions.Assertions in project fabric8 by fabric8io.

the class KubernetesAssert method deployments.

/**
 * Finds all the resources that create pod selections (Deployment, DeploymentConfig, ReplicaSet, ReplicationController)
 * and create a {@link HasPodSelectionAssert} to make assertions on their pods that they startup etc.
 *
 * @return the assertion object for the deployment
 */
public HasPodSelectionAssert deployments() {
    List<HasPodSelectionAssert> asserters = new ArrayList<>();
    List<HasMetadata> resources = new ArrayList<>();
    try {
        resources = KubernetesHelper.findKubernetesResourcesOnClasspath(new Controller(client));
    } catch (IOException e) {
        fail("Failed to load kubernetes resources on the classpath: " + e, e);
    }
    for (HasMetadata resource : resources) {
        HasPodSelectionAssert asserter = createPodSelectionAssert(resource);
        if (asserter != null) {
            asserters.add(asserter);
        }
    }
    String message = "No pod selection kinds found on the classpath such as Deployment, DeploymentConfig, ReplicaSet, ReplicationController";
    // TODO we don't yet support size > 1
    assertThat(asserters).describedAs(message).isNotEmpty();
    if (asserters.size() == 1) {
        return asserters.get(0);
    }
    return new MultiHasPodSelectionAssert(asserters);
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Controller(io.fabric8.kubernetes.api.Controller) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController)

Example 3 with Assertions

use of io.fabric8.kubernetes.assertions.Assertions in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method createCluster.

private void createCluster(TestContext context, ConfigMap clusterCm) {
    // 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);
    // Create a CM
    String clusterCmName = clusterCm.getMetadata().getName();
    String clusterCmNamespace = clusterCm.getMetadata().getNamespace();
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockServiceOps.endpointReadiness(anyString(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<StatefulSet> ssCaptor = ArgumentCaptor.forClass(StatefulSet.class);
    when(mockZsOps.reconcile(anyString(), anyString(), ssCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockZsOps.scaleDown(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(null));
    when(mockZsOps.rollingUpdate(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockZsOps.scaleUp(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockKsOps.reconcile(anyString(), anyString(), ssCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockKsOps.scaleDown(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(null));
    when(mockKsOps.rollingUpdate(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockKsOps.scaleUp(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    ArgumentCaptor<Deployment> depCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDepOps.reconcile(anyString(), anyString(), depCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    // when(mockSsOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    // when(mockPodOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    // when(mockEndpointOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    KafkaCluster kafkaCluster = KafkaCluster.fromConfigMap(clusterCm);
    ZookeeperCluster zookeeperCluster = ZookeeperCluster.fromConfigMap(clusterCm);
    TopicController topicController = TopicController.fromConfigMap(clusterCm);
    ArgumentCaptor<ConfigMap> metricsCaptor = ArgumentCaptor.forClass(ConfigMap.class);
    ArgumentCaptor<String> metricsNameCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.reconcile(anyString(), metricsNameCaptor.capture(), metricsCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps);
    // Now try to create 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());
        // No metrics config  => no CMs created
        Set<String> metricsNames = new HashSet<>();
        if (kafkaCluster.isMetricsEnabled()) {
            metricsNames.add(KafkaCluster.metricConfigsName(clusterCmName));
        }
        if (zookeeperCluster.isMetricsEnabled()) {
            metricsNames.add(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
        }
        /*
            Map<String, ConfigMap> cmsByName = new HashMap<>();
            Iterator<ConfigMap> it2 = metricsCaptor.getAllValues().iterator();
            for (Iterator<String> it = metricsNameCaptor.getAllValues().iterator(); it.hasNext(); ) {
                cmsByName.put(it.next(), it2.next());
            }
            context.assertEquals(metricsNames, cmsByName.keySet(),
                    "Unexpected metrics ConfigMaps");
            if (kafkaCluster.isMetricsEnabled()) {
                ConfigMap kafkaMetricsCm = cmsByName.get(KafkaCluster.metricConfigsName(clusterCmName));
                context.assertEquals(ResourceUtils.labels(Labels.STRIMZI_TYPE_LABEL, "kafka",
                        Labels.STRIMZI_CLUSTER_LABEL, clusterCmName,
                        "my-user-label", "cromulent"), kafkaMetricsCm.getMetadata().getLabels());
            }
            if (zookeeperCluster.isMetricsEnabled()) {
                ConfigMap zookeeperMetricsCm = cmsByName.get(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
                context.assertEquals(ResourceUtils.labels(Labels.STRIMZI_TYPE_LABEL, "zookeeper",
                        Labels.STRIMZI_CLUSTER_LABEL, clusterCmName,
                        "my-user-label", "cromulent"), zookeeperMetricsCm.getMetadata().getLabels());
            }*/
        // We expect a headless and headful service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(4, capturedServices.size());
        context.assertEquals(set(KafkaCluster.kafkaClusterName(clusterCmName), KafkaCluster.headlessName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName), ZookeeperCluster.zookeeperHeadlessName(clusterCmName)), capturedServices.stream().map(svc -> svc.getMetadata().getName()).collect(Collectors.toSet()));
        // Assertions on the statefulset
        List<StatefulSet> capturedSs = ssCaptor.getAllValues();
        // We expect a statefulSet for kafka and zookeeper...
        context.assertEquals(set(KafkaCluster.kafkaClusterName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName)), capturedSs.stream().map(ss -> ss.getMetadata().getName()).collect(Collectors.toSet()));
        // if topic controller configuration was defined in the CM
        if (topicController != null) {
            List<Deployment> capturedDeps = depCaptor.getAllValues();
            context.assertEquals(1, capturedDeps.size());
            context.assertEquals(TopicController.topicControllerName(clusterCmName), capturedDeps.get(0).getMetadata().getName());
        }
        // PvcOperations only used for deletion
        verifyNoMoreInteractions(mockPvcOps);
        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) 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) Reconciliation(io.strimzi.controller.cluster.Reconciliation) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) HashSet(java.util.HashSet) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) ZookeeperCluster(io.strimzi.controller.cluster.model.ZookeeperCluster) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 4 with Assertions

use of io.fabric8.kubernetes.assertions.Assertions in project strimzi by strimzi.

the class OpenShiftTemplatesTest method testStrimziEphemeralWithCustomParameters.

@Test
public void testStrimziEphemeralWithCustomParameters() {
    String clusterName = "test-ephemeral-with-custom-parameters";
    oc.newApp("strimzi-ephemeral", map("CLUSTER_NAME", clusterName, "ZOOKEEPER_HEALTHCHECK_DELAY", "30", "ZOOKEEPER_HEALTHCHECK_TIMEOUT", "10", "KAFKA_HEALTHCHECK_DELAY", "30", "KAFKA_HEALTHCHECK_TIMEOUT", "10", "KAFKA_DEFAULT_REPLICATION_FACTOR", "2", "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", "5", "KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "5"));
    // TODO Add assertions to check that Kafka brokers have a custom configuration
    ConfigMap cm = client.configMaps().inNamespace(NAMESPACE).withName(clusterName).get();
    assertNotNull(cm);
    Map<String, String> cmData = cm.getData();
    assertEquals("30", cmData.get("zookeeper-healthcheck-delay"));
    assertEquals("10", cmData.get("zookeeper-healthcheck-timeout"));
    assertEquals("30", cmData.get("kafka-healthcheck-delay"));
    assertEquals("10", cmData.get("kafka-healthcheck-timeout"));
    assertEquals("2", cmData.get("KAFKA_DEFAULT_REPLICATION_FACTOR"));
    assertEquals("5", cmData.get("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR"));
    assertEquals("5", cmData.get("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR"));
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Test(org.junit.Test)

Aggregations

ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 Test (org.junit.Test)2 Controller (io.fabric8.kubernetes.api.Controller)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)1 Service (io.fabric8.kubernetes.api.model.Service)1 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)1 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)1 Reconciliation (io.strimzi.controller.cluster.Reconciliation)1 KafkaCluster (io.strimzi.controller.cluster.model.KafkaCluster)1 TopicController (io.strimzi.controller.cluster.model.TopicController)1 ZookeeperCluster (io.strimzi.controller.cluster.model.ZookeeperCluster)1 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)1 DeploymentOperator (io.strimzi.controller.cluster.operator.resource.DeploymentOperator)1 KafkaSetOperator (io.strimzi.controller.cluster.operator.resource.KafkaSetOperator)1 PvcOperator (io.strimzi.controller.cluster.operator.resource.PvcOperator)1 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)1 ZookeeperSetOperator (io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator)1 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1