Skip to content

Commit

Permalink
test : Add unit test for old deprecated Config constructor
Browse files Browse the repository at this point in the history
- Add unit test for `@Deprecated` Config constructor. We don't know whether this public constructor is being used by any user. Adding a test just in case to verify that it behaves as expected.
- Organize tests in `ConfigTest` to be grouped under various load sources
- Add new test `ConfigSourcePrecedenceTest` for verifying Config source load precedence

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Jul 24, 2024
1 parent 1545ba2 commit 66c4e8d
Show file tree
Hide file tree
Showing 9 changed files with 1,287 additions and 517 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client;

import io.fabric8.kubernetes.client.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class ConfigSourcePrecedenceTest {
@Nested
@DisplayName("kubeconfig")
class KubeConfigSource {
@BeforeEach
void setUp() {
System.setProperty("kubeconfig",
Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/config-source-precedence/kube-config")));
}

@Test
@DisplayName("no other source provided, use kubeconfig attributes in Config")
void whenNoOtherSourceProvided_thenUseKubeConfig() {
// Given
Config config = new ConfigBuilder().build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://172.28.128.4:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-kubeconfig")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-from-kubeconfig");
}

@Test
@DisplayName("User configuration via builder given most precedence")
void whenUserConfigurationOverridesSomeFields_thenUserConfigurationGivenPrecedence() {
// Given
Config config = new ConfigBuilder()
.withMasterUrl("https://user-configuration-override:8443")
.withNamespace("namespace-from-user")
.build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-user")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-from-kubeconfig");
}

@Test
@DisplayName("System Properties configured, then give precedence to System Properties")
void whenSystemPropertiesUsedForSomeFields_thenSystemPropertiesGivenPrecedence() {
try {
// Given
System.setProperty("kubernetes.master", "https://user-configuration-override:8443");
System.setProperty("kubernetes.auth.token", "token-overridden");

// When
Config config = new ConfigBuilder().build();

// Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-kubeconfig")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-overridden");
} finally {
System.clearProperty("kubernetes.master");
System.clearProperty("kubernetes.namespace");
System.clearProperty("kubernetes.auth.token");
}
}

@Test
@DisplayName("Service Account files provided, then do NOT load ServiceAccount attributes in Config")
void whenServiceAccountPropertyConfigured_thenDoNotUseServiceAccount() {
try {
// Given
System.setProperty("KUBERNETES_SERVICE_HOST", "10.96.0.1");
System.setProperty("KUBERNETES_SERVICE_PORT", "443");
System.setProperty("kubernetes.auth.serviceAccount.token",
Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/config-source-precedence/serviceaccount/token")));
System.setProperty("kubenamespace",
Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/config-source-precedence/serviceaccount/namespace")));
Config config = new ConfigBuilder().build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://172.28.128.4:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-kubeconfig")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-from-kubeconfig");
} finally {
System.clearProperty("KUBERNETES_SERVICE_HOST");
System.clearProperty("KUBERNETES_SERVICE_PORT");
System.clearProperty("kubernetes.auth.serviceAccount.token");
System.clearProperty("kubenamespace");
}
}
}

@Nested
@DisplayName("In Kubernetes Cluster")
class InsideKubernetesCluster {
@BeforeEach
void setUp() {
System.setProperty("kubeconfig", "/dev/null");
System.setProperty("KUBERNETES_SERVICE_HOST", "10.96.0.1");
System.setProperty("KUBERNETES_SERVICE_PORT", "443");
System.setProperty("kubernetes.auth.serviceAccount.token",
Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/config-source-precedence/serviceaccount/token")));
System.setProperty("kubenamespace",
Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/config-source-precedence/serviceaccount/namespace")));
}

@Test
@DisplayName("no other source provided, use ServiceAccount attributes in Config")
void whenNoOtherSourceProvided_thenUseServiceAccount() {
// Given
Config config = new ConfigBuilder().build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://10.96.0.1:443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-mounted-serviceaccount")
.extracting(Config::getAutoOAuthToken)
.asString()
.contains("token-from-mounted-serviceaccount");
}

@Test
@DisplayName("User configuration via builder given most precedence")
void whenUserConfigurationOverridesSomeFields_thenUserConfigurationGivenPrecedence() {
// Given
Config config = new ConfigBuilder()
.withMasterUrl("https://user-configuration-override:8443")
.withNamespace("namespace-from-user")
.build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-user")
.extracting(Config::getAutoOAuthToken)
.asString()
.contains("token-from-mounted-serviceaccount");
}

@Test
@DisplayName("System Properties configured, then give precedence to System Properties")
void whenSystemPropertiesUsedForSomeFields_thenSystemPropertiesGivenPrecedence() {
try {
System.setProperty("kubernetes.master", "https://properties-configuration-override:8443");
System.setProperty("kubernetes.auth.token", "token-from-properties");
Config config = new ConfigBuilder().build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://properties-configuration-override:8443/")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-from-properties")
.hasFieldOrPropertyWithValue("namespace", "namespace-from-mounted-serviceaccount");
} finally {
System.clearProperty("kubernetes.master");
System.clearProperty("kubernetes.namespace");
System.clearProperty("kubernetes.auth.token");
}
}

@AfterEach
void tearDown() {
System.clearProperty("KUBERNETES_SERVICE_HOST");
System.clearProperty("KUBERNETES_SERVICE_PORT");
System.clearProperty("kubernetes.auth.serviceAccount.token");
System.clearProperty("kubenamespace");
System.clearProperty("kubeconfig");
}
}

@Nested
@DisplayName("System Properties")
class SystemPropertiesSource {
@BeforeEach
void setUp() {
System.setProperty("kubernetes.master", "https://property-configuration-override:8443");
System.setProperty("kubernetes.namespace", "namespace-set-via-properties");
System.setProperty("kubernetes.auth.token", "token-set-via-properties");
}

@Test
@DisplayName("when no other sources configured, then read from System properties")
void whenNoOtherSourceProvided_thenUseSystemProperties() {
assertThat(new ConfigBuilder().build())
.hasFieldOrPropertyWithValue("masterUrl", "https://property-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-set-via-properties")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-set-via-properties");
}

@Test
@DisplayName("User configuration via builder given most precedence")
void whenUserConfigurationOverridesSomeFields_thenUserConfigurationGivenPrecedence() {
// Given
Config config = new ConfigBuilder()
.withMasterUrl("https://user-configuration-override:8443")
.withNamespace("namespace-overridden-by-user")
.build();

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-overridden-by-user")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-set-via-properties");
}

@AfterEach
void tearDown() {
System.clearProperty("kubernetes.master");
System.clearProperty("kubernetes.namespace");
System.clearProperty("kubernetes.auth.token");
}
}
}
Loading

0 comments on commit 66c4e8d

Please sign in to comment.