Skip to content

Commit 09294de

Browse files
authored
Merge pull request #123 from aguibert/hollowMode-mpRestClient
Do not translate MP Rest Client vars if running in non-Testcontainer mode
2 parents 59b70d2 + 4c439a7 commit 09294de

9 files changed

Lines changed: 214 additions & 20 deletions

File tree

modules/liberty/src/main/java/org/testcontainers/containers/liberty/LibertyAdapter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void setConfigProperties(Map<String, String> properties) {
6767
if (WLP_USR_DIR == null)
6868
WLP_USR_DIR = System.getProperty("wlp.user.dir");
6969
if (WLP_USR_DIR == null)
70-
throw new IllegalStateException("The 'w.p.user.dir', 'WLP_USR_DIR', or '" + CONFIG_FILE_PROP
70+
throw new IllegalStateException("The 'wlp.user.dir', 'WLP_USR_DIR', or '" + CONFIG_FILE_PROP
7171
+ "' property must be set in order to dynamically set config properties");
7272
Path usrDir = Paths.get(WLP_USR_DIR);
7373
configFile = usrDir.resolve("servers/defaultServer/configDropins/defaults/system-test-vars.xml");
@@ -111,10 +111,12 @@ public ImageFromDockerfile getDefaultImage(File appFile) {
111111
if (configDirExists) {
112112
builder.copy("/config", "/config");
113113
}
114-
// Best practice is to run configure.sh after the app is added, but we will
115-
// run it before adding the app because due to how often the app changes while
116-
// running tests this will yeild the most overall time saved
117-
builder.run("configure.sh");
114+
// // Best practice is to run configure.sh after the app is added, but we will
115+
// // run it before adding the app because due to how often the app changes while
116+
// // running tests this will yeild the most overall time saved
117+
// TODO: Cache does not work correctly when running the previous docker line
118+
// which causes configure.sh to be run every time. See https://github.com/MicroShed/microshed-testing/issues/122
119+
// builder.run("configure.sh");
118120
builder.add("/config/dropins/" + appName, "/config/dropins/" + appName);
119121
builder.build();
120122
})

modules/testcontainers/build.gradle

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
ext.title = "MicroShed Testing Framework :: Testcontainers extension"
22
description = "Extensions for using MicroShed Testing with Testcontainers for managing application and resource lifecycle"
33

4+
configurations {
5+
intTestImplementation.extendsFrom testImplementation
6+
intTestCompile.extendsFrom testCompile
7+
intTestRuntimeOnly.extendsFrom testRuntimeOnly
8+
}
9+
10+
sourceSets {
11+
integrationTest {
12+
compileClasspath += sourceSets.main.output + configurations.testCompile
13+
runtimeClasspath += output + compileClasspath + configurations.intTestImplementation
14+
}
15+
}
16+
417
dependencies {
518
compile project(':microshed-testing-core')
619
compile 'org.testcontainers:junit-jupiter:1.12.4'
@@ -19,3 +32,23 @@ test {
1932
apply from: publishScript
2033

2134
publishToMavenLocal.dependsOn ':microshed-testing-core:publishToMavenLocal'
35+
36+
task integrationTest(type: Test) {
37+
description = 'Runs integration tests.'
38+
group = 'verification'
39+
defaultCharacterEncoding = "UTF-8"
40+
useJUnitPlatform()
41+
testLogging {
42+
displayGranularity 1
43+
showStandardStreams = true
44+
showStackTraces = true
45+
exceptionFormat = 'full'
46+
events 'PASSED', 'FAILED', 'SKIPPED'
47+
}
48+
49+
testClassesDirs = sourceSets.integrationTest.output.classesDirs
50+
classpath = sourceSets.integrationTest.runtimeClasspath
51+
shouldRunAfter test
52+
}
53+
54+
check.dependsOn integrationTest
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2019 IBM Corporation and others
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.microshed.testing.testcontainers;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
import java.nio.file.Paths;
24+
import java.util.Map;
25+
26+
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
27+
import org.junit.jupiter.api.Test;
28+
29+
public class ApplicationContainerIT {
30+
31+
// Base uri configured with: com_example_StringRestClient_mp_rest_url
32+
@RegisterRestClient
33+
public static class SampleRestClient1 {
34+
}
35+
36+
// Base uri configured with: rc_config_key_mp_rest_url
37+
@RegisterRestClient(configKey = "rc-config-key")
38+
public static class SampleRestClient2 {
39+
}
40+
41+
// Base uri configured with: CLIENT_CONFIG_KEY_mp_rest_url
42+
@RegisterRestClient(configKey = "CLIENT_CONFIG_KEY")
43+
public static class SampleRestClient3 {
44+
}
45+
46+
@Test
47+
public void testMpRestClient() {
48+
final String clientUrl = "http://example.com";
49+
50+
@SuppressWarnings("resource")
51+
ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"))
52+
.withMpRestClient("com.example.StringRestClient", clientUrl)
53+
.withMpRestClient(SampleRestClient1.class, clientUrl)
54+
.withMpRestClient(SampleRestClient2.class, clientUrl)
55+
.withMpRestClient(SampleRestClient3.class, clientUrl);
56+
57+
Map<String, String> appEnv = app.getEnvMap();
58+
assertEquals(clientUrl, appEnv.get("com_example_StringRestClient_mp_rest_url"));
59+
assertEquals(clientUrl, appEnv.get("org_microshed_testing_testcontainers_ApplicationContainerIT_SampleRestClient1_mp_rest_url"));
60+
assertEquals(clientUrl, appEnv.get("rc_config_key_mp_rest_url"));
61+
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY_mp_rest_url"));
62+
}
63+
64+
@Test
65+
public void testCorrectServerAdapter() {
66+
@SuppressWarnings("resource")
67+
ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"));
68+
assertEquals("org.microshed.testing.testcontainers.ApplicationContainer.DefaultServerAdapter", app.getServerAdapter().getClass().getCanonicalName());
69+
}
70+
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2019 IBM Corporation and others
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* You may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.microshed.testing.testcontainers.config;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import java.nio.file.Paths;
26+
27+
import org.junit.jupiter.api.Test;
28+
import org.microshed.testing.ApplicationEnvironment;
29+
import org.microshed.testing.jupiter.MicroShedTest;
30+
import org.microshed.testing.testcontainers.ApplicationContainer;
31+
import org.testcontainers.containers.MockServerContainer;
32+
import org.testcontainers.junit.jupiter.Container;
33+
34+
@MicroShedTest
35+
public class TestcontainersConfigurationIT {
36+
37+
@Container
38+
public static ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"))
39+
.withEnv("SVC_HOST", "mockserver")
40+
.withEnv("SVC_PORT", "1080")
41+
.withEnv("SVC_URL1", "mockserver")
42+
.withEnv("SVC_URL2", "mockserver:1080")
43+
.withEnv("SVC_URL3", "http://mockserver:1080")
44+
.withEnv("SVC_URL4", "http://mockserver:1080/hello/world")
45+
.withEnv("SVC_URL5", "http://mockserver:1080/hello/mockserver")
46+
.withEnv("SVC_URL6", oldValue -> "http://mockserver:1080")
47+
.withMpRestClient("com.foo.ExampleClass", "http://mockserver:1080");
48+
49+
@Container
50+
public static MockServerContainer mockServer = new MockServerContainer()
51+
.withNetworkAliases("mockserver")
52+
.withEnv("STAYS_UNCHANGED", "mockserver");
53+
54+
@Test
55+
public void testCorrectEnvironment() {
56+
assertEquals(TestcontainersConfiguration.class, ApplicationEnvironment.load().getClass());
57+
assertTrue(ApplicationEnvironment.isSelected(TestcontainersConfiguration.class),
58+
"Expected TestcontainersConfiguration to be selected but it was not");
59+
}
60+
61+
@Test
62+
public void testExposedPort() {
63+
assertTrue(app.getMappedPort(9080) != 9080, "Port 9080 should have been mapped to a random port but was not");
64+
assertTrue(mockServer.getMappedPort(1080) != 1080, "Port 9080 should have been mapped to a random port but was not");
65+
}
66+
67+
@Test
68+
public void testApplicationURL() {
69+
String appUrl = ApplicationEnvironment.load().getApplicationURL();
70+
assertNotNull(appUrl);
71+
assertEquals(appUrl, app.getApplicationURL());
72+
assertTrue(appUrl.startsWith("http://"), "Application URL did not start with 'http://' " + appUrl);
73+
}
74+
75+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FROM openliberty/open-liberty:full-java8-openj9-ubi
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
log4j.rootLogger=INFO, stdout
2+
3+
log4j.appender=org.apache.log4j.ConsoleAppender
4+
log4j.appender.layout=org.apache.log4j.PatternLayout
5+
6+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.stdout.layout.ConversionPattern=%r %p %c %x - %m%n

modules/testcontainers/src/main/java/org/microshed/testing/testcontainers/ApplicationContainer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,14 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
427427
* @return the current instance
428428
*/
429429
public ApplicationContainer withMpRestClient(String restClientClass, String hostUrl) {
430-
// Sanitize environment variable name using Environment Variables Mapping Rules defined in MP Config:
430+
// If we will be running in Docker, sanitize environment variable name using Environment Variables Mapping Rules defined in MP Config:
431431
// https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#environment-variables-mapping-rules
432-
String envName = restClientClass.replaceAll("[^a-zA-Z0-9_]", "_") + "_mp_rest_url";
433-
return withEnv(envName, hostUrl);
432+
if (ApplicationEnvironment.isSelected(TestcontainersConfiguration.class)) {
433+
restClientClass = restClientClass.replaceAll("[^a-zA-Z0-9_]", "_") + "_mp_rest_url";
434+
} else {
435+
restClientClass += "/mp-rest/url";
436+
}
437+
return withEnv(restClientClass, hostUrl);
434438
}
435439

436440
/**

modules/testcontainers/src/test/java/org/microshed/testing/testcontainers/ApplicationContainerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public void testMpRestClient() {
5555
.withMpRestClient(SampleRestClient3.class, clientUrl);
5656

5757
Map<String, String> appEnv = app.getEnvMap();
58-
assertEquals(clientUrl, appEnv.get("com_example_StringRestClient_mp_rest_url"));
59-
assertEquals(clientUrl, appEnv.get("org_microshed_testing_testcontainers_ApplicationContainerTest_SampleRestClient1_mp_rest_url"));
60-
assertEquals(clientUrl, appEnv.get("rc_config_key_mp_rest_url"));
61-
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY_mp_rest_url"));
58+
assertEquals(clientUrl, appEnv.get("com.example.StringRestClient/mp-rest/url"), appEnv.toString());
59+
assertEquals(clientUrl, appEnv.get("org.microshed.testing.testcontainers.ApplicationContainerTest.SampleRestClient1/mp-rest/url"), appEnv.toString());
60+
assertEquals(clientUrl, appEnv.get("rc-config-key/mp-rest/url"), appEnv.toString());
61+
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY/mp-rest/url"), appEnv.toString());
6262
}
6363

6464
@Test

modules/testcontainers/src/test/java/org/microshed/testing/testcontainers/config/HollowTestcontainersConfigurationTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ public void testFixedExposedPort() {
7070
@Test
7171
public void testEnvVarTranslation() {
7272
Map<String, String> envMap = app.getEnvMap();
73-
assertEquals("localhost", envMap.get("SVC_HOST"));
74-
assertEquals("localhost", envMap.get("SVC_URL1"));
75-
assertEquals("localhost:1080", envMap.get("SVC_URL2"));
76-
assertEquals("http://localhost:1080", envMap.get("SVC_URL3"));
77-
assertEquals("http://localhost:1080/hello/world", envMap.get("SVC_URL4"));
78-
assertEquals("http://localhost:1080/hello/mockserver", envMap.get("SVC_URL5"));
79-
assertEquals("http://localhost:1080", envMap.get("SVC_URL6"));
80-
assertEquals("http://localhost:1080", envMap.get("com_foo_ExampleClass_mp_rest_url"));
73+
assertEquals("localhost", envMap.get("SVC_HOST"), envMap.toString());
74+
assertEquals("localhost", envMap.get("SVC_URL1"), envMap.toString());
75+
assertEquals("localhost:1080", envMap.get("SVC_URL2"), envMap.toString());
76+
assertEquals("http://localhost:1080", envMap.get("SVC_URL3"), envMap.toString());
77+
assertEquals("http://localhost:1080/hello/world", envMap.get("SVC_URL4"), envMap.toString());
78+
assertEquals("http://localhost:1080/hello/mockserver", envMap.get("SVC_URL5"), envMap.toString());
79+
assertEquals("http://localhost:1080", envMap.get("SVC_URL6"), envMap.toString());
80+
assertEquals("http://localhost:1080", envMap.get("com.foo.ExampleClass/mp-rest/url"), envMap.toString());
8181
}
8282

8383
@Test

0 commit comments

Comments
 (0)