Skip to content

Commit

Permalink
bump mockito to support jdk21, IC-2024.2 runs tests with it regardless
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jan 7, 2025
1 parent ce7e878 commit 4b9fe5c
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 28 deletions.
7 changes: 5 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
import org.jetbrains.intellij.platform.gradle.models.ProductRelease
Expand Down Expand Up @@ -59,9 +60,8 @@ dependencies {
// for unit tests
testImplementation(libs.junit)
testImplementation(libs.assertj.core)
testImplementation(libs.mockito.inline)
testImplementation(libs.mockito.core)
testImplementation(libs.opentest4j) // known issue: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-faq.html#missing-opentest4j-dependency-in-test-framework

}

tasks {
Expand All @@ -73,6 +73,9 @@ tasks {
useJUnit()
systemProperty("tools.dl.path", temporaryDir)
jvmArgs("-Djava.awt.headless=true")
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
}

withType<Test> {
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ commons-lang3 = "3.12.0"
commons-exec = "1.3"
common-lang = "3.9.4"
assertj-core = "3.22.0"
mockito-inline = "4.5.1"
mockito-core = "5.14.2"
opentest4j = "1.3.0"

# plugins
Expand All @@ -22,7 +22,7 @@ kubernetes-httpclient-okhttp = { group = "io.fabric8", name = "kubernetes-httpcl
jackson-core = { group = "com.fasterxml.jackson.core", name = "jackson-core", version.ref = "jackson-core" }
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version.ref = "commons-lang3" }
assertj-core = { group = "org.assertj", name = "assertj-core", version.ref = "assertj-core" }
mockito-inline = { group = "org.mockito", name = "mockito-inline", version.ref = "mockito-inline" }
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockito-core" }
commons-exec = { group = "org.apache.commons", name = "commons-exec", version.ref = "commons-exec" }
common-lang = { group = "com.twelvemonkeys.common", name = "common-lang", version.ref = "common-lang" }
opentest4j = { group = "org.opentest4j", name = "opentest4j", version.ref = "opentest4j" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,40 @@ public class ConfigHelper {
* Returns {@code true} if the given {@link io.fabric8.kubernetes.client.Config}s are equal.
* They are considered equal if they're equal in
* <ul>
* <li>current context (cluster, user, current namespace, extensions)</li>
* <li>(authentication) token</li>
* <li>current context (cluster, user, current namespace)</li>
* <li>auth info</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in context, contexts and token
*
* @see #areEqualContext(NamedContext, NamedContext)
* @see #areEqualCurrentContext(Config, Config)
* @see #areEqualCluster(Config, Config)
* @see #areEqualAuthInfo(Config, Config)
*/
public static boolean areEqual(Config thisConfig, Config thatConfig) {
return areEqualCurrentContext(thisConfig, thatConfig)
&& areEqualToken(thisConfig, thatConfig);
&& areEqualCluster(thisConfig, thatConfig)
&& areEqualAuthInfo(thisConfig, thatConfig);
}

/**
* Returns {@code true} if the given {@link io.fabric8.kubernetes.client.Config}s are equal in current context.
* They are considered equal if they're equal in
* <ul>
* <li>current context (cluster, user, current namespace, extensions)</li>
* <li>(existing) contexts</li>
* <li>(authentication) token</li>
* <li>name</li>
* <li>cluster</li>
* <li>user</li>
* <li>current namespace</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in context, contexts and token
* @return true if both configs are equal in context, existing contexts and token
*
* @see Config#getCurrentContext()
* @see #areEqualContext(NamedContext, NamedContext)
*/
public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
Expand All @@ -64,9 +69,9 @@ public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfi
}

/**
* Returns {@code true} if both given {@link NamedContext} are equal.
* They are considered equal if they're equal in
* Returns {@code true} if both given {@link NamedContext} are equal in
* <ul>
* <li>name</li>
* <li>cluster</li>
* <li>user</li>
* <li>current namespace</li>
Expand All @@ -76,20 +81,19 @@ public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfi
* @param thatContext the second context to compare
* @return true if both contexts are equal
*
* @see #areEqualContext(Context, Context)
* @see NamedContext
* @see Context
*/
private static boolean areEqualContext(NamedContext thisContext, NamedContext thatContext) {
public static boolean areEqualContext(NamedContext thisContext, NamedContext thatContext) {
if (thisContext == null) {
return thatContext == null;
} else if (thatContext == null) {
return false;
}
if (!Objects.equals(thisContext.getName(), thatContext.getName())) {
return false;
}

return areEqualContext(thisContext.getContext(), thatContext.getContext());
return Objects.equals(thisContext.getName(), thatContext.getName())
&& areEqualContext(thisContext.getContext(), thatContext.getContext());
}

/**
Expand All @@ -115,13 +119,132 @@ private static boolean areEqualContext(Context thisContext, Context thatContext)
return false;
}

if (!Objects.equals(thisContext.getCluster(), thatContext.getCluster())){
return Objects.equals(thisContext.getCluster(), thatContext.getCluster())
&& Objects.equals(thisContext.getUser(), thatContext.getUser())
&& Objects.equals(thisContext.getNamespace(), thatContext.getNamespace());
}

/**
* Returns {@code true} if both given {@link Config} are equal in
* <ul>
* <li>master url</li>
* <li>(blindly) trust certificates</li>
* <li>proxies</li>
* <li>auth info</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in master url, trust certs, proxies and auth info
*
* @see Config
*/
public static boolean areEqualCluster(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
return thatConfig == null;
} else if (thatConfig == null) {
return false;
}

return Objects.equals(thisConfig.getMasterUrl(), thatConfig.getMasterUrl())
&& areEqualTrustCerts(thisConfig, thatConfig)
&& areEqualProxy(thisConfig, thatConfig)
&& areEqualAuthInfo(thisConfig, thatConfig);
}

/**
* Returns {@code true} if both given {@link Config} are equal in
* <ul>
* <li>http proxy</li>
* <li>https proxy</li>
* <li>proxy username</li>
* <li>proxy password</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in http- & https-proxy, proxy username & password
*
* @see Config
*/
private static boolean areEqualProxy(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
return thatConfig == null;
} else if (thatConfig == null) {
return false;
}

return Objects.equals(thisConfig.getHttpProxy(), thatConfig.getHttpProxy())
&& Objects.equals(thisConfig.getHttpsProxy(), thatConfig.getHttpsProxy())
&& Objects.equals(thisConfig.getProxyUsername(), thatConfig.getProxyUsername())
&& Objects.equals(thisConfig.getProxyPassword(), thatConfig.getProxyPassword());
}

/**
* Returns {@code true} if both given {@link Config} are equal in
* <ul>
* <li>(blindly) trusting certificates</li>
* <li>disable hostname verification</li>
* <li>ca cert data</li>
* <li>ca cert file</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in trusting certs, disabling hostname verification, ca cert data & file
*
* @see Config
*/
private static boolean areEqualTrustCerts(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
return thatConfig == null;
} else if (thatConfig == null) {
return false;
} else if (!Objects.equals(thisContext.getNamespace(), thatContext.getNamespace())){
}

return thisConfig.isTrustCerts() == thatConfig.isTrustCerts()
&& thisConfig.isDisableHostnameVerification() == thatConfig.isDisableHostnameVerification()
&& Objects.equals(thisConfig.getCaCertData(), thatConfig.getCaCertData())
&& Objects.equals(thisConfig.getCaCertFile(), thatConfig.getCaCertFile());
}

/**
* Returns {@code true} if both given {@link Config} are equal in auth info
* <ul>
* <li>client cert file</li>
* <li>client cert data</li>
* <li>client key file</li>
* <li>client key data</li>
* <li>client key algo</li>
* <li>username</li>
* <li>password</li>
* <li>proxies</li>
* <li>token</li>
* </ul>
*
* @param thisConfig the first config to compare
* @param thatConfig the second config to compare
* @return true if both configs are equal in client cert file/data, key file/data/algo, username, password
* proxies and token
*
* @see Config
*/
public static boolean areEqualAuthInfo(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
return thatConfig == null;
} else if (thatConfig == null) {
return false;
} else {
return Objects.equals(thisContext.getUser(), thatContext.getUser());
}

return Objects.equals(thisConfig.getClientCertFile(), thatConfig.getClientCertFile())
&& Objects.equals(thisConfig.getClientCertData(), thatConfig.getClientCertData())
&& Objects.equals(thisConfig.getClientKeyFile(), thatConfig.getClientKeyFile())
&& Objects.equals(thisConfig.getClientKeyData(), thatConfig.getClientKeyData())
&& Objects.equals(thisConfig.getClientKeyAlgo(), thatConfig.getClientKeyAlgo())
&& Objects.equals(thisConfig.getUsername(), thatConfig.getUsername())
&& Objects.equals(thisConfig.getPassword(), thatConfig.getPassword())
&& areEqualProxy(thisConfig, thatConfig)
&& areEqualToken(thisConfig, thatConfig);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import com.intellij.openapi.diagnostic.Logger;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;

import java.io.IOException;
import java.nio.file.FileSystems;
Expand Down Expand Up @@ -132,15 +131,23 @@ private void watch(Consumer<io.fabric8.kubernetes.client.Config> listener, Watch
for (WatchKey key = service.take(); key != null; key = service.take()) {
key.pollEvents().forEach((event) -> {
Path changed = getAbsolutePath(directory, (Path) event.context());
if (isConfigPath(changed)) {
Config config = new ConfigBuilder().build();
listener.accept(config);
}
notifyListener(listener, changed);
});
key.reset();
}
}

private void notifyListener(Consumer<Config> listener, Path changed) {
if (isConfigPath(changed)) {
try {
var config = Config.autoConfigure(null);
listener.accept(config);
} catch (Exception e) {
LOG.warn("Loading config at '" + changed + "' failed.", e);
}
}
}

protected boolean isConfigPath(Path path) {
return configs != null
&& configs.contains(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@ public void areEqualContexts_returns_false_given_config_has_additional_context()
assertThat(equal).isFalse();
}

@Test
public void areEqualAuthInfo_returns_false_given_contexts_differ_in_username() {
// given
Config config1 = clientConfig("yoda", null);
Config config2 = clientConfig("obiwan", null);
// when
boolean equal = ConfigHelper.areEqualAuthInfo(config1, config2);
// then
assertThat(equal).isFalse();
}

@Test
public void areEqualAuthInfo_returns_false_given_contexts_differ_in_password() {
// given
Config config1 = clientConfig("yoda", "the force");
Config config2 = clientConfig("yoda", "the light saber");
// when
boolean equal = ConfigHelper.areEqualAuthInfo(config1, config2);
// then
assertThat(equal).isFalse();
}

@Test
public void areEqualToken_returns_true_given_contexts_have_same_token() {
// given
Expand Down Expand Up @@ -242,6 +264,15 @@ private static Config clientConfig(String token) {
return clientConfig(token, null, null);
}

private static Config clientConfig(String username, String password) {
Config config = clientConfig(null, null, null);
doReturn(username)
.when(config).getUsername();
doReturn(password)
.when(config).getPassword();
return config;
}

private static Config clientConfig(NamedContext currentContext) {
return clientConfig(null, currentContext, null);
}
Expand Down

0 comments on commit 4b9fe5c

Please sign in to comment.