Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : Add unit test for old deprecated Config constructor #6157

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

rohanKanojia
Copy link
Member

@rohanKanojia rohanKanojia commented Jul 19, 2024

Description

Prerequisite to #6153

  • 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
    • Use AssertJ throughout the test for assertions
    • Group common tests sharing common initialization logic in @Nested classes
  • Add new test ConfigSourcePrecedenceTest for verifying Config source load precedence

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change
  • Chore (non-breaking change which doesn't affect codebase;
    test, version modification, documentation, etc.)

Checklist

  • Code contributed by me aligns with current project license: Apache 2.0
  • I Added CHANGELOG entry regarding this change
  • I have implemented unit tests to cover my changes
  • I have added/updated the javadocs and other documentation accordingly
  • No new bugs, code smells, etc. in SonarCloud report
  • I tested my code in Kubernetes
  • I tested my code in OpenShift

@rohanKanojia rohanKanojia force-pushed the tests/config-constructor branch 10 times, most recently from 35dbbce to d2b916a Compare July 23, 2024 13:05
@rohanKanojia rohanKanojia marked this pull request as ready for review July 23, 2024 14:06
@manusa manusa added this to the 7.0.0 milestone Jul 24, 2024 — with automated-tasks
class KubeConfigSource {
@BeforeEach
void setUp() {
System.setProperty("kubeconfig", Utils.filePath(ConfigSourcePrecedenceTest.class.getResource("/test-kubeconfig")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is reusing a .kube/config shared with other tests.

It's better if you create one specific for this test and store it as /config-source-precedence/kube-config or something similar.

Comment on lines 50 to 64
@Nested
@DisplayName("user configuration overrides KubeConfig attributes")
class UserConfigurationOverKubeConfig extends UserConfigurationViaConfigBuilder {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's going on here

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")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@Test
@DisplayName("Service Account files provided, then do NOT use it")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's it in this context?

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")));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 84 to 95
System.setProperty("KUBERNETES_SERVICE_HOST", "10.96.0.1");
System.setProperty("KUBERNETES_SERVICE_PORT", "443");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this look like environment variables?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Config class uses Utils.getSystemPropertyOrEnvVar to resolve kubernetes apiserver host and port

String masterHost = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_HOST_PROPERTY, (String) null);
String masterPort = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_PORT_PROPERTY, (String) null);

This method can handle converting property name kubernetes.service.host to environment variable KUBERNETES_SERVICE_HOST; but I think we're passing it incorrectly. At the moment, it only seems to work as both property and env names as KUBERNETES_SERVICE_HOST

public static final String KUBERNETES_SERVICE_HOST_PROPERTY = "KUBERNETES_SERVICE_HOST";
public static final String KUBERNETES_SERVICE_PORT_PROPERTY = "KUBERNETES_SERVICE_PORT";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe report this as a bug?

Also, given your context, not sure if this is the valid test name then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm, I'm not sure if it's a bug as KubernetesClient doesn't document anywhere usage of kubernetes.service.host and kubernetes.service.port properties in https://github.com/fabric8io/kubernetes-client#configuring-the-client

KUBERNETES_SERVICE_HOST/PORT environment variables are something that's exposed via Kubernetes. KubernetesClient is just trying to detect it in auto configuration phase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed adding KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT to System Properties as they're not System Properties.

Comment on lines 133 to 153
@Nested
@DisplayName("user configuration overrides ServiceAccount's Config attributes")
class UserConfigurationOverServiceAccount extends UserConfigurationViaConfigBuilder {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 149 to 151
.hasFieldOrPropertyWithValue("masterUrl", "https://user-configuration-override:8443/")
.hasFieldOrPropertyWithValue("namespace", "namespace-overridden")
.hasFieldOrPropertyWithValue("autoOAuthToken", "token-overridden");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add an assertion for one of the configurations that's read from the initial source? -> precedence values preserved <-

Comment on lines 188 to 191
@Nested
@DisplayName("User configuration overrides Config attributes configured via System Properties")
class UserConfigurationOverSystemProperties extends UserConfigurationViaConfigBuilder {
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 201 to 224
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");
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not worth reusing this test.
It obfuscates things and makes it hard to understand what's going on, especially in case of failure.
In addition, there's no check for precedence (as suggested in one of the other comments). You'd specifically want one assertion to ensure that 1. some values are overridden, and 2. some values are preserved from the precedent config

@rohanKanojia rohanKanojia force-pushed the tests/config-constructor branch 2 times, most recently from ddae5ab to 66c4e8d Compare July 24, 2024 10:19
- 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]>
@rohanKanojia rohanKanojia force-pushed the tests/config-constructor branch from 66c4e8d to 7a94b68 Compare July 24, 2024 10:59
Copy link

@manusa manusa self-requested a review July 24, 2024 11:58
Comment on lines +410 to +412
@Test
@DisplayName("override via system property should get more precedence over kubeconfig")
void testOverrideViaSystemProperties() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant, please remove any of these redundant tests in a follow-up PR

Copy link
Member

@manusa manusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@manusa manusa merged commit 00db1be into fabric8io:main Jul 24, 2024
20 of 21 checks passed
@rohanKanojia rohanKanojia deleted the tests/config-constructor branch July 24, 2024 13:12
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this pull request Jul 24, 2024
…asses in ConfigTest

- Remove `@Nested` class BuildConfigured as it doesn’t have any common setup, teardown blocks
- Remove these tests as they were already covered by ConfigSourcePrecedenceTest:
  - `testOverrideViaSystemProperties`
  - `testSystemPropertiesAndBuilderGetMorePrecedenceOverKubeconfig`
- Rename tests with legacy names starting with `testXXXX`
- Add `@DisplayName` annotation for tests wherever applicable
- Remove `assertConfig` method and inline assertions in tests wherever it’s getting used
- Remove abstract test class introduced in fabric8io#6157 `AutoConfiguredDisabledScenarios`, duplicate tests so that it’s more clear in case of failure

Signed-off-by: Rohan Kumar <[email protected]>
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this pull request Jul 25, 2024
…asses in ConfigTest

- Remove `@Nested` class BuildConfigured as it doesn’t have any common setup, teardown blocks
- Remove these tests as they were already covered by ConfigSourcePrecedenceTest:
  - `testOverrideViaSystemProperties`
  - `testSystemPropertiesAndBuilderGetMorePrecedenceOverKubeconfig`
- Rename tests with legacy names starting with `testXXXX`
- Add `@DisplayName` annotation for tests wherever applicable
- Remove `assertConfig` method and inline assertions in tests wherever it’s getting used
- Remove abstract test class introduced in fabric8io#6157 `AutoConfiguredDisabledScenarios`, duplicate tests so that it’s more clear in case of failure
  - Move this test outside of ConfigTest as a separate test class
    `ConfigDisableAutoConfigurationTest`

Signed-off-by: Rohan Kumar <[email protected]>
rohanKanojia added a commit to rohanKanojia/kubernetes-client that referenced this pull request Jul 25, 2024
…asses in ConfigTest

- Remove `@Nested` class BuildConfigured as it doesn’t have any common setup, teardown blocks
- Remove these tests as they were already covered by ConfigSourcePrecedenceTest:
  - `testOverrideViaSystemProperties`
  - `testSystemPropertiesAndBuilderGetMorePrecedenceOverKubeconfig`
- Rename tests with legacy names starting with `testXXXX`
- Add `@DisplayName` annotation for tests wherever applicable
- Remove `assertConfig` method and inline assertions in tests wherever it’s getting used
- Remove abstract test class introduced in fabric8io#6157 `AutoConfiguredDisabledScenarios`, duplicate tests so that it’s more clear in case of failure
  - Move this test outside of ConfigTest as a separate test class
    `ConfigDisableAutoConfigurationTest`

Signed-off-by: Rohan Kumar <[email protected]>
manusa pushed a commit that referenced this pull request Jul 26, 2024
…sses in ConfigTest

- Remove `@Nested` class BuildConfigured as it doesn’t have any common setup, teardown blocks
- Remove these tests as they were already covered by ConfigSourcePrecedenceTest:
  - `testOverrideViaSystemProperties`
  - `testSystemPropertiesAndBuilderGetMorePrecedenceOverKubeconfig`
- Rename tests with legacy names starting with `testXXXX`
- Add `@DisplayName` annotation for tests wherever applicable
- Remove `assertConfig` method and inline assertions in tests wherever it’s getting used
- Remove abstract test class introduced in #6157 `AutoConfiguredDisabledScenarios`, duplicate tests so that it’s more clear in case of failure
  - Move this test outside of ConfigTest as a separate test class
    `ConfigDisableAutoConfigurationTest`

Signed-off-by: Rohan Kumar <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants