Skip to content

Commit

Permalink
Trying harder to delete agent-work-dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 18, 2025
1 parent 8ecc1f3 commit 7fcc00b
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ public class CheckFilterTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public void setup() throws Exception {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public void setUp() {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/cloudbees/jenkins/support/SupportTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -48,6 +51,7 @@
* @since 2.26
*/
public class SupportTestUtils {
private static final Logger LOGGER = Logger.getLogger(SupportTestUtils.class.getName());

/**
* Invoke a component, and return the component contents as a String.
Expand Down Expand Up @@ -337,4 +341,37 @@ public static void testPermissionToDisplayAction(
public static boolean isJava8OrBelow() {
return System.getProperty("java.specification.version").startsWith("1.");
}

public static void stopAgents() throws Exception {
for (var agent : Jenkins.get().getNodes()) {
LOGGER.info("stopping " + agent);
agent.toComputer().disconnect(null).get();
}
var dir = Jenkins.get().getRootDir().toPath().resolve("agent-work-dirs");
for (int i = 0; i < 60; i++) {
try {
delete(dir);
LOGGER.info("successfully deleted " + dir);
return;
} catch (IOException x) {
LOGGER.log(Level.WARNING, "failed to delete " + dir + " on attempt #" + i, x);
Thread.sleep(1000);
}
}
LOGGER.warning("never managed to delete " + dir);
}

private static void delete(Path p) throws IOException {
LOGGER.info(() -> "deleting " + p);
if (Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS)) {
try (DirectoryStream<Path> children = Files.newDirectoryStream(p)) {
for (Path child : children) {
delete(child);
}
}
}
Files.deleteIfExists(p);
}

private SupportTestUtils() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public class SupportComputerActionTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ public class AgentsConfigFileTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.cloudbees.jenkins.support.SupportTestUtils;
import hudson.model.FreeStyleProject;
import hudson.model.ListView;
import hudson.model.User;
Expand All @@ -42,10 +43,7 @@ public class SensitiveContentFilterTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Issue("JENKINS-21670")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public class AboutJenkinsTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public class DumpExportTableTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public class FileDescriptorLimitTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public class NodeExecutorsTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public class NodeRemoteDirectoryComponentTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ public class SlaveCommandStatisticsTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public void twoAgents() throws Throwable {
allOf(
containsString("Z agent1] Remoting version: "),
containsString("Z agent2] Remoting version: ")));
for (var agent : r.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public class SlaveLaunchLogsTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.Assert.assertNotNull;

import com.cloudbees.jenkins.support.SupportPlugin;
import com.cloudbees.jenkins.support.SupportTestUtils;
import com.cloudbees.jenkins.support.api.Component;
import com.cloudbees.jenkins.support.filter.ContentFilters;
import hudson.ExtensionList;
Expand Down Expand Up @@ -31,10 +32,7 @@ public class SmartLogCleanerTest {

@After
public void stopAgents() throws Exception {
for (var agent : j.jenkins.getNodes()) {
System.err.println("Stopping " + agent);
agent.toComputer().disconnect(null).get();
}
SupportTestUtils.stopAgents();
}

@Test
Expand Down

0 comments on commit 7fcc00b

Please sign in to comment.