Polish "Use more precise variables to detect Azure App Service"

See gh-27819
This commit is contained in:
Stephane Nicoll 2021-09-06 08:28:58 +02:00
parent 69b23470c7
commit d6cc1f6d7d
2 changed files with 41 additions and 16 deletions

View File

@ -16,6 +16,9 @@
package org.springframework.boot.cloud; package org.springframework.boot.cloud;
import java.util.Arrays;
import java.util.List;
import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.EnumerablePropertySource;
@ -138,19 +141,12 @@ public enum CloudPlatform {
*/ */
AZURE_APP_SERVICE { AZURE_APP_SERVICE {
private static final String WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME"; private final List<String> azureEnvVariables = Arrays.asList("WEBSITE_SITE_NAME", "WEBSITE_INSTANCE_ID",
"WEBSITE_RESOURCE_GROUP", "WEBSITE_SKU");
private static final String WEBSITE_INSTANCE_ID = "WEBSITE_INSTANCE_ID";
private static final String WEBSITE_RESOURCE_GROUP = "WEBSITE_RESOURCE_GROUP";
private static final String WEBSITE_SKU = "WEBSITE_SKU";
@Override @Override
public boolean isDetected(Environment environment) { public boolean isDetected(Environment environment) {
return environment.containsProperty(WEBSITE_SITE_NAME) && environment.containsProperty(WEBSITE_INSTANCE_ID) return this.azureEnvVariables.stream().allMatch(environment::containsProperty);
&& environment.containsProperty(WEBSITE_RESOURCE_GROUP)
&& environment.containsProperty(WEBSITE_SKU);
} }
}; };

View File

@ -133,7 +133,7 @@ class CloudPlatformTests {
} }
@Test @Test
void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() { void getActiveWhenHasAllAzureEnvVariablesShouldReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>(); Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_SITE_NAME", "---"); envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITE_INSTANCE_ID", "1234"); envVars.put("WEBSITE_INSTANCE_ID", "1234");
@ -146,16 +146,45 @@ class CloudPlatformTests {
} }
@Test @Test
void getActiveWhenHasWebsiteSiteNameAndNoWebsitesEnableAppServiceStorageShouldNotReturnAzureAppService() { void getActiveWhenHasMissingWebsiteSiteNameShouldNotReturnAzureAppService() {
Environment environment = getEnvironmentWithEnvVariables(Collections.singletonMap("WEBSITE_SITE_NAME", "---")); Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_INSTANCE_ID", "1234");
envVars.put("WEBSITE_RESOURCE_GROUP", "test");
envVars.put("WEBSITE_SKU", "1234");
Environment environment = getEnvironmentWithEnvVariables(envVars);
CloudPlatform platform = CloudPlatform.getActive(environment); CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull(); assertThat(platform).isNull();
} }
@Test @Test
void getActiveWhenHasWebsitesEnableAppServiceStorageAndNoWebsiteSiteNameShouldNotReturnAzureAppService() { void getActiveWhenHasMissingWebsiteInstanceIdShouldNotReturnAzureAppService() {
Environment environment = getEnvironmentWithEnvVariables( Map<String, Object> envVars = new HashMap<>();
Collections.singletonMap("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false")); envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITE_RESOURCE_GROUP", "test");
envVars.put("WEBSITE_SKU", "1234");
Environment environment = getEnvironmentWithEnvVariables(envVars);
CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull();
}
@Test
void getActiveWhenHasMissingWebsiteResourceGroupShouldNotReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITE_INSTANCE_ID", "1234");
envVars.put("WEBSITE_SKU", "1234");
Environment environment = getEnvironmentWithEnvVariables(envVars);
CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull();
}
@Test
void getActiveWhenHasMissingWebsiteSkuShouldNotReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITE_INSTANCE_ID", "1234");
envVars.put("WEBSITE_RESOURCE_GROUP", "test");
Environment environment = getEnvironmentWithEnvVariables(envVars);
CloudPlatform platform = CloudPlatform.getActive(environment); CloudPlatform platform = CloudPlatform.getActive(environment);
assertThat(platform).isNull(); assertThat(platform).isNull();
} }