Skip to content

Commit 3158ad5

Browse files
Migrate SSHLauncherTest to JUnit5 (#605)
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Remove docker-fixtures * Minor code cleanup
1 parent 011c4e0 commit 3158ad5

File tree

2 files changed

+63
-85
lines changed

2 files changed

+63
-85
lines changed

pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,18 +125,6 @@
125125
<artifactId>sshd</artifactId>
126126
<scope>test</scope>
127127
</dependency>
128-
<dependency>
129-
<groupId>org.jenkins-ci.test</groupId>
130-
<artifactId>docker-fixtures</artifactId>
131-
<version>200.v22a_e8766731c</version>
132-
<scope>test</scope>
133-
<exclusions>
134-
<exclusion>
135-
<groupId>com.fasterxml.jackson.core</groupId>
136-
<artifactId>jackson-databind</artifactId>
137-
</exclusion>
138-
</exclusions>
139-
</dependency>
140128
<!-- Jupiter/JUnit 5 testcontainers https://java.testcontainers.org/test_framework_integration/junit_5/ -->
141129
<dependency>
142130
<groupId>org.testcontainers</groupId>

src/test/java/hudson/plugins/sshslaves/SSHLauncherTest.java

Lines changed: 63 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import static org.hamcrest.MatcherAssert.assertThat;
3030
import static org.hamcrest.core.IsNull.notNullValue;
3131
import static org.hamcrest.core.IsNull.nullValue;
32-
import static org.junit.Assert.assertEquals;
33-
import static org.junit.Assert.assertFalse;
34-
import static org.junit.Assert.assertNotNull;
35-
import static org.junit.Assert.assertNotSame;
36-
import static org.junit.Assert.assertTrue;
32+
import static org.junit.jupiter.api.Assertions.assertEquals;
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
34+
import static org.junit.jupiter.api.Assertions.assertNotNull;
35+
import static org.junit.jupiter.api.Assertions.assertNotSame;
36+
import static org.junit.jupiter.api.Assertions.assertTrue;
3737

3838
import com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey;
3939
import com.cloudbees.plugins.credentials.CredentialsProvider;
@@ -55,32 +55,26 @@
5555
import java.io.InputStream;
5656
import java.nio.file.Paths;
5757
import java.util.Collections;
58-
import org.apache.commons.io.IOUtils;
5958
import org.htmlunit.html.HtmlPage;
60-
import org.jenkinsci.test.acceptance.docker.DockerRule;
61-
import org.jenkinsci.test.acceptance.docker.fixtures.JavaContainer;
62-
import org.junit.Assert;
63-
import org.junit.ClassRule;
64-
import org.junit.Rule;
65-
import org.junit.Test;
66-
import org.junit.rules.TemporaryFolder;
67-
import org.jvnet.hudson.test.BuildWatcher;
59+
import org.junit.jupiter.api.BeforeEach;
60+
import org.junit.jupiter.api.Test;
61+
import org.junit.jupiter.api.io.TempDir;
6862
import org.jvnet.hudson.test.Issue;
6963
import org.jvnet.hudson.test.JenkinsRule;
64+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
7065

71-
public class SSHLauncherTest {
66+
@WithJenkins
67+
class SSHLauncherTest {
7268

73-
@ClassRule
74-
public static BuildWatcher buildWatcher = new BuildWatcher();
69+
@TempDir
70+
private File temporaryFolder;
7571

76-
@Rule
77-
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
72+
private JenkinsRule j;
7873

79-
@Rule
80-
public JenkinsRule j = new JenkinsRule();
81-
82-
@Rule
83-
public DockerRule<JavaContainer> javaContainer = new DockerRule<>(JavaContainer.class);
74+
@BeforeEach
75+
void setUp(JenkinsRule rule) {
76+
j = rule;
77+
}
8478

8579
private void checkRoundTrip(String host) throws Exception {
8680
SystemCredentialsProvider.getInstance()
@@ -92,7 +86,8 @@ private void checkRoundTrip(String host) throws Exception {
9286
SSHLauncher launcher = new SSHLauncher(host, 123, "dummyCredentialId");
9387
launcher.setSshHostKeyVerificationStrategy(new KnownHostsFileKeyVerificationStrategy());
9488
assertEquals(host.trim(), launcher.getHost());
95-
DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher);
89+
DumbSlave agent =
90+
new DumbSlave("agent", newFolder(temporaryFolder, "junit").getAbsolutePath(), launcher);
9691
j.jenkins.addNode(agent);
9792

9893
HtmlPage p = j.createWebClient().getPage(agent, "configure");
@@ -105,12 +100,12 @@ private void checkRoundTrip(String host) throws Exception {
105100
}
106101

107102
@Test
108-
public void configurationRoundTrip() throws Exception {
103+
void configurationRoundTrip() throws Exception {
109104
checkRoundTrip("localhost");
110105
}
111106

112107
@Test
113-
public void fillCredentials() {
108+
void fillCredentials() {
114109
SystemCredentialsProvider.getInstance()
115110
.getDomainCredentialsMap()
116111
.put(
@@ -138,7 +133,7 @@ public void fillCredentials() {
138133
}
139134

140135
@Test
141-
public void checkJavaPathWhiteSpaces() {
136+
void checkJavaPathWhiteSpaces() {
142137
SSHLauncher.DescriptorImpl desc = (SSHLauncher.DescriptorImpl) j.jenkins.getDescriptorOrDie(SSHLauncher.class);
143138
assertEquals(FormValidation.ok(), desc.doCheckJavaPath("/usr/lib/jdk/bin/java"));
144139
assertEquals(FormValidation.ok(), desc.doCheckJavaPath("\"/usr/lib/jdk/bin/java\""));
@@ -149,15 +144,15 @@ public void checkJavaPathWhiteSpaces() {
149144
}
150145

151146
@Test
152-
public void checkHost() {
147+
void checkHost() {
153148
SSHLauncher.DescriptorImpl desc = (SSHLauncher.DescriptorImpl) j.jenkins.getDescriptorOrDie(SSHLauncher.class);
154149
assertEquals(FormValidation.ok(), desc.doCheckHost("hostname"));
155150
assertEquals(FormValidation.Kind.ERROR, desc.doCheckHost("").kind);
156151
assertEquals(FormValidation.Kind.ERROR, desc.doCheckHost(null).kind);
157152
}
158153

159154
@Test
160-
public void checkPort() {
155+
void checkPort() {
161156
SSHLauncher.DescriptorImpl desc = (SSHLauncher.DescriptorImpl) j.jenkins.getDescriptorOrDie(SSHLauncher.class);
162157
assertEquals(FormValidation.ok(), desc.doCheckPort("22"));
163158
assertEquals(FormValidation.Kind.ERROR, desc.doCheckPort("").kind);
@@ -167,15 +162,15 @@ public void checkPort() {
167162
}
168163

169164
@Test
170-
public void trimWhiteSpace() throws Exception {
165+
void trimWhiteSpace() throws Exception {
171166
checkRoundTrip(" localhost");
172167
checkRoundTrip("localhost ");
173168
checkRoundTrip(" localhost ");
174169
}
175170

176171
@Issue("JENKINS-38832")
177172
@Test
178-
public void trackCredentialsWithUsernameAndPassword() throws Exception {
173+
void trackCredentialsWithUsernameAndPassword() throws Exception {
179174
UsernamePasswordCredentialsImpl credentials =
180175
new UsernamePasswordCredentialsImpl(CredentialsScope.SYSTEM, "dummyCredentialId", null, "user", "pass");
181176
SystemCredentialsProvider.getInstance()
@@ -185,7 +180,8 @@ public void trackCredentialsWithUsernameAndPassword() throws Exception {
185180
launcher.setLaunchTimeoutSeconds(5);
186181
launcher.setRetryWaitTime(5);
187182
launcher.setMaxNumRetries(2);
188-
DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher);
183+
DumbSlave agent =
184+
new DumbSlave("agent", newFolder(temporaryFolder, "junit").getAbsolutePath(), launcher);
189185

190186
Fingerprint fingerprint = CredentialsProvider.getFingerprintOf(credentials);
191187
assertThat("No fingerprint created until use", fingerprint, nullValue());
@@ -202,7 +198,7 @@ public void trackCredentialsWithUsernameAndPassword() throws Exception {
202198

203199
@Issue("JENKINS-38832")
204200
@Test
205-
public void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
201+
void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
206202
BasicSSHUserPrivateKey credentials =
207203
new BasicSSHUserPrivateKey(CredentialsScope.SYSTEM, "dummyCredentialId", "user", null, "", "desc");
208204
SystemCredentialsProvider.getInstance()
@@ -212,7 +208,8 @@ public void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
212208
launcher.setLaunchTimeoutSeconds(5);
213209
launcher.setRetryWaitTime(5);
214210
launcher.setMaxNumRetries(2);
215-
DumbSlave agent = new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher);
211+
DumbSlave agent =
212+
new DumbSlave("agent", newFolder(temporaryFolder, "junit").getAbsolutePath(), launcher);
216213

217214
Fingerprint fingerprint = CredentialsProvider.getFingerprintOf(credentials);
218215
assertThat("No fingerprint created until use", fingerprint, nullValue());
@@ -229,7 +226,7 @@ public void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
229226

230227
@Issue("JENKINS-44111")
231228
@Test
232-
public void workDirTest() {
229+
void workDirTest() {
233230
String rootFS = "/home/user";
234231
String anotherWorkDir = "/another/workdir";
235232

@@ -246,7 +243,7 @@ public void workDirTest() {
246243
15,
247244
new NonVerifyingKeyVerificationStrategy());
248245
// use rootFS
249-
Assert.assertEquals(
246+
assertEquals(
250247
WORK_DIR_PARAM + rootFS + JAR_CACHE_PARAM + rootFS + JAR_CACHE_DIR, launcher.getWorkDirParam(rootFS));
251248

252249
launcher = new SSHLauncher(
@@ -262,10 +259,10 @@ public void workDirTest() {
262259
15,
263260
new NonVerifyingKeyVerificationStrategy());
264261
// if worDir is in suffix return ""
265-
Assert.assertEquals("", launcher.getWorkDirParam(rootFS));
262+
assertEquals("", launcher.getWorkDirParam(rootFS));
266263
// if worDir is in suffix return "", even do you set workDir in configuration
267264
launcher.setWorkDir(anotherWorkDir);
268-
Assert.assertEquals("", launcher.getWorkDirParam(rootFS));
265+
assertEquals("", launcher.getWorkDirParam(rootFS));
269266

270267
launcher = new SSHLauncher(
271268
"Hostname",
@@ -281,13 +278,13 @@ public void workDirTest() {
281278
new NonVerifyingKeyVerificationStrategy());
282279
// user the workDir set in configuration
283280
launcher.setWorkDir(anotherWorkDir);
284-
Assert.assertEquals(
281+
assertEquals(
285282
WORK_DIR_PARAM + anotherWorkDir + JAR_CACHE_PARAM + anotherWorkDir + JAR_CACHE_DIR,
286283
launcher.getWorkDirParam(rootFS));
287284
}
288285

289286
@Test
290-
public void timeoutAndRetrySettings() {
287+
void timeoutAndRetrySettings() {
291288
final SSHLauncher launcher = new SSHLauncher(
292289
"Hostname",
293290
22,
@@ -307,7 +304,7 @@ public void timeoutAndRetrySettings() {
307304

308305
@Issue("JENKINS-54934")
309306
@Test
310-
public void timeoutAndRetrySettingsAllowZero() {
307+
void timeoutAndRetrySettingsAllowZero() {
311308
final SSHLauncher launcher = new SSHLauncher(
312309
"Hostname",
313310
22,
@@ -325,7 +322,7 @@ public void timeoutAndRetrySettingsAllowZero() {
325322
}
326323

327324
@Test
328-
public void timeoutAndRetrySettingsSetDefaultsIfOutOfRange() {
325+
void timeoutAndRetrySettingsSetDefaultsIfOutOfRange() {
329326
final SSHLauncher launcher = new SSHLauncher(
330327
"Hostname",
331328
22,
@@ -360,41 +357,27 @@ public void timeoutAndRetrySettingsSetDefaultsIfOutOfRange() {
360357
}
361358

362359
@Test
363-
public void getMd5Hash() {
364-
365-
try {
366-
byte[] bytes = "Leave me alone!".getBytes();
367-
String result = SSHLauncher.getMd5Hash(bytes);
368-
assertEquals("1EB226C8E950BAC1494BE197E84A264C", result);
369-
} catch (Exception e) {
370-
e.printStackTrace();
371-
}
360+
void getMd5Hash() throws Exception {
361+
byte[] bytes = "Leave me alone!".getBytes();
362+
String result = SSHLauncher.getMd5Hash(bytes);
363+
assertEquals("1EB226C8E950BAC1494BE197E84A264C", result);
372364
}
373365

374366
@Test
375-
public void readInputStreamIntoByteArrayAndClose() {
376-
377-
InputStream inputStream = null;
378-
File testFile;
379-
try {
380-
381-
testFile = new File("target" + File.separator + "test-classes", "readInputStreamIntoByteArrayTestFile.txt");
367+
void readInputStreamIntoByteArrayAndClose() throws Exception {
368+
File testFile =
369+
new File("target" + File.separator + "test-classes", "readInputStreamIntoByteArrayTestFile.txt");
370+
try (InputStream inputStream = new FileInputStream(testFile)) {
382371
assertTrue(testFile.exists());
383-
inputStream = new FileInputStream(testFile);
384372
byte[] bytes = SSHLauncher.readInputStreamIntoByteArrayAndClose(inputStream);
385373
assertNotNull(bytes);
386374
assertTrue(bytes.length > 0);
387375
assertEquals("Don't change me or add newlines!", new String(bytes));
388-
389-
} catch (Exception e) {
390-
e.printStackTrace();
391-
} finally {
392-
IOUtils.closeQuietly(inputStream);
393376
}
394377
}
395378

396379
@Test
397-
public void retryTest() throws IOException, InterruptedException, Descriptor.FormException {
380+
void retryTest() throws IOException, InterruptedException, Descriptor.FormException {
398381
DumbSlave agent = getPermanentAgentHostNotExist();
399382
j.jenkins.addNode(agent);
400383
String log = "";
@@ -411,14 +394,22 @@ public void retryTest() throws IOException, InterruptedException, Descriptor.For
411394
assertFalse(log.contains("There are 4 more retries left."));
412395
}
413396

397+
@Test
398+
void knownHostsFileDefaultConfig() {
399+
String defaultPath = Paths.get(System.getProperty("user.home"), ".ssh", "known_hosts")
400+
.toString();
401+
KnownHostsFileKeyVerificationStrategy khvs = new KnownHostsFileKeyVerificationStrategy();
402+
assertEquals(khvs.getKnownHostsFile().getPath(), defaultPath);
403+
}
404+
414405
private DumbSlave getPermanentAgentHostNotExist() throws Descriptor.FormException, IOException {
415406
fakeCredentials("dummyCredentialId");
416407
final SSHLauncher launcher = new SSHLauncher("HostNotExists", 22, "dummyCredentialId");
417408
launcher.setSshHostKeyVerificationStrategy(new NonVerifyingKeyVerificationStrategy());
418409
launcher.setLaunchTimeoutSeconds(5);
419410
launcher.setRetryWaitTime(1);
420411
launcher.setMaxNumRetries(3);
421-
return new DumbSlave("agent", temporaryFolder.newFolder().getAbsolutePath(), launcher);
412+
return new DumbSlave("agent", newFolder(temporaryFolder, "junit").getAbsolutePath(), launcher);
422413
}
423414

424415
private void fakeCredentials(String id) {
@@ -429,11 +420,10 @@ private void fakeCredentials(String id) {
429420
.put(Domain.global(), Collections.singletonList(credentials));
430421
}
431422

432-
@Test
433-
public void KnownHostsFileDefaultConfig() {
434-
String defaultPath = Paths.get(System.getProperty("user.home"), ".ssh", "known_hosts")
435-
.toString();
436-
KnownHostsFileKeyVerificationStrategy khvs = new KnownHostsFileKeyVerificationStrategy();
437-
assertEquals(khvs.getKnownHostsFile().getPath(), defaultPath);
423+
private static File newFolder(File root, String... subDirs) {
424+
String subFolder = String.join("/", subDirs);
425+
File result = new File(root, subFolder);
426+
assertFalse(!result.exists() && !result.mkdirs(), "Couldn't create folders " + result);
427+
return result;
438428
}
439429
}

0 commit comments

Comments
 (0)