Skip to content

Commit

Permalink
Fixes fabric8io#960 Property placeholders are not interpolated when t…
Browse files Browse the repository at this point in the history
…hey are only thing in XML

Somehow these ${...} parameters are not being picked up by maven
properly when used solely without any suffix string literal, the maven
parameter string comes off as NULL. So adding a workaround for this.
We'll use +${...} as a parameter which would be handled by dmp afterwards
  • Loading branch information
rohanKanojia committed Mar 6, 2018
1 parent 73433ff commit 992ed24
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/main/asciidoc/inc/misc/_env.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@


When creating a container one or more environment variables can be set via configuration with the `env` parameter
When creating a container one or more environment variables can be set via configuration with the `env` parameter. Somehow maven is showing weird behavior in case of interpolating single ${..} parameter; so we have provided a workaround for that. An `env` can take the following forms:

.Env format
[cols="2,5"]
|===
| Format | Description

| ${docker.container.test.ip}:8500
| Tuple consisting of two values separated by a `:`, this would get resolved as standard IP:PORT notation used across dmp.

| +${docker.container.test.ip}
| Tuple consisting only one value as a maven parameter, Somehow maven fails to interpolate a single maven parameter when no suffix is attached to the parameter. So we've added this workaround to deal with these cases; this would get resolved to standard IP by dmp.

| aStringLiteral
| Tuple consisting only one value; simple string literal. This would be interpolated as a normal string.

|===

.Example
[source,xml]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public ContainerCreateConfig environment(String envPropsFile, Map<String, String
String value = entry.getValue();
if (value == null) {
value = "";
} else if(value.contains("+")) {
/*
* This case is to handle the Maven interpolation issue which used
* to occur when using ${..} only without any suffix.
*/
value = value.substring(1, value.length());
}
envProps.put(entry.getKey(), StrSubstitutor.replace(value, mavenProps));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ public void testEnvironment() throws Exception {
cc.environment(copyPropsToFile(), envMap, Collections.<String, String>emptyMap());
JSONArray env = getEnvArray(cc);
assertNotNull(env);
assertEquals(3, env.length());
assertEquals(4, env.length());
List<String> envAsString = convertToList(env);
assertTrue(envAsString.contains("JAVA_OPTS=-Xmx512m"));
assertTrue(envAsString.contains("TEST_SERVICE=SECURITY"));
assertTrue(envAsString.contains("EXTERNAL_ENV=TRUE"));
assertTrue(envAsString.contains("TEST_HTTP_ADDR=${docker.container.consul.ip}"));
}

@Test
public void testEnvironmentEmptyPropertiesFile() {
ContainerCreateConfig cc = new ContainerCreateConfig("testImage");
cc.environment(null, getEnvMap(),Collections.<String, String>emptyMap());
JSONArray env = getEnvArray(cc);
assertEquals(2, env.length());
assertEquals(3, env.length());
}

@Test
Expand Down Expand Up @@ -122,6 +123,7 @@ private Map<String, String> getEnvMap() {
Map<String,String> envMap = new HashMap<>();
envMap.put("JAVA_OPTS", "-Xmx512m");
envMap.put("TEST_SERVICE", "LOGGING");
envMap.put("TEST_HTTP_ADDR", "+${docker.container.consul.ip}");
return envMap;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright 2015-2016 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.fabric8.maven.docker.config;

import org.apache.commons.lang3.SerializationUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RunImageConfigurationTest {
private RunImageConfiguration runImageConfiguration;

@Test
public void testEnv() {
Map<String, String> envMap = new HashMap<>();

envMap.put("ADDR_TYPE_1", "${docker.container.consul.ip}:8500");
envMap.put("ADDR_TYPE_2", "+${docker.container.consul.ip}");

runImageConfiguration = new RunImageConfiguration.Builder()
.env(envMap)
.build();

Assert.assertNotNull(runImageConfiguration);
Assert.assertNotNull(runImageConfiguration.getEnv());
Assert.assertEquals("${docker.container.consul.ip}:8500", runImageConfiguration.getEnv().get("ADDR_TYPE_1"));
Assert.assertEquals("+${docker.container.consul.ip}", runImageConfiguration.getEnv().get("ADDR_TYPE_2"));
}

@Test
public void testNetworkingConfig() {
runImageConfiguration = new RunImageConfiguration.Builder()
.network(null)
.build();
Assert.assertNotNull(runImageConfiguration);
Assert.assertNotNull(runImageConfiguration.getNetworkingConfig());
}

@Test
public void testSerialization() {
runImageConfiguration = new RunImageConfiguration.Builder()
.cmd("sleep infinity")
.env(Collections.singletonMap("foo", "bar"))
.domainname("https://fabric8.io/")
.network(new NetworkConfig.Builder()
.name("test-networkconfig")
.aliases(Arrays.asList("alias1", "alias2"))
.build())
.links(Arrays.asList("link1", "link2", "link3"))
.restartPolicy(RestartPolicy.DEFAULT)
.workingDir("/home/jenkins/")
.dependsOn(Arrays.asList("link1"))
.labels(Collections.singletonMap("foo", "bar"))
.privileged(false)
.imagePullPolicy(ImagePullPolicy.IfNotPresent.name())
.hostname("localhost")
.build();

byte[] serializedObj = serializeObj(runImageConfiguration);
Assert.assertNotNull(serializedObj);

runImageConfiguration = (RunImageConfiguration)deserializeObj(serializedObj);
Assert.assertNotNull(runImageConfiguration);
Assert.assertEquals("sleep infinity", runImageConfiguration.getCmd().getShell());
Assert.assertEquals("bar", runImageConfiguration.getEnv().get("foo"));
Assert.assertEquals("https://fabric8.io/", runImageConfiguration.getDomainname());
Assert.assertArrayEquals(new Object[] {"alias1", "alias2"}, runImageConfiguration.getNetworkingConfig().getAliases().toArray());
Assert.assertArrayEquals(new Object[] {"link1", "link2", "link3"}, runImageConfiguration.getLinks().toArray());
Assert.assertEquals(RestartPolicy.DEFAULT.getName(), runImageConfiguration.getRestartPolicy().getName());
Assert.assertEquals("/home/jenkins/", runImageConfiguration.getWorkingDir());
Assert.assertArrayEquals(new Object[] {"link1"}, runImageConfiguration.getDependsOn().toArray());
Assert.assertEquals("bar", runImageConfiguration.getLabels().get("foo"));
Assert.assertFalse(runImageConfiguration.getPrivileged());
Assert.assertEquals(ImagePullPolicy.IfNotPresent.toString(), runImageConfiguration.getImagePullPolicy());
Assert.assertEquals("localhost", runImageConfiguration.getHostname());
}

private byte[] serializeObj(Object aObject) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(aObject);
out.flush();
byte[] convertedBytes = bos.toByteArray();
return convertedBytes;
} catch(IOException exception) {
// Ignore
} finally {
try {
bos.close();
} catch(IOException exception) {
// Ignore
}
}
return null;
}

private Object deserializeObj(byte[] bytes) {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object aObject = in.readObject();
return aObject;
} catch (Exception exception) {
// Ignore
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException exception) {
// Ignore
}
}
return null;
}
}

0 comments on commit 992ed24

Please sign in to comment.