|
| 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