files;
+
+ private final SpringApplication springApplication = Mockito.mock(SpringApplication.class);
+
+ private final ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
+
+ private static final AbstractKubernetesProfileEnvironmentPostProcessor POST_PROCESSOR_INSIDE = new AbstractKubernetesProfileEnvironmentPostProcessor() {
+ @Override
+ protected boolean isInsideKubernetes(Environment environment) {
+ return true;
+ }
+ };
+
+ private static final AbstractKubernetesProfileEnvironmentPostProcessor POST_PROCESSOR_OUTSIDE = new AbstractKubernetesProfileEnvironmentPostProcessor() {
+ @Override
+ protected boolean isInsideKubernetes(Environment environment) {
+ return false;
+ }
+ };
+
+ @Before
+ public void before() {
+ paths = Mockito.mockStatic(Paths.class);
+ files = Mockito.mockStatic(Files.class);
+ }
+
+ @After
+ public void after() {
+ paths.close();
+ files.close();
+ }
+
+ /**
+ *
+ * 1) "spring.cloud.kubernetes.enabled" is false; thus nothing happens
+ *
+ */
+ @Test
+ public void testKubernetesDisabled() {
+ TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, "spring.cloud.kubernetes.enabled=false");
+ POST_PROCESSOR_INSIDE.postProcessEnvironment(context.getEnvironment(), springApplication);
+
+ assertKubernetesProfileNotPresent();
+ assertKubernetesPropertySourceNotPresent();
+
+ }
+
+ /**
+ *
+ * 1) "spring.cloud.kubernetes.enabled" is true
+ * 2) "spring.cloud.kubernetes.client.serviceAccountNamespacePath" is present, but does not resolve to an actual File
+ *
+ */
+ @Test
+ public void testKubernetesEnabledAndServiceAccountNamespacePathIsNotResolved() {
+ TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, "spring.cloud.kubernetes.enabled=true",
+ "spring.cloud.kubernetes.client.serviceAccountNamespacePath=" + PATH);
+ serviceAccountFileResolved(false, PATH);
+ POST_PROCESSOR_INSIDE.postProcessEnvironment(context.getEnvironment(), springApplication);
+
+ assertKubernetesProfilePresent();
+ assertKubernetesPropertySourceNotPresent();
+
+ }
+
+ /**
+ *
+ * 1) "spring.cloud.kubernetes.enabled" is true
+ * 2) "spring.cloud.kubernetes.client.serviceAccountNamespacePath" is present and resolves to an actual File
+ *
+ */
+ @Test
+ public void testKubernetesEnabledAndServiceAccountNamespacePathIsResolved() {
+ TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, "spring.cloud.kubernetes.enabled=true",
+ "spring.cloud.kubernetes.client.serviceAccountNamespacePath=" + PATH);
+
+ Path path = serviceAccountFileResolved(true, PATH);
+ mockServiceAccountNamespace(path);
+
+ POST_PROCESSOR_INSIDE.postProcessEnvironment(context.getEnvironment(), springApplication);
+
+ assertKubernetesProfilePresent();
+ assertKubernetesPropertySourcePresent();
+
+ }
+
+ /**
+ *
+ * 1) "spring.cloud.kubernetes.enabled" is true
+ * 2) "spring.cloud.kubernetes.client.serviceAccountNamespacePath" is not present, as such:
+ * 3) "/var/run/secrets/kubernetes.io/serviceaccount/namespace" is picked up, which is resolved and present
+ *
+ */
+ @Test
+ public void testKubernetesEnabledAndServiceAccountNamespacePathIsResolvedViaDefaultLocation() {
+ TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, "spring.cloud.kubernetes.enabled=true");
+
+ Path path = serviceAccountFileResolved(true, "/var/run/secrets/kubernetes.io/serviceaccount/namespace");
+ mockServiceAccountNamespace(path);
+
+ POST_PROCESSOR_INSIDE.postProcessEnvironment(context.getEnvironment(), springApplication);
+
+ assertKubernetesProfilePresent();
+ assertKubernetesPropertySourcePresent();
+ }
+
+ /**
+ *
+ * 1) "spring.cloud.kubernetes.enabled" is true
+ * 2) isInsideKubernetes returns false
+ * 3) "spring.cloud.kubernetes.client.serviceAccountNamespacePath" is not present, as such:
+ * 4) "/var/run/secrets/kubernetes.io/serviceaccount/namespace" is picked up, which is resolved and present
+ *
+ */
+ @Test
+ public void testOutsideKubernetes() {
+ TestPropertySourceUtils.addInlinedPropertiesToEnvironment(context, "spring.cloud.kubernetes.enabled=true");
+
+ Path path = serviceAccountFileResolved(true, "/var/run/secrets/kubernetes.io/serviceaccount/namespace");
+ mockServiceAccountNamespace(path);
+
+ POST_PROCESSOR_OUTSIDE.postProcessEnvironment(context.getEnvironment(), springApplication);
+
+ assertKubernetesProfileNotPresent();
+ assertKubernetesPropertySourcePresent();
+ }
+
+ /*
+ * 'kubernetes' profile is not present
+ */
+ private void assertKubernetesProfileNotPresent() {
+ Assert.assertFalse("'kubernetes' profile must not be present when 'spring.cloud.kubernetes.enabled' is false",
+ kubernetesProfile().isPresent());
+ }
+
+ /*
+ * 'kubernetes' profile is present
+ */
+ private void assertKubernetesProfilePresent() {
+ Assert.assertTrue("'kubernetes' profile must be present when 'spring.cloud.kubernetes.enabled' is true",
+ kubernetesProfile().isPresent());
+ }
+
+ /*
+ * 'KUBERNETES_NAMESPACE_PROPERTY_SOURCE' source is not present
+ */
+ private void assertKubernetesPropertySourceNotPresent() {
+ Optional> kubernetesPropertySource = kubernetesPropertySource();
+
+ Assert.assertFalse(
+ "'KUBERNETES_NAMESPACE_PROPERTY_SOURCE' source must not be present when 'spring.cloud.kubernetes.enabled' is false",
+ kubernetesPropertySource.isPresent());
+ }
+
+ /*
+ * 'KUBERNETES_NAMESPACE_PROPERTY_SOURCE' source is present
+ */
+ private void assertKubernetesPropertySourcePresent() {
+
+ Optional> kubernetesPropertySource = kubernetesPropertySource();
+ Assert.assertTrue(
+ "'KUBERNETES_NAMESPACE_PROPERTY_SOURCE' source must be present when 'spring.cloud.kubernetes.enabled' is true",
+ kubernetesPropertySource.isPresent());
+
+ String property = (String) kubernetesPropertySource.get()
+ .getProperty("spring.cloud.kubernetes.client.namespace");
+ Assert.assertEquals("'spring.cloud.kubernetes.client.namespace' must be set to 'foundIt'", property, FOUNT_IT);
+ }
+
+ /**
+ *
+ * 1) serviceAccountNamespace File is present or not
+ * 2) if the above is present, under what actualPath
+ *
+ */
+ private Path serviceAccountFileResolved(boolean present, String actualPath) {
+ Path path = Mockito.mock(Path.class);
+ paths.when(() -> Paths.get(actualPath)).thenReturn(path);
+ files.when(() -> Files.isRegularFile(path)).thenReturn(present);
+ return path;
+ }
+
+ /*
+ * returns "foundIt" for service account namespace
+ */
+ private void mockServiceAccountNamespace(Path path) {
+ files.when(() -> Files.readAllBytes(path)).thenReturn(FOUNT_IT.getBytes());
+ }
+
+ private Optional kubernetesProfile() {
+ return Arrays.stream(context.getEnvironment().getActiveProfiles()).filter("kubernetes"::equals).findFirst();
+ }
+
+ private Optional> kubernetesPropertySource() {
+ return context.getEnvironment().getPropertySources().stream()
+ .filter(x -> "KUBERNETES_NAMESPACE_PROPERTY_SOURCE".equals(x.getName())).findAny();
+ }
+
+}