use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.
the class KafkaAssemblyOperatorMockIT method testKafkaScaleUp.
/**
* Create a cluster from a Kafka Cluster CM
*/
@Test
public void testKafkaScaleUp(TestContext context) {
KafkaAssemblyOperator kco = createCluster(context);
Async updateAsync = context.async();
int newScale = kafkaReplicas + 1;
String newPod = KafkaCluster.kafkaPodName(CLUSTER_NAME, kafkaReplicas);
context.assertNull(mockClient.pods().inNamespace(NAMESPACE).withName(newPod).get());
HashMap<String, String> data = new HashMap<>(cluster.getData());
data.put(KafkaCluster.KEY_REPLICAS, String.valueOf(newScale));
ConfigMap changedClusterCm = new ConfigMapBuilder(cluster).withData(data).build();
mockClient.configMaps().inNamespace(NAMESPACE).withName(CLUSTER_NAME).patch(changedClusterCm);
LOGGER.info("Scaling up to {} Kafka pods", newScale);
kco.reconcileAssembly(new Reconciliation("test-trigger", AssemblyType.KAFKA, NAMESPACE, CLUSTER_NAME), ar -> {
if (ar.failed())
ar.cause().printStackTrace();
context.assertTrue(ar.succeeded());
context.assertEquals(newScale, mockClient.apps().statefulSets().inNamespace(NAMESPACE).withName(KafkaCluster.kafkaClusterName(CLUSTER_NAME)).get().getSpec().getReplicas());
context.assertNotNull(mockClient.pods().inNamespace(NAMESPACE).withName(newPod).get(), "Expected pod " + newPod + " to have been created");
// TODO assert no rolling update
updateAsync.complete();
});
updateAsync.await();
}
use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.
the class KafkaAssemblyOperatorMockIT method testKafkaScaleDown.
/**
* Create a cluster from a Kafka Cluster CM
*/
@Test
public void testKafkaScaleDown(TestContext context) {
if (kafkaReplicas <= 1) {
LOGGER.info("Skipping scale down test because there's only 1 broker");
return;
}
KafkaAssemblyOperator kco = createCluster(context);
Async updateAsync = context.async();
int newScale = kafkaReplicas - 1;
String deletedPod = KafkaCluster.kafkaPodName(CLUSTER_NAME, newScale);
context.assertNotNull(mockClient.pods().inNamespace(NAMESPACE).withName(deletedPod).get());
HashMap<String, String> data = new HashMap<>(cluster.getData());
data.put(KafkaCluster.KEY_REPLICAS, String.valueOf(newScale));
ConfigMap changedClusterCm = new ConfigMapBuilder(cluster).withData(data).build();
mockClient.configMaps().inNamespace(NAMESPACE).withName(CLUSTER_NAME).patch(changedClusterCm);
LOGGER.info("Scaling down to {} Kafka pods", newScale);
kco.reconcileAssembly(new Reconciliation("test-trigger", AssemblyType.KAFKA, NAMESPACE, CLUSTER_NAME), ar -> {
if (ar.failed())
ar.cause().printStackTrace();
context.assertTrue(ar.succeeded());
context.assertEquals(newScale, mockClient.apps().statefulSets().inNamespace(NAMESPACE).withName(KafkaCluster.kafkaClusterName(CLUSTER_NAME)).get().getSpec().getReplicas());
context.assertNull(mockClient.pods().inNamespace(NAMESPACE).withName(deletedPod).get(), "Expected pod " + deletedPod + " to have been deleted");
// TODO assert no rolling update
updateAsync.complete();
});
updateAsync.await();
}
use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.
the class KafkaConnectAssemblyOperatorTest method testUpdateCluster.
@Test
public void testUpdateCluster(TestContext context) {
ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
ServiceOperator mockServiceOps = mock(ServiceOperator.class);
DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
String clusterCmName = "foo";
String clusterCmNamespace = "test";
ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, clusterCmName);
KafkaConnectCluster connect = KafkaConnectCluster.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.generateDeployment());
ArgumentCaptor<String> serviceNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
when(mockServiceOps.reconcile(eq(clusterCmNamespace), serviceNameCaptor.capture(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
ArgumentCaptor<String> dcNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Deployment> dcCaptor = ArgumentCaptor.forClass(Deployment.class);
when(mockDcOps.reconcile(eq(clusterCmNamespace), dcNameCaptor.capture(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
ArgumentCaptor<String> dcScaleUpNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Integer> dcScaleUpReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
when(mockDcOps.scaleUp(eq(clusterCmNamespace), dcScaleUpNameCaptor.capture(), dcScaleUpReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
ArgumentCaptor<String> dcScaleDownNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Integer> dcScaleDownReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
when(mockDcOps.scaleDown(eq(clusterCmNamespace), dcScaleDownNameCaptor.capture(), dcScaleDownReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps);
Async async = context.async();
ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
context.assertTrue(createResult.succeeded());
KafkaConnectCluster compareTo = KafkaConnectCluster.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
List<Deployment> capturedDc = dcCaptor.getAllValues();
context.assertEquals(1, capturedDc.size());
Deployment dc = capturedDc.get(0);
context.assertEquals(compareTo.getName(), dc.getMetadata().getName());
context.assertEquals(compareTo.generateDeployment(), dc, "Deployments are not equal");
// Verify scaleDown / scaleUp were not called
context.assertEquals(1, dcScaleDownNameCaptor.getAllValues().size());
context.assertEquals(1, dcScaleUpNameCaptor.getAllValues().size());
async.complete();
});
}
use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.
the class KafkaConnectAssemblyOperatorTest method testReconcile.
@Test
public void testReconcile(TestContext context) {
ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
ServiceOperator mockServiceOps = mock(ServiceOperator.class);
DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
String clusterCmNamespace = "test";
ConfigMap foo = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "foo");
ConfigMap bar = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "bar");
ConfigMap baz = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, "baz");
when(mockCmOps.list(eq(clusterCmNamespace), any())).thenReturn(asList(foo, bar));
// when requested ConfigMap for a specific Kafka Connect cluster
when(mockCmOps.get(eq(clusterCmNamespace), eq("foo"))).thenReturn(foo);
when(mockCmOps.get(eq(clusterCmNamespace), eq("bar"))).thenReturn(bar);
// providing the list of ALL Deployments for all the Kafka Connect clusters
Labels newLabels = Labels.forType(AssemblyType.CONNECT);
when(mockDcOps.list(eq(clusterCmNamespace), eq(newLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(bar).generateDeployment(), KafkaConnectCluster.fromConfigMap(baz).generateDeployment()));
// providing the list Deployments for already "existing" Kafka Connect clusters
Labels barLabels = Labels.forCluster("bar");
when(mockDcOps.list(eq(clusterCmNamespace), eq(barLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(bar).generateDeployment()));
Labels bazLabels = Labels.forCluster("baz");
when(mockDcOps.list(eq(clusterCmNamespace), eq(bazLabels))).thenReturn(asList(KafkaConnectCluster.fromConfigMap(baz).generateDeployment()));
Set<String> createdOrUpdated = new HashSet<>();
Set<String> deleted = new HashSet<>();
Async async = context.async(3);
KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps) {
@Override
public void createOrUpdate(Reconciliation reconciliation, ConfigMap assemblyCm, Handler h) {
createdOrUpdated.add(assemblyCm.getMetadata().getName());
async.countDown();
h.handle(Future.succeededFuture());
}
@Override
public void delete(Reconciliation reconciliation, Handler h) {
deleted.add(reconciliation.assemblyName());
async.countDown();
h.handle(Future.succeededFuture());
}
};
// Now try to reconcile all the Kafka Connect clusters
ops.reconcileAll("test", clusterCmNamespace, Labels.EMPTY);
async.await();
context.assertEquals(new HashSet(asList("foo", "bar")), createdOrUpdated);
context.assertEquals(singleton("baz"), deleted);
}
use of io.fabric8.kubernetes.api.model.Context in project strimzi by strimzi.
the class KafkaConnectAssemblyOperatorTest method testUpdateClusterScaleDown.
@Test
public void testUpdateClusterScaleDown(TestContext context) {
int scaleTo = 2;
String newReplicas = String.valueOf(scaleTo);
ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
ServiceOperator mockServiceOps = mock(ServiceOperator.class);
DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
String clusterCmName = "foo";
String clusterCmNamespace = "test";
ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, clusterCmName);
KafkaConnectCluster connect = KafkaConnectCluster.fromConfigMap(clusterCm);
// Change replicas to create ScaleDown
clusterCm.getData().put(KafkaConnectCluster.KEY_REPLICAS, newReplicas);
when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeployment());
when(mockServiceOps.reconcile(eq(clusterCmNamespace), any(), any())).thenReturn(Future.succeededFuture());
when(mockDcOps.reconcile(eq(clusterCmNamespace), any(), any())).thenReturn(Future.succeededFuture());
doAnswer(i -> Future.succeededFuture(scaleTo)).when(mockDcOps).scaleUp(clusterCmNamespace, connect.getName(), scaleTo);
doAnswer(i -> Future.succeededFuture(scaleTo)).when(mockDcOps).scaleDown(clusterCmNamespace, connect.getName(), scaleTo);
KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps);
Async async = context.async();
ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
context.assertTrue(createResult.succeeded());
verify(mockDcOps).scaleUp(clusterCmNamespace, connect.getName(), scaleTo);
async.complete();
});
}
Aggregations