Skip to content

feat: generate integration test in bootstrapper #2146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bootstrapper-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>bootstrapper</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private void addJavaFiles(File projectDir, String groupId, String artifactId) {
try {
var packages = groupId.replace(".", File.separator);
var targetDir = new File(projectDir, "src/main/java/" + packages);
var targetTestDir = new File(projectDir, "src/test/java/" + packages);
FileUtils.forceMkdir(targetDir);
var classFileNamePrefix = artifactClassId(artifactId);
JAVA_FILES.forEach(f -> addTemplatedFile(projectDir, f, groupId, artifactId, targetDir,
Expand All @@ -65,6 +66,9 @@ private void addJavaFiles(File projectDir, String groupId, String artifactId) {
addTemplatedFile(projectDir, "Runner.java", groupId, artifactId, targetDir, null);
addTemplatedFile(projectDir, "ConfigMapDependentResource.java", groupId, artifactId,
targetDir, null);
addTemplatedFile(projectDir, "ReconcilerIntegrationTest.java", groupId,
artifactId,
targetTestDir, artifactClassId(artifactId) + "ReconcilerIntegrationTest.java");
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
public class ConfigMapDependentResource
extends CRUDKubernetesDependentResource<ConfigMap, {{artifactClassId}}CustomResource> {

public static final String KEY = "key";

public ConfigMapDependentResource() {
super(ConfigMap.class);
}
Expand All @@ -28,7 +30,7 @@ protected ConfigMap desired({{artifactClassId}}CustomResource primary,
.withName(primary.getMetadata().getName())
.withNamespace(primary.getMetadata().getNamespace())
.build())
.withData(Map.of("data", primary.getSpec().getValue()))
.withData(Map.of(KEY, primary.getSpec().getValue()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package {{groupId}};

import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static {{groupId}}.ConfigMapDependentResource.KEY;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

class {{artifactClassId}}ReconcilerIntegrationTest {

public static final String RESOURCE_NAME = "test1";
public static final String INITIAL_VALUE = "initial value";
public static final String CHANGED_VALUE = "changed value";

@RegisterExtension
LocallyRunOperatorExtension extension =
LocallyRunOperatorExtension.builder()
.withReconciler({{artifactClassId}}Reconciler.class)
.build();

@Test
void testCRUDOperations() {
var cr = extension.create(testResource());

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, RESOURCE_NAME);
assertThat(cm).isNotNull();
assertThat(cm.getData()).containsEntry(KEY, INITIAL_VALUE);
});

cr.getSpec().setValue(CHANGED_VALUE);
cr = extension.replace(cr);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, RESOURCE_NAME);
assertThat(cm.getData()).containsEntry(KEY, CHANGED_VALUE);
});

extension.delete(cr);

await().untilAsserted(() -> {
var cm = extension.get(ConfigMap.class, RESOURCE_NAME);
assertThat(cm).isNull();
});
}

{{artifactClassId}}CustomResource testResource() {
var resource = new {{artifactClassId}}CustomResource();
resource.setMetadata(new ObjectMetaBuilder()
.withName(RESOURCE_NAME)
.build());
resource.setSpec(new {{artifactClassId}}Spec());
resource.getSpec().setValue(INITIAL_VALUE);
return resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<artifactId>operator-framework</artifactId>
<version>${josdk.version}</version>
</dependency>
<dependency>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-junit-5</artifactId>
<version>${josdk.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-apt</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void copiesFilesToTarget() {
private void assertProjectCompiles() {
try {
var process = Runtime.getRuntime()
.exec("mvn clean install -f target/test-project/pom.xml");
.exec("mvn clean install -f target/test-project/pom.xml -DskipTests");

BufferedReader stdOut = new BufferedReader(new InputStreamReader(process.getInputStream()));

Expand Down
2 changes: 1 addition & 1 deletion caffeine-bounded-cache-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion micrometer-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion operator-framework-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.javaoperatorsdk</groupId>
<artifactId>operator-framework-bom</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
<name>Operator SDK - Bill of Materials</name>
<packaging>pom</packaging>
<description>Java SDK for implementing Kubernetes operators</description>
Expand Down
2 changes: 1 addition & 1 deletion operator-framework-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>java-operator-sdk</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion operator-framework-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion operator-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>java-operator-sdk</artifactId>
<groupId>io.javaoperatorsdk</groupId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.javaoperatorsdk</groupId>
<artifactId>java-operator-sdk</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
<name>Operator SDK for Java</name>
<description>Java SDK for implementing Kubernetes operators</description>
<packaging>pom</packaging>
Expand Down
2 changes: 1 addition & 1 deletion sample-operators/leader-election/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>sample-operators</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>sample-leader-election</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sample-operators/mysql-schema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>sample-operators</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>sample-mysql-schema-operator</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sample-operators/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>java-operator-sdk</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>sample-operators</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sample-operators/tomcat-operator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>sample-operators</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>sample-tomcat-operator</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sample-operators/webpage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.javaoperatorsdk</groupId>
<artifactId>sample-operators</artifactId>
<version>4.6.1-SNAPSHOT</version>
<version>4.7.0-SNAPSHOT</version>
</parent>

<artifactId>sample-webpage-operator</artifactId>
Expand Down