Skip to content

Commit

Permalink
fix(#1290): Fix default Kubernetes actor
Browse files Browse the repository at this point in the history
- Use default Kubernetes test actor only when feature is enabled
  • Loading branch information
christophd committed Jan 8, 2025
1 parent c2aa4ed commit a988c5e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class KubernetesActor extends TestActor {
/** Kubernetes' connection state, checks connectivity to Kubernetes cluster */
private static AtomicBoolean connected;

public KubernetesActor() {
this(null);
}

public KubernetesActor(KubernetesClient kubernetesClient) {
super("k8s");

Expand Down Expand Up @@ -66,7 +70,12 @@ public boolean isDisabled() {
public static boolean verifyConnected(KubernetesClient kubernetesClient) {
try {
Future<Boolean> future = Executors.newSingleThreadExecutor().submit(() -> {
kubernetesClient.pods().list();
var pods = kubernetesClient.pods();
if (pods == null) {
logger.warn("Skipping Kubernetes action as no proper Kubernetes environment is available on host system!");
return false;
}
pods.list();
return true;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class KubernetesSettings {
private static final String ENABLED_ENV = KUBERNETES_ENV_PREFIX + "ENABLED";
private static final String ENABLED_DEFAULT = "true";

private static final String USE_DEFAULT_ACTOR_PROPERTY = KUBERNETES_PROPERTY_PREFIX + "use.default.actor";
private static final String USE_DEFAULT_ACTOR_ENV = KUBERNETES_ENV_PREFIX + "USE_DEFAULT_ACTOR";
private static final String USE_DEFAULT_ACTOR_DEFAULT = "false";

private static final String AUTO_CREATE_SERVER_BINDING_PROPERTY = KUBERNETES_PROPERTY_PREFIX + "auto.create.server.binding";
private static final String AUTO_CREATE_SERVER_BINDING_ENV = KUBERNETES_ENV_PREFIX + "AUTO_CREATE_SERVER_BINDING";
private static final String AUTO_CREATE_SERVER_BINDING_DEFAULT = "true";
Expand Down Expand Up @@ -309,4 +313,13 @@ public static String getTestIdLabel() {
return System.getProperty(TEST_ID_LABEL_PROPERTY,
System.getenv(TEST_ID_LABEL_ENV) != null ? System.getenv(TEST_ID_LABEL_ENV) : TEST_ID_LABEL_DEFAULT);
}

/**
* True when all Kubernetes test actions should use a default test actor.
* @return
*/
public static boolean isUseDefaultKubernetesActor() {
return Boolean.parseBoolean(System.getProperty(USE_DEFAULT_ACTOR_PROPERTY,
System.getenv(USE_DEFAULT_ACTOR_ENV) != null ? System.getenv(USE_DEFAULT_ACTOR_ENV) : USE_DEFAULT_ACTOR_DEFAULT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public final T build() {
}
}

if (getActor() == null) {
if (getActor() == null && KubernetesSettings.isUseDefaultKubernetesActor()) {
actor(new KubernetesActor(kubernetesClient));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void setupMocks() {
public void shouldVerifyConnectedState() {
try {
when(dockerClient.pingCmd()).thenReturn(connected);
TestcontainersActor.resetConnectionState();
Assert.assertFalse(new TestcontainersActor(dockerClient).isDisabled());
} finally {
TestcontainersActor.resetConnectionState();
Expand All @@ -74,9 +75,11 @@ public void shouldOverruleConnectedState() {
try {
System.setProperty("citrus.testcontainers.enabled", "false");
when(dockerClient.pingCmd()).thenReturn(connected);
TestcontainersActor.resetConnectionState();
Assert.assertTrue(new TestcontainersActor(dockerClient).isDisabled());
} finally {
System.setProperty("citrus.testcontainers.enabled", Boolean.toString(initial));
TestcontainersActor.resetConnectionState();
reset(dockerClient);
}

Expand All @@ -87,6 +90,7 @@ public void shouldOverruleConnectedState() {
Assert.assertTrue(new TestcontainersActor(dockerClient).isDisabled());
} finally {
System.setProperty("citrus.test.actor.testcontainers.enabled", Boolean.toString(initial));
TestcontainersActor.resetConnectionState();
}
}
}

0 comments on commit a988c5e

Please sign in to comment.