|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.cloud;
|
18 | 18 |
|
19 |
| -import java.io.File; |
20 | 19 | import java.util.Collections;
|
21 | 20 | import java.util.HashMap;
|
22 | 21 | import java.util.Map;
|
@@ -85,45 +84,57 @@ void getActiveWhenHasHcLandscapeShouldReturnSap() {
|
85 | 84 | }
|
86 | 85 |
|
87 | 86 | @Test
|
88 |
| - void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() { |
89 |
| - MockEnvironment environment = new MockEnvironment(); |
90 |
| - Map<String, Object> source = new HashMap<>(); |
91 |
| - source.put("EXAMPLE_SERVICE_HOST", "---"); |
92 |
| - source.put("EXAMPLE_SERVICE_PORT", "8080"); |
93 |
| - PropertySource<?> propertySource = new SystemEnvironmentPropertySource( |
94 |
| - StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source); |
95 |
| - environment.getPropertySources().addFirst(propertySource); |
| 87 | + void getActiveWhenHasKubernetesServiceHostAndPortShouldReturnKubernetes() { |
| 88 | + Map<String, Object> envVars = new HashMap<>(); |
| 89 | + envVars.put("KUBERNETES_SERVICE_HOST", "---"); |
| 90 | + envVars.put("KUBERNETES_SERVICE_PORT", "8080"); |
| 91 | + Environment environment = getEnvironmentWithEnvVariables(envVars); |
96 | 92 | CloudPlatform platform = CloudPlatform.getActive(environment);
|
97 | 93 | assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
|
98 | 94 | assertThat(platform.isActive(environment)).isTrue();
|
99 | 95 | }
|
100 | 96 |
|
101 | 97 | @Test
|
102 |
| - void getActiveWhenHasKubernetesHostAndPortShouldReturnKubernetes() { |
103 |
| - MockEnvironment environment = new MockEnvironment(); |
104 |
| - Map<String, Object> source = new HashMap<>(); |
105 |
| - source.put("KUBERNETES_SERVICE_HOST", "---"); |
106 |
| - source.put("KUBERNETES_SERVICE_PORT", "8080"); |
107 |
| - PropertySource<?> propertySource = new SystemEnvironmentPropertySource( |
108 |
| - StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, source); |
109 |
| - environment.getPropertySources().addFirst(propertySource); |
| 98 | + void getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes() { |
| 99 | + Environment environment = getEnvironmentWithEnvVariables( |
| 100 | + Collections.singletonMap("KUBERNETES_SERVICE_HOST", "---")); |
| 101 | + CloudPlatform platform = CloudPlatform.getActive(environment); |
| 102 | + assertThat(platform).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void getActiveWhenHasKubernetesServicePortAndNoKubernetesServiceHostShouldNotReturnKubernetes() { |
| 107 | + Environment environment = getEnvironmentWithEnvVariables( |
| 108 | + Collections.singletonMap("KUBERNETES_SERVICE_PORT", "8080")); |
| 109 | + CloudPlatform platform = CloudPlatform.getActive(environment); |
| 110 | + assertThat(platform).isNull(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes() { |
| 115 | + Map<String, Object> envVars = new HashMap<>(); |
| 116 | + envVars.put("EXAMPLE_SERVICE_HOST", "---"); |
| 117 | + envVars.put("EXAMPLE_SERVICE_PORT", "8080"); |
| 118 | + Environment environment = getEnvironmentWithEnvVariables(envVars); |
110 | 119 | CloudPlatform platform = CloudPlatform.getActive(environment);
|
111 | 120 | assertThat(platform).isEqualTo(CloudPlatform.KUBERNETES);
|
112 | 121 | assertThat(platform.isActive(environment)).isTrue();
|
113 | 122 | }
|
114 | 123 |
|
115 | 124 | @Test
|
116 | 125 | void getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes() {
|
| 126 | + Environment environment = getEnvironmentWithEnvVariables( |
| 127 | + Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---")); |
| 128 | + CloudPlatform platform = CloudPlatform.getActive(environment); |
| 129 | + assertThat(platform).isNull(); |
| 130 | + } |
| 131 | + |
| 132 | + private Environment getEnvironmentWithEnvVariables(Map<String, Object> environmentVariables) { |
117 | 133 | MockEnvironment environment = new MockEnvironment();
|
118 | 134 | PropertySource<?> propertySource = new SystemEnvironmentPropertySource(
|
119 |
| - StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, |
120 |
| - Collections.singletonMap("EXAMPLE_SERVICE_HOST", "---")); |
| 135 | + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, environmentVariables); |
121 | 136 | environment.getPropertySources().addFirst(propertySource);
|
122 |
| - CloudPlatform platform = CloudPlatform.getActive(environment); |
123 |
| - File path = new File("/var/run/secrets/kubernetes.io"); |
124 |
| - if (!path.exists() && !path.isDirectory()) { |
125 |
| - assertThat(platform).isNull(); |
126 |
| - } |
| 137 | + return environment; |
127 | 138 | }
|
128 | 139 |
|
129 | 140 | }
|
0 commit comments