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 df857b2
Show file tree
Hide file tree
Showing 5 changed files with 923 additions and 337 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(Config.KUBERNETES_KUBECONFIG_FILE, Utils.filePath(ConfigTest.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(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "https://user-configuration-override:8443");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "namespace-overridden");
System.setProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY, "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(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY);
}
}

@Test
@DisplayName("Service Account files provided, then do NOT use it")
void whenServiceAccountPropertyConfigured_thenDoNotUseIt() {
try {
// Given
System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "10.96.0.1");
System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443");
System.setProperty(Config.KUBERNETES_AUTH_SERVICEACCOUNT_TOKEN_FILE_SYSTEM_PROPERTY,
Utils.filePath(ConfigTest.class.getResource("/test-serviceaccount/token")));
System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, Utils.filePath(ConfigTest.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(Config.KUBERNETES_SERVICE_HOST_PROPERTY);
System.clearProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY);
System.clearProperty(Config.KUBERNETES_AUTH_SERVICEACCOUNT_TOKEN_FILE_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_NAMESPACE_FILE);
}
}
}

@Nested
@DisplayName("In Cluster mounted ServiceAccount")
class MountedServiceAccountSource {
@BeforeEach
void setUp() {
System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null");
System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "10.96.0.1");
System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443");
System.setProperty(Config.KUBERNETES_AUTH_SERVICEACCOUNT_TOKEN_FILE_SYSTEM_PROPERTY,
Utils.filePath(ConfigTest.class.getResource("/test-serviceaccount/token")));
System.setProperty(Config.KUBERNETES_NAMESPACE_FILE, Utils.filePath(ConfigTest.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(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "https://user-configuration-override:8443");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "namespace-overridden");
System.setProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY, "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(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY);
}
}

@AfterEach
void tearDown() {
System.clearProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY);
System.clearProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY);
System.clearProperty(Config.KUBERNETES_AUTH_SERVICEACCOUNT_TOKEN_FILE_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_NAMESPACE_FILE);
System.clearProperty(Config.KUBERNETES_KUBECONFIG_FILE);
}
}

@Nested
@DisplayName("System Properties")
class SystemPropertiesSource {
@BeforeEach
void setUp() {
System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, "https://user-configuration-override:8443");
System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "namespace-overridden");
System.setProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY, "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(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY);
System.clearProperty(Config.KUBERNETES_OAUTH_TOKEN_SYSTEM_PROPERTY);
}
}

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 df857b2

Please sign in to comment.