Skip to content

Commit e366af8

Browse files
authored
Merge pull request #1025 from oracle/fast_rest_test
Rewrite RestTest to use in-memory container
2 parents 92e5629 + 634ab2c commit e366af8

File tree

6 files changed

+369
-405
lines changed

6 files changed

+369
-405
lines changed

operator/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,24 @@
347347
<version>1.3</version>
348348
<scope>test</scope>
349349
</dependency>
350+
<dependency>
351+
<groupId>org.glassfish.jersey.test-framework</groupId>
352+
<artifactId>jersey-test-framework-core</artifactId>
353+
<version>${jersey-version}</version>
354+
<scope>test</scope>
355+
</dependency>
356+
<dependency>
357+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
358+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
359+
<version>${jersey-version}</version>
360+
<scope>test</scope>
361+
</dependency>
362+
<dependency>
363+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
364+
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
365+
<version>${jersey-version}</version>
366+
<scope>test</scope>
367+
</dependency>
350368
<dependency>
351369
<groupId>org.httpunit</groupId>
352370
<artifactId>httpunit</artifactId>

operator/src/main/java/oracle/kubernetes/operator/rest/AuthenticationFilter.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2017, Oracle Corporation and/or its affiliates. All rights reserved.
1+
// Copyright 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
22
// Licensed under the Universal Permissive License v 1.0 as shown at
33
// http://oss.oracle.com/licenses/upl.
44

@@ -44,8 +44,7 @@ public class AuthenticationFilter extends BaseDebugLoggingFilter implements Cont
4444
@Context private Application application; // TBD - does this work?
4545

4646
public static final String REST_BACKEND_PROPERTY = "RestBackend";
47-
48-
private static final String ACCESS_TOKEN_PREFIX = "Bearer ";
47+
public static final String ACCESS_TOKEN_PREFIX = "Bearer ";
4948

5049
private static LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
5150

@@ -63,12 +62,9 @@ public void filter(ContainerRequestContext req) throws IOException {
6362
String t = getAccessToken(req);
6463
RestBackend be = r.getBackend(t);
6564
req.setProperty(REST_BACKEND_PROPERTY, be);
66-
} catch (RuntimeException re) {
65+
} catch (RuntimeException | Error re) {
6766
authenticationFailure(re);
6867
throw re; // stop the filter chain if we can't authenticate
69-
} catch (Error er) {
70-
authenticationFailure(er);
71-
throw er; // stop the filter chain if we can't authenticate
7268
}
7369
LOGGER.exiting();
7470
}

operator/src/main/java/oracle/kubernetes/operator/rest/RestServer.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import java.nio.file.Files;
1212
import java.security.SecureRandom;
1313
import java.util.Collection;
14-
import java.util.HashMap;
1514
import java.util.Map;
1615
import java.util.concurrent.Executors;
1716
import java.util.concurrent.ThreadFactory;
1817
import javax.net.ssl.KeyManager;
1918
import javax.net.ssl.SSLContext;
2019
import oracle.kubernetes.operator.logging.LoggingFacade;
2120
import oracle.kubernetes.operator.logging.LoggingFactory;
21+
import oracle.kubernetes.operator.rest.resource.VersionsResource;
2222
import oracle.kubernetes.operator.work.Container;
2323
import oracle.kubernetes.operator.work.ContainerResolver;
2424
import org.apache.commons.codec.binary.Base64;
@@ -323,8 +323,21 @@ private HttpServer createHttpsServer(Container container, SSLContext ssl, String
323323

324324
private ResourceConfig createResourceConfig() {
325325
LOGGER.entering();
326-
// create a resource config that scans for JAX-RS resources and providers
327-
// in oracle.kubernetes.operator.rest package
326+
327+
ResourceConfig rc = createResourceConfig(config);
328+
329+
LOGGER.exiting();
330+
return rc;
331+
}
332+
333+
/**
334+
* Defines a resource configuration that scans for JAX-RS resources and providers in the REST
335+
* package.
336+
*
337+
* @param restConfig the operator REST configuration
338+
* @return a resource configuration
339+
*/
340+
static ResourceConfig createResourceConfig(RestConfig restConfig) {
328341
ResourceConfig rc =
329342
new ResourceConfig()
330343
.register(JacksonFeature.class)
@@ -334,15 +347,8 @@ private ResourceConfig createResourceConfig() {
334347
.register(RequestDebugLoggingFilter.class)
335348
.register(ResponseDebugLoggingFilter.class)
336349
.register(ExceptionMapper.class)
337-
.packages("oracle.kubernetes.operator.rest.resource");
338-
Map<String, Object> extraProps = new HashMap<>();
339-
340-
// attach the rest backend impl to the resource config
341-
// so that the resource impls can find it
342-
extraProps.put(RestConfig.REST_CONFIG_PROPERTY, config);
343-
rc.addProperties(extraProps);
344-
345-
LOGGER.exiting();
350+
.packages(VersionsResource.class.getPackageName());
351+
rc.setProperties(Map.of(RestConfig.REST_CONFIG_PROPERTY, restConfig));
346352
return rc;
347353
}
348354

operator/src/test/java/oracle/kubernetes/TestUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static com.meterware.simplestub.Stub.createStub;
88

9+
import ch.qos.logback.classic.LoggerContext;
910
import com.meterware.simplestub.Memento;
1011
import java.util.ArrayList;
1112
import java.util.Arrays;
@@ -18,6 +19,7 @@
1819
import java.util.logging.Logger;
1920
import java.util.logging.SimpleFormatter;
2021
import oracle.kubernetes.operator.logging.LoggingFactory;
22+
import org.slf4j.LoggerFactory;
2123

2224
public class TestUtils {
2325
/**
@@ -199,4 +201,36 @@ public <T> T getOriginalValue() {
199201
throw new UnsupportedOperationException();
200202
}
201203
}
204+
205+
public static Memento silenceJsonPathLogger() {
206+
return new JsonPathLoggerMemento();
207+
}
208+
209+
static class JsonPathLoggerMemento implements Memento {
210+
211+
private final ch.qos.logback.classic.Logger log;
212+
private final ch.qos.logback.classic.Level originalLogLevel;
213+
214+
static Memento silenceLogger() {
215+
return new JsonPathLoggerMemento();
216+
}
217+
218+
private JsonPathLoggerMemento() {
219+
LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
220+
log = logContext.getLogger("com.jayway.jsonpath.internal.path.CompiledPath");
221+
originalLogLevel = log.getLevel();
222+
log.setLevel(ch.qos.logback.classic.Level.INFO);
223+
}
224+
225+
@Override
226+
public void revert() {
227+
log.setLevel(originalLogLevel);
228+
}
229+
230+
@SuppressWarnings("unchecked")
231+
@Override
232+
public <T> T getOriginalValue() {
233+
return (T) originalLogLevel;
234+
}
235+
}
202236
}

0 commit comments

Comments
 (0)