Skip to content

Commit 5b836f3

Browse files
committed
Merge branch '2.3.x' into 2.4.x
Closes gh-26124
2 parents fb3796d + 930c637 commit 5b836f3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,6 +29,7 @@
2929
*
3030
* @author Phillip Webb
3131
* @author Brian Clozel
32+
* @author Nguyen Sach
3233
* @since 1.3.0
3334
*/
3435
public enum CloudPlatform {
@@ -140,7 +141,8 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
140141
* @return if the platform is active.
141142
*/
142143
public boolean isActive(Environment environment) {
143-
return isEnforced(environment) || isDetected(environment);
144+
String platformProperty = environment.getProperty(PROPERTY_NAME);
145+
return isEnforced(platformProperty) || (platformProperty == null && isDetected(environment));
144146
}
145147

146148
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.HashMap;
2121
import java.util.Map;
22+
import java.util.stream.Stream;
2223

2324
import org.junit.jupiter.api.Test;
2425

@@ -36,6 +37,7 @@
3637
* Tests for {@link CloudPlatform}.
3738
*
3839
* @author Phillip Webb
40+
* @author Nguyen Sach
3941
*/
4042
class CloudPlatformTests {
4143

@@ -177,6 +179,16 @@ void isEnforcedWhenBinderPropertyIsMissingReturnsFalse() {
177179
assertThat(CloudPlatform.KUBERNETES.isEnforced(binder)).isFalse();
178180
}
179181

182+
void isActiveWhenNoCloudPlatformIsEnforcedAndHasKubernetesServiceHostAndKubernetesServicePort() {
183+
Map<String, Object> envVars = new HashMap<>();
184+
envVars.put("EXAMPLE_SERVICE_HOST", "---");
185+
envVars.put("EXAMPLE_SERVICE_PORT", "8080");
186+
Environment environment = getEnvironmentWithEnvVariables(envVars);
187+
((MockEnvironment) environment).setProperty("spring.main.cloud-platform", "none");
188+
assertThat(Stream.of(CloudPlatform.values()).filter((platform) -> platform.isActive(environment)))
189+
.containsExactly(CloudPlatform.NONE);
190+
}
191+
180192
private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) {
181193
MockEnvironment environment = new MockEnvironment();
182194
PropertySource<?> propertySource = new SystemEnvironmentPropertySource(

0 commit comments

Comments
 (0)