Skip to content

Commit

Permalink
feat: E2E Test
Browse files Browse the repository at this point in the history
Signed-off-by: csviri <[email protected]>
  • Loading branch information
csviri committed Apr 5, 2024
1 parent d8eab2b commit 487fe8d
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 25 deletions.
33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*E2E.java</exclude>
</excludes>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
Expand Down Expand Up @@ -204,7 +210,34 @@
</java>
</configuration>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<id>e2e-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<includes>
<exclude>**/*E2E.java</exclude>
</includes>
<excludes>
<include>**/*Test.java</include>
</excludes>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<maven.home>${maven.home}</maven.home>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
14 changes: 12 additions & 2 deletions src/test/java/io/csviri/operator/resourceglue/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -39,14 +40,18 @@ public static GlueOperator loadResourceFlowOperator(String path) {
}
}

public static GenericKubernetesResource load(String path) {
public static <T extends HasMetadata> T load(String path, Class<T> clazz) {
try (InputStream is = TestUtils.class.getResourceAsStream(path)) {
return Serialization.unmarshal(is, GenericKubernetesResource.class);
return Serialization.unmarshal(is, clazz);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static GenericKubernetesResource load(String path) {
return load(path, GenericKubernetesResource.class);
}

public static GenericKubernetesResource createOrUpdate(KubernetesClient client, String path) {
return client.resource(load(path)).createOr(NonDeletingOperation::update);
}
Expand All @@ -55,6 +60,11 @@ public static void applyCrd(Class<? extends HasMetadata> resourceClass, Kubernet
applyCrd(ReconcilerUtils.getResourceTypeName(resourceClass), client);
}

public static void applyCrd(KubernetesClient client,
Class<? extends HasMetadata>... resourceClasses) {
Arrays.stream(resourceClasses).forEach(c -> applyCrd(c, client));
}

public static void applyCrd(String resourceTypeName, KubernetesClient client) {
String path = "/META-INF/fabric8/" + resourceTypeName + "-v1.yml";
try (InputStream is = TestUtils.class.getResourceAsStream(path)) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/io/csviri/operator/resourceglue/WebPageE2E.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.csviri.operator.resourceglue;

import org.junit.jupiter.api.BeforeEach;

import io.csviri.operator.resourceglue.customresource.glue.Glue;
import io.csviri.operator.resourceglue.customresource.operator.GlueOperator;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

public class WebPageE2E {

private KubernetesClient client = new KubernetesClientBuilder().build();

@BeforeEach
void applyCRDs() {
TestUtils.applyCrd(client, Glue.class, GlueOperator.class);

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.csviri.operator.resourceglue.sample.webpage;

import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Plural;
import io.fabric8.kubernetes.model.annotation.Singular;
import io.fabric8.kubernetes.model.annotation.Version;

@Version(value = "v1", storage = true, served = true)
@Group("resourceglueoperator.sample")
@Singular("webpage")
@Plural("webpages")
@javax.annotation.processing.Generated("io.fabric8.java.generator.CRGeneratorRunner")
public class WebPage extends io.fabric8.kubernetes.client.CustomResource<WebPageSpec, WebPageStatus>
implements io.fabric8.kubernetes.api.model.Namespaced {
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.csviri.operator.resourceglue.sample.webpage;

import java.util.HashMap;

import org.junit.jupiter.api.Test;

import io.csviri.operator.resourceglue.TestBase;
import io.csviri.operator.resourceglue.TestUtils;
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
Expand All @@ -21,10 +19,10 @@ public class WebPageSampleTest extends TestBase {

@Test
void webPageCRUD() {

createOrUpdate(TestUtils.load("/sample/webpage/webpage.crd.yml"));
createOrUpdate(TestUtils.load("/sample/webpage/webpage.operator.yaml"));
var webPage = createOrUpdate(TestUtils.load("/sample/webpage/webpage.sample.yaml"));
WebPage webPage =
createOrUpdate(TestUtils.load("/sample/webpage/webpage.sample.yaml", WebPage.class));

await().untilAsserted(() -> {
var deployment = get(Deployment.class, webPage.getMetadata().getName());
Expand All @@ -39,8 +37,17 @@ void webPageCRUD() {
assertThat(configMap.getData().get("index.html")).contains("Hello World!");
});

setExposed(webPage);
setNewHtml(webPage);
webPage.getSpec().setExposed(true);
webPage.getSpec().setHtml("""
<html>
<head>
<title>Hello Operator World</title>
</head>
<body>
Hello World 2!
</body>
</html>
""");
update(webPage);

await().untilAsserted(() -> {
Expand All @@ -58,21 +65,4 @@ void webPageCRUD() {
assertThat(deployment).isNull();
});
}

private void setNewHtml(GenericKubernetesResource webPage) {
((HashMap<String, Object>) webPage.getAdditionalProperties().get("spec")).put("html", """
<html>
<head>
<title>Hello Operator World</title>
</head>
<body>
Hello World 2!
</body>
</html>
""");
}

private void setExposed(GenericKubernetesResource webPage) {
((HashMap<String, Object>) webPage.getAdditionalProperties().get("spec")).put("exposed", true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.csviri.operator.resourceglue.sample.webpage;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"exposed", "html"})
public class WebPageSpec {

private Boolean exposed;

private String html;

public Boolean getExposed() {
return exposed;
}

public void setExposed(Boolean exposed) {
this.exposed = exposed;
}

public String getHtml() {
return html;
}

public void setHtml(String html) {
this.html = html;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.csviri.operator.resourceglue.sample.webpage;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

@JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({})
@JsonDeserialize(using = com.fasterxml.jackson.databind.JsonDeserializer.None.class)
public class WebPageStatus {
}

0 comments on commit 487fe8d

Please sign in to comment.