Skip to content

Commit

Permalink
Move Probe tests from examples to tests
Browse files Browse the repository at this point in the history
In this PR: dekorateio#875, we added the Probe coverage under the examples, where it should go under the tests modules.
  • Loading branch information
Sgitario committed Feb 28, 2022
1 parent 6659baf commit 08ac818
Show file tree
Hide file tree
Showing 20 changed files with 588 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {

Expand All @@ -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<Container, Probe> 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()));
}

<T extends HasMetadata> Optional<T> findFirst(KubernetesList list, Class<T> t) {
return (Optional<T>) list.getItems().stream()
.filter(i -> t.isInstance(i))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,80 +1,39 @@
/**
* 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.*;

class VertxTest {

@Test
public void shouldContainService() {
public void shouldContainService() throws Exception {
KubernetesList list = loadGenerated("kubernetes");
Optional<Service> service = findFirst(list, Service.class);
assertTrue(service.isPresent());
}

@Test
public void shouldContainProbes() {
KubernetesList list = loadGenerated("kubernetes");
Optional<Deployment> 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<Container, Probe> 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()));
}
}

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,80 +1,39 @@
/**
* 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.*;

class VertxTest {

@Test
public void shouldContainService() {
public void shouldContainService() throws Exception {
KubernetesList list = loadGenerated("openshift");
Optional<Service> service = findFirst(list, Service.class);
assertTrue(service.isPresent());
}

@Test
public void shouldContainProbes() {
KubernetesList list = loadGenerated("openshift");
Optional<DeploymentConfig> 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<Container, Probe> 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()));
}
}

2 changes: 1 addition & 1 deletion tests/feat-642-knative-traffic-splitting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>io.dekorate</groupId>
<artifactId>feat-642-knative-traffic-splitting</artifactId>
<name>Dekorate :: Tests :: Annotations :: Knative :: Traffci #642</name>
<name>Dekorate :: Tests :: Annotations :: Knative :: Traffic #642</name>
<description></description>

<dependencies>
Expand Down
58 changes: 58 additions & 0 deletions tests/feat-knative-probes/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>dekorate-tests</artifactId>
<groupId>io.dekorate</groupId>
<version>2.9-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<groupId>io.dekorate</groupId>
<version>2.9-SNAPSHOT</version>
<artifactId>feat-probes-knative</artifactId>
<name>Dekorate :: Tests :: Probes :: Knative</name>

<dependencies>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>knative-annotations</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>dekorate-spring-boot</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${version.spring-boot}</version>
</dependency>

<!-- Testing -->
<dependency>
<groupId>io.dekorate</groupId>
<artifactId>knative-junit-starter</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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";
}
}
Loading

0 comments on commit 08ac818

Please sign in to comment.