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 23, 2024
1 parent 82f8406 commit 35dbbce
Show file tree
Hide file tree
Showing 5 changed files with 1,071 additions and 474 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* 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("/test-kubeconfig")));
}

@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", "testns")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token");
}

@Nested
@DisplayName("user configuration overrides KubeConfig attributes")
class UserConfigurationOverKubeConfig extends UserConfigurationViaConfigBuilder {
}

@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.namespace", "namespace-overridden");
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-overridden")
.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 use it")
void whenServiceAccountPropertyConfigured_thenDoNotUseIt() {
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("/test-serviceaccount/token")));
System.setProperty("kubenamespace", Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/test-namespace")));
Config config = new ConfigBuilder().build();

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

@Nested
@DisplayName("In Cluster mounted ServiceAccount")
class MountedServiceAccountSource {
@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("/test-serviceaccount/token")));
System.setProperty("kubenamespace", Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/test-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", "testnsfrompath")
.extracting(Config::getAutoOAuthToken)
.asString()
.contains("token-from-mounted-serviceaccount");
}

@Nested
@DisplayName("user configuration overrides ServiceAccount's Config attributes")
class UserConfigurationOverServiceAccount extends UserConfigurationViaConfigBuilder {
}

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

// When + Then
assertThat(config)
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-overridden")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-overridden");
} 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://user-configuration-override:8443");
System.setProperty("kubernetes.namespace", "namespace-overridden");
System.setProperty("kubernetes.auth.token", "token-overridden");
}

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

@Nested
@DisplayName("User configuration overrides Config attributes configured via System Properties")
class UserConfigurationOverSystemProperties extends UserConfigurationViaConfigBuilder {
}

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

private abstract static class UserConfigurationViaConfigBuilder {
@SuppressWarnings("unused")
@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")
.withAutoOAuthToken("token-overridden")
.build();

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

0 comments on commit 35dbbce

Please sign in to comment.