Skip to content

Commit

Permalink
Polish "Use more precise variables to detect Azure App Service"
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Sep 6, 2021
1 parent 69b2347 commit d6cc1f6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Expand Up @@ -16,6 +16,9 @@

package org.springframework.boot.cloud;

import java.util.Arrays;
import java.util.List;

import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
Expand Down Expand Up @@ -138,19 +141,12 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
*/
AZURE_APP_SERVICE {

private static final String WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME";

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";
private final List<String> azureEnvVariables = Arrays.asList("WEBSITE_SITE_NAME", "WEBSITE_INSTANCE_ID",
"WEBSITE_RESOURCE_GROUP", "WEBSITE_SKU");

@Override
public boolean isDetected(Environment environment) {
return environment.containsProperty(WEBSITE_SITE_NAME) && environment.containsProperty(WEBSITE_INSTANCE_ID)
&& environment.containsProperty(WEBSITE_RESOURCE_GROUP)
&& environment.containsProperty(WEBSITE_SKU);
return this.azureEnvVariables.stream().allMatch(environment::containsProperty);
}

};
Expand Down
Expand Up @@ -133,7 +133,7 @@ void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
}

@Test
void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() {
void getActiveWhenHasAllAzureEnvVariablesShouldReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITE_INSTANCE_ID", "1234");
Expand All @@ -146,16 +146,45 @@ void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldRetu
}

@Test
void getActiveWhenHasWebsiteSiteNameAndNoWebsitesEnableAppServiceStorageShouldNotReturnAzureAppService() {
Environment environment = getEnvironmentWithEnvVariables(Collections.singletonMap("WEBSITE_SITE_NAME", "---"));
void getActiveWhenHasMissingWebsiteSiteNameShouldNotReturnAzureAppService() {
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);
assertThat(platform).isNull();
}

@Test
void getActiveWhenHasWebsitesEnableAppServiceStorageAndNoWebsiteSiteNameShouldNotReturnAzureAppService() {
Environment environment = getEnvironmentWithEnvVariables(
Collections.singletonMap("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false"));
void getActiveWhenHasMissingWebsiteInstanceIdShouldNotReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>();
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);
assertThat(platform).isNull();
}
Expand Down

0 comments on commit d6cc1f6

Please sign in to comment.