Skip to content

Commit

Permalink
Merge pull request #123 from aguibert/hollowMode-mpRestClient
Browse files Browse the repository at this point in the history
Do not translate MP Rest Client vars if running in non-Testcontainer mode
  • Loading branch information
aguibert committed Dec 6, 2019
2 parents 59b70d2 + 4c439a7 commit 09294de
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void setConfigProperties(Map<String, String> properties) {
if (WLP_USR_DIR == null)
WLP_USR_DIR = System.getProperty("wlp.user.dir");
if (WLP_USR_DIR == null)
throw new IllegalStateException("The 'w.p.user.dir', 'WLP_USR_DIR', or '" + CONFIG_FILE_PROP
throw new IllegalStateException("The 'wlp.user.dir', 'WLP_USR_DIR', or '" + CONFIG_FILE_PROP
+ "' property must be set in order to dynamically set config properties");
Path usrDir = Paths.get(WLP_USR_DIR);
configFile = usrDir.resolve("servers/defaultServer/configDropins/defaults/system-test-vars.xml");
Expand Down Expand Up @@ -111,10 +111,12 @@ public ImageFromDockerfile getDefaultImage(File appFile) {
if (configDirExists) {
builder.copy("/config", "/config");
}
// Best practice is to run configure.sh after the app is added, but we will
// run it before adding the app because due to how often the app changes while
// running tests this will yeild the most overall time saved
builder.run("configure.sh");
// // Best practice is to run configure.sh after the app is added, but we will
// // run it before adding the app because due to how often the app changes while
// // running tests this will yeild the most overall time saved
// TODO: Cache does not work correctly when running the previous docker line
// which causes configure.sh to be run every time. See https://github.com/MicroShed/microshed-testing/issues/122
// builder.run("configure.sh");
builder.add("/config/dropins/" + appName, "/config/dropins/" + appName);
builder.build();
})
Expand Down
33 changes: 33 additions & 0 deletions modules/testcontainers/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
ext.title = "MicroShed Testing Framework :: Testcontainers extension"
description = "Extensions for using MicroShed Testing with Testcontainers for managing application and resource lifecycle"

configurations {
intTestImplementation.extendsFrom testImplementation
intTestCompile.extendsFrom testCompile
intTestRuntimeOnly.extendsFrom testRuntimeOnly
}

sourceSets {
integrationTest {
compileClasspath += sourceSets.main.output + configurations.testCompile
runtimeClasspath += output + compileClasspath + configurations.intTestImplementation
}
}

dependencies {
compile project(':microshed-testing-core')
compile 'org.testcontainers:junit-jupiter:1.12.4'
Expand All @@ -19,3 +32,23 @@ test {
apply from: publishScript

publishToMavenLocal.dependsOn ':microshed-testing-core:publishToMavenLocal'

task integrationTest(type: Test) {
description = 'Runs integration tests.'
group = 'verification'
defaultCharacterEncoding = "UTF-8"
useJUnitPlatform()
testLogging {
displayGranularity 1
showStandardStreams = true
showStackTraces = true
exceptionFormat = 'full'
events 'PASSED', 'FAILED', 'SKIPPED'
}

testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
shouldRunAfter test
}

check.dependsOn integrationTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019 IBM Corporation and others
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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 org.microshed.testing.testcontainers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.file.Paths;
import java.util.Map;

import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.junit.jupiter.api.Test;

public class ApplicationContainerIT {

// Base uri configured with: com_example_StringRestClient_mp_rest_url
@RegisterRestClient
public static class SampleRestClient1 {
}

// Base uri configured with: rc_config_key_mp_rest_url
@RegisterRestClient(configKey = "rc-config-key")
public static class SampleRestClient2 {
}

// Base uri configured with: CLIENT_CONFIG_KEY_mp_rest_url
@RegisterRestClient(configKey = "CLIENT_CONFIG_KEY")
public static class SampleRestClient3 {
}

@Test
public void testMpRestClient() {
final String clientUrl = "http://example.com";

@SuppressWarnings("resource")
ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"))
.withMpRestClient("com.example.StringRestClient", clientUrl)
.withMpRestClient(SampleRestClient1.class, clientUrl)
.withMpRestClient(SampleRestClient2.class, clientUrl)
.withMpRestClient(SampleRestClient3.class, clientUrl);

Map<String, String> appEnv = app.getEnvMap();
assertEquals(clientUrl, appEnv.get("com_example_StringRestClient_mp_rest_url"));
assertEquals(clientUrl, appEnv.get("org_microshed_testing_testcontainers_ApplicationContainerIT_SampleRestClient1_mp_rest_url"));
assertEquals(clientUrl, appEnv.get("rc_config_key_mp_rest_url"));
assertEquals(clientUrl, appEnv.get("CLIENT_CONFIG_KEY_mp_rest_url"));
}

@Test
public void testCorrectServerAdapter() {
@SuppressWarnings("resource")
ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"));
assertEquals("org.microshed.testing.testcontainers.ApplicationContainer.DefaultServerAdapter", app.getServerAdapter().getClass().getCanonicalName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2019 IBM Corporation and others
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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 org.microshed.testing.testcontainers.config;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;
import org.microshed.testing.ApplicationEnvironment;
import org.microshed.testing.jupiter.MicroShedTest;
import org.microshed.testing.testcontainers.ApplicationContainer;
import org.testcontainers.containers.MockServerContainer;
import org.testcontainers.junit.jupiter.Container;

@MicroShedTest
public class TestcontainersConfigurationIT {

@Container
public static ApplicationContainer app = new ApplicationContainer(Paths.get("src", "integrationTest", "resources", "Dockerfile"))
.withEnv("SVC_HOST", "mockserver")
.withEnv("SVC_PORT", "1080")
.withEnv("SVC_URL1", "mockserver")
.withEnv("SVC_URL2", "mockserver:1080")
.withEnv("SVC_URL3", "http://mockserver:1080")
.withEnv("SVC_URL4", "http://mockserver:1080/hello/world")
.withEnv("SVC_URL5", "http://mockserver:1080/hello/mockserver")
.withEnv("SVC_URL6", oldValue -> "http://mockserver:1080")
.withMpRestClient("com.foo.ExampleClass", "http://mockserver:1080");

@Container
public static MockServerContainer mockServer = new MockServerContainer()
.withNetworkAliases("mockserver")
.withEnv("STAYS_UNCHANGED", "mockserver");

@Test
public void testCorrectEnvironment() {
assertEquals(TestcontainersConfiguration.class, ApplicationEnvironment.load().getClass());
assertTrue(ApplicationEnvironment.isSelected(TestcontainersConfiguration.class),
"Expected TestcontainersConfiguration to be selected but it was not");
}

@Test
public void testExposedPort() {
assertTrue(app.getMappedPort(9080) != 9080, "Port 9080 should have been mapped to a random port but was not");
assertTrue(mockServer.getMappedPort(1080) != 1080, "Port 9080 should have been mapped to a random port but was not");
}

@Test
public void testApplicationURL() {
String appUrl = ApplicationEnvironment.load().getApplicationURL();
assertNotNull(appUrl);
assertEquals(appUrl, app.getApplicationURL());
assertTrue(appUrl.startsWith("http://"), "Application URL did not start with 'http://' " + appUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM openliberty/open-liberty:full-java8-openj9-ubi
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
log4j.rootLogger=INFO, stdout

log4j.appender=org.apache.log4j.ConsoleAppender
log4j.appender.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%r %p %c %x - %m%n
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,14 @@ private String readMpRestClientConfigKey(Class<?> restClientClass) {
* @return the current instance
*/
public ApplicationContainer withMpRestClient(String restClientClass, String hostUrl) {
// Sanitize environment variable name using Environment Variables Mapping Rules defined in MP Config:
// If we will be running in Docker, sanitize environment variable name using Environment Variables Mapping Rules defined in MP Config:
// https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#environment-variables-mapping-rules
String envName = restClientClass.replaceAll("[^a-zA-Z0-9_]", "_") + "_mp_rest_url";
return withEnv(envName, hostUrl);
if (ApplicationEnvironment.isSelected(TestcontainersConfiguration.class)) {
restClientClass = restClientClass.replaceAll("[^a-zA-Z0-9_]", "_") + "_mp_rest_url";
} else {
restClientClass += "/mp-rest/url";
}
return withEnv(restClientClass, hostUrl);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public void testMpRestClient() {
.withMpRestClient(SampleRestClient3.class, clientUrl);

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public void testFixedExposedPort() {
@Test
public void testEnvVarTranslation() {
Map<String, String> envMap = app.getEnvMap();
assertEquals("localhost", envMap.get("SVC_HOST"));
assertEquals("localhost", envMap.get("SVC_URL1"));
assertEquals("localhost:1080", envMap.get("SVC_URL2"));
assertEquals("http://localhost:1080", envMap.get("SVC_URL3"));
assertEquals("http://localhost:1080/hello/world", envMap.get("SVC_URL4"));
assertEquals("http://localhost:1080/hello/mockserver", envMap.get("SVC_URL5"));
assertEquals("http://localhost:1080", envMap.get("SVC_URL6"));
assertEquals("http://localhost:1080", envMap.get("com_foo_ExampleClass_mp_rest_url"));
assertEquals("localhost", envMap.get("SVC_HOST"), envMap.toString());
assertEquals("localhost", envMap.get("SVC_URL1"), envMap.toString());
assertEquals("localhost:1080", envMap.get("SVC_URL2"), envMap.toString());
assertEquals("http://localhost:1080", envMap.get("SVC_URL3"), envMap.toString());
assertEquals("http://localhost:1080/hello/world", envMap.get("SVC_URL4"), envMap.toString());
assertEquals("http://localhost:1080/hello/mockserver", envMap.get("SVC_URL5"), envMap.toString());
assertEquals("http://localhost:1080", envMap.get("SVC_URL6"), envMap.toString());
assertEquals("http://localhost:1080", envMap.get("com.foo.ExampleClass/mp-rest/url"), envMap.toString());
}

@Test
Expand Down

0 comments on commit 09294de

Please sign in to comment.