-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...eduler/src/main/java/org/cloudfoundry/autoscaler/scheduler/util/health/EndpointsEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...udfoundry/autoscaler/scheduler/rest/healthControllerTest/HealthEndpointMixedAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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"); | ||
} | ||
} |