public static void assertComponentTypes( Map<String, ComponentTemplate<?>> components) { assertNotEmpty(components, "components"); Set<String> yamlLiterals = Stream.of(ResourceType.getValues()) .map(type -> type.getContentType()) .collect(Collectors.toSet()); components.forEach((componentName, component) -> { if (!yamlLiterals.contains(component.type)) { String errorMessage = String.format("Component '%s' has an unsupported type '%s'", componentName, component.type); throw new LocalizableValidationException(errorMessage, "compute.template.components.unsupported.type", componentName, component.type); } }); }
public static ComponentTemplate<ContainerVolumeDescription> fromContainerVolumeDescriptionToComponentTemplate( ContainerVolumeDescription description) { assertNotNull(description, "description"); ComponentTemplate<ContainerVolumeDescription> template = new ComponentTemplate<>(); template.type = ResourceType.VOLUME_TYPE.getContentType(); template.data = description; template.data.id = null; return template; }
public static ComponentTemplate<ClosureDescription> fromClosureDescriptionToComponentTemplate( ClosureDescription description) { assertNotNull(description, "description"); ComponentTemplate<ClosureDescription> template = new ComponentTemplate<>(); template.type = ResourceType.CLOSURE_TYPE.getContentType(); template.data = description; template.data.id = null; return template; }
public static ComponentTemplate<ContainerNetworkDescription> fromContainerNetworkDescriptionToComponentTemplate( ContainerNetworkDescription description) { assertNotNull(description, "description"); ComponentTemplate<ContainerNetworkDescription> template = new ComponentTemplate<>(); template.type = ResourceType.NETWORK_TYPE.getContentType(); template.data = description; transformDriversToComponentTemplate(description); template.data.id = null; return template; }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static CompositeTemplate createCompositeTemplate( List<? extends ResourceState> containerDescriptions, List<Binding.ComponentBinding> componentBindings) { CompositeTemplate compositeTemplate = new CompositeTemplate(); compositeTemplate.components = containerDescriptions.stream() .collect(Collectors.toMap(cd -> cd.name, cd -> { ComponentTemplate componentTemplate = new ComponentTemplate(); componentTemplate.type = ResourceType.CONTAINER_TYPE.getContentType(); componentTemplate.data = cd; return componentTemplate; })); compositeTemplate.bindings = componentBindings; return compositeTemplate; }
@Test public void getContentType() { assertEquals("App.Container", ResourceType.CONTAINER_TYPE.getContentType()); try { ResourceType.CONTAINER_LOAD_BALANCER_TYPE.getContentType(); fail("CONTAINER_LOAD_BALANCER_TYPE should throw error"); } catch (Exception e) { assertTrue(e instanceof IllegalStateException); } }
public static ComponentTemplate<ResourceState> fromDescriptionToComponentTemplate( NestedState nestedState, String resourceTypeName) { ResourceState description = (ResourceState) nestedState.object; assertNotNull(description, "description"); ComponentTemplate<ResourceState> template = new ComponentTemplate<>(); ResourceType resourceType = ResourceType.fromName(resourceTypeName); template.data = description; template.type = resourceType.getContentType(); template.children = nestedState.children; if (description instanceof ContainerDescription) { template.dependsOn = ((ContainerDescription) description).dependsOn; ((ContainerDescription) description).dependsOn = null; } if (description instanceof ContainerNetworkDescription) { transformDriversToComponentTemplate((ContainerNetworkDescription) description); } return template; }
@Test public void testMapper() throws Exception { URL templateUrl = getClass().getClassLoader().getResource( "WordPress_with_MySQL_containers.yaml"); ObjectMapper mapper = YamlMapper.objectMapper(); CompositeTemplate bp = mapper.readValue(templateUrl, CompositeTemplate.class); assertEquals("id", null, bp.id); assertEquals("name", "wordPressWithMySql", bp.name); assertNotNull("properties", bp.properties); assertEquals("properties.size", 1, bp.properties.size()); assertNotNull("components", bp.components); assertEquals("components.size", 2, bp.components.size()); ComponentTemplate<?> mysqlComponent = bp.components.get("mysql"); assertNotNull("components[mysql]", mysqlComponent); assertEquals("components[mysql].type", ResourceType.CONTAINER_TYPE.getContentType(), mysqlComponent.type); } }
@Test public void testDeserializeSerializeSimpleCompositeTemplateWithNetwork() throws IOException { String expectedContent = getContent("composite.simple.network.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 2, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); ContainerDescription appData = (ContainerDescription) template.components.get("app").data; assertNotNull(appData); assertTrue(appData.networks.containsKey("front")); assertTrue(appData.networks.containsKey("back")); }
@Test public void testDeserializeSerializeSimpleCompositeTemplate() throws IOException { String expectedContent = getContent("composite.simple.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 1, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); template.id = "new-id"; template.status = Status.RETIRED; ContainerDescription helloData = (ContainerDescription) template.components .get("hello").data; helloData.healthConfig = new HealthConfig(); helloData.logConfig = new LogConfig(); String content = serializeCompositeTemplate(template); assertNotNull(content); assertFalse(content.contains("new-id")); assertFalse(content.contains(Status.RETIRED.toString())); assertFalse(content.contains("health_config")); assertFalse(content.contains("log_config")); }
@Test public void testDeserializeSerializeComplexDockerComposeWithNetwork() throws IOException { String expectedContent = getContent("docker.complex.network.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeComplexCompositeTemplate() throws IOException { String expectedContent = getContent("composite.complex.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 5, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); String content = serializeCompositeTemplate(template); expectedContent = expectedContent.replace("h5-name", "h5"); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content), true); DockerCompose compose = fromCompositeTemplateToDockerCompose(template); String contentCompose = serializeDockerCompose(compose); assertTrue((contentCompose != null) && (!contentCompose.isEmpty())); }
@Test public void testDeserializeSerializeComplexCompositeTemplateWithVolume() throws IOException { String expectedContent = getContent("composite.complex.volume.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 3, template.components); String content = serializeCompositeTemplate(template); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content), true); DockerCompose compose = fromCompositeTemplateToDockerCompose(template); String contentCompose = serializeDockerCompose(compose); assertTrue((contentCompose != null) && (!contentCompose.isEmpty())); }
@Test public void testDeserializeSerializeComplexDockerCompose() throws IOException { String expectedContent = getContent("docker.complex.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 4, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeSimpleDockerComposeWithNetwork() throws IOException { String expectedContent = getContent("docker.simple.network.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEquals(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 2, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeSimpleDockerComposeWithVolume() throws IOException { String expectedContent = getContent("docker.simple.volume.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 2, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 2, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeComplexDockerComposeWithVolume() throws IOException { String expectedContent = getContent("docker.complex.volume.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEqualsYamls(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 3, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeComplexCompositeTemplateWithNetwork() throws IOException { String expectedContent = getContent("composite.complex.network.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 3, template.components); String content = serializeCompositeTemplate(template); expectedContent = expectedContent.replace("h5-name", "h5"); assertEqualsYamls(toUnixLineEnding(getContent("composite.complex.network.expected.yaml")), toUnixLineEnding(content), true); DockerCompose compose = fromCompositeTemplateToDockerCompose(template); String contentCompose = serializeDockerCompose(compose); assertTrue((contentCompose != null) && (!contentCompose.isEmpty())); }
assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template1.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 2, template1.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template1.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template2.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 3, template2.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template2.components);
@Test public void testPutExpanded() throws Throwable { ContainerDescription container = new ContainerDescription(); container.name = "container"; container.image = "registry.hub.docker.com/kitematic/hello-world-nginx"; container = doPost(container, ContainerDescriptionService.FACTORY_LINK); ComponentDescription containerComponent = new ComponentDescription(); containerComponent.name = "container"; container.name = "updated"; containerComponent.updateServiceDocument(container); containerComponent.type = ResourceType.CONTAINER_TYPE.getContentType(); CompositeDescription cd = new CompositeDescription(); cd.name = "testComposite"; cd = doPost(cd, CompositeDescriptionFactoryService.SELF_LINK); // Make PUT but as expanded state, so that components are also updated CompositeDescriptionExpanded cdUpdate = new CompositeDescriptionExpanded(); cdUpdate.documentSelfLink = cd.documentSelfLink; cdUpdate.name = cd.name; cdUpdate.componentDescriptions = new ArrayList<>(); cdUpdate.componentDescriptions.add(containerComponent); cdUpdate = doPut(cdUpdate); // Explicitly search for document to validate that the list returns the right document kind CompositeDescription foundCd = searchForDocument(CompositeDescription.class, cd.documentSelfLink); assertEquals(Utils.buildKind(CompositeDescription.class), foundCd.documentKind); container = getDocument(ContainerDescription.class, container.documentSelfLink); assertEquals("updated", container.name); }