From 98a1017ee771c1d321d8b70b6f45a261feac6c0f Mon Sep 17 00:00:00 2001 From: Julien Dubois Date: Tue, 30 Mar 2021 13:01:32 +0200 Subject: [PATCH 1/2] Add detection of Azure App Service to CloudPlatform See gh-25829 --- .../boot/cloud/CloudPlatform.java | 17 ++++++++++++ .../boot/cloud/CloudPlatformTests.java | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java index c4f61b2a87..bae1ed64ab 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java @@ -45,6 +45,23 @@ public enum CloudPlatform { }, + /** + * Azure App Service platform. + */ + 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"; + + @Override + public boolean isDetected(Environment environment) { + return environment.containsProperty(WEBSITE_SITE_NAME) + && environment.containsProperty(WEBSITES_ENABLE_APP_SERVICE_STORAGE); + } + + }, + /** * Cloud Foundry platform. */ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java index 36bc7903f3..1d50f3082c 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java @@ -50,7 +50,33 @@ class CloudPlatformTests { Environment environment = new MockEnvironment(); CloudPlatform platform = CloudPlatform.getActive(environment); assertThat(platform).isNull(); + } + @Test + void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() { + Map envVars = new HashMap<>(); + envVars.put("WEBSITE_SITE_NAME", "---"); + envVars.put("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false"); + Environment environment = getEnvironmentWithEnvVariables(envVars); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isEqualTo(CloudPlatform.AZURE_APP_SERVICE); + assertThat(platform.isActive(environment)).isTrue(); + } + + @Test + void getActiveWhenHasWebsiteSiteNameShouldReturnNull() { + Environment environment = getEnvironmentWithEnvVariables( + Collections.singletonMap("WEBSITE_SITE_NAME", "---")); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isNull(); + } + + @Test + void getActiveWhenHasWebsitesEnableAppServiceStorageShouldReturnNull() { + Environment environment = getEnvironmentWithEnvVariables( + Collections.singletonMap("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "---")); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isNull(); } @Test From 4d311821231a42b09377558cef0607dbe49695c5 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 7 Apr 2021 12:32:40 +0100 Subject: [PATCH 2/2] Polish "Add detection of Azure App Service to CloudPlatform" See gh-25829 --- .../boot/cloud/CloudPlatform.java | 36 ++++++------ .../boot/cloud/CloudPlatformTests.java | 55 +++++++++---------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java index bae1ed64ab..4f5fcff119 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java @@ -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. @@ -45,23 +45,6 @@ public enum CloudPlatform { }, - /** - * Azure App Service platform. - */ - 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"; - - @Override - public boolean isDetected(Environment environment) { - return environment.containsProperty(WEBSITE_SITE_NAME) - && environment.containsProperty(WEBSITES_ENABLE_APP_SERVICE_STORAGE); - } - - }, - /** * Cloud Foundry platform. */ @@ -147,6 +130,23 @@ public enum CloudPlatform { return false; } + }, + + /** + * Azure App Service platform. + */ + 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"; + + @Override + public boolean isDetected(Environment environment) { + return environment.containsProperty(WEBSITE_SITE_NAME) + && environment.containsProperty(WEBSITES_ENABLE_APP_SERVICE_STORAGE); + } + }; private static final String PROPERTY_NAME = "spring.main.cloud-platform"; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java index 1d50f3082c..9cbb7c1b77 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java @@ -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. @@ -52,33 +52,6 @@ class CloudPlatformTests { assertThat(platform).isNull(); } - @Test - void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() { - Map envVars = new HashMap<>(); - envVars.put("WEBSITE_SITE_NAME", "---"); - envVars.put("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false"); - Environment environment = getEnvironmentWithEnvVariables(envVars); - CloudPlatform platform = CloudPlatform.getActive(environment); - assertThat(platform).isEqualTo(CloudPlatform.AZURE_APP_SERVICE); - assertThat(platform.isActive(environment)).isTrue(); - } - - @Test - void getActiveWhenHasWebsiteSiteNameShouldReturnNull() { - Environment environment = getEnvironmentWithEnvVariables( - Collections.singletonMap("WEBSITE_SITE_NAME", "---")); - CloudPlatform platform = CloudPlatform.getActive(environment); - assertThat(platform).isNull(); - } - - @Test - void getActiveWhenHasWebsitesEnableAppServiceStorageShouldReturnNull() { - Environment environment = getEnvironmentWithEnvVariables( - Collections.singletonMap("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "---")); - CloudPlatform platform = CloudPlatform.getActive(environment); - assertThat(platform).isNull(); - } - @Test void getActiveWhenHasVcapApplicationShouldReturnCloudFoundry() { Environment environment = new MockEnvironment().withProperty("VCAP_APPLICATION", "---"); @@ -157,6 +130,32 @@ class CloudPlatformTests { assertThat(platform).isNull(); } + @Test + void getActiveWhenHasWebsiteSiteNameAndWebsitesEnableAppServiceStorageShouldReturnAzureAppService() { + Map envVars = new HashMap<>(); + envVars.put("WEBSITE_SITE_NAME", "---"); + envVars.put("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "false"); + 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", "---")); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isNull(); + } + + @Test + void getActiveWhenHasWebsitesEnableAppServiceStorageAndNoWebsiteNameShouldNotReturnAzureAppService() { + Environment environment = getEnvironmentWithEnvVariables( + Collections.singletonMap("WEBSITES_ENABLE_APP_SERVICE_STORAGE", "---")); + CloudPlatform platform = CloudPlatform.getActive(environment); + assertThat(platform).isNull(); + } + @Test void getActiveWhenHasEnforcedCloudPlatform() { Environment environment = getEnvironmentWithEnvVariables(