Skip to content

Commit

Permalink
InboundAgentRule should use a temp file for agent.jar
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Sep 20, 2024
1 parent 4f0199c commit 64c1ecc
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/main/java/org/jvnet/hudson/test/InboundAgentRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public final class InboundAgentRule extends ExternalResource {
private final String id = UUID.randomUUID().toString();
private final Map<String, Process> procs = Collections.synchronizedMap(new HashMap<>());
private final Set<String> workDirs = Collections.synchronizedSet(new HashSet<>());
private final Set<File> jars = Collections.synchronizedSet(new HashSet<>());

/**
* The options used to (re)start an inbound agent.
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 64c1ecc

Please sign in to comment.