diff --git a/Makefile b/Makefile index 6ff9ed8901..44124c04a8 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,6 @@ all_modules:= $(go_modules) db scheduler MVN_OPTS="-Dmaven.test.skip=true -Dmaven.plugin.validation=VERBOSE" OS:=$(shell . /etc/lsb-release &>/dev/null && echo $${DISTRIB_ID} || uname ) db_type:=postgres -DB_HOST:=localhost DBURL := $(shell case "${db_type}" in\ (postgres) printf "postgres://postgres:postgres@${DB_HOST}/autoscaler?sslmode=disable"; ;; \ (mysql) printf "root@tcp(${DB_HOST})/autoscaler?tls=false"; ;; esac) @@ -25,6 +24,7 @@ DEST?=build export BUILDIN_MODE?=false export DEBUG?=false export ACCEPTANCE_TESTS_FILE?=${DEST}/app-autoscaler-acceptance-tests-v${VERSION}.tgz +export DB_HOST:=localhost $(shell mkdir -p target) $(shell mkdir -p build) @@ -131,7 +131,6 @@ test-autoscaler-suite: check-db_type init init-db test-certs autoscaler schedule @make -C src/autoscaler testsuite TEST=${TEST} DBURL="${DBURL}" OPTS="${OPTS}" test-scheduler: check-db_type init init-db test-certs - @export DB_HOST=${DB_HOST}; \ cd src && mvn test --no-transfer-progress -Dspring.profiles.include=${db_type} && cd .. test-changelog: init @make -C src/changelog test diff --git a/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/health/EndpointsEnum.java b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/health/EndpointsEnum.java new file mode 100644 index 0000000000..21b98b9809 --- /dev/null +++ b/src/scheduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/health/EndpointsEnum.java @@ -0,0 +1,51 @@ +package org.cloudfoundry.autoscaler.scheduler.util.health; + +import java.util.Set; + +public enum EndpointsEnum { + PROMETHEUS("/health/prometheus"), + LIVENESS("/health/liveness"); + + private final String url; + + EndpointsEnum(String url) { + this.url = url; + } + + public String getUrl() { + return url; + } + + public static EndpointsEnum valueOfEndpoint(String url) { + for (EndpointsEnum endpoint : values()) { + if (endpoint.url.equals(url)) { + return endpoint; + } + } + throw new IllegalArgumentException("Enum for " + url); + } + + public static boolean isValidEndpoint(String url) { + EndpointsEnum endpointsEnum = valueOfEndpoint(url); + if (endpointsEnum != null) { + return true; + } + return false; + } + + public static boolean configuredEndpointsExists(Set userDefindHealthEndpoints) { + + for (String configuredEndpoint : userDefindHealthEndpoints) { + return isValidEndpoint(configuredEndpoint); + } + return false; + } + + public static String displayAllEndpointValues() { + String endpointValues = EndpointsEnum.values()[0].getUrl(); + for (int i = 1; i < EndpointsEnum.values().length; i++) { + endpointValues += "," + EndpointsEnum.values()[i].getUrl(); + } + return endpointValues; + } +} diff --git a/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/rest/healthControllerTest/HealthEndpointMixedAuthTest.java b/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/rest/healthControllerTest/HealthEndpointMixedAuthTest.java new file mode 100644 index 0000000000..6413eab638 --- /dev/null +++ b/src/scheduler/src/test/java/org/cloudfoundry/autoscaler/scheduler/rest/healthControllerTest/HealthEndpointMixedAuthTest.java @@ -0,0 +1,58 @@ +package org.cloudfoundry.autoscaler.scheduler.rest.healthControllerTest; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import org.cloudfoundry.autoscaler.scheduler.conf.HealthServerConfiguration; +import org.cloudfoundry.autoscaler.scheduler.util.HealthUtils; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@DirtiesContext(classMode = ClassMode.BEFORE_CLASS) +@ActiveProfiles("HealthAuth") +@TestPropertySource( + properties = "scheduler.healthserver.unprotectedEndpoints=" + "/health/liveness") +public class HealthEndpointMixedAuthTest { + @Autowired private TestRestTemplate restTemplate; + @Autowired private HealthServerConfiguration healthServerConfig; + + /* + // case 2.1 ; config ["/health/liveness"] + here is – by configuration – one protected endpoint "/health/prometheus" and one unprotected "/health/liveness". + The user is authenticated. + The user queries on "/health/prometheus". + Expected behaviour: The request will be handled successfully. + */ + @Test + public void givenLivenessUnprotectedAndUserIsAuthenticatedShouldReturnPrometheusWith200() + throws MalformedURLException, URISyntaxException { + + ResponseEntity prometheusResponse = + this.restTemplate + .withBasicAuth("prometheus", "someHash") + .getForEntity(HealthUtils.prometheusMetricsUrl().toURI(), String.class); + + assertThat(prometheusResponse.getStatusCode().value()) + .describedAs("Http status code should be OK") + .isEqualTo(200); + assertThat(prometheusResponse.getHeaders().getContentType()) + .isEqualTo(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8)); + assertThat(prometheusResponse.getBody()) + .contains("autoscaler_scheduler_data_source_initial_size 0.0"); + } +}