Skip to content

Commit

Permalink
test(config): ensure Config.refresh works with scattered kubeconfig f…
Browse files Browse the repository at this point in the history
…iles

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored Nov 8, 2024
1 parent 0a8d711 commit bb2834d
Showing 1 changed file with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static org.assertj.core.api.Assertions.assertThat;

class ConfigRefreshTest {
Expand Down Expand Up @@ -90,8 +92,8 @@ void manualConfig() {
}

@Test
@DisplayName("When autoconfigure enabled, then refresh (returns new instance)")
void autoConfiguredConfig() {
@DisplayName("When autoconfigure enabled (from properties), then refresh (returns new instance)")
void autoConfiguredConfigFromProperties() {
try {
// Given
System.setProperty("kubernetes.master", "https://test.example.com/");
Expand All @@ -102,12 +104,44 @@ void autoConfiguredConfig() {
// Then
assertThat(result)
.isNotSameAs(config)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.hasFieldOrPropertyWithValue("masterUrl", "https://test-updated.example.com/");
} finally {
System.clearProperty("kubernetes.master");
}
}

@Test
@DisplayName("When autoconfigure enabled (from files), then refresh (returns new instance)")
void autoConfiguredConfigFromFiles() throws IOException {
try {
// Given
final var kubeConfigUserFile = tempDir.resolve("kubeconfig-user");
final var kubeConfigUser = new io.fabric8.kubernetes.api.model.ConfigBuilder()
.addAllToUsers(kubeConfigOriginal.getUsers())
.build();
kubeConfigOriginal.getUsers().clear();
Files.writeString(kubeConfigFile, Serialization.asYaml(kubeConfigOriginal), TRUNCATE_EXISTING);
Files.writeString(kubeConfigUserFile, Serialization.asYaml(kubeConfigUser), CREATE);
System.setProperty("kubeconfig", kubeConfigFile.toFile().getAbsolutePath() + File.pathSeparator +
kubeConfigUserFile.toFile().getAbsolutePath());
final var config = new ConfigBuilder().build();
final var kubeConfigUserUpdated = new io.fabric8.kubernetes.api.model.ConfigBuilder(kubeConfigUser)
.editFirstUser().editUser().withToken("the-token-updated").endUser().endUser().build();
Files.writeString(kubeConfigUserFile, Serialization.asYaml(kubeConfigUserUpdated), CREATE);
// When
final var result = config.refresh();
// Then
assertThat(result)
.isNotSameAs(config)
.hasFieldOrPropertyWithValue("autoConfigure", true)
.hasFieldOrPropertyWithValue("autoOAuthToken", "the-token-updated");
;
} finally {
System.clearProperty("kubeconfig");
}
}

@Test
@DisplayName("When config from file, then refresh (returns new instance)")
void configFromFile() throws IOException {
Expand All @@ -121,6 +155,7 @@ void configFromFile() throws IOException {
// Then
assertThat(result)
.isNotSameAs(config)
.hasFieldOrPropertyWithValue("autoConfigure", false)
.hasFieldOrPropertyWithValue("masterUrl", "https://the-cluster:8443/")
.hasFieldOrPropertyWithValue("autoOAuthToken", "the-token-updated");
}
Expand All @@ -136,6 +171,7 @@ void configFromFileEmptied() throws IOException {
// Then
assertThat(result)
.isSameAs(config)
.hasFieldOrPropertyWithValue("autoConfigure", false)
.hasFieldOrPropertyWithValue("masterUrl", "https://the-cluster:8443/")
.hasFieldOrPropertyWithValue("autoOAuthToken", "the-token");
}
Expand Down

0 comments on commit bb2834d

Please sign in to comment.