Skip to content

Commit 696d93c

Browse files
committed
CredentialsInPipelineTest: modernize to JUnit 5
Signed-off-by: Jim Klimov <[email protected]>
1 parent 95cb844 commit 696d93c

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/test/java/com/cloudbees/plugins/credentials/CredentialsInPipelineTest.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,27 @@
4141
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
4242
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
4343

44-
import org.junit.Before;
45-
import org.junit.Ignore;
44+
import org.junit.jupiter.api.BeforeEach;
45+
import org.junit.jupiter.api.Disabled;
4646
import org.junit.Rule;
47-
import org.junit.Test;
48-
import org.junit.rules.TemporaryFolder;
47+
import org.junit.jupiter.api.Test;
48+
import org.junit.jupiter.api.io.TempDir;
4949
import org.jvnet.hudson.test.Issue;
5050
import org.jvnet.hudson.test.JenkinsRule;
51+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5152

5253
import java.io.ByteArrayOutputStream;
5354
import java.io.File;
55+
import java.nio.file.Path;
5456
import java.io.FileOutputStream;
5557
import java.io.IOException;
5658
import java.net.URL;
5759
import java.nio.file.Files;
5860

5961
import static org.hamcrest.CoreMatchers.*;
60-
import static org.junit.Assume.assumeThat;
62+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
6163

64+
@WithJenkins
6265
public class CredentialsInPipelineTest {
6366
/**
6467
* The CredentialsInPipelineTest suite prepares pipeline scripts to
@@ -74,14 +77,13 @@ public class CredentialsInPipelineTest {
7477
// mvn test -Dtest="CredentialsInPipelineTest"
7578
private boolean verbosePipelines = false;
7679

77-
@Rule
78-
public JenkinsRule r = new JenkinsRule();
80+
private JenkinsRule r;
7981

8082
// Data for build agent setup
81-
@Rule
82-
public TemporaryFolder tmpAgent = new TemporaryFolder();
83-
@Rule
84-
public TemporaryFolder tmpWorker = new TemporaryFolder();
83+
@TempDir
84+
private File tmpAgent;
85+
@TempDir
86+
private File tmpWorker;
8587
// Where did we save that file?..
8688
private File agentJar = null;
8789
// Can this be reused for many test cases?
@@ -90,15 +92,20 @@ public class CredentialsInPipelineTest {
9092
private Boolean agentUsable = null;
9193

9294
// From CertificateCredentialImplTest
93-
@Rule
94-
public TemporaryFolder tmp = new TemporaryFolder();
95+
@TempDir
96+
private File tmp;
9597
private File p12;
9698

97-
@Before
99+
@BeforeEach
98100
public void setup() {
99101
r.jenkins.setCrumbIssuer(null);
100102
}
101103

104+
@BeforeEach
105+
void setup(JenkinsRule r) throws IOException {
106+
this.r = r;
107+
}
108+
102109
Boolean isAvailableAgent() {
103110
// Can be used to skip optional tests if we know we could not set up an agent
104111
if (agentJar == null)
@@ -108,7 +115,7 @@ Boolean isAvailableAgent() {
108115
return agentUsable;
109116
}
110117

111-
Boolean setupAgent() throws IOException, InterruptedException, OutOfMemoryError {
118+
Boolean setupAgent() throws IOException, InterruptedException, OutOfMemoryError, FormException {
112119
// Note we anticipate this might fail; it should not block the whole test suite from running
113120
// Loosely inspired by
114121
// https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/create-agent-node-from-groovy
@@ -121,7 +128,7 @@ Boolean setupAgent() throws IOException, InterruptedException, OutOfMemoryError
121128
if (agentJar == null) {
122129
try {
123130
URL url = new URL(r.jenkins.getRootUrl() + "jnlpJars/agent.jar");
124-
agentJar = tmpAgent.newFile("agent.jar");
131+
agentJar = new File(tmpAgent, "agent.jar");
125132
FileOutputStream out = new FileOutputStream(agentJar);
126133
out.write(url.openStream().readAllBytes());
127134
out.close();
@@ -140,14 +147,14 @@ Boolean setupAgent() throws IOException, InterruptedException, OutOfMemoryError
140147
// (including spaces in directory names) and Unix/Linux
141148
ComputerLauncher launcher = new CommandLauncher(
142149
"\"" + System.getProperty("java.home") + File.separator + "bin" +
143-
File.separator + "java\" -jar \"" + agentJar.getAbsolutePath().toString() + "\""
150+
File.separator + "java\" -jar \"" + agentJar.getAbsolutePath() + "\""
144151
);
145152

146153
try {
147154
// Define a "Permanent Agent"
148155
agent = new DumbSlave(
149156
"worker",
150-
tmpWorker.getRoot().getAbsolutePath().toString(),
157+
tmpWorker.getAbsolutePath(),
151158
launcher);
152159
agent.setNodeDescription("Worker in another JVM, remoting used");
153160
agent.setNumExecutors(1);
@@ -209,7 +216,7 @@ private void prepareUploadedKeystore(String id, String password) throws IOExcept
209216
if (p12 == null) {
210217
// Contains a private key + openvpn certs,
211218
// as alias named "1" (according to keytool)
212-
p12 = tmp.newFile("test.p12");
219+
p12 = File.createTempFile("test.p12", null, tmp);
213220
FileUtils.copyURLToFile(CertificateCredentialsImplTest.class.getResource("test.p12"), p12);
214221
}
215222

@@ -333,7 +340,7 @@ public void testCertKeyStoreReadableOnNodeRemote() throws Exception {
333340
// Check that credentials are usable with pipeline script
334341
// running on a remote node{} with separate JVM (check
335342
// that remoting/snapshot work properly)
336-
assumeThat("This test needs a separate build agent", this.setupAgent(), is(true));
343+
assumeTrue(this.setupAgent() == true, "This test needs a separate build agent");
337344

338345
prepareUploadedKeystore();
339346

@@ -402,7 +409,7 @@ String cpsScriptCertCredentialTestWithCredentials(String id, String password, St
402409
}
403410

404411
@Test
405-
@Ignore("Work with keystore file requires a node")
412+
@Disabled("Work with keystore file requires a node")
406413
@Issue("JENKINS-70101")
407414
public void testCertWithCredentialsOnController() throws Exception {
408415
// Check that credentials are usable with pipeline script
@@ -458,7 +465,7 @@ public void testCertWithCredentialsOnNodeRemote() throws Exception {
458465
// Check that credentials are usable with pipeline script
459466
// running on a remote node{} with separate JVM (check
460467
// that remoting/snapshot work properly)
461-
assumeThat("This test needs a separate build agent", this.setupAgent(), is(true));
468+
assumeTrue(this.setupAgent() == true, "This test needs a separate build agent");
462469

463470
prepareUploadedKeystore();
464471

@@ -585,7 +592,7 @@ public void testCertHttpRequestOnNodeRemote() throws Exception {
585592
// Check that credentials are usable with pipeline script
586593
// running on a remote node{} with separate JVM (check
587594
// that remoting/snapshot work properly)
588-
assumeThat("This test needs a separate build agent", this.setupAgent(), is(true));
595+
assumeTrue(this.setupAgent() == true, "This test needs a separate build agent");
589596

590597
prepareUploadedKeystore();
591598

@@ -678,7 +685,7 @@ public void testUsernamePasswordHttpRequestOnNodeRemote() throws Exception {
678685
// Check that credentials are usable with pipeline script
679686
// running on a remote node{} with separate JVM (check
680687
// that remoting/snapshot work properly)
681-
assumeThat("This test needs a separate build agent", this.setupAgent(), is(true));
688+
assumeTrue(this.setupAgent() == true, "This test needs a separate build agent");
682689

683690
prepareUsernamePassword();
684691

0 commit comments

Comments
 (0)