Skip to content

Commit

Permalink
test: load test resources from files
Browse files Browse the repository at this point in the history
  • Loading branch information
jsenko committed Oct 7, 2024
1 parent 375529b commit 350b06f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ private void addSelectorLabels(Map<String, String> labels, ApicurioRegistry3 pri
// spotless:on
}

private <T extends HasMetadata> T deserialize(String path, Class<T> klass) {
public static <T extends HasMetadata> T deserialize(String path, Class<T> klass) {
try {
return YAML_MAPPER.readValue(load(path), klass);
} catch (JsonProcessingException ex) {
throw new OperatorException("Could not deserialize default resource: " + path, ex);
}
}

private String load(String path) {
private static String load(String path) {
try (var stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) {
return new String(stream.readAllBytes(), Charset.defaultCharset());
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry3
metadata:
name: example-apicurioregistry3
name: simple
spec: {}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private static void createGeneratedResources() throws Exception {
resources.getItems().stream().forEach(r -> {
if (r.getKind().equals("ClusterRoleBinding") && r instanceof ClusterRoleBinding) {
var crb = (ClusterRoleBinding) r;
crb.getSubjects().stream().forEach(s -> s.setNamespace(getNamespace()));
crb.getSubjects().stream().forEach(s -> s.setNamespace(namespace));
// TODO: We need to patch the generated resources, because the referenced ClusterRole name
// is
// wrong.
Expand All @@ -115,7 +115,7 @@ private static void createGeneratedResources() throws Exception {
crb.getRoleRef().setName("apicurioregistry3reconciler-cluster-role");
}
}
client.resource(r).inNamespace(getNamespace()).createOrReplace();
client.resource(r).inNamespace(namespace).createOrReplace();
});
}
}
Expand All @@ -129,9 +129,9 @@ private static void cleanGeneratedResources() throws Exception {
resources.getItems().stream().forEach(r -> {
if (r.getKind().equals("ClusterRoleBinding") && r instanceof ClusterRoleBinding) {
var crb = (ClusterRoleBinding) r;
crb.getSubjects().stream().forEach(s -> s.setNamespace(getNamespace()));
crb.getSubjects().stream().forEach(s -> s.setNamespace(namespace));
}
client.resource(r).inNamespace(getNamespace()).delete();
client.resource(r).inNamespace(namespace).delete();
});
}
}
Expand Down Expand Up @@ -213,9 +213,4 @@ public static void after() throws Exception {
}
client.close();
}

public static String getNamespace() {
return namespace;
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package io.apicurio.registry.operator.it;

import io.apicurio.registry.operator.api.v1.ApicurioRegistry3;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3Spec;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3SpecApp;
import io.apicurio.registry.operator.api.v1.ApicurioRegistry3SpecUI;
import io.apicurio.registry.operator.resource.ResourceFactory;
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.quarkus.test.junit.QuarkusTest;
import org.assertj.core.api.InstanceOfAssertFactories;
Expand All @@ -29,94 +26,82 @@ public class SmokeITTest extends ITBase {

@Test
void demoDeployment() {
// spotless:off
var registry = new ApicurioRegistry3();
var meta = new ObjectMeta();
meta.setName("demo");
meta.setNamespace(getNamespace());
registry.setMetadata(meta);
registry.setSpec(ApicurioRegistry3Spec.builder()
.app(ApicurioRegistry3SpecApp.builder()
.host(ingressManager.getIngressHost("demo-app"))
.build())
.ui(ApicurioRegistry3SpecUI.builder()
.host(ingressManager.getIngressHost("demo-ui"))
.build())
.build());
// spotless:on

// Act
var registry = ResourceFactory.deserialize("/k8s/examples/simple.apicurioregistry3.yaml",
ApicurioRegistry3.class);
registry.getMetadata().setNamespace(namespace);
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("ui"));

client.resource(registry).create();

// Deployments
await().ignoreExceptions().until(() -> {
assertThat(client.apps().deployments().inNamespace(getNamespace()).withName("demo-app-deployment")
.get().getStatus().getReadyReplicas()).isEqualTo(1);
assertThat(client.apps().deployments().inNamespace(getNamespace()).withName("demo-ui-deployment")
.get().getStatus().getReadyReplicas()).isEqualTo(1);
assertThat(client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-deployment").get().getStatus()
.getReadyReplicas()).isEqualTo(1);
assertThat(client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-deployment").get().getStatus()
.getReadyReplicas()).isEqualTo(1);
return true;
});

// Services
await().ignoreExceptions().until(() -> {
assertThat(client.services().inNamespace(getNamespace()).withName("demo-app-service").get()
.getSpec().getClusterIP()).isNotBlank();
assertThat(client.services().inNamespace(getNamespace()).withName("demo-ui-service").get()
.getSpec().getClusterIP()).isNotBlank();
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-service").get().getSpec()
.getClusterIP()).isNotBlank();
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-service").get().getSpec()
.getClusterIP()).isNotBlank();
return true;
});

// Ingresses
await().ignoreExceptions().until(() -> {
assertThat(client.network().v1().ingresses().inNamespace(getNamespace())
.withName("demo-app-ingress").get().getSpec().getRules().get(0).getHost())
.isEqualTo(registry.getSpec().getApp().getHost());
assertThat(client.network().v1().ingresses().inNamespace(getNamespace())
.withName("demo-ui-ingress").get().getSpec().getRules().get(0).getHost())
.isEqualTo(registry.getSpec().getUi().getHost());
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get().getSpec().getRules()
.get(0).getHost()).isEqualTo(registry.getSpec().getApp().getHost());
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get().getSpec().getRules()
.get(0).getHost()).isEqualTo(registry.getSpec().getUi().getHost());
return true;
});
}

@Test
void testService() {
// spotless:off
var registry = new ApicurioRegistry3();
var meta = new ObjectMeta();
meta.setName("demo");
meta.setNamespace(namespace);
registry.setMetadata(meta);
registry.setSpec(ApicurioRegistry3Spec.builder()
.app(ApicurioRegistry3SpecApp.builder()
.host(ingressManager.getIngressHost("demo-app"))
.build())
.ui(ApicurioRegistry3SpecUI.builder()
.host(ingressManager.getIngressHost("demo-ui"))
.build())
.build());
// spotless:on

// Act
client.resources(ApicurioRegistry3.class).inNamespace(namespace).create(registry);
var registry = ResourceFactory.deserialize("/k8s/examples/simple.apicurioregistry3.yaml",
ApicurioRegistry3.class);
registry.getMetadata().setNamespace(namespace);
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("ui"));

client.resource(registry).create();

// Wait for Services
await().ignoreExceptions().until(() -> {
assertThat(client.services().inNamespace(namespace).withName("demo-app-service").get().getSpec()
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-service").get().getSpec()
.getClusterIP()).isNotBlank();
assertThat(client.services().inNamespace(namespace).withName("demo-ui-service").get().getSpec()
assertThat(client.services().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-service").get().getSpec()
.getClusterIP()).isNotBlank();
return true;
});

int appServicePort = portForwardManager.startPortForward("demo-app-service", 8080);
int appServicePort = portForwardManager
.startPortForward(registry.getMetadata().getName() + "-app-service", 8080);

await().ignoreExceptions().until(() -> {
given().get(new URI("http://localhost:" + appServicePort + "/apis/registry/v3/system/info"))
.then().statusCode(200);
return true;
});

int uiServicePort = portForwardManager.startPortForward("demo-ui-service", 8080);
int uiServicePort = portForwardManager
.startPortForward(registry.getMetadata().getName() + "-ui-service", 8080);

await().ignoreExceptions().until(() -> {
given().get(new URI("http://localhost:" + uiServicePort + "/config.js")).then().statusCode(200);
Expand All @@ -127,85 +112,66 @@ void testService() {
@Test
@DisabledIfSystemProperty(named = INGRESS_SKIP_PROP, matches = "true")
void testIngress() {
// spotless:off
var registry = new ApicurioRegistry3();
var meta = new ObjectMeta();
meta.setName("demo");
meta.setNamespace(namespace);
registry.setMetadata(meta);
registry.setSpec(ApicurioRegistry3Spec.builder()
.app(ApicurioRegistry3SpecApp.builder()
.host(ingressManager.getIngressHost("demo-app"))
.build())
.ui(ApicurioRegistry3SpecUI.builder()
.host(ingressManager.getIngressHost("demo-ui"))
.build())
.build());
// spotless:on

// Act
client.resources(ApicurioRegistry3.class).inNamespace(namespace).create(registry);
var registry = ResourceFactory.deserialize("/k8s/examples/simple.apicurioregistry3.yaml",
ApicurioRegistry3.class);
registry.getMetadata().setNamespace(namespace);
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("ui"));

client.resource(registry).create();

// Wait for Ingresses
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-app-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-ui-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNotNull();
});

await().ignoreExceptions().until(() -> {
ingressManager.startHttpRequest("demo-app-ingress").basePath("/apis/registry/v3/system/info")
.get().then().statusCode(200);
ingressManager.startHttpRequest(registry.getMetadata().getName() + "-app-ingress")
.basePath("/apis/registry/v3/system/info").get().then().statusCode(200);
return true;
});

await().ignoreExceptions().until(() -> {
ingressManager.startHttpRequest("demo-ui-ingress").basePath("/config.js").get().then()
.statusCode(200);
ingressManager.startHttpRequest(registry.getMetadata().getName() + "-ui-ingress")
.basePath("/config.js").get().then().statusCode(200);
return true;
});
}

@Test
void testEmptyHostDisablesIngress() {
// spotless:off
var registry = new ApicurioRegistry3();
var meta = new ObjectMeta();
meta.setName("demo");
meta.setNamespace(namespace);
registry.setMetadata(meta);
registry.setSpec(ApicurioRegistry3Spec.builder()
.app(ApicurioRegistry3SpecApp.builder()
.host(ingressManager.getIngressHost("demo-app"))
.build())
.ui(ApicurioRegistry3SpecUI.builder()
.host(ingressManager.getIngressHost("demo-ui"))
.build())
.build());
// spotless:on

var registry = ResourceFactory.deserialize("/k8s/examples/simple.apicurioregistry3.yaml",
ApicurioRegistry3.class);
registry.getMetadata().setNamespace(namespace);
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("ui"));

client.resource(registry).create();

// Wait for Ingresses
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-app-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-ui-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNotNull();
});

// Check that REGISTRY_API_URL is set
var uiDeployment = client.apps().deployments().inNamespace(namespace).withName("demo-ui-deployment")
.get();
var uiDeployment = client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-deployment").get();
verify_REGISTRY_API_URL_isSet(registry, uiDeployment);

// Disable host and therefore Ingress
registry.getSpec().getApp().setHost("");
registry.getSpec().getUi().setHost("");

// The remote test does not work properly. As a workaround the CR will be deleted and recreated
// instead of updated.
// TODO: The remote test does not work properly. As a workaround the CR will be deleted and recreated
// instead of updated:
// client.resource(registry).update();
client.resource(registry).delete();
await().untilAsserted(() -> {
Expand All @@ -214,16 +180,16 @@ void testEmptyHostDisablesIngress() {
client.resource(registry).create();

await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-app-ingress")
.get()).isNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNull();
});
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-ui-ingress")
.get()).isNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNull();
});

uiDeployment = client.apps().deployments().inNamespace(namespace).withName("demo-ui-deployment")
.get();
uiDeployment = client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-deployment").get();
assertThat(uiDeployment).isNotNull();
// spotless:off
assertThat(uiDeployment.getSpec().getTemplate().getSpec().getContainers())
Expand All @@ -234,21 +200,21 @@ void testEmptyHostDisablesIngress() {
// spotless:on

// Enable again
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("demo-app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("demo-ui"));
registry.getSpec().getApp().setHost(ingressManager.getIngressHost("app"));
registry.getSpec().getUi().setHost(ingressManager.getIngressHost("ui"));
client.resource(registry).update();

// Verify Ingresses are back
await().untilAsserted(() -> {
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-app-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace).withName("demo-ui-ingress")
.get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-app-ingress").get()).isNotNull();
assertThat(client.network().v1().ingresses().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-ingress").get()).isNotNull();
});

// Check that REGISTRY_API_URL is set again
uiDeployment = client.apps().deployments().inNamespace(namespace).withName("demo-ui-deployment")
.get();
uiDeployment = client.apps().deployments().inNamespace(namespace)
.withName(registry.getMetadata().getName() + "-ui-deployment").get();
verify_REGISTRY_API_URL_isSet(registry, uiDeployment);
}

Expand Down

0 comments on commit 350b06f

Please sign in to comment.