From 64c1eccc1addb6b153ecc5f5dbd1f9ea0298a288 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 20 Sep 2024 14:15:43 -0400 Subject: [PATCH] `InboundAgentRule` should use a temp file for `agent.jar` --- .../jvnet/hudson/test/InboundAgentRule.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java b/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java index 952dbdd9d..2fc7c1bd2 100644 --- a/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java +++ b/src/main/java/org/jvnet/hudson/test/InboundAgentRule.java @@ -88,6 +88,7 @@ public final class InboundAgentRule extends ExternalResource { private final String id = UUID.randomUUID().toString(); private final Map procs = Collections.synchronizedMap(new HashMap<>()); private final Set workDirs = Collections.synchronizedSet(new HashSet<>()); + private final Set jars = Collections.synchronizedSet(new HashSet<>()); /** * The options used to (re)start an inbound agent. @@ -317,7 +318,9 @@ public void start(@NonNull JenkinsRule r, Options options) throws Exception { String name = options.getName(); Objects.requireNonNull(name); stop(r, name); - start(getAgentArguments(r, name), options); + var args = getAgentArguments(r, name); + jars.add(args.agentJar); + start(args, options); waitForAgentOnline(r, name, options.loggers); } @@ -328,7 +331,9 @@ public void start(@NonNull RealJenkinsRule r, Options options) throws Throwable String name = options.getName(); Objects.requireNonNull(name); stop(r, name); - start(r.runRemotely(InboundAgentRule::getAgentArguments, name), options); + var args = r.runRemotely(InboundAgentRule::getAgentArguments, name); + jars.add(args.agentJar); + start(args, options); r.runRemotely(InboundAgentRule::waitForAgentOnline, name, options.loggers); } @@ -456,6 +461,14 @@ public boolean isAlive(String name) { LOGGER.log(Level.WARNING, null, x); } } + for (var jar : jars) { + LOGGER.info(() -> "Deleting " + jar); + try { + Files.deleteIfExists(jar.toPath()); + } catch (IOException x) { + LOGGER.log(Level.WARNING, null, x); + } + } } /** @@ -481,10 +494,8 @@ private static AgentArguments getAgentArguments(JenkinsRule r, String name) thro if (!launcher.getWorkDirSettings().isDisabled()) { commandLineArgs = launcher.getWorkDirSettings().toCommandLineArgs(c); } - File agentJar = new File(System.getProperty("java.io.tmpdir"), "agent.jar"); - if (!agentJar.isFile()) { - FileUtils.copyURLToFile(new Slave.JnlpJar("agent.jar").getURL(), agentJar); - } + File agentJar = Files.createTempFile(Path.of(System.getProperty("java.io.tmpdir")), "agent", ".jar").toFile(); + FileUtils.copyURLToFile(new Slave.JnlpJar("agent.jar").getURL(), agentJar); return new AgentArguments(r.jenkins.getRootUrl() + "computer/" + name + "/slave-agent.jnlp", agentJar, c.getJnlpMac(), r.jenkins.getNodes().size(), commandLineArgs); }