From 08ac81886d2e2fbfa5296f7fa6ebc25b3ef11160 Mon Sep 17 00:00:00 2001 From: Jose Date: Mon, 28 Feb 2022 08:26:20 +0100 Subject: [PATCH] Move Probe tests from `examples` to `tests` In this PR: https://github.com/dekorateio/dekorate/pull/875, we added the Probe coverage under the examples, where it should go under the tests modules. --- .../main/java/io/dekorate/example/Main.java | 8 +- .../dekorate/example/KnativeExampleTest.java | 38 --------- .../src/main/resources/application.properties | 8 +- .../java/io/dekorate/example/VertxTest.java | 53 ++----------- .../src/main/resources/application.properties | 8 +- .../java/io/dekorate/example/VertxTest.java | 53 ++----------- .../pom.xml | 2 +- tests/feat-knative-probes/pom.xml | 58 ++++++++++++++ .../java/io/dekorate/example/Controller.java | 28 +++++++ .../main/java/io/dekorate/example/Main.java | 31 ++++++++ .../dekorate/example/KnativeExampleTest.java | 76 ++++++++++++++++++ tests/feat-kubernetes-probes/pom.xml | 57 ++++++++++++++ .../main/java/io/dekorate/example/Main.java | 42 ++++++++++ .../src/main/resources/application.properties | 11 +++ .../java/io/dekorate/example/VertxTest.java | 78 +++++++++++++++++++ tests/feat-openshift-probes/pom.xml | 57 ++++++++++++++ .../main/java/io/dekorate/example/Main.java | 42 ++++++++++ .../src/main/resources/application.properties | 11 +++ .../java/io/dekorate/example/VertxTest.java | 78 +++++++++++++++++++ tests/pom.xml | 3 + 20 files changed, 588 insertions(+), 154 deletions(-) create mode 100644 tests/feat-knative-probes/pom.xml create mode 100644 tests/feat-knative-probes/src/main/java/io/dekorate/example/Controller.java create mode 100644 tests/feat-knative-probes/src/main/java/io/dekorate/example/Main.java create mode 100644 tests/feat-knative-probes/src/test/java/io/dekorate/example/KnativeExampleTest.java create mode 100644 tests/feat-kubernetes-probes/pom.xml create mode 100644 tests/feat-kubernetes-probes/src/main/java/io/dekorate/example/Main.java create mode 100644 tests/feat-kubernetes-probes/src/main/resources/application.properties create mode 100644 tests/feat-kubernetes-probes/src/test/java/io/dekorate/example/VertxTest.java create mode 100644 tests/feat-openshift-probes/pom.xml create mode 100644 tests/feat-openshift-probes/src/main/java/io/dekorate/example/Main.java create mode 100644 tests/feat-openshift-probes/src/main/resources/application.properties create mode 100644 tests/feat-openshift-probes/src/test/java/io/dekorate/example/VertxTest.java diff --git a/examples/knative-example/src/main/java/io/dekorate/example/Main.java b/examples/knative-example/src/main/java/io/dekorate/example/Main.java index 340201f3d..e02089295 100644 --- a/examples/knative-example/src/main/java/io/dekorate/example/Main.java +++ b/examples/knative-example/src/main/java/io/dekorate/example/Main.java @@ -16,16 +16,10 @@ package io.dekorate.example; import io.dekorate.knative.annotation.KnativeApplication; -import io.dekorate.kubernetes.annotation.Probe; - import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -@KnativeApplication(minScale = 1, maxScale = 5, scaleToZeroEnabled = false, - readinessProbe = @Probe(httpActionPath = "/readiness", periodSeconds = 30, timeoutSeconds = 10), - livenessProbe = @Probe(httpActionPath = "/liveness", periodSeconds = 31, timeoutSeconds = 11), - startupProbe = @Probe(httpActionPath = "/startup", periodSeconds = 32, timeoutSeconds = 12) -) +@KnativeApplication(minScale = 1, maxScale = 5, scaleToZeroEnabled = false) @SpringBootApplication public class Main { diff --git a/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java b/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java index 0712d8790..c518bae52 100644 --- a/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java +++ b/examples/knative-example/src/test/java/io/dekorate/example/KnativeExampleTest.java @@ -18,10 +18,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Optional; -import java.util.function.Function; import org.junit.jupiter.api.Test; @@ -31,7 +29,6 @@ import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.HasMetadata; import io.fabric8.kubernetes.api.model.KubernetesList; -import io.fabric8.kubernetes.api.model.Probe; class KnativeExampleTest { @@ -56,41 +53,6 @@ public void shouldContainServiceWithPortNamedHttp1() { assertEquals("false", configMap.getData().get("enable-scale-to-zero")); } - @Test - public void shouldContainProbes() { - KubernetesList list = Serialization.unmarshalAsList(KnativeExampleTest.class.getClassLoader().getResourceAsStream("META-INF/dekorate/knative.yml")); - Service service = findFirst(list, Service.class).orElseThrow(() -> new IllegalStateException("No knative service found!")); - - assertReadinessProbe(service, "/readiness", 30, 10); - assertLivenessProbe(service, "/liveness", 31, 11); - assertStartupProbe(service, "/startup", 32, 12); - } - - private static void assertReadinessProbe(Service service, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(service, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertLivenessProbe(Service service, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(service, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertStartupProbe(Service service, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(service, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertProbe(Service service, - Function probeFunction, - String actionPath, int periodSeconds, int timeoutSeconds) { - - assertTrue(service.getSpec().getTemplate().getSpec().getContainers().stream() - .map(probeFunction) - .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) - && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); - } - Optional findFirst(KubernetesList list, Class t) { return (Optional) list.getItems().stream() .filter(i -> t.isInstance(i)) diff --git a/examples/vertx-on-kubernetes-example/src/main/resources/application.properties b/examples/vertx-on-kubernetes-example/src/main/resources/application.properties index d46cac1ab..ed3ec18b0 100644 --- a/examples/vertx-on-kubernetes-example/src/main/resources/application.properties +++ b/examples/vertx-on-kubernetes-example/src/main/resources/application.properties @@ -1,11 +1,5 @@ dekorate.kubernetes.ports[0].name=http dekorate.kubernetes.ports[0].containerPort=8080 -dekorate.kubernetes.readinessProbe.httpActionPath=/readiness +dekorate.kubernetes.readinessProbe.httpActionPath=/ dekorate.kubernetes.readinessProbe.periodSeconds=30 dekorate.kubernetes.readinessProbe.timeoutSeconds=10 -dekorate.kubernetes.livenessProbe.httpActionPath=/liveness -dekorate.kubernetes.livenessProbe.periodSeconds=31 -dekorate.kubernetes.livenessProbe.timeoutSeconds=11 -dekorate.kubernetes.startupProbe.httpActionPath=/startup -dekorate.kubernetes.startupProbe.periodSeconds=32 -dekorate.kubernetes.startupProbe.timeoutSeconds=12 diff --git a/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java b/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java index 2eff0ef8d..b3b360f7c 100644 --- a/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java +++ b/examples/vertx-on-kubernetes-example/src/test/java/io/dekorate/example/VertxTest.java @@ -1,33 +1,28 @@ /** * Copyright 2018 The original authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * -**/ + * + **/ package io.dekorate.example; import java.util.Optional; -import java.util.function.Function; - import org.junit.jupiter.api.Test; -import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.Probe; import io.fabric8.kubernetes.api.model.Service; -import io.fabric8.kubernetes.api.model.apps.Deployment; import static org.junit.jupiter.api.Assertions.assertTrue; import static io.dekorate.testing.KubernetesResources.*; @@ -35,46 +30,10 @@ class VertxTest { @Test - public void shouldContainService() { + public void shouldContainService() throws Exception { KubernetesList list = loadGenerated("kubernetes"); Optional service = findFirst(list, Service.class); assertTrue(service.isPresent()); } - - @Test - public void shouldContainProbes() { - KubernetesList list = loadGenerated("kubernetes"); - Optional deployment = findFirst(list, Deployment.class); - assertTrue(deployment.isPresent(), "Deployment not found!"); - - assertReadinessProbe(deployment.get(), "/readiness", 30, 10); - assertLivenessProbe(deployment.get(), "/liveness", 31, 11); - assertStartupProbe(deployment.get(), "/startup", 32, 12); - } - - private static void assertReadinessProbe(Deployment deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertLivenessProbe(Deployment deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertStartupProbe(Deployment deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertProbe(Deployment deployment, - Function probeFunction, - String actionPath, int periodSeconds, int timeoutSeconds) { - - assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() - .map(probeFunction) - .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) - && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); - } } diff --git a/examples/vertx-on-openshift-example/src/main/resources/application.properties b/examples/vertx-on-openshift-example/src/main/resources/application.properties index 3516afb38..f55e23f33 100644 --- a/examples/vertx-on-openshift-example/src/main/resources/application.properties +++ b/examples/vertx-on-openshift-example/src/main/resources/application.properties @@ -1,11 +1,5 @@ dekorate.openshift.ports[0].name=http dekorate.openshift.ports[0].containerPort=8080 -dekorate.openshift.readinessProbe.httpActionPath=/readiness +dekorate.openshift.readinessProbe.httpActionPath=/ dekorate.openshift.readinessProbe.periodSeconds=30 dekorate.openshift.readinessProbe.timeoutSeconds=10 -dekorate.openshift.livenessProbe.httpActionPath=/liveness -dekorate.openshift.livenessProbe.periodSeconds=31 -dekorate.openshift.livenessProbe.timeoutSeconds=11 -dekorate.openshift.startupProbe.httpActionPath=/startup -dekorate.openshift.startupProbe.periodSeconds=32 -dekorate.openshift.startupProbe.timeoutSeconds=12 diff --git a/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java b/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java index fe6895c31..021fd5e6f 100644 --- a/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java +++ b/examples/vertx-on-openshift-example/src/test/java/io/dekorate/example/VertxTest.java @@ -1,33 +1,28 @@ /** * Copyright 2018 The original authors. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * -**/ + * + **/ package io.dekorate.example; import java.util.Optional; -import java.util.function.Function; - import org.junit.jupiter.api.Test; -import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.HasMetadata; -import io.fabric8.kubernetes.api.model.Probe; import io.fabric8.kubernetes.api.model.Service; -import io.fabric8.openshift.api.model.DeploymentConfig; import static org.junit.jupiter.api.Assertions.assertTrue; import static io.dekorate.testing.KubernetesResources.*; @@ -35,46 +30,10 @@ class VertxTest { @Test - public void shouldContainService() { + public void shouldContainService() throws Exception { KubernetesList list = loadGenerated("openshift"); Optional service = findFirst(list, Service.class); assertTrue(service.isPresent()); } - - @Test - public void shouldContainProbes() { - KubernetesList list = loadGenerated("openshift"); - Optional deployment = findFirst(list, DeploymentConfig.class); - assertTrue(deployment.isPresent(), "DeploymentConfig not found!"); - - assertReadinessProbe(deployment.get(), "/readiness", 30, 10); - assertLivenessProbe(deployment.get(), "/liveness", 31, 11); - assertStartupProbe(deployment.get(), "/startup", 32, 12); - } - - private static void assertReadinessProbe(DeploymentConfig deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertLivenessProbe(DeploymentConfig deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertStartupProbe(DeploymentConfig deployment, String actionPath, - int periodSeconds, int timeoutSeconds) { - assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); - } - - private static void assertProbe(DeploymentConfig deployment, - Function probeFunction, - String actionPath, int periodSeconds, int timeoutSeconds) { - - assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() - .map(probeFunction) - .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) - && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); - } } diff --git a/tests/feat-642-knative-traffic-splitting/pom.xml b/tests/feat-642-knative-traffic-splitting/pom.xml index 10ae3c72f..06d773f6d 100644 --- a/tests/feat-642-knative-traffic-splitting/pom.xml +++ b/tests/feat-642-knative-traffic-splitting/pom.xml @@ -12,7 +12,7 @@ io.dekorate feat-642-knative-traffic-splitting - Dekorate :: Tests :: Annotations :: Knative :: Traffci #642 + Dekorate :: Tests :: Annotations :: Knative :: Traffic #642 diff --git a/tests/feat-knative-probes/pom.xml b/tests/feat-knative-probes/pom.xml new file mode 100644 index 000000000..2f6279188 --- /dev/null +++ b/tests/feat-knative-probes/pom.xml @@ -0,0 +1,58 @@ + + + + 4.0.0 + + + dekorate-tests + io.dekorate + 2.9-SNAPSHOT + ../ + + + io.dekorate + 2.9-SNAPSHOT + feat-probes-knative + Dekorate :: Tests :: Probes :: Knative + + + + io.dekorate + knative-annotations + ${project.version} + + + io.dekorate + dekorate-spring-boot + ${project.version} + + + org.springframework.boot + spring-boot-starter-web + ${version.spring-boot} + + + + + io.dekorate + knative-junit-starter + ${project.version} + test + + + diff --git a/tests/feat-knative-probes/src/main/java/io/dekorate/example/Controller.java b/tests/feat-knative-probes/src/main/java/io/dekorate/example/Controller.java new file mode 100644 index 000000000..ca675cb4a --- /dev/null +++ b/tests/feat-knative-probes/src/main/java/io/dekorate/example/Controller.java @@ -0,0 +1,28 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.dekorate.example; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + @RequestMapping("/") + public String hello() { + return "Hello world"; + } +} diff --git a/tests/feat-knative-probes/src/main/java/io/dekorate/example/Main.java b/tests/feat-knative-probes/src/main/java/io/dekorate/example/Main.java new file mode 100644 index 000000000..9471ebf9d --- /dev/null +++ b/tests/feat-knative-probes/src/main/java/io/dekorate/example/Main.java @@ -0,0 +1,31 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.dekorate.example; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import io.dekorate.knative.annotation.KnativeApplication; +import io.dekorate.kubernetes.annotation.Probe; + +@KnativeApplication(minScale = 1, maxScale = 5, scaleToZeroEnabled = false, readinessProbe = @Probe(httpActionPath = "/readiness", periodSeconds = 30, timeoutSeconds = 10), livenessProbe = @Probe(httpActionPath = "/liveness", periodSeconds = 31, timeoutSeconds = 11), startupProbe = @Probe(httpActionPath = "/startup", periodSeconds = 32, timeoutSeconds = 12)) +@SpringBootApplication +public class Main { + + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} diff --git a/tests/feat-knative-probes/src/test/java/io/dekorate/example/KnativeExampleTest.java b/tests/feat-knative-probes/src/test/java/io/dekorate/example/KnativeExampleTest.java new file mode 100644 index 000000000..71a983727 --- /dev/null +++ b/tests/feat-knative-probes/src/test/java/io/dekorate/example/KnativeExampleTest.java @@ -0,0 +1,76 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.dekorate.example; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import io.dekorate.utils.Serialization; +import io.fabric8.knative.serving.v1.Service; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.Probe; + +class KnativeExampleTest { + + @Test + public void shouldContainProbes() { + KubernetesList list = Serialization + .unmarshalAsList(KnativeExampleTest.class.getClassLoader().getResourceAsStream("META-INF/dekorate/knative.yml")); + Service service = findFirst(list, Service.class).orElseThrow(() -> new IllegalStateException("No knative service found!")); + + assertReadinessProbe(service, "/readiness", 30, 10); + assertLivenessProbe(service, "/liveness", 31, 11); + assertStartupProbe(service, "/startup", 32, 12); + } + + private static void assertReadinessProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(Service service, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(service, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(Service service, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(service.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } + + Optional findFirst(KubernetesList list, Class t) { + return (Optional) list.getItems().stream() + .filter(i -> t.isInstance(i)) + .findFirst(); + } +} diff --git a/tests/feat-kubernetes-probes/pom.xml b/tests/feat-kubernetes-probes/pom.xml new file mode 100644 index 000000000..67839f231 --- /dev/null +++ b/tests/feat-kubernetes-probes/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + dekorate-tests + io.dekorate + 2.9-SNAPSHOT + ../ + + + io.dekorate + 2.9-SNAPSHOT + feat-probes-kubernetes + Dekorate :: Tests :: Probes :: Kubernetes + + + 3.8.2 + + + + + io.dekorate + kubernetes-annotations + ${project.version} + + + io.vertx + vertx-core + ${version.vertx} + + + + + io.dekorate + kubernetes-junit-starter + ${project.version} + test + + + diff --git a/tests/feat-kubernetes-probes/src/main/java/io/dekorate/example/Main.java b/tests/feat-kubernetes-probes/src/main/java/io/dekorate/example/Main.java new file mode 100644 index 000000000..f2298e6f4 --- /dev/null +++ b/tests/feat-kubernetes-probes/src/main/java/io/dekorate/example/Main.java @@ -0,0 +1,42 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.example; + +import io.dekorate.annotation.Dekorate; +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; + +@Dekorate +public class Main extends AbstractVerticle { + + @Override + public void start(Future future) { + vertx + .createHttpServer() + .requestHandler(r -> { + r.response().end("hello world!"); + }) + .listen(8080, result -> { + if (result.succeeded()) { + future.complete(); + } else { + future.fail(result.cause()); + } + }); + } +} diff --git a/tests/feat-kubernetes-probes/src/main/resources/application.properties b/tests/feat-kubernetes-probes/src/main/resources/application.properties new file mode 100644 index 000000000..d46cac1ab --- /dev/null +++ b/tests/feat-kubernetes-probes/src/main/resources/application.properties @@ -0,0 +1,11 @@ +dekorate.kubernetes.ports[0].name=http +dekorate.kubernetes.ports[0].containerPort=8080 +dekorate.kubernetes.readinessProbe.httpActionPath=/readiness +dekorate.kubernetes.readinessProbe.periodSeconds=30 +dekorate.kubernetes.readinessProbe.timeoutSeconds=10 +dekorate.kubernetes.livenessProbe.httpActionPath=/liveness +dekorate.kubernetes.livenessProbe.periodSeconds=31 +dekorate.kubernetes.livenessProbe.timeoutSeconds=11 +dekorate.kubernetes.startupProbe.httpActionPath=/startup +dekorate.kubernetes.startupProbe.periodSeconds=32 +dekorate.kubernetes.startupProbe.timeoutSeconds=12 diff --git a/tests/feat-kubernetes-probes/src/test/java/io/dekorate/example/VertxTest.java b/tests/feat-kubernetes-probes/src/test/java/io/dekorate/example/VertxTest.java new file mode 100644 index 000000000..8d7e2b873 --- /dev/null +++ b/tests/feat-kubernetes-probes/src/test/java/io/dekorate/example/VertxTest.java @@ -0,0 +1,78 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.example; + +import static io.dekorate.testing.KubernetesResources.*; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.Probe; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.kubernetes.api.model.apps.Deployment; + +class VertxTest { + + @Test + public void shouldContainService() { + KubernetesList list = loadGenerated("kubernetes"); + Optional service = findFirst(list, Service.class); + assertTrue(service.isPresent()); + } + + @Test + public void shouldContainProbes() { + KubernetesList list = loadGenerated("kubernetes"); + Optional deployment = findFirst(list, Deployment.class); + assertTrue(deployment.isPresent(), "Deployment not found!"); + + assertReadinessProbe(deployment.get(), "/readiness", 30, 10); + assertLivenessProbe(deployment.get(), "/liveness", 31, 11); + assertStartupProbe(deployment.get(), "/startup", 32, 12); + } + + private static void assertReadinessProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(Deployment deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(Deployment deployment, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } +} diff --git a/tests/feat-openshift-probes/pom.xml b/tests/feat-openshift-probes/pom.xml new file mode 100644 index 000000000..523847063 --- /dev/null +++ b/tests/feat-openshift-probes/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + dekorate-tests + io.dekorate + 2.9-SNAPSHOT + ../ + + + io.dekorate + 2.9-SNAPSHOT + feat-probes-openshift + Dekorate :: Tests :: Probes :: OpenShift + + + 3.8.2 + + + + + io.dekorate + openshift-annotations + ${project.version} + + + io.vertx + vertx-core + ${version.vertx} + + + + + io.dekorate + openshift-junit-starter + ${project.version} + test + + + diff --git a/tests/feat-openshift-probes/src/main/java/io/dekorate/example/Main.java b/tests/feat-openshift-probes/src/main/java/io/dekorate/example/Main.java new file mode 100644 index 000000000..f2298e6f4 --- /dev/null +++ b/tests/feat-openshift-probes/src/main/java/io/dekorate/example/Main.java @@ -0,0 +1,42 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +**/ + +package io.dekorate.example; + +import io.dekorate.annotation.Dekorate; +import io.vertx.core.AbstractVerticle; +import io.vertx.core.Future; + +@Dekorate +public class Main extends AbstractVerticle { + + @Override + public void start(Future future) { + vertx + .createHttpServer() + .requestHandler(r -> { + r.response().end("hello world!"); + }) + .listen(8080, result -> { + if (result.succeeded()) { + future.complete(); + } else { + future.fail(result.cause()); + } + }); + } +} diff --git a/tests/feat-openshift-probes/src/main/resources/application.properties b/tests/feat-openshift-probes/src/main/resources/application.properties new file mode 100644 index 000000000..3516afb38 --- /dev/null +++ b/tests/feat-openshift-probes/src/main/resources/application.properties @@ -0,0 +1,11 @@ +dekorate.openshift.ports[0].name=http +dekorate.openshift.ports[0].containerPort=8080 +dekorate.openshift.readinessProbe.httpActionPath=/readiness +dekorate.openshift.readinessProbe.periodSeconds=30 +dekorate.openshift.readinessProbe.timeoutSeconds=10 +dekorate.openshift.livenessProbe.httpActionPath=/liveness +dekorate.openshift.livenessProbe.periodSeconds=31 +dekorate.openshift.livenessProbe.timeoutSeconds=11 +dekorate.openshift.startupProbe.httpActionPath=/startup +dekorate.openshift.startupProbe.periodSeconds=32 +dekorate.openshift.startupProbe.timeoutSeconds=12 diff --git a/tests/feat-openshift-probes/src/test/java/io/dekorate/example/VertxTest.java b/tests/feat-openshift-probes/src/test/java/io/dekorate/example/VertxTest.java new file mode 100644 index 000000000..1766ef799 --- /dev/null +++ b/tests/feat-openshift-probes/src/test/java/io/dekorate/example/VertxTest.java @@ -0,0 +1,78 @@ +/** + * Copyright 2018 The original authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + **/ + +package io.dekorate.example; + +import static io.dekorate.testing.KubernetesResources.*; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.Optional; +import java.util.function.Function; + +import org.junit.jupiter.api.Test; + +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.Probe; +import io.fabric8.kubernetes.api.model.Service; +import io.fabric8.openshift.api.model.DeploymentConfig; + +class VertxTest { + + @Test + public void shouldContainService() { + KubernetesList list = loadGenerated("openshift"); + Optional service = findFirst(list, Service.class); + assertTrue(service.isPresent()); + } + + @Test + public void shouldContainProbes() { + KubernetesList list = loadGenerated("openshift"); + Optional deployment = findFirst(list, DeploymentConfig.class); + assertTrue(deployment.isPresent(), "DeploymentConfig not found!"); + + assertReadinessProbe(deployment.get(), "/readiness", 30, 10); + assertLivenessProbe(deployment.get(), "/liveness", 31, 11); + assertStartupProbe(deployment.get(), "/startup", 32, 12); + } + + private static void assertReadinessProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getReadinessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertLivenessProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getLivenessProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertStartupProbe(DeploymentConfig deployment, String actionPath, + int periodSeconds, int timeoutSeconds) { + assertProbe(deployment, Container::getStartupProbe, actionPath, periodSeconds, timeoutSeconds); + } + + private static void assertProbe(DeploymentConfig deployment, + Function probeFunction, + String actionPath, int periodSeconds, int timeoutSeconds) { + + assertTrue(deployment.getSpec().getTemplate().getSpec().getContainers().stream() + .map(probeFunction) + .anyMatch(probe -> actionPath.equals(probe.getHttpGet().getPath()) + && periodSeconds == probe.getPeriodSeconds() && timeoutSeconds == probe.getTimeoutSeconds())); + } +} diff --git a/tests/pom.xml b/tests/pom.xml index a3b76e56e..4db0de568 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -60,6 +60,9 @@ issue-775-sb-server-port issue-791-missing-imagestream-with-default-configuration issue-821-docker-build-on-ocp-test + feat-kubernetes-probes + feat-openshift-probes + feat-knative-probes