Skip to content

Commit

Permalink
Merge pull request #27819 from jdubois
Browse files Browse the repository at this point in the history
* pr/27819:
  Polish "Use more precise variables to detect Azure App Service"
  Use more precise variables to detect Azure App Service

Closes gh-27819
  • Loading branch information
snicoll committed Sep 6, 2021
2 parents b82da0a + d6cc1f6 commit 222a570
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 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,14 +141,12 @@ private boolean isAutoDetected(EnumerablePropertySource<?> environmentPropertySo
*/
AZURE_APP_SERVICE {

private static final String WEBSITE_SITE_NAME = "WEBSITE_SITE_NAME";

private static final String WEBSITES_ENABLE_APP_SERVICE_STORAGE = "WEBSITES_ENABLE_APP_SERVICE_STORAGE";
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(WEBSITES_ENABLE_APP_SERVICE_STORAGE);
return this.azureEnvVariables.stream().allMatch(environment::containsProperty);
}

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

@Test
void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() {
void getActiveWhenHasAllAzureEnvVariablesShouldReturnAzureAppService() {
Map<String, Object> envVars = new HashMap<>();
envVars.put("WEBSITE_SITE_NAME", "---");
envVars.put("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false");
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).isEqualTo(CloudPlatform.AZURE_APP_SERVICE);
assertThat(platform.isActive(environment)).isTrue();
}

@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 222a570

Please sign in to comment.