Skip to content

Commit ba20028

Browse files
committed
fix: test now uses REST app to verify that beans are properly injected
This is needed because we cannot currently use QuarkusUnitTest because of an issue with the kubernetes extension and it's not possible to inject beans in a test using QuarkusProdModeTest. The workaround then is to create a separate app that's started using QuarkusProdModeTest and that exposes some information that we can check over REST about the injected beans to verify that they are indeed properly injected.
1 parent 247b02b commit ba20028

File tree

7 files changed

+144
-14
lines changed

7 files changed

+144
-14
lines changed

operator-framework-quarkus-extension/deployment/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@
4848
<artifactId>quarkus-junit5-internal</artifactId>
4949
<scope>test</scope>
5050
</dependency>
51+
<dependency>
52+
<groupId>io.rest-assured</groupId>
53+
<artifactId>rest-assured</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.assertj</groupId>
58+
<artifactId>assertj-core</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.javaoperatorsdk</groupId>
63+
<artifactId>operator-framework-quarkus-tests-support</artifactId>
64+
<version>${project.version}</version>
65+
</dependency>
5166
</dependencies>
5267

5368
<build>
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,60 @@
11
package io.javaoperatorsdk.quarkus.extension.deployment;
22

3-
import static org.junit.jupiter.api.Assertions.assertNotNull;
3+
import static io.restassured.RestAssured.given;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
46

5-
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
7+
import io.javaoperatorsdk.quarkus.it.TestController;
8+
import io.javaoperatorsdk.quarkus.it.TestOperatorApp;
9+
import io.javaoperatorsdk.quarkus.it.TestResource;
610
import io.quarkus.test.QuarkusProdModeTest;
7-
import javax.inject.Inject;
811
import org.jboss.shrinkwrap.api.ShrinkWrap;
912
import org.jboss.shrinkwrap.api.spec.JavaArchive;
1013
import org.junit.jupiter.api.Test;
1114
import org.junit.jupiter.api.extension.RegisterExtension;
1215

16+
/**
17+
* This tests creates an application based on the application code found in the {@code
18+
* operator-framework-quarkus-tests-support} module. The app is started and accessed over REST to
19+
* assess that injected values are present and what we expect.
20+
*/
1321
public class QuarkusExtensionProcessorTest {
1422

1523
@RegisterExtension
1624
static final QuarkusProdModeTest config =
1725
new QuarkusProdModeTest()
1826
.setArchiveProducer(
19-
() -> ShrinkWrap.create(JavaArchive.class).addClasses(TestController.class))
27+
() ->
28+
ShrinkWrap.create(JavaArchive.class)
29+
.addClasses(TestOperatorApp.class, TestController.class))
2030
.setApplicationName("basic-app")
21-
.setApplicationVersion("0.1-SNAPSHOT");
31+
.setApplicationVersion("0.1-SNAPSHOT")
32+
.setRun(true);
2233

23-
@Inject TestController controller;
24-
@Inject ConfigurationService configurationService;
34+
@Test
35+
void controllerShouldExist() {
36+
// first check that we're not always returning true for any controller name :)
37+
given().when().get("/operator/does_not_exist").then().statusCode(200).body(is("false"));
38+
39+
// given the name of the TestController, the app should reply true meaning that it is indeed
40+
// injected
41+
given().when().get("/operator/" + TestController.NAME).then().statusCode(200).body(is("true"));
42+
}
2543

2644
@Test
27-
void test() {
28-
assertNotNull(controller);
29-
assertNotNull(configurationService);
30-
assertNotNull(configurationService.getConfigurationFor(controller));
45+
void configurationForControllerShouldExist() {
46+
// check that the config for the test controller can be retrieved and is conform to our
47+
// expectations
48+
final var resourceName = TestResource.class.getCanonicalName();
49+
given()
50+
.when()
51+
.get("/operator/" + TestController.NAME + "/config")
52+
.then()
53+
.statusCode(200)
54+
.body(
55+
"customResourceClass", equalTo(resourceName),
56+
"doneableClass", equalTo(resourceName + "Doneable"),
57+
"name", equalTo(TestController.NAME),
58+
"crdName", equalTo(TestController.CRD_NAME));
3159
}
3260
}

operator-framework-quarkus-extension/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<modules>
2828
<module>deployment</module>
2929
<module>runtime</module>
30+
<module>tests</module>
3031
</modules>
3132
<dependencyManagement>
3233
<dependencies>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>operator-framework-quarkus-extension-parent</artifactId>
7+
<groupId>io.javaoperatorsdk</groupId>
8+
<version>1.6.2-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>operator-framework-quarkus-tests-support</artifactId>
13+
<name>Operator SDK - Quarkus Extension - Tests Support</name>
14+
15+
16+
<properties>
17+
<maven.compiler.source>11</maven.compiler.source>
18+
<maven.compiler.target>11</maven.compiler.target>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>io.javaoperatorsdk</groupId>
24+
<artifactId>operator-framework-quarkus-extension</artifactId>
25+
<version>${project.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>io.quarkus</groupId>
29+
<artifactId>quarkus-resteasy-jackson</artifactId>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<configuration>
39+
<parameters>true</parameters>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>io.quarkus</groupId>
44+
<artifactId>quarkus-maven-plugin</artifactId>
45+
<version>${quarkus.version}</version>
46+
<executions>
47+
<execution>
48+
<goals>
49+
<goal>build</goal>
50+
</goals>
51+
</execution>
52+
</executions>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
</project>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
package io.javaoperatorsdk.quarkus.extension.deployment;
1+
package io.javaoperatorsdk.quarkus.it;
22

33
import io.javaoperatorsdk.operator.api.Context;
44
import io.javaoperatorsdk.operator.api.Controller;
55
import io.javaoperatorsdk.operator.api.DeleteControl;
66
import io.javaoperatorsdk.operator.api.ResourceController;
77
import io.javaoperatorsdk.operator.api.UpdateControl;
88

9-
@Controller(crdName = "test.example.com")
9+
@Controller(crdName = TestController.CRD_NAME, name = TestController.NAME)
1010
public class TestController implements ResourceController<TestResource> {
11+
public static final String NAME = "test";
12+
public static final String CRD_NAME = "test.example.com";
1113

1214
@Override
1315
public DeleteControl deleteResource(TestResource resource, Context<TestResource> context) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.javaoperatorsdk.quarkus.it;
2+
3+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
4+
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
5+
import javax.inject.Inject;
6+
import javax.ws.rs.GET;
7+
import javax.ws.rs.Path;
8+
import javax.ws.rs.PathParam;
9+
10+
@Path("/operator")
11+
public class TestOperatorApp {
12+
13+
@Inject TestController controller;
14+
@Inject ConfigurationService configurationService;
15+
16+
@GET
17+
@Path("{name}")
18+
// @Produces(MediaType.TEXT_PLAIN)
19+
public boolean getController(@PathParam("name") String name) {
20+
return name.equals(controller.getName());
21+
}
22+
23+
@GET
24+
@Path("{name}/config")
25+
public ControllerConfiguration getConfig(@PathParam("name") String name) {
26+
return getController(name) ? configurationService.getConfigurationFor(controller) : null;
27+
}
28+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.javaoperatorsdk.quarkus.extension.deployment;
1+
package io.javaoperatorsdk.quarkus.it;
22

33
import io.fabric8.kubernetes.client.CustomResource;
44

0 commit comments

Comments
 (0)