-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14821 from FroMage/14744
Support KubernetesServer test resource for CRUD operations
- Loading branch information
Showing
24 changed files
with
809 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...src/test/java/io/quarkus/it/kubernetes/client/CustomKubernetesTestServerTestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import java.util.Base64; | ||
|
||
import io.fabric8.kubernetes.api.model.ConfigMapBuilder; | ||
import io.fabric8.kubernetes.api.model.SecretBuilder; | ||
import io.quarkus.test.kubernetes.client.KubernetesServerTestResource; | ||
|
||
public class CustomKubernetesTestServerTestResource extends KubernetesServerTestResource { | ||
|
||
// setup the ConfigMap objects that the application expects to lookup configuration from | ||
@Override | ||
protected void configureServer() { | ||
server.getClient().inNamespace("test").configMaps().create(configMapBuilder("cmap1") | ||
.addToData("dummy", "dummy") | ||
.addToData("overridden.secret", "cm") // will be overridden since secrets have a higher priority | ||
.addToData("some.prop1", "val1") | ||
.addToData("some.prop2", "val2") | ||
.addToData("some.prop4", "v4") // will be overridden since cmap2 has a higher priority | ||
.addToData("some.prop5", "val5") | ||
.addToData("application.properties", "some.prop3=val3") | ||
.addToData("application.yaml", "some:\n prop4: val4").build()); | ||
|
||
server.getClient().inNamespace("test").configMaps().create(configMapBuilder("cmap2") | ||
.addToData("application.yaml", "some:\n prop4: val4").build()); | ||
|
||
server.getClient().inNamespace("test").configMaps().create(configMapBuilder("cmap3") | ||
.addToData("dummy", "dummyFromDemo") | ||
.addToData("some.prop1", "val1FromDemo") | ||
.addToData("some.prop2", "val2FromDemo") | ||
.addToData("some.prop5", "val5FromDemo") | ||
.addToData("application.properties", "some.prop3=val3FromDemo") | ||
.addToData("application.yaml", "some:\n prop4: val4FromDemo").build()); | ||
|
||
server.getClient().inNamespace("test").secrets().create(secretBuilder("s1") | ||
.addToData("dummysecret", encodeValue("dummysecret")) | ||
.addToData("overridden.secret", encodeValue("secret")) | ||
.addToData("secret.prop1", encodeValue("val1")) | ||
.addToData("secret.prop2", encodeValue("val2")) | ||
.addToData("application.properties", encodeValue("secret.prop3=val3")) | ||
.addToData("application.yaml", encodeValue("secret:\n prop4: val4")).build()); | ||
|
||
server.getClient().inNamespace("test").secrets().create(secretBuilder("s1") | ||
.addToData("dummysecret", encodeValue("dummysecretFromDemo")) | ||
.addToData("overridden.secret", encodeValue("secretFromDemo")) | ||
.addToData("secret.prop1", encodeValue("val1FromDemo")) | ||
.addToData("secret.prop2", encodeValue("val2FromDemo")) | ||
.addToData("application.properties", encodeValue("secret.prop3=val3FromDemo")) | ||
.addToData("application.yaml", encodeValue("secret:\n prop4: val4FromDemo")).build()); | ||
} | ||
|
||
private ConfigMapBuilder configMapBuilder(String name) { | ||
return new ConfigMapBuilder().withNewMetadata() | ||
.withName(name).endMetadata(); | ||
} | ||
|
||
private SecretBuilder secretBuilder(String name) { | ||
return new SecretBuilder().withNewMetadata() | ||
.withName(name).endMetadata(); | ||
} | ||
|
||
private String encodeValue(String value) { | ||
return Base64.getEncoder().encodeToString(value.getBytes()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ernetes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesNewClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.kubernetes.api.model.PodBuilder; | ||
import io.fabric8.kubernetes.client.server.mock.KubernetesServer; | ||
import io.quarkus.test.common.QuarkusTestResource; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.kubernetes.client.KubernetesTestServer; | ||
import io.restassured.RestAssured; | ||
|
||
/* | ||
* KubernetesClientTest.TestResource contains the entire process of setting up the Mock Kubernetes API Server | ||
* It has to live there otherwise the Kubernetes client in native mode won't be able to locate the mock API Server | ||
*/ | ||
@QuarkusTestResource(value = CustomKubernetesTestServerTestResource.class, restrictToAnnotatedClass = true) | ||
@QuarkusTest | ||
public class KubernetesNewClientTest { | ||
|
||
@KubernetesTestServer | ||
private KubernetesServer mockServer; | ||
|
||
@Test | ||
public void testInteractionWithAPIServer() throws InterruptedException { | ||
setupMockServerForTest(); | ||
|
||
RestAssured.when().get("/pod/test").then() | ||
.body("size()", is(2)).body(containsString("pod1"), containsString("pod2")); | ||
|
||
RestAssured.when().delete("/pod/test").then() | ||
.statusCode(204); | ||
|
||
RestAssured.when().put("/pod/test").then() | ||
.body(containsString("value1")); | ||
|
||
RestAssured.when().post("/pod/test").then() | ||
.body(containsString("12345")); | ||
} | ||
|
||
private void setupMockServerForTest() { | ||
Pod pod1 = new PodBuilder().withNewMetadata().withName("pod1").withNamespace("test").and().build(); | ||
Pod pod2 = new PodBuilder().withNewMetadata().withName("pod2").withNamespace("test").and().build(); | ||
|
||
mockServer.getClient().inNamespace("test").pods().create(pod1); | ||
mockServer.getClient().inNamespace("test").pods().create(pod2); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...netes-client/src/test/java/io/quarkus/it/kubernetes/client/KubernetesNewClientTestIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import io.quarkus.test.junit.NativeImageTest; | ||
|
||
@NativeImageTest | ||
public class KubernetesNewClientTestIT extends KubernetesNewClientTest { | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...ient/src/test/java/io/quarkus/it/kubernetes/client/KubernetesTestServerOnProfileTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.it.kubernetes.client; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.fabric8.kubernetes.client.server.mock.KubernetesServer; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.quarkus.test.kubernetes.client.KubernetesTestServer; | ||
import io.quarkus.test.kubernetes.client.WithKubernetesTestServer; | ||
|
||
/* | ||
* This class has no native-image test because it relies on setting config overrides that clash | ||
* with native image build config. | ||
* This is the same test as KubernetesTestServerTest but with the test resource annotation on the profile | ||
*/ | ||
@TestProfile(KubernetesTestServerOnProfileTest.MyProfile.class) | ||
@QuarkusTest | ||
public class KubernetesTestServerOnProfileTest { | ||
|
||
private static KubernetesServer setupServer; | ||
|
||
public static class Setup implements Consumer<KubernetesServer> { | ||
|
||
@Override | ||
public void accept(KubernetesServer t) { | ||
setupServer = t; | ||
} | ||
|
||
} | ||
|
||
@KubernetesTestServer | ||
private KubernetesServer mockServer; | ||
|
||
@Test | ||
public void testConfiguration() throws InterruptedException { | ||
// we can't really test CRUD, and HTTPS doesn't work | ||
Assertions.assertEquals(10001, mockServer.getMockServer().getPort()); | ||
Assertions.assertSame(mockServer, setupServer); | ||
} | ||
|
||
@WithKubernetesTestServer(https = false, crud = true, port = 10001, setup = KubernetesTestServerOnProfileTest.Setup.class) | ||
public static class MyProfile implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
Map<String, String> overrides = new HashMap<>(); | ||
// do not fetch config from kubernetes | ||
overrides.put("quarkus.kubernetes-config.enabled", "false"); | ||
overrides.put("quarkus.kubernetes-config.secrets.enabled", "false"); | ||
// get rid of errors due to us not populating config from kubernetes | ||
overrides.put("dummy", "asd"); | ||
overrides.put("some.prop1", "asd"); | ||
overrides.put("some.prop2", "asd"); | ||
overrides.put("some.prop3", "asd"); | ||
overrides.put("some.prop4", "asd"); | ||
overrides.put("some.prop5", "asd"); | ||
overrides.put("secret.prop1", "asd"); | ||
overrides.put("secret.prop2", "asd"); | ||
overrides.put("secret.prop3", "asd"); | ||
overrides.put("secret.prop4", "asd"); | ||
overrides.put("overridden.secret", "asd"); | ||
overrides.put("dummysecret", "asd"); | ||
return overrides; | ||
} | ||
} | ||
} |
Oops, something went wrong.