Skip to content

Commit

Permalink
fix fabric8io#3912: simplifying extension mocking (fabric8io#3913)
Browse files Browse the repository at this point in the history
* fix fabric8io#3912: simplifying extension mocking

for now everything that a user could be using was deprecated, rather
than removed

* switching to just a single mock annotation and extension

* further unifying the mock framework

* applying spotless to the branch

* correcting the migration doc to be EnableKubernetesMockClient

* using formatter off to preserve comments
  • Loading branch information
shawkins authored Mar 8, 2022
1 parent f61d13d commit 850044a
Show file tree
Hide file tree
Showing 117 changed files with 2,585 additions and 2,374 deletions.
2 changes: 2 additions & 0 deletions doc/MIGRATION-v6.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ Client.isAdaptable and Client.adapt will check first if the existing instance is

- ApiVersionUtil classes in each extension have been deprecated, you should use io.fabric8.kubernetes.client.utils.ApiVersionUtil instead.

- Extension specific EnableXXXMockClient and XXXMockServer classes have been deprecated. You can simply use EnableKubernetesMockClient and KubernetesMockServer instead. Dependencies on the xxx-mock jar are then no longer needed, just a dependency to kubernetes-server-mock.

## Object Sorting

KubernetesList and Template will no longer automatically sort their objects by default. You may use the HasMetadataComparator to sort the items as needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import java.util.Map;
import java.util.Queue;

/**
* @deprecated use {@link KubernetesMockServer} instead
*/
@Deprecated
public class CamelKMockServer extends KubernetesMockServer {

public CamelKMockServer() {
Expand All @@ -37,13 +41,14 @@ public CamelKMockServer(boolean useHttps) {
super(useHttps);
}

public CamelKMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses, Dispatcher dispatcher, boolean useHttps) {
public CamelKMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses,
Dispatcher dispatcher, boolean useHttps) {
super(context, server, responses, dispatcher, useHttps);
}

@Override
public String[] getRootPaths() {
return new String[]{"/api","/apis/camel.apache.org"};
return new String[] { "/api", "/apis/camel.apache.org" };
}

public NamespacedCamelKClient createCamelKClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
*/
package io.fabric8.camelk.mock;

import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotation that is used for enabling CamelKMockServerExtension JUnit5 extension.
* You may set here two parameters of `CamelKServer`: crudMode and https
*
* @deprecated use {@link EnableKubernetesMockClient} instead
*/
@Deprecated
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@ExtendWith(CamelKMockServerExtension.class)
Expand Down
3 changes: 1 addition & 2 deletions extensions/camel-k/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>camel-k-mock</artifactId>
<version>${project.version}</version>
<artifactId>kubernetes-server-mock</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,22 @@
*/
package io.fabric8.camelk.test;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.camelk.client.CamelKClient;
import io.fabric8.camelk.mock.CamelKMockServer;
import io.fabric8.kubernetes.client.KubernetesClient;

import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableKubernetesMockClient
class AdaptTest {

private io.fabric8.camelk.mock.CamelKMockServer mock = new CamelKMockServer();

@BeforeEach
public void setUp() {
mock.init();
}

@AfterEach
void tearDown() throws IOException {
mock.destroy();
}
KubernetesClient client;

@Test
void testAdapt() {
CamelKClient sc = mock.createCamelKClient();
KubernetesClient kc = new DefaultKubernetesClient(sc.getConfiguration());
assertNotNull(kc.adapt(CamelKClient.class));
assertTrue(client.isAdaptable(CamelKClient.class));
assertNotNull(client.adapt(CamelKClient.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
package io.fabric8.camelk.test.crud;

import io.fabric8.camelk.client.CamelKClient;
import io.fabric8.camelk.mock.EnableCamelKMockClient;
import io.fabric8.camelk.v1.Integration;
import io.fabric8.camelk.v1.IntegrationBuilder;
import io.fabric8.camelk.v1.IntegrationList;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableCamelKMockClient(crud = true)
@EnableKubernetesMockClient(crud = true)
class IntegrationCrudTest {


CamelKClient client;

@Test
void shouldReturnEmptyList() {

Expand Down Expand Up @@ -67,27 +70,27 @@ void shouldDeleteAIntegration() {
void shouldLoadAIntegrationWithParams() {

String definition = String.join("\n", Arrays.asList(
"apiVersion: camel.apache.org/v1alpha1",
"kind: Integration",
"metadata:",
" name: integration4",
"spec:",
" flows:",
" - from:",
" parameters:",
" period: \"1000\"",
" steps:",
" - set-body:",
" constant: Hello Camel K from yaml",
" - to: log:info",
" uri: timer:yaml",
" traits:",
" container:",
" configuration:",
" requestCPU: \"1\""
));

Integration i = client.v1().integrations().inNamespace("ns4").load(new ByteArrayInputStream(definition.getBytes())).createOrReplace();
"apiVersion: camel.apache.org/v1alpha1",
"kind: Integration",
"metadata:",
" name: integration4",
"spec:",
" flows:",
" - from:",
" parameters:",
" period: \"1000\"",
" steps:",
" - set-body:",
" constant: Hello Camel K from yaml",
" - to: log:info",
" uri: timer:yaml",
" traits:",
" container:",
" configuration:",
" requestCPU: \"1\""));

Integration i = client.v1().integrations().inNamespace("ns4").load(new ByteArrayInputStream(definition.getBytes()))
.createOrReplace();
assertNotNull(i);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
import java.util.Map;
import java.util.Queue;

/**
* @deprecated use {@link KubernetesMockServer} instead
*/
@Deprecated
public class CertManagerMockServer extends KubernetesMockServer {

public CertManagerMockServer() {
Expand All @@ -38,13 +42,14 @@ public CertManagerMockServer(boolean useHttps) {
super(useHttps);
}

public CertManagerMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses, Dispatcher dispatcher, boolean useHttps) {
public CertManagerMockServer(Context context, MockWebServer server, Map<ServerRequest, Queue<ServerResponse>> responses,
Dispatcher dispatcher, boolean useHttps) {
super(context, server, responses, dispatcher, useHttps);
}

@Override
public String[] getRootPaths() {
return new String[]{"/api", "/apis/cert-manager.io"};
return new String[] { "/api", "/apis/cert-manager.io" };
}

public NamespacedCertManagerClient createCertManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
*/
package io.fabric8.certmanager.server.mock;

import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotation that is used for enabling CertManagerMockServerExtension JUnit5 extension.
* You may set here two parameters of `CertManagerKServer`: crudMode and https
*
* @deprecated use {@link EnableKubernetesMockClient} instead
*/
@Deprecated
@Target({ TYPE, METHOD, ANNOTATION_TYPE })
@Retention(RUNTIME)
@ExtendWith(CertManagerMockServerExtension.class)
Expand Down
5 changes: 2 additions & 3 deletions extensions/certmanager/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@
<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<artifactId>certmanager-client</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>certmanager-server-mock</artifactId>
<version>${project.version}</version>
<artifactId>kubernetes-server-mock</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
import io.fabric8.certmanager.api.model.v1.CertificateBuilder;
import io.fabric8.certmanager.api.model.v1.CertificateList;
import io.fabric8.certmanager.client.CertManagerClient;
import io.fabric8.certmanager.server.mock.EnableCertManagerMockClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@EnableCertManagerMockClient(crud=true)
@EnableKubernetesMockClient(crud = true)
class V1CertificateCrudTest {
CertManagerClient client;
CertManagerClient client;

@Test
void shouldReturnEmptyList() {
Expand Down Expand Up @@ -62,20 +64,20 @@ void shouldDeleteACertificate() {
void shouldLoadCertificate() {

String certificateDefinition = String.join("\n", Arrays.asList(
"apiVersion: cert-manager.io/v1beta1",
"kind: Certificate",
"metadata:",
" name: ca-issuer",
"spec:",
" isCA: true",
" secretName: ca-key-pair",
" commonName: my-csi-app",
" issuerRef:",
" name: selfsigned-issuer",
" kind: Issuer",
" group: cert-manager.io"
));
io.fabric8.certmanager.api.model.v1beta1.Certificate certificate = client.v1beta1().certificates().inNamespace("ns4").load(new ByteArrayInputStream(certificateDefinition.getBytes())).createOrReplace();
"apiVersion: cert-manager.io/v1beta1",
"kind: Certificate",
"metadata:",
" name: ca-issuer",
"spec:",
" isCA: true",
" secretName: ca-key-pair",
" commonName: my-csi-app",
" issuerRef:",
" name: selfsigned-issuer",
" kind: Issuer",
" group: cert-manager.io"));
io.fabric8.certmanager.api.model.v1beta1.Certificate certificate = client.v1beta1().certificates().inNamespace("ns4")
.load(new ByteArrayInputStream(certificateDefinition.getBytes())).createOrReplace();
assertEquals("ca-issuer", certificate.getMetadata().getName());
assertEquals("ca-key-pair", certificate.getSpec().getSecretName());
assertEquals("my-csi-app", certificate.getSpec().getCommonName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import io.fabric8.certmanager.api.model.v1.CertificateRequest;
import io.fabric8.certmanager.api.model.v1.CertificateRequestBuilder;
import io.fabric8.certmanager.client.CertManagerClient;
import io.fabric8.certmanager.server.mock.CertManagerMockServer;
import io.fabric8.certmanager.server.mock.EnableCertManagerMockClient;
import io.fabric8.kubernetes.api.model.Duration;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import org.junit.jupiter.api.Test;

import java.net.HttpURLConnection;
Expand All @@ -30,19 +30,19 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@EnableCertManagerMockClient
@EnableKubernetesMockClient
class V1CertificateRequestTest {

CertManagerClient client;
CertManagerMockServer server;
KubernetesMockServer server;

@Test
void testCreate() throws Exception {
// Given
CertificateRequest certificateRequest = createCertificateRequest();
server.expect().post().withPath("/apis/cert-manager.io/v1/namespaces/ns1/certificaterequests")
.andReturn(HttpURLConnection.HTTP_CREATED, certificateRequest)
.once();
.andReturn(HttpURLConnection.HTTP_CREATED, certificateRequest)
.once();

// When
CertificateRequest createdRequest = client.v1().certificateRequests().inNamespace("ns1").create(certificateRequest);
Expand All @@ -54,18 +54,18 @@ void testCreate() throws Exception {

private CertificateRequest createCertificateRequest() throws ParseException {
return new CertificateRequestBuilder()
.withNewMetadata().withName("my-ca-cr").endMetadata()
.withNewSpec()
.withRequest("LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQzNqQ0N")
.withIsCA(false)
.addToUsages("signing", "digital signature", "server auth")
.withDuration(Duration.parse("90d"))
.withIssuerRef(new ObjectReferenceBuilder()
.withName("ca-issuer")
.withKind("Issuer")
.withGroup("cert-manager.io")
.build())
.endSpec()
.build();
.withNewMetadata().withName("my-ca-cr").endMetadata()
.withNewSpec()
.withRequest("LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQzNqQ0N")
.withIsCA(false)
.addToUsages("signing", "digital signature", "server auth")
.withDuration(Duration.parse("90d"))
.withIssuerRef(new ObjectReferenceBuilder()
.withName("ca-issuer")
.withKind("Issuer")
.withGroup("cert-manager.io")
.build())
.endSpec()
.build();
}
}
Loading

0 comments on commit 850044a

Please sign in to comment.