Skip to content

Commit

Permalink
ARTEMIS-3168 - more idomatic usage of mock-server-netty - with hasSta…
Browse files Browse the repository at this point in the history
…rted
  • Loading branch information
gtully committed Dec 8, 2022
1 parent f790911 commit bd72a4f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ public KubernetesClientImpl() {
String host = getParam(KUBERNETES_HOST);
String port = getParam(KUBERNETES_PORT);
this.apiUri = URI.create(String.format(KUBERNETES_TOKENREVIEW_URI_PATTERN, host, port));
logger.debug("using apiUri {}", apiUri);
}

private String getParam(String name, String defaultValue) {
String value = System.getenv(name);
public String getParam(String name, String defaultValue) {
String value = System.getProperty(name);
if (value == null) {
value = System.getProperty(name, defaultValue);
value = System.getenv(name);
}
if (value == null) {
return defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.apache.activemq.artemis.spi.core.security.jaas.KubernetesLoginModuleTest.USERNAME;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
Expand All @@ -31,20 +32,29 @@
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.model.JsonBody.json;

import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.util.Map;
import java.util.Set;

import org.apache.activemq.artemis.spi.core.security.jaas.kubernetes.model.TokenReview;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockserver.configuration.Configuration;
import org.mockserver.configuration.ConfigurationProperties;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.matchers.MatchType;
import org.mockserver.socket.PortFactory;
import org.mockserver.verify.VerificationTimes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KubernetesClientImplTest {

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static final String API_PATH = "/apis/authentication.k8s.io/v1/tokenreviews";
private static ClientAndServer mockServer;
private static final String host = "localhost";
Expand All @@ -60,53 +70,25 @@ public class KubernetesClientImplTest {
public static void startServer() {
ConfigurationProperties.dynamicallyCreateCertificateAuthorityCertificate(true);
ConfigurationProperties.directoryToSaveDynamicSSLCertificate("target/test-classes");
ConfigurationProperties.preventCertificateDynamicUpdate(true);
ConfigurationProperties.preventCertificateDynamicUpdate(false);
ConfigurationProperties.proactivelyInitialiseTLS(true);

mockServer = ClientAndServer.startClientAndServer(PortFactory.findFreePort());
Configuration configuration = Configuration.configuration();

mockServer = ClientAndServer.startClientAndServer(configuration, PortFactory.findFreePort());
port = Integer.toString(mockServer.getPort());

assertNotNull(mockServer);
assertTrue(mockServer.isRunning());
assertTrue(mockServer.hasStarted());
System.setProperty("KUBERNETES_SERVICE_HOST", host);
System.setProperty("KUBERNETES_SERVICE_PORT", port);
System.setProperty("KUBERNETES_TOKEN_PATH",
KubernetesClientImplTest.class.getClassLoader().getResource("client_token").getPath());

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH)
.withBody(json(BOB_REQUEST, MatchType.STRICT)))
.respond(
response()
.withStatusCode(HTTP_CREATED)
.withBody(UNAUTH_JSON));

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH)
.withBody(json(KERMIT_REQUEST, MatchType.STRICT)))
.respond(
response()
.withStatusCode(HTTP_CREATED)
.withBody(AUTH_JSON));

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH))
.respond(
response()
.withStatusCode(HTTP_INTERNAL_ERROR));


// proactivelyInitialiseTLS to dynamicallyCreateCertificateAuthorityCertificate
// only kicks in when the client is created to support the mock responses
URL caPath = KubernetesClientImplTest.class.getClassLoader()
.getResource("CertificateAuthorityCertificate.pem");
assertNotNull(caPath);
logger.info("Setting KUBERNETES_CA_PATH {}", caPath.getPath());
System.setProperty("KUBERNETES_CA_PATH", caPath.getPath());
}

Expand All @@ -119,9 +101,42 @@ public static void stopServer() {
mockServer.stop();
}

@Before
public void reset() {
mockServer.reset();
}

@Test
public void testGetTokenReview() {

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH)
.withBody(json(BOB_REQUEST, MatchType.STRICT)))
.respond(
response()
.withStatusCode(HTTP_CREATED)
.withBody(UNAUTH_JSON));

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH)
.withBody(json(KERMIT_REQUEST, MatchType.STRICT)))
.respond(
response()
.withStatusCode(HTTP_CREATED)
.withBody(AUTH_JSON));

mockServer.when(
request()
.withMethod("POST")
.withPath(API_PATH))
.respond(
response()
.withStatusCode(HTTP_INTERNAL_ERROR));

KubernetesClient client = new KubernetesClientImpl();

TokenReview tr = client.getTokenReview("bob_token");
Expand All @@ -144,4 +159,39 @@ public void testGetTokenReview() {

}

@Test
public void testGetParam() throws Exception {
Set<Map.Entry<String, String>> env = System.getenv().entrySet();

for (Map.Entry<String, String> envKv : env) {

if (System.getProperty(envKv.getKey()) == null) {

KubernetesClientImpl clientImpl = new KubernetesClientImpl();
assertEquals(envKv.getValue(), clientImpl.getParam(envKv.getKey(), null));

final String valFromProp = "bla";
try {
System.setProperty(envKv.getKey(), valFromProp);
assertEquals(valFromProp, clientImpl.getParam(envKv.getKey(), null));
} finally {
System.clearProperty(envKv.getKey());
}

// verify default param for non exist env or prop
String candidate = valFromProp;
for (int i = 0; i < 10; i++) {
if (System.getenv(candidate) == null && System.getProperty(candidate) == null) {
assertEquals(candidate, clientImpl.getParam(candidate, candidate));
break;
}
candidate += i;
}

// one test is sufficient!
break;
}
}
}

}

0 comments on commit bd72a4f

Please sign in to comment.