Skip to content

Commit

Permalink
[JENKINS-35246] Test that all jenkins nodes are deleted after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossg committed Aug 20, 2017
1 parent d2b61a3 commit ba0e6ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import hudson.model.Node;
import io.fabric8.kubernetes.api.model.Cluster;
import io.fabric8.kubernetes.api.model.Config;
import io.fabric8.kubernetes.api.model.NamedCluster;
Expand All @@ -58,6 +59,7 @@
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
import io.fabric8.kubernetes.client.internal.KubeConfigUtils;
import io.fabric8.kubernetes.client.utils.Utils;
import jenkins.model.Jenkins;

public class KubernetesTestUtil {

Expand Down Expand Up @@ -101,6 +103,32 @@ public static KubernetesCloud setupCloud() throws UnrecoverableKeyException, Cer
return cloud;
}

/**
* Wait for Jenkins nodes (agents) to be deleted
*/
public static void waitForNodeDeletion(Jenkins jenkins) throws Exception {
try {
new ForkJoinPool(1).submit(() -> IntStream.range(1, 1_000_000).anyMatch(i -> {
try {
List<Node> nodes = jenkins.getNodes();
LOGGER.log(INFO, "Still waiting for nodes to be deleted: {0}", nodes);
if (nodes.isEmpty()) {
LOGGER.log(INFO, "All nodes are deleted");
} else {
LOGGER.log(INFO, "Still waiting for nodes to be deleted: {0}", nodes);
Thread.sleep(5000);
}
return nodes.isEmpty();
} catch (InterruptedException e) {
LOGGER.log(INFO, "Waiting for nodes to be deleted - interrupted");
return true;
}
})).get(30, TimeUnit.SECONDS);
} catch (TimeoutException e) {
LOGGER.log(INFO, "Waiting for nodes to be deleted - timed out");
}
}

/**
* Delete pods with matching labels
*
Expand All @@ -119,9 +147,8 @@ public static boolean deletePods(KubernetesClient client, Map<String, String> la
// wait for 30 seconds for all pods to be terminated
if (wait) {
LOGGER.log(INFO, "Waiting for pods to terminate");
ForkJoinPool forkJoinPool = new ForkJoinPool(1);
try {
forkJoinPool.submit(() -> IntStream.range(1, 1_000_000).anyMatch(i -> {
new ForkJoinPool(1).submit(() -> IntStream.range(1, 1_000_000).anyMatch(i -> {
try {
FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods = client.pods()
.withLabels(labels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public void configureTemplates() throws Exception {

@After
public void cleanup() throws Exception {
waitForNodeDeletion(r.getInstance());
assertEquals("There are agents left in Jenkins after test execution", Collections.emptyList(),
r.getInstance().getNodes());
assertFalse("There are pods leftover after test execution, see previous logs",
deletePods(cloud.connect(), KubernetesCloud.DEFAULT_POD_LABELS, true));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRuleNonLocalhost;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import hudson.model.Node;
import hudson.slaves.DumbSlave;
import hudson.slaves.NodeProperty;
import hudson.slaves.RetentionStrategy;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.jvnet.hudson.test.RestartableJenkinsRule;

/**
* @author Carlos Sanchez
Expand Down

0 comments on commit ba0e6ab

Please sign in to comment.