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

Stop agents before exiting test cases, and reliably close all_*.log when agents exit #624

Merged
merged 17 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import jenkins.model.Jenkins;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -75,6 +76,11 @@ public class CheckFilterTest {
@Rule
public LoggerRule logging = new LoggerRule().record(AsyncResultCache.class, Level.FINER);

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void checkFilterTest() throws Exception {
// Create the files to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -51,6 +52,11 @@ public void setup() throws Exception {
setupAgent();
}

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
@Issue("SECURITY-2186")
public void secretsFilterWhenSystemPropertyContainsPasswordThenValueRedacted() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.htmlunit.html.HtmlButton;
import org.htmlunit.html.HtmlForm;
import org.htmlunit.html.HtmlPage;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -84,6 +85,11 @@ public void setUp() {
root = ExtensionList.lookupSingleton(SupportAction.class);
}

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void download() throws IOException, SAXException {
downloadBundle("/download?json={\"components\":1}");
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 @@ -14,6 +14,7 @@
import java.util.stream.Stream;
import java.util.zip.ZipFile;
import jenkins.model.Jenkins;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -26,6 +27,11 @@ public class SupportComputerActionTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void onlyAdminCanSeeAction() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import hudson.EnvVars;
import java.util.Map;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -45,6 +46,11 @@ public class AgentsConfigFileTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void agentsConfigFile() throws Exception {
j.createSlave("node1", "node1", new EnvVars());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@

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;
import java.io.IOException;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -39,6 +41,11 @@ public class SensitiveContentFilterTest {
@ClassRule
public static JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Issue("JENKINS-21670")
@Test
public void anonymizeAgentsAndLabels() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jenkins.model.Jenkins;
import jenkins.model.identity.IdentityRootAction;
import jenkins.slaves.RemotingVersionInfo;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -31,6 +32,11 @@ public class AboutJenkinsTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
@Issue("JENKINS-56245")
public void testAboutJenkinsContent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -23,6 +24,11 @@ public class DumpExportTableTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void testAddContents() throws Exception {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
import org.hamcrest.MatcherAssert;
import org.junit.After;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -44,6 +45,11 @@ public class FileDescriptorLimitTest {
@Rule
public LoggerRule logging = new LoggerRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void addContents() throws Exception {
Assume.assumeTrue(!Functions.isWindows());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.After;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -38,6 +39,11 @@ public class NodeExecutorsTest {
@ClassRule
public static BuildWatcher bw = new BuildWatcher();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void addContents() throws Exception {
DumbSlave agent = j.createOnlineSlave(Label.parseExpression("test"), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.model.Label;
import hudson.slaves.DumbSlave;
import java.util.Map;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -19,6 +20,11 @@ public class NodeRemoteDirectoryComponentTest {
@Rule
public JenkinsRule j = new JenkinsRule();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

/*
* Test adding agent remote directory content with the defaults.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.IOException;
import java.util.logging.Level;
import jenkins.MasterToSlaveFileCallable;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
Expand All @@ -57,6 +58,11 @@ public class SlaveCommandStatisticsTest {
@Rule
public LoggerRule logging = new LoggerRule().record(SlaveComputer.class, Level.FINEST);

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void smokes() throws Exception {
DumbSlave s = j.createSlave();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void twoAgents() throws Throwable {
allOf(
containsString("Z agent1] Remoting version: "),
containsString("Z agent2] Remoting version: ")));
SupportTestUtils.stopAgents();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.TreeMap;
import java.util.logging.Level;
import jenkins.slaves.StandardOutputSwapper;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.InboundAgentRule;
Expand All @@ -43,6 +44,11 @@ public class SlaveLaunchLogsTest {
@Rule
public LoggerRule logging = new LoggerRule().record(SlaveLaunchLogs.class, Level.FINE);

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void onlineOutboundAgent() throws Exception {
var s = j.createOnlineSlave();
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 All @@ -15,6 +16,7 @@
import java.util.Collections;
import java.util.List;
import java.util.zip.ZipFile;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -28,6 +30,11 @@ public class SmartLogCleanerTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();

@After
public void stopAgents() throws Exception {
SupportTestUtils.stopAgents();
}

@Test
public void cleanUp() throws Exception {
File cacheDir = new File(SupportPlugin.getLogsDirectory(), "winsw");
Expand Down
Loading