Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 859293a

Browse files
authored
improve: submodule refactors and test move (#28)
1 parent 04209da commit 859293a

File tree

13 files changed

+107
-31
lines changed

13 files changed

+107
-31
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,20 @@ Project is in early phases, heading towards mvp release.
1010

1111
## Usage
1212

13-
### Unit Tests
13+
Include dependency:
14+
15+
```xml
16+
<dependency>
17+
<groupId>io.javaoperatorsdk</groupId>
18+
<artifactId>jenvtest</artifactId>
19+
<version>[version]</version>
20+
<scope>test</scope>
21+
</dependency>
22+
```
23+
24+
### In Unit Tests
1425

15-
See sample unit test [here](https://github.com/csviri/jenvtest/blob/main/src/test/java/com/csviri/jenvtest/junit/JUnitExtensionTest.java)
26+
See sample unit test [here](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/JUnitExtensionTest.java#L10-L10)
1627

1728
```java
1829

@@ -42,6 +53,12 @@ class JUnitExtensionTest {
4253
}
4354
```
4455

56+
### API
57+
58+
The underlying API can be used directly. See [KubeApiServer](https://github.com/java-operator-sdk/jenvtest/blob/main/core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServer.java#L47-L47)
59+
60+
https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java#L12-L35
61+
4562
### Testing Mutation and Validation Webhooks
4663

4764
An additional benefits os running K8S API Server this way, is that it makes easy to test
@@ -50,7 +67,7 @@ and/or
5067
[Dynamic Admission Controllers](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/)
5168

5269
In general using additional standard frameworks to implement webhookhooks is adviced, like [kubernetes-webooks-framework](https://github.com/java-operator-sdk/kubernetes-webooks-framework)
53-
with Quarkus or Spring. However, we demonstrate how it works in [this test](https://github.com/csviri/jenvtest/blob/main/src/test/java/com/csviri/jenvtest/KubernetesMutationHookHandlingTest.java)
70+
with Quarkus or Spring. However, we demonstrate how it works in [this test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubernetesMutationHookHandlingTest.java#L53-L53)
5471

5572
### How does it work
5673

core/pom.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
<groupId>org.bouncycastle</groupId>
8484
<artifactId>bcpkix-jdk18on</artifactId>
8585
</dependency>
86-
<dependency>
87-
<groupId>io.javaoperatorsdk</groupId>
88-
<artifactId>kubernetes-webhooks-framework-core</artifactId>
89-
<scope>test</scope>
90-
</dependency>
86+
9187
</dependencies>
9288
</project>

core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServerConfig.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ public class KubeAPIServerConfig {
1010
private final String jenvtestDir;
1111

1212
/**
13-
* If not set the latest binary will be selected automatically Sample: 1.26.1, 1.25.0.
13+
* If not set the latest binary will be selected automatically. Sample: 1.26.1, 1.25.0.
1414
*/
1515
private final String apiServerVersion;
1616

1717
/**
1818
* If true, tries to download binaries. If the apiServerVersion is not set and some local binaries
1919
* found won't try to download them again.
2020
*/
21-
private final boolean downloadBinaries;
21+
private final boolean offlineMode;
2222

23-
KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean downloadBinaries) {
23+
KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean offlineMode) {
2424
this.jenvtestDir = jenvtestDir;
2525
this.apiServerVersion = apiServerVersion;
26-
this.downloadBinaries = downloadBinaries;
26+
this.offlineMode = offlineMode;
2727
}
2828

2929
public String getJenvtestDir() {
@@ -34,7 +34,7 @@ public Optional<String> getApiServerVersion() {
3434
return Optional.ofNullable(apiServerVersion);
3535
}
3636

37-
public boolean isDownloadBinaries() {
38-
return downloadBinaries;
37+
public boolean isOfflineMode() {
38+
return offlineMode;
3939
}
4040
}

core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServerConfigBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public final class KubeAPIServerConfigBuilder {
1212

1313
private String jenvtestDir;
1414
private String apiServerVersion;
15-
private Boolean downloadBinaries;
15+
private Boolean offlineMode;
1616

1717
public KubeAPIServerConfigBuilder() {}
1818

@@ -30,8 +30,8 @@ public KubeAPIServerConfigBuilder withApiServerVersion(String apiServerVersion)
3030
return this;
3131
}
3232

33-
public KubeAPIServerConfigBuilder withDownloadBinaries(boolean downloadBinaries) {
34-
this.downloadBinaries = downloadBinaries;
33+
public KubeAPIServerConfigBuilder withOfflineMode(boolean downloadBinaries) {
34+
this.offlineMode = downloadBinaries;
3535
return this;
3636
}
3737

@@ -44,12 +44,12 @@ public KubeAPIServerConfig build() {
4444
this.jenvtestDir = new File(System.getProperty("user.home"), DIRECTORY_NAME).getPath();
4545
}
4646
}
47-
if (downloadBinaries == null) {
47+
if (offlineMode == null) {
4848
var downloadBinariesEnvVal = System.getenv(JENVTEST_DOWNLOAD_BINARIES);
4949
if (downloadBinariesEnvVal != null) {
50-
this.downloadBinaries = Boolean.parseBoolean(downloadBinariesEnvVal);
50+
this.offlineMode = Boolean.parseBoolean(downloadBinariesEnvVal);
5151
} else {
52-
this.downloadBinaries = true;
52+
this.offlineMode = false;
5353
}
5454
}
5555
if (apiServerVersion == null) {
@@ -58,6 +58,6 @@ public KubeAPIServerConfig build() {
5858
this.apiServerVersion = apiServerVersionEnvVar;
5959
}
6060
}
61-
return new KubeAPIServerConfig(jenvtestDir, apiServerVersion, downloadBinaries);
61+
return new KubeAPIServerConfig(jenvtestDir, apiServerVersion, offlineMode);
6262
}
6363
}

core/src/main/java/io/javaoperatorsdk/jenvtest/binary/BinaryManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void initAndDownloadIfRequired() {
2929
File binaryDir = maybeBinaryDir.orElse(null);
3030

3131
if (maybeBinaryDir.isEmpty()) {
32-
if (!config.isDownloadBinaries()) {
32+
if (config.isOfflineMode()) {
3333
throw new JenvtestException("Binaries cannot be found, and download is turned off");
3434
}
3535
binaryDir = config.getApiServerVersion().isEmpty() ? downloader.downloadLatest()

core/src/test/java/io/javaoperatorsdk/jenvtest/binary/BinaryManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BinaryManagerTest {
1212
@Test
1313
void throwsExceptionIfBinaryNotPresentAndInOfflineMode() {
1414
BinaryManager binaryManager = new BinaryManager(KubeAPIServerConfigBuilder.anAPIServerConfig()
15-
.withDownloadBinaries(false)
15+
.withOfflineMode(true)
1616
.withApiServerVersion("1.0.1")
1717
.build());
1818

samples/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,54 @@
1818
<groupId>io.javaoperatorsdk</groupId>
1919
<artifactId>jenvtest</artifactId>
2020
<version>${project.version}</version>
21+
<scope>test</scope>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.junit.jupiter</groupId>
26+
<artifactId>junit-jupiter-api</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.junit.jupiter</groupId>
30+
<artifactId>junit-jupiter-engine</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.mockito</groupId>
34+
<artifactId>mockito-core</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.slf4j</groupId>
38+
<artifactId>slf4j-api</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.logging.log4j</groupId>
42+
<artifactId>log4j-slf4j-impl</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.apache.logging.log4j</groupId>
47+
<artifactId>log4j-core</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.fabric8</groupId>
52+
<artifactId>kubernetes-client</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.eclipse.jetty</groupId>
57+
<artifactId>jetty-server</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.assertj</groupId>
62+
<artifactId>assertj-core</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.javaoperatorsdk</groupId>
67+
<artifactId>kubernetes-webhooks-framework-core</artifactId>
68+
<scope>test</scope>
2169
</dependency>
2270
</dependencies>
2371
</project>
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
package io.javaoperatorsdk.jenvtest.junit;
1+
package io.javaoperatorsdk.jenvtest;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45

56
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
6-
import io.javaoperatorsdk.jenvtest.TestUtils;
7-
8-
import static io.javaoperatorsdk.jenvtest.TestUtils.testConfigMap;
9-
import static org.assertj.core.api.Assertions.assertThat;
7+
import io.javaoperatorsdk.jenvtest.junit.EnableKubeAPIServer;
108

119
@EnableKubeAPIServer
1210
class JUnitExtensionTest {
1311

1412
@Test
1513
void testCommunication() {
1614
var client = new KubernetesClientBuilder().build();
17-
client.resource(testConfigMap()).createOrReplace();
15+
client.resource(TestUtils.testConfigMap()).createOrReplace();
1816
var cm = client.resource(TestUtils.testConfigMap()).get();
1917

20-
assertThat(cm).isNotNull();
18+
Assertions.assertThat(cm).isNotNull();
2119
}
2220
}

core/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java renamed to samples/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.jenvtest;
22

3+
import org.assertj.core.api.Assertions;
34
import org.junit.jupiter.api.Test;
45

56
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
@@ -23,11 +24,12 @@ void apiServerWithSpecificVersion() {
2324

2425
void testWithAPIServer(KubeAPIServer kubeApi) {
2526
kubeApi.start();
27+
2628
var client = new KubernetesClientBuilder().build();
2729
client.resource(TestUtils.testConfigMap()).create();
2830
var cm = client.resource(TestUtils.testConfigMap()).get();
2931

30-
assertThat(cm).isNotNull();
32+
Assertions.assertThat(cm).isNotNull();
3133

3234
kubeApi.stop();
3335
}

0 commit comments

Comments
 (0)