private CubeConverter(String content) { this.dockerCubeDefinitionMap = ConfigUtil.load(content); }
private CubeConverter(String content) { this.dockerCubeDefinitionMap = ConfigUtil.load(content); }
private CubeConverter(Path location) throws IOException { FileInputStream inputStream = new FileInputStream(location.toFile()); this.dockerCubeDefinitionMap = ConfigUtil.load(inputStream); inputStream.close(); }
private CubeConverter(Path location) throws IOException { FileInputStream inputStream = new FileInputStream(location.toFile()); this.dockerCubeDefinitionMap = ConfigUtil.load(inputStream); inputStream.close(); }
@SuppressWarnings("unchecked") public static DockerCompositions load(InputStream inputStream) { // TODO: Figure out how to map root Map<String, Type> objects. Workaround by mapping it to Map structure then dumping it into individual objects //Apply Docker compose substitutions to cube format as well final String content = DockerComposeEnvironmentVarResolver.replaceParameters(inputStream); Yaml yaml = new Yaml(new CubeConstructor()); Map<String, Object> rawLoad = (Map<String, Object>) yaml.load(content); DockerCompositions containers = new DockerCompositions(); for (Map.Entry<String, Object> rawLoadEntry : rawLoad.entrySet()) { if (NETWORKS.equals(rawLoadEntry.getKey())) { Map<String, Object> rawNetworks = (Map<String, Object>) rawLoadEntry.getValue(); for (Map.Entry<String, Object> rawNetworkEntry : rawNetworks.entrySet()) { Network network = yaml.loadAs(yaml.dump(rawNetworkEntry.getValue()), Network.class); containers.add(rawNetworkEntry.getKey(), network); } } else if (CONTAINERS.equals(rawLoadEntry.getKey())) { Map<String, Object> rawContainers = (Map<String, Object>) rawLoadEntry.getValue(); for (Map.Entry<String, Object> rawContainerEntry : rawContainers.entrySet()) { CubeContainer cubeContainer = yaml.loadAs(yaml.dump(rawContainerEntry.getValue()), CubeContainer.class); containers.add(rawContainerEntry.getKey(), cubeContainer); } } else { CubeContainer container = yaml.loadAs(yaml.dump(rawLoadEntry.getValue()), CubeContainer.class); containers.add(rawLoadEntry.getKey(), container); } } return applyExtendsRules(containers); }
@SuppressWarnings("unchecked") public static DockerCompositions load(InputStream inputStream) { // TODO: Figure out how to map root Map<String, Type> objects. Workaround by mapping it to Map structure then dumping it into individual objects //Apply Docker compose substitutions to cube format as well final String content = DockerComposeEnvironmentVarResolver.replaceParameters(inputStream); Yaml yaml = new Yaml(new CubeConstructor()); Map<String, Object> rawLoad = (Map<String, Object>) yaml.load(content); DockerCompositions containers = new DockerCompositions(); for (Map.Entry<String, Object> rawLoadEntry : rawLoad.entrySet()) { if (NETWORKS.equals(rawLoadEntry.getKey())) { Map<String, Object> rawNetworks = (Map<String, Object>) rawLoadEntry.getValue(); for (Map.Entry<String, Object> rawNetworkEntry : rawNetworks.entrySet()) { Network network = yaml.loadAs(yaml.dump(rawNetworkEntry.getValue()), Network.class); containers.add(rawNetworkEntry.getKey(), network); } } else if (CONTAINERS.equals(rawLoadEntry.getKey())) { Map<String, Object> rawContainers = (Map<String, Object>) rawLoadEntry.getValue(); for (Map.Entry<String, Object> rawContainerEntry : rawContainers.entrySet()) { CubeContainer cubeContainer = yaml.loadAs(yaml.dump(rawContainerEntry.getValue()), CubeContainer.class); containers.add(rawContainerEntry.getKey(), cubeContainer); } } else { CubeContainer container = yaml.loadAs(yaml.dump(rawLoadEntry.getValue()), CubeContainer.class); containers.add(rawLoadEntry.getKey(), container); } } return applyExtendsRules(containers); }
@Before public void setup() { when(cubeDockerConfiguration.getDockerServerIp()).thenReturn("192.168.99.100"); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(ConfigUtil.load(SIMPLE_SCENARIO)); }
@Test public void should_resolve_to_docker_host_if_no_url_provided() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn(null); assertThatThrownBy(() -> { dockerCubeCustomizableURLResourceProvider.lookup(null); }) .hasMessage("Arquillian Cube Graphene integration should provide a URL in Graphene extension configuration."); }
@Test public void shouldGetLogFromContainer() throws IOException { File newFolder = temporaryFolder.newFolder(); String content = CONTAINER_LOG_CONFIGURATION; content += newFolder.getAbsolutePath(); content += "mylog.log"; DockerCompositions configuration = ConfigUtil.load(content); CubeContainer config = configuration.get("tomcat_default"); Mockito.when(cube.configuration()).thenReturn(config); fire(new BeforeStop(CUBE_CONTAINER_NAME)); verify(dockerClientExecutor, times(1)).copyLog(eq(CUBE_CONTAINER_NAME), eq(false), eq(false), eq(false), eq(false), eq(-1), any(OutputStream.class)); }
@Test public void shouldCopyFileFromContainer() throws IOException { File newFolder = temporaryFolder.newFolder(); String content = CONTAINER_COPY_CONFIGURATION; content += newFolder.getAbsolutePath(); DockerCompositions configuration = ConfigUtil.load(content); CubeContainer config = configuration.get("tomcat_default"); Mockito.when(cube.configuration()).thenReturn(config); Mockito.when(dockerClientExecutor.getFileOrDirectoryFromContainerAsTar(eq(CUBE_CONTAINER_NAME), anyString())) .thenReturn(BeforeStopContainerObserverTest.class.getResourceAsStream("/hello.tar")); fire(new BeforeStop(CUBE_CONTAINER_NAME)); verify(dockerClientExecutor).getFileOrDirectoryFromContainerAsTar(eq(CUBE_CONTAINER_NAME), eq("/test")); assertThat(new File(newFolder, "hello.txt").exists(), is(true)); }
@Test public void shouldExecuteCustomBeforeStopOperationForContainer() throws IOException { DockerCompositions configuration = ConfigUtil.load(CONTAINER_CUSTOM_BEFORE_STOP_ACTION_CONFIGURATION); CubeContainer config = configuration.get("tomcat_default"); Mockito.when(cube.configuration()).thenReturn(config); fire(new BeforeStop(CUBE_CONTAINER_NAME)); verify(dockerClientExecutor, times(1)).getDockerUri(); } }
@Test public void should_not_resolve_ip() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn("http://192.168.99.101:80/context"); final URL url = (URL) dockerCubeCustomizableURLResourceProvider.lookup(null); assertThat(url) .hasProtocol("http") .hasHost("192.168.99.101") .hasPort(80) .hasPath("/context"); }
@Test public void should_resolve_internal_ip_of_container() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn("http://helloworld:80/context"); final URL url = (URL) dockerCubeCustomizableURLResourceProvider.lookup(null); assertThat(url) .hasProtocol("http") .hasHost("192.168.99.100") .hasPort(80) .hasPath("/context"); }
@Test public void should_resolve_docker_host_in_relative_url() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn("http://dockerHost:80/context"); final URL url = (URL) dockerCubeCustomizableURLResourceProvider.lookup(null); assertThat(url) .hasProtocol("http") .hasHost("192.168.99.100") .hasPort(80) .hasPath("/context"); }
@Test public void should_resolve_docker_host_in_relative_url_with_default_port() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn("http://dockerHost/context"); final URL url = (URL) dockerCubeCustomizableURLResourceProvider.lookup(null); assertThat(url) .hasProtocol("http") .hasHost("192.168.99.100") .hasPort(80) .hasPath("/context"); } }
@Test public void should_resolve_internal_ip_of_container_with_default_port() { final DockerCompositions compositions = ConfigUtil.load(SIMPLE_SCENARIO); when(cubeDockerConfiguration.getDockerContainersContent()).thenReturn(compositions); when(grapheneConfiguration.getUrl()).thenReturn("http://helloworld/context"); final URL url = (URL) dockerCubeCustomizableURLResourceProvider.lookup(null); assertThat(url) .hasProtocol("http") .hasHost("192.168.99.100") .hasPort(80) .hasPath("/context"); }
@Test public void should_parse_command() { String containerDefinition = "tomcat:\n" + " image: tutum/tomcat:7.0\n" + " exposedPorts: [8089/tcp]\n" + " await:\n" + " strategy: docker_health\n" + " iterations: 5 # <1>\n" + " sleepPollingTime: 200 s # <2>\n" + " command: [\"curl\", \"localhost:8089\"] # <3>"; final DockerCompositions load = ConfigUtil.load(new ByteArrayInputStream(containerDefinition.getBytes())); Await await = load.get("tomcat").getAwait(); String[] expected = {"curl", "localhost:8089"}; Assert.assertArrayEquals(expected, await.getCommand()); } }
@Test public void should_be_able_to_create_http_await_strategy() { String containerDefinition = "tomcat:\n" + " image: tutum/tomcat:7.0\n" + " exposedPorts: [8089/tcp]\n" + " await:\n" + " strategy: http\n" + " iterations: 10\n" + " sleepPollingTime: 200 s\n" + " url: http://localhost:8080/ping\n" + " responseCode: 201\n" + " match: 'Server startup'\n" + " headers: \n" + " X-Cube: Docker\n"; final DockerCompositions load = ConfigUtil.load(new ByteArrayInputStream(containerDefinition.getBytes())); final CubeContainer tomcat = load.getContainers().get("tomcat"); HttpAwaitStrategy awaitStrategy = new HttpAwaitStrategy(cube, dockerClientExecutor, tomcat.getAwait()); assertThat(awaitStrategy.getPollIterations(), is(10)); assertThat(awaitStrategy.getUrl(), is("http://localhost:8080/ping")); assertThat(awaitStrategy.getResponseCode(), is(201)); assertThat(awaitStrategy.getMatcher(), is("Server startup")); assertThat((String) awaitStrategy.getHeaders().get("X-Cube"), is("Docker")); }
@Test public void shouldBeAbleToLoadStrategy() throws Exception { DockerCompositions containers = ConfigUtil.load(CONTENT); CubeContainer container = containers.get("tomcat");