private List<Operation> createOperations(List<String> kubernetesDefinitions) { List<Operation> ops = kubernetesDefinitions.stream() .map(yaml -> { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = yaml; return Operation.createPost(this, KubernetesDescriptionService.FACTORY_LINK) .setBody(description); }).collect(Collectors.toList()); return ops; }
@Test public void testMapApplicationSuffix() throws IOException { String suffix = "generate-mcm-10"; String serviceYaml = serviceYamlFormat.replaceAll("_sufix", ""); String expetedMappedServiceYaml = serviceYamlFormat.replaceAll("_sufix", "-" + suffix); KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = serviceYaml; kd = KubernetesUtil.mapApplicationAffix(kd, suffix); assertEquals(expetedMappedServiceYaml, kd.kubernetesEntity); }
@Test public void testCreateKubernetesDescriptionWithInvalidYamlInputShouldFail() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = "invalid\nyaml\ninput";
KubernetesDescription kubernetesDescription = new KubernetesDescription(); kubernetesDescription.kubernetesEntity = serializeKubernetesEntity; kubernetesDescription.type = KubernetesUtil.DEPLOYMENT_TYPE;
@Test public void testCreateKubernetesDescription() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = sampleYamlDefinition; Operation op = Operation.createPost(UriUtils.buildUri(host, KubernetesDescriptionService .FACTORY_LINK)) .setBody(description) .setCompletion((o, ex) -> { if (ex != null) { host.log("Creating kubernetes description failed."); host.failIteration(ex); return; } else { KubernetesDescription desc = o.getBody(KubernetesDescription.class); try { assertEquals(description.kubernetesEntity, desc.kubernetesEntity); assertEquals("Service", desc.type); assertEquals(description.getKubernetesEntityAsJson(), desc.getKubernetesEntityAsJson()); } catch (Throwable e) { host.log(Utils.toString(e)); host.failIteration(e); } host.completeIteration(); } }); host.testStart(1); host.send(op); host.testWait(); }
@Test public void testSetApplicationLabelOnReplicationController() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = replicationControllerYaml; kd.type = KubernetesUtil.REPLICATION_CONTROLLER_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); ReplicationController replicationController = kd .getKubernetesEntity(ReplicationController.class); assertNotNull(replicationController); assertNotNull(replicationController.metadata); assertNotNull(replicationController.metadata.labels); assertEquals(testCompositeId, replicationController.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(replicationController.spec); assertNotNull(replicationController.spec.template); assertNotNull(replicationController.spec.template.metadata); assertNotNull(replicationController.spec.template.metadata.labels); assertEquals(testCompositeId, replicationController.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
@Test public void testSetApplicationLabelOnReplicaSet() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = replicaSetYaml; kd.type = KubernetesUtil.REPLICA_SET_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); ReplicaSet replicaSet = kd.getKubernetesEntity(ReplicaSet.class); assertNotNull(replicaSet); assertNotNull(replicaSet.metadata); assertNotNull(replicaSet.metadata.labels); assertEquals(testCompositeId, replicaSet.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(replicaSet.spec); assertNotNull(replicaSet.spec.template); assertNotNull(replicaSet.spec.template.metadata); assertNotNull(replicaSet.spec.template.metadata.labels); assertEquals(testCompositeId, replicaSet.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
@Test public void testSetApplicationLabelOnDeployment() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = deploymentYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Deployment deployment = kd.getKubernetesEntity(Deployment.class); assertNotNull(deployment); assertNotNull(deployment.metadata); assertNotNull(deployment.metadata.labels); assertEquals(testCompositeId, deployment.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); assertNotNull(deployment.spec); assertNotNull(deployment.spec.template); assertNotNull(deployment.spec.template.metadata); assertNotNull(deployment.spec.template.metadata.labels); assertEquals(testCompositeId, deployment.spec.template.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
@Test public void testCreateKubernetesDescriptionWithInvalidKubernetesShouldFail() { KubernetesDescription description = new KubernetesDescription(); description.kubernetesEntity = sampleYamlInvalidKubernetesDefinition; Operation op = Operation.createPost(UriUtils.buildUri(host, KubernetesDescriptionService .FACTORY_LINK)) .setBody(description) .setCompletion((o, ex) -> { if (ex != null) { host.log("Creating kubernetes description failed."); host.completeIteration(); return; } else { host.failIteration(new IllegalStateException("Creation of Kubernetes " + "Description with invalid yaml succeeded")); } }); host.testStart(1); host.send(op); host.testWait(); }
@Test public void testSetApplicationLabelOnDeploymentWithoutSpec() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = deploymentWithoutSpecYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; // Make sure the is no NPE kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Deployment deployment = kd.getKubernetesEntity(Deployment.class); assertNotNull(deployment); assertNotNull(deployment.metadata); assertNotNull(deployment.metadata.labels); assertEquals(testCompositeId, deployment.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
@Test public void testSetApplicationLabelOnService() throws IOException { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = serviceYamlFormat; kd.type = SERVICE_TYPE; String testCompositeId = "123456"; kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); Service service = kd.getKubernetesEntity(Service.class); assertNotNull(service); assertNotNull(service.metadata); assertNotNull(service.metadata.labels); assertEquals(testCompositeId, service.metadata.labels.get(KUBERNETES_LABEL_APP_ID)); }
public static KubernetesDescription createKubernetesEntityDescription( BaseKubernetesState state) { KubernetesDescription entityDescription = new KubernetesDescription(); entityDescription.documentSelfLink = state.descriptionLink; entityDescription.documentDescription = state.documentDescription; entityDescription.tenantLinks = state.tenantLinks; entityDescription.name = state.name; entityDescription.id = state.id; entityDescription.type = state.getType(); entityDescription.customProperties = state.customProperties; return entityDescription; }
private static KubernetesDescription createKubernetesDescription(String apiVersionPrefix, String namespace, String entityKind) { KubernetesDescription description = new KubernetesDescription(); description.apiVersion = apiVersionPrefix; description.namespace = namespace; description.type = entityKind; return description; } }
@Test public void testSetApplicationLabelOnInvalidDeployment() { KubernetesDescription kd = new KubernetesDescription(); kd.kubernetesEntity = invalidDeploymentYaml; kd.type = DEPLOYMENT_TYPE; String testCompositeId = "123456"; // Make sure the is no NPE kd = KubernetesUtil.setApplicationLabel(kd, testCompositeId); }