Skip to content

Commit

Permalink
Merge pull request #25455 from nguyensach
Browse files Browse the repository at this point in the history
* pr/25455:
  Polish 'Don't detect CloudPlatform when property is set'
  Don't detect CloudPlatform when property is set

Closes gh-25455
  • Loading branch information
philwebb committed Apr 15, 2021
2 parents fcb2210 + 61ff3c9 commit 930c637
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
*
* @author Phillip Webb
* @author Brian Clozel
* @author Nguyen Sach
* @since 1.3.0
*/
public enum CloudPlatform {
Expand Down Expand Up @@ -131,13 +132,16 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo

};

private static final String PROPERTY_NAME = "spring.main.cloud-platform";

/**
* Determines if the platform is active (i.e. the application is running in it).
* @param environment the environment
* @return if the platform is active.
*/
public boolean isActive(Environment environment) {
return isEnforced(environment) || isDetected(environment);
String platformProperty = environment.getProperty(PROPERTY_NAME);
return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment));
}

/**
Expand All @@ -148,7 +152,10 @@ public boolean isActive(Environment environment) {
* @since 2.3.0
*/
public boolean isEnforced(Environment environment) {
String platform = environment.getProperty("spring.main.cloud-platform");
return isEnforced(environment.getProperty(PROPERTY_NAME));
}

private boolean isEnforced(String platform) {
return name().equalsIgnoreCase(platform);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;

Expand All @@ -34,6 +35,7 @@
* Tests for {@link CloudPlatform}.
*
* @author Phillip Webb
* @author Nguyen Sach
*/
class CloudPlatformTests {

Expand Down Expand Up @@ -137,6 +139,17 @@ void getActiveWhenHasEnforcedCloudPlatform() {
assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
}

@Test
void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("EXAMPLE_SERVICE_HOST", "---");
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
Environment environment = getEnvironmentWithEnvVariables(envVars);
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment)))
.containsExactly(CloudPlatform.NONE);
}

private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
MockEnvironment environment = new MockEnvironment();
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
Expand Down

0 comments on commit 930c637

Please sign in to comment.