Skip to content

Commit d518ee3

Browse files
authored
feat: introduce JUnit5 extension (#545)
1 parent d4d3406 commit d518ee3

28 files changed

+717
-588
lines changed

Diff for: operator-framework-core/pom.xml

-10
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,6 @@
8888
<scope>test</scope>
8989
</dependency>
9090

91-
<dependency>
92-
<groupId>org.apache.logging.log4j</groupId>
93-
<artifactId>log4j-slf4j-impl</artifactId>
94-
<scope>test</scope>
95-
</dependency>
96-
<dependency>
97-
<groupId>org.apache.logging.log4j</groupId>
98-
<artifactId>log4j-core</artifactId>
99-
<scope>test</scope>
100-
</dependency>
10191
<dependency>
10292
<groupId>io.fabric8</groupId>
10393
<artifactId>kubernetes-server-mock</artifactId>

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/Operator.java

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.Closeable;
44
import java.io.IOException;
55
import java.net.ConnectException;
6+
import java.util.Collections;
67
import java.util.LinkedList;
78
import java.util.List;
89
import java.util.concurrent.locks.ReentrantLock;
@@ -57,6 +58,10 @@ public ConfigurationService getConfigurationService() {
5758
return configurationService;
5859
}
5960

61+
public List<ConfiguredController> getControllers() {
62+
return Collections.unmodifiableList(controllers.controllers);
63+
}
64+
6065
/**
6166
* Finishes the operator startup process. This is mostly used in injection-aware applications
6267
* where there is no obvious entrypoint to the application which can trigger the injection process
@@ -99,6 +104,8 @@ public void close() {
99104
"Operator SDK {} is shutting down...", configurationService.getVersion().getSdkVersion());
100105

101106
controllers.close();
107+
108+
k8sClient.close();
102109
}
103110

104111
/**

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AbstractConfigurationService.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@
77
import java.util.Set;
88
import java.util.concurrent.ConcurrentHashMap;
99
import java.util.stream.Stream;
10-
import org.slf4j.Logger;
11-
import org.slf4j.LoggerFactory;
1210

1311
public abstract class AbstractConfigurationService implements ConfigurationService {
14-
15-
public static final String LOGGER_NAME = "Default ConfigurationService implementation";
16-
protected static final Logger log = LoggerFactory.getLogger(LOGGER_NAME);
17-
1812
private final Map<String, ControllerConfiguration> configurations = new ConcurrentHashMap<>();
1913
private final Version version;
2014

@@ -60,12 +54,14 @@ public <R extends CustomResource> ControllerConfiguration<R> getConfigurationFor
6054
final var key = keyFor(controller);
6155
final var configuration = configurations.get(key);
6256
if (configuration == null) {
63-
log.warn(
64-
"Configuration for controller '{}' was not found. {}", key, getControllersNameMessage());
57+
logMissingControllerWarning(key, getControllersNameMessage());
6558
}
6659
return configuration;
6760
}
6861

62+
protected abstract void logMissingControllerWarning(String controllerKey,
63+
String controllersNameMessage);
64+
6965
private String getControllersNameMessage() {
7066
return "Known controllers: "
7167
+ getKnownControllerNames().stream().reduce((s, s2) -> s + ", " + s2).orElse("None")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.javaoperatorsdk.operator.api.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class BaseConfigurationService extends AbstractConfigurationService {
7+
8+
private static final String LOGGER_NAME = "Default ConfigurationService implementation";
9+
private static final Logger logger = LoggerFactory.getLogger(LOGGER_NAME);
10+
11+
public BaseConfigurationService(Version version) {
12+
super(version);
13+
}
14+
15+
@Override
16+
protected void logMissingControllerWarning(String controllerKey, String controllersNameMessage) {
17+
logger.warn("Configuration for controller '{}' was not found. {}", controllerKey,
18+
controllersNameMessage);
19+
}
20+
21+
public String getLoggerName() {
22+
return LOGGER_NAME;
23+
}
24+
25+
protected Logger getLogger() {
26+
return logger;
27+
}
28+
}

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/Utils.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ public static Version loadFromProperties() {
3737

3838
Date builtTime;
3939
try {
40-
builtTime =
41-
// RFC 822 date is the default format used by git-commit-id-plugin
42-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
43-
.parse(properties.getProperty("git.build.time"));
40+
String time = properties.getProperty("git.build.time");
41+
if (time != null) {
42+
builtTime =
43+
// RFC 822 date is the default format used by git-commit-id-plugin
44+
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(time);
45+
} else {
46+
builtTime = Date.from(Instant.EPOCH);
47+
}
4448
} catch (Exception e) {
4549
log.debug("Couldn't parse git.build.time property", e);
4650
builtTime = Date.from(Instant.EPOCH);

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/Version.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.javaoperatorsdk.operator.api.config;
22

3+
import java.time.Instant;
34
import java.util.Date;
45

56
/** A class encapsulating the version information associated with this SDK instance. */
67
public class Version {
78

9+
public static final Version UNKNOWN = new Version("unknown", "unknown", Date.from(Instant.EPOCH));
10+
811
private final String sdk;
912
private final String commit;
1013
private final Date builtTime;

Diff for: operator-framework-junit5/pom.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-operator-sdk</artifactId>
7+
<groupId>io.javaoperatorsdk</groupId>
8+
<version>1.9.7-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>operator-framework-junit-5</artifactId>
13+
<name>Operator SDK - Framework - JUnit 5 extension</name>
14+
15+
<properties>
16+
<maven.compiler.source>11</maven.compiler.source>
17+
<maven.compiler.target>11</maven.compiler.target>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>io.javaoperatorsdk</groupId>
23+
<artifactId>operator-framework-core</artifactId>
24+
<version>${project.version}</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-api</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.junit.jupiter</groupId>
32+
<artifactId>junit-jupiter-engine</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.assertj</groupId>
36+
<artifactId>assertj-core</artifactId>
37+
<version>3.20.2</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.awaitility</groupId>
41+
<artifactId>awaitility</artifactId>
42+
<version>4.1.0</version>
43+
</dependency>
44+
</dependencies>
45+
46+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.javaoperatorsdk.operator.junit;
2+
3+
import io.fabric8.kubernetes.client.KubernetesClient;
4+
5+
public interface HasKubernetesClient {
6+
KubernetesClient getKubernetesClient();
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.javaoperatorsdk.operator.junit;
2+
3+
import io.fabric8.kubernetes.client.KubernetesClient;
4+
5+
public interface KubernetesClientAware extends HasKubernetesClient {
6+
void setKubernetesClient(KubernetesClient kubernetesClient);
7+
}

0 commit comments

Comments
 (0)