@Override public String[] getEnvironmentVariables() { return Optional.ofNullable(configuration.getEnv()) .map(env -> env.stream().toArray(String[]::new)) .orElse(null); }
@Override public String[] getEnvironmentVariables() { return Optional.ofNullable(configuration.getEnv()) .map(env -> env.stream().toArray(String[]::new)) .orElse(null); }
private void addEnvironment(Properties properties) { if (configuration.getEnv() != null) { Properties oldProperties = getProperties(configuration.getEnv()); oldProperties.putAll(properties); configuration.setEnv(toEnvironment(oldProperties)); } else { configuration.setEnv(toEnvironment(properties)); } }
private void addEnvironment(Properties properties) { if (configuration.getEnv() != null) { Properties oldProperties = getProperties(configuration.getEnv()); oldProperties.putAll(properties); configuration.setEnv(toEnvironment(oldProperties)); } else { configuration.setEnv(toEnvironment(properties)); } }
private void mergeContainerObjectConfiguration() { mergedConfiguration = new CubeContainer(); if (providedConfiguration != null) { mergedConfiguration.merge(providedConfiguration); } mergedConfiguration.merge(generatedConfigutation); // TODO if both provided and generated configurations have environment variables or volumes, they must be merged instead. // TODO should this be handled in CubeContainer::merge() instead? if (providedConfiguration != null) { // environment variables if (providedConfiguration.getEnv() != null && generatedConfigutation.getEnv() != null) { Collection<String> env = new ArrayList<>(); env.addAll(mergedConfiguration.getEnv()); env.addAll(generatedConfigutation.getEnv()); mergedConfiguration.setEnv(env); } // volumes if (providedConfiguration.getBinds() != null && generatedConfigutation.getBinds() != null) { Collection<String> binds = new ArrayList<>(); binds.addAll(mergedConfiguration.getBinds()); binds.addAll(generatedConfigutation.getBinds()); mergedConfiguration.setBinds(binds); } } }
private void mergeContainerObjectConfiguration() { mergedConfiguration = new CubeContainer(); if (providedConfiguration != null) { mergedConfiguration.merge(providedConfiguration); } mergedConfiguration.merge(generatedConfigutation); // TODO if both provided and generated configurations have environment variables or volumes, they must be merged instead. // TODO should this be handled in CubeContainer::merge() instead? if (providedConfiguration != null) { // environment variables if (providedConfiguration.getEnv() != null && generatedConfigutation.getEnv() != null) { Collection<String> env = new ArrayList<>(); env.addAll(mergedConfiguration.getEnv()); env.addAll(generatedConfigutation.getEnv()); mergedConfiguration.setEnv(env); } // volumes if (providedConfiguration.getBinds() != null && generatedConfigutation.getBinds() != null) { Collection<String> binds = new ArrayList<>(); binds.addAll(mergedConfiguration.getBinds()); binds.addAll(generatedConfigutation.getBinds()); mergedConfiguration.setBinds(binds); } } }
public static void adaptLinksToParallelRun(UUID uuid, CubeContainer cubeContainer) { final Collection<Link> links = cubeContainer.getLinks(); if (links == null) { return; } for (Link link : links) { if (link.getName().endsWith("*")) { String linkTemplate = link.getName().substring(0, link.getName().lastIndexOf('*')); link.setName(generateNewName(linkTemplate, uuid)); String environmentVariable = linkTemplate.toUpperCase() + "_HOSTNAME=" + link.getName(); if (link.isAliasSet()) { link.setAlias(generateNewName(link.getAlias(), uuid)); environmentVariable = linkTemplate.toUpperCase() + "_HOSTNAME=" + link.getAlias(); } final Collection<String> env = cubeContainer.getEnv(); if (env != null) { // to avoid duplicates if (env.contains(environmentVariable)) { env.remove(environmentVariable); } } else { cubeContainer.setEnv(new ArrayList<>()); } cubeContainer.getEnv().add(environmentVariable); } } }
public static void adaptLinksToParallelRun(UUID uuid, CubeContainer cubeContainer) { final Collection<Link> links = cubeContainer.getLinks(); if (links == null) { return; } for (Link link : links) { if (link.getName().endsWith("*")) { String linkTemplate = link.getName().substring(0, link.getName().lastIndexOf('*')); link.setName(generateNewName(linkTemplate, uuid)); String environmentVariable = linkTemplate.toUpperCase() + "_HOSTNAME=" + link.getName(); if (link.isAliasSet()) { link.setAlias(generateNewName(link.getAlias(), uuid)); environmentVariable = linkTemplate.toUpperCase() + "_HOSTNAME=" + link.getAlias(); } final Collection<String> env = cubeContainer.getEnv(); if (env != null) { // to avoid duplicates if (env.contains(environmentVariable)) { env.remove(environmentVariable); } } else { cubeContainer.setEnv(new ArrayList<>()); } cubeContainer.getEnv().add(environmentVariable); } } }
@Test public void shouldReadEnvironmentVarsFromFile() throws URISyntaxException, IOException { URI readEnvsDockerCompose = DockerComposeConverterTest.class.getResource("/read-envs-docker-compose.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(readEnvsDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("webapp"); assertThat(webapp.getEnv(), is(notNullValue())); Collection<String> env = webapp.getEnv(); assertThat(env, containsInAnyOrder("RACK_ENV=development")); }
@Test public void shouldExtendDockerCompose() throws URISyntaxException, IOException { URI readEnvsDockerCompose = DockerComposeConverterTest.class.getResource("/extends-docker-compose.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(readEnvsDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("web"); assertThat(webapp.getEnv(), is(notNullValue())); Collection<String> env = webapp.getEnv(); assertThat(env, containsInAnyOrder("REDIS_HOST=redis-production.example.com")); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("8080->8080"))); }
@Test public void shouldExtendDockerComposeV2() throws URISyntaxException, IOException { URI readEnvsDockerCompose = DockerComposeConverterTest.class.getResource("/extends-docker-compose-v2.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(readEnvsDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("web"); assertThat(webapp.getEnv(), is(notNullValue())); Collection<String> env = webapp.getEnv(); assertThat(env, containsInAnyOrder("REDIS_HOST=redis-production.example.com")); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("8080->8080"))); }
@Test public void shouldExtendDockerComposeWithEnvResolution() throws URISyntaxException, IOException { String oldValue = System.getProperty("ports"); System.setProperty("ports", "9090:8080"); try { URI readEnvsDockerCompose = DockerComposeConverterTest.class.getResource("/extends-docker-compose-env.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(readEnvsDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("web"); assertThat(webapp.getEnv(), is(notNullValue())); Collection<String> env = webapp.getEnv(); assertThat(env, containsInAnyOrder("REDIS_HOST=redis-production.example.com")); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("9090->8080"))); } finally { System.clearProperty("ports"); if (oldValue != null) System.setProperty("ports", oldValue); } }
@Test public void shouldExtendDockerComposeV2WithEnvResolution() throws URISyntaxException, IOException { String oldValue = System.getProperty("ports"); System.setProperty("ports", "9090:8080"); try { URI readEnvsDockerCompose = DockerComposeConverterTest.class.getResource("/extends-docker-compose-env-v2.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(readEnvsDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("web"); assertThat(webapp.getEnv(), is(notNullValue())); Collection<String> env = webapp.getEnv(); assertThat(env, containsInAnyOrder("REDIS_HOST=redis-production.example.com")); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("9090->8080"))); } finally { System.clearProperty("ports"); if (oldValue != null) System.setProperty("ports", oldValue); } }
@Test public void shouldStartAContainerObjectDefinedAsImageAndEnvironmentVariables() { CubeContainerObjectTestEnricher cubeContainerObjectTestEnricher = new CubeContainerObjectTestEnricher(); cubeContainerObjectTestEnricher.containerObjectFactoryInstance = () -> dockerContainerObjectFactory; when(serviceLoader.all(TestEnricher.class)).thenReturn(Arrays.asList(cubeContainerObjectTestEnricher)); FifthInjectableTest injectableTest = new FifthInjectableTest(); cubeContainerObjectTestEnricher.enrich(injectableTest); final org.arquillian.cube.spi.Cube<?> image = cubeRegistry.getCube("image"); assertThat(image, is(notNullValue())); assertThat(image.hasMetadata(IsContainerObject.class), is(true)); assertThat(image.getMetadata(IsContainerObject.class).getTestClass().getName(), is(FifthInjectableTest.class.getName())); verify(cubeController, times(1)).start("image"); verify(cubeController, times(1)).create("image"); DockerCube dockerCube = (DockerCube) image; assertThat(dockerCube.configuration().getImage().toImageRef(), is("tomee:8-jre-1.7.2-webprofile")); assertThat(dockerCube.configuration().getEnv(), hasItems("a=b", "c=d")); }
@Test public void shouldTransformSimpleDockerComposeV2FormatWithNetworks() throws URISyntaxException, IOException { URI simpleDockerCompose = DockerComposeConverterTest.class.getResource("/simple-docker-compose-v2.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(simpleDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.getContainers().get("webapp"); assertThat(webapp.getBuildImage(), is(notNullValue())); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("8000->8000"))); assertThat(webapp.getDevices(), is(notNullValue())); assertThat(webapp.getVolumes(), is(notNullValue())); Collection<String> webappVolumes = (Collection<String>) webapp.getVolumes(); assertThat(webappVolumes, containsInAnyOrder("/data")); CubeContainer webapp2 = convert.getContainers().get("webapp2"); assertThat(webapp2.getImage(), is(notNullValue())); assertThat(webapp2.getPortBindings(), is(notNullValue())); assertThat(webapp2.getLinks(), is(notNullValue())); Collection<Link> links = webapp2.getLinks(); assertThat(links, containsInAnyOrder(Link.valueOf("webapp:webapp"))); assertThat(webapp2.getEnv(), is(notNullValue())); Collection<String> env = webapp2.getEnv(); assertThat(env, containsInAnyOrder("RACK_ENV=development")); Map<String, Network> networks = convert.getNetworks(); Network network = networks.get("front-tier"); assertThat(network.getDriver(), is("bridge")); Network network2 = networks.get("back-tier"); assertThat(network2.getDriver(), is("bridge")); }
@Test public void shouldTransformSimpleDockerComposeFormat() throws URISyntaxException, IOException { URI simpleDockerCompose = DockerComposeConverterTest.class.getResource("/simple-docker-compose.yml").toURI(); DockerComposeConverter dockerComposeConverter = DockerComposeConverter.create(Paths.get(simpleDockerCompose)); DockerCompositions convert = dockerComposeConverter.convert(); CubeContainer webapp = convert.get("webapp"); assertThat(webapp.getBuildImage(), is(notNullValue())); assertThat(webapp.getPortBindings(), is(notNullValue())); Collection<PortBinding> ports = webapp.getPortBindings(); assertThat(ports, containsInAnyOrder(PortBinding.valueOf("8000->8000"))); assertThat(webapp.getDevices(), is(notNullValue())); assertThat(webapp.getVolumes(), is(notNullValue())); Collection<String> volumes = (Collection<String>) webapp.getVolumes(); assertThat(volumes, containsInAnyOrder("/data")); CubeContainer webapp2 = convert.get("webapp2"); assertThat(webapp2.getImage(), is(notNullValue())); assertThat(webapp2.getPortBindings(), is(notNullValue())); assertThat(webapp2.getLinks(), is(notNullValue())); Collection<Link> links = webapp2.getLinks(); assertThat(links, containsInAnyOrder(Link.valueOf("webapp:webapp"))); assertThat(webapp2.getEnv(), is(notNullValue())); Collection<String> env = webapp2.getEnv(); assertThat(env, containsInAnyOrder("RACK_ENV=development")); }
@Test public void shouldStartAContainerObjectDefinedUsingImageAndEnvironmentVariables() { final AtomicReference<DockerCube> cubeRef = new AtomicReference<>(); try { CubeContainer ccconfig = new CubeContainer(); ccconfig.setEnv(Collections.singleton("e=f")); CubeContainerObjectConfiguration ccoconfig = new CubeContainerObjectConfiguration(ccconfig); TestContainerObjectDefinedUsingImageAndEnvironmentVariables containerObject = new DockerContainerObjectBuilder<TestContainerObjectDefinedUsingImageAndEnvironmentVariables>(dockerClientExecutor, cubeController, cubeRegistry) .withContainerObjectClass(TestContainerObjectDefinedUsingImageAndEnvironmentVariables.class) .withContainerObjectConfiguration(ccoconfig) .onCubeCreated(cubeRef::set) .build(); assertThat(containerObject, is(notNullValue())); } catch (IllegalAccessException|InvocationTargetException|IOException e) { fail(); } DockerCube cube = cubeRef.get(); assertThat(cube, is(notNullValue())); assertThat(cube.hasMetadata(IsContainerObject.class), is(true)); assertThat(cube.getMetadata(IsContainerObject.class).getTestClass(), is(nullValue())); assertThat(cube.configuration().getImage().toImageRef(), is(BASE_IMAGE)); assertThat(cube.configuration().getEnv(), hasItems("a=b", "c=d", "e=f")); verify(cubeController, times(1)).create("containerWithEnvironmentVariables"); verify(cubeController, times(1)).start("containerWithEnvironmentVariables"); }
@Test public void shouldAddEnvVarsWithHostNameInParallelizeStarCubes() { String content = "tomcat*:\n" + " image: tutum/tomcat:8.0\n" + " portBindings: [8080/tcp]\n" + " links:\n" + " - ping*\n" + "ping*:\n" + " image: jonmorehouse/ping-pong\n" + " exposedPorts: [8089/tcp]\n" + "storage:\n" + " image: tutum/mongodb"; Map<String, String> parameters = new HashMap<String, String>(); parameters.put("dockerContainers", content); parameters.put("definitionFormat", DefinitionFormat.CUBE.name()); CubeDockerConfiguration cubeConfiguration = CubeDockerConfiguration.fromMap(parameters, null); CubeDockerConfigurator cubeDockerConfigurator = new CubeDockerConfigurator(); final CubeDockerConfiguration cubeDockerConfiguration = cubeDockerConfigurator.resolveDynamicNames(cubeConfiguration); final Set<String> containerIds = cubeDockerConfiguration.getDockerContainersContent().getContainerIds(); final String tomcat = findElementStartingWith(containerIds, "tomcat"); final String ping = findElementStartingWith(containerIds, "ping"); final CubeContainer tomcatContainer = cubeDockerConfiguration.getDockerContainersContent().get(tomcat); assertThat(getFirst(tomcatContainer.getEnv()), is("PING_HOSTNAME=" + ping)); }