ARTEMIS-3168 - more idomatic usage of mock-server-netty - with hasStarted
This commit is contained in:
parent
f790911c44
commit
bd72a4f38d
|
@ -66,12 +66,13 @@ public class KubernetesClientImpl implements KubernetesClient {
|
|||
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;
|
||||
|
|
|
@ -23,6 +23,7 @@ import static org.apache.activemq.artemis.spi.core.security.jaas.KubernetesLogin
|
|||
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;
|
||||
|
@ -31,20 +32,29 @@ import static org.mockserver.model.HttpRequest.request;
|
|||
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";
|
||||
|
@ -60,19 +70,45 @@ 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());
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopServer() {
|
||||
System.clearProperty("KUBERNETES_SERVICE_HOST");
|
||||
System.clearProperty("KUBERNETES_SERVICE_PORT");
|
||||
System.clearProperty("KUBERNETES_TOKEN_PATH");
|
||||
System.clearProperty("KUBERNETES_CA_PATH");
|
||||
mockServer.stop();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void reset() {
|
||||
mockServer.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTokenReview() {
|
||||
|
||||
mockServer.when(
|
||||
request()
|
||||
.withMethod("POST")
|
||||
|
@ -101,27 +137,6 @@ public class KubernetesClientImplTest {
|
|||
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);
|
||||
System.setProperty("KUBERNETES_CA_PATH", caPath.getPath());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopServer() {
|
||||
System.clearProperty("KUBERNETES_SERVICE_HOST");
|
||||
System.clearProperty("KUBERNETES_SERVICE_PORT");
|
||||
System.clearProperty("KUBERNETES_TOKEN_PATH");
|
||||
System.clearProperty("KUBERNETES_CA_PATH");
|
||||
mockServer.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetTokenReview() {
|
||||
|
||||
KubernetesClient client = new KubernetesClientImpl();
|
||||
|
||||
TokenReview tr = client.getTokenReview("bob_token");
|
||||
|
@ -144,4 +159,39 @@ public class KubernetesClientImplTest {
|
|||
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue