Skip to content

Commit

Permalink
add test to verify the application config param
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Feb 7, 2023
1 parent 07676f3 commit c2eaf74
Showing 1 changed file with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.assertj.core.util.Strings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -57,7 +58,7 @@ void shouldDiscoverNamespacedKnativeServices() {

String knSvcName = "my-knservice";

registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null);
registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null, null);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

Expand All @@ -77,25 +78,57 @@ void shouldDiscoverNamespacedKnativeServices() {
entry("serving.knative.dev/lastModifier", "kubernetes-admin"));
}

private void registerKnativeServices(String knativeService, String url, String namespace) {
@Test
void shouldDiscoverNamespacedKnativeServicesWithApp() {
TestConfigProvider.addServiceConfig("my-knservice", null, "knative",
null, Map.of("knative-host", k8sMasterUrl, "knative-namespace", "test", "application", "greetingApp"));

Stork stork = StorkTestUtils.getNewStorkInstance();

String knSvcName = "my-knservice";
String applicationName = "greetingApp";

registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null, applicationName);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

io.smallrye.stork.api.Service service = stork.getService(knSvcName);
service.getServiceDiscovery().getServiceInstances()
.onFailure().invoke(th -> fail("Failed to get service instances from the cluster", th))
.subscribe().with(instances::set);

Service knSvc = buildKnService(knativeService, url, namespace);
await().atMost(Duration.ofSeconds(5))
.until(() -> instances.get() != null);

assertThat(instances.get()).hasSize(1);
assertThat(instances.get().get(0).getHost()).isEqualTo("http://hello.test.127.0.0.1.sslip.io");
assertThat(instances.get().get(0).getPort()).isEqualTo(8080);
Map<String, String> labels = instances.get().get(0).getLabels();
assertThat(labels).contains(entry("serving.knative.dev/creator", "kubernetes-admin"),
entry("serving.knative.dev/lastModifier", "kubernetes-admin"));
}

private void registerKnativeServices(String knativeService, String url, String namespace, String application) {

Service knSvc = buildKnService(knativeService, url, namespace, application);
if (namespace != null) {
kn.services().inNamespace(namespace).resource(knSvc).create();
} else {
kn.services().resource(knSvc).create();
}
}

private static Service buildKnService(String knativeService, String url, String namespace) {
private static Service buildKnService(String knativeService, String url, String namespace, String applicationName) {
Map<String, String> serviceLabels = new HashMap<>();
serviceLabels.put("serving.knative.dev/creator", "kubernetes-admin");
serviceLabels.put("serving.knative.dev/lastModifier", "kubernetes-admin");

String name = Strings.isNullOrEmpty(applicationName) ? knativeService : applicationName;

ServiceStatus serviceStatus = new ServiceStatusBuilder().withUrl(url)
.withLatestCreatedRevisionName("revisionName").build();
Service knSvc = new ServiceBuilder().withNewMetadata().withNamespace(namespace).withLabels(serviceLabels)
.withName(knativeService)
.withName(name)
.endMetadata().withStatus(serviceStatus)
.build();
return knSvc;
Expand All @@ -109,8 +142,8 @@ void shouldDiscoverKnativeServicesInAllNs() {

String knativeService = "my-knservice";

registerKnativeServices(knativeService, "http://hello.ns1.127.0.0.1.sslip.io", "ns1");
registerKnativeServices(knativeService, "http://hello.ns2.127.0.0.1.sslip.io", "ns2");
registerKnativeServices(knativeService, "http://hello.ns1.127.0.0.1.sslip.io", "ns1", null);
registerKnativeServices(knativeService, "http://hello.ns2.127.0.0.1.sslip.io", "ns2", null);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

Expand All @@ -135,7 +168,7 @@ void shouldGetServiceFromK8sDefaultNamespaceUsingProgrammaticAPI() {

String knativeService = "my-knservice";

registerKnativeServices(knativeService, "http://hello.test.127.0.0.1.sslip.io", null);
registerKnativeServices(knativeService, "http://hello.test.127.0.0.1.sslip.io", null, null);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

Expand Down Expand Up @@ -165,7 +198,7 @@ void shouldHandleSecureAttribute() {

String knSvcName = "my-knservice";

registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null);
registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null, null);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

Expand Down Expand Up @@ -204,7 +237,7 @@ void shouldFetchInstancesFromTheClusterWhenCacheIsInvalidated() {

String knSvcName = "my-knservice";

registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null);
registerKnativeServices(knSvcName, "http://hello.test.127.0.0.1.sslip.io", null, null);

AtomicReference<List<ServiceInstance>> instances = new AtomicReference<>();

Expand Down Expand Up @@ -252,7 +285,7 @@ void shouldFetchInstancesFromTheCache() throws InterruptedException {
server.expect().get().withPath("/apis/serving.knative.dev/v1/namespaces/test/services/my-knservice")
.andReply(200, r -> {
serverHit.incrementAndGet();
return buildKnService(knSvcName, "http://hello.test.127.0.0.1.sslip.io", "test");
return buildKnService(knSvcName, "http://hello.test.127.0.0.1.sslip.io", "test", null);
}).always();

TestConfigProvider.addServiceConfig("my-knservice", null, "knative",
Expand Down

0 comments on commit c2eaf74

Please sign in to comment.