Skip to content

Commit 88b7ea6

Browse files
authored
Merge pull request spring-cloud#143 from gytis/add-kubernetes-profile-duplication
Remove duplicate logic from addKubernetesProfile
2 parents 98d4b88 + de6cae7 commit 88b7ea6

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

spring-cloud-kubernetes-core/src/main/java/org/springframework/cloud/kubernetes/profile/KubernetesProfileApplicationListener.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.cloud.kubernetes.profile;
1818

19-
import io.fabric8.kubernetes.api.model.Pod;
20-
2119
import org.apache.commons.logging.Log;
2220
import org.apache.commons.logging.LogFactory;
2321
import org.springframework.cloud.kubernetes.PodUtils;
@@ -47,13 +45,6 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
4745
}
4846

4947
void addKubernetesProfile(ConfigurableEnvironment environment) {
50-
Pod current = utils.currentPod().get();
51-
if (current != null) {
52-
if (!hasKubernetesProfile(environment)) {
53-
environment.addActiveProfile(KUBERNETES_PROFILE);
54-
}
55-
}
56-
5748
if (utils.isInsideKubernetes()) {
5849
if (hasKubernetesProfile(environment)) {
5950
if (LOG.isDebugEnabled()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.springframework.cloud.kubernetes.profile;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.Mock;
7+
import org.mockito.runners.MockitoJUnitRunner;
8+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
9+
import org.springframework.cloud.kubernetes.PodUtils;
10+
import org.springframework.core.env.ConfigurableEnvironment;
11+
12+
import static org.mockito.Mockito.times;
13+
import static org.mockito.Mockito.verify;
14+
import static org.mockito.Mockito.when;
15+
16+
@RunWith(MockitoJUnitRunner.class)
17+
public class KubernetesProfileApplicationListenerTest {
18+
19+
private static final String[] ACTIVE_PROFILES = new String[0];
20+
21+
@Mock
22+
private ConfigurableEnvironment mockEnvironment;
23+
24+
@Mock
25+
private PodUtils mockPodUtils;
26+
27+
@Mock
28+
private ApplicationEnvironmentPreparedEvent mockEvent;
29+
30+
private KubernetesProfileApplicationListener listener;
31+
32+
@Before
33+
public void before() {
34+
when(mockEnvironment.getActiveProfiles()).thenReturn(ACTIVE_PROFILES);
35+
when(mockEvent.getEnvironment()).thenReturn(mockEnvironment);
36+
listener = new KubernetesProfileApplicationListener(mockPodUtils);
37+
}
38+
39+
@Test
40+
public void shouldEnableKubernetesProfile() {
41+
when(mockPodUtils.isInsideKubernetes()).thenReturn(true);
42+
listener.onApplicationEvent(mockEvent);
43+
verify(mockEnvironment).addActiveProfile("kubernetes");
44+
}
45+
46+
@Test
47+
public void shouldNotEnableKubernetesProfile() {
48+
when(mockPodUtils.isInsideKubernetes()).thenReturn(false);
49+
listener.onApplicationEvent(mockEvent);
50+
verify(mockEnvironment, times(0)).addActiveProfile("kubernetes");
51+
}
52+
53+
}

0 commit comments

Comments
 (0)