29
29
import static org .hamcrest .MatcherAssert .assertThat ;
30
30
import static org .hamcrest .core .IsNull .notNullValue ;
31
31
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 ;
37
37
38
38
import com .cloudbees .jenkins .plugins .sshcredentials .impl .BasicSSHUserPrivateKey ;
39
39
import com .cloudbees .plugins .credentials .CredentialsProvider ;
55
55
import java .io .InputStream ;
56
56
import java .nio .file .Paths ;
57
57
import java .util .Collections ;
58
- import org .apache .commons .io .IOUtils ;
59
58
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 ;
68
62
import org .jvnet .hudson .test .Issue ;
69
63
import org .jvnet .hudson .test .JenkinsRule ;
64
+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
70
65
71
- public class SSHLauncherTest {
66
+ @ WithJenkins
67
+ class SSHLauncherTest {
72
68
73
- @ ClassRule
74
- public static BuildWatcher buildWatcher = new BuildWatcher () ;
69
+ @ TempDir
70
+ private File temporaryFolder ;
75
71
76
- @ Rule
77
- public final TemporaryFolder temporaryFolder = new TemporaryFolder ();
72
+ private JenkinsRule j ;
78
73
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
+ }
84
78
85
79
private void checkRoundTrip (String host ) throws Exception {
86
80
SystemCredentialsProvider .getInstance ()
@@ -92,7 +86,8 @@ private void checkRoundTrip(String host) throws Exception {
92
86
SSHLauncher launcher = new SSHLauncher (host , 123 , "dummyCredentialId" );
93
87
launcher .setSshHostKeyVerificationStrategy (new KnownHostsFileKeyVerificationStrategy ());
94
88
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 );
96
91
j .jenkins .addNode (agent );
97
92
98
93
HtmlPage p = j .createWebClient ().getPage (agent , "configure" );
@@ -105,12 +100,12 @@ private void checkRoundTrip(String host) throws Exception {
105
100
}
106
101
107
102
@ Test
108
- public void configurationRoundTrip () throws Exception {
103
+ void configurationRoundTrip () throws Exception {
109
104
checkRoundTrip ("localhost" );
110
105
}
111
106
112
107
@ Test
113
- public void fillCredentials () {
108
+ void fillCredentials () {
114
109
SystemCredentialsProvider .getInstance ()
115
110
.getDomainCredentialsMap ()
116
111
.put (
@@ -138,7 +133,7 @@ public void fillCredentials() {
138
133
}
139
134
140
135
@ Test
141
- public void checkJavaPathWhiteSpaces () {
136
+ void checkJavaPathWhiteSpaces () {
142
137
SSHLauncher .DescriptorImpl desc = (SSHLauncher .DescriptorImpl ) j .jenkins .getDescriptorOrDie (SSHLauncher .class );
143
138
assertEquals (FormValidation .ok (), desc .doCheckJavaPath ("/usr/lib/jdk/bin/java" ));
144
139
assertEquals (FormValidation .ok (), desc .doCheckJavaPath ("\" /usr/lib/jdk/bin/java\" " ));
@@ -149,15 +144,15 @@ public void checkJavaPathWhiteSpaces() {
149
144
}
150
145
151
146
@ Test
152
- public void checkHost () {
147
+ void checkHost () {
153
148
SSHLauncher .DescriptorImpl desc = (SSHLauncher .DescriptorImpl ) j .jenkins .getDescriptorOrDie (SSHLauncher .class );
154
149
assertEquals (FormValidation .ok (), desc .doCheckHost ("hostname" ));
155
150
assertEquals (FormValidation .Kind .ERROR , desc .doCheckHost ("" ).kind );
156
151
assertEquals (FormValidation .Kind .ERROR , desc .doCheckHost (null ).kind );
157
152
}
158
153
159
154
@ Test
160
- public void checkPort () {
155
+ void checkPort () {
161
156
SSHLauncher .DescriptorImpl desc = (SSHLauncher .DescriptorImpl ) j .jenkins .getDescriptorOrDie (SSHLauncher .class );
162
157
assertEquals (FormValidation .ok (), desc .doCheckPort ("22" ));
163
158
assertEquals (FormValidation .Kind .ERROR , desc .doCheckPort ("" ).kind );
@@ -167,15 +162,15 @@ public void checkPort() {
167
162
}
168
163
169
164
@ Test
170
- public void trimWhiteSpace () throws Exception {
165
+ void trimWhiteSpace () throws Exception {
171
166
checkRoundTrip (" localhost" );
172
167
checkRoundTrip ("localhost " );
173
168
checkRoundTrip (" localhost " );
174
169
}
175
170
176
171
@ Issue ("JENKINS-38832" )
177
172
@ Test
178
- public void trackCredentialsWithUsernameAndPassword () throws Exception {
173
+ void trackCredentialsWithUsernameAndPassword () throws Exception {
179
174
UsernamePasswordCredentialsImpl credentials =
180
175
new UsernamePasswordCredentialsImpl (CredentialsScope .SYSTEM , "dummyCredentialId" , null , "user" , "pass" );
181
176
SystemCredentialsProvider .getInstance ()
@@ -185,7 +180,8 @@ public void trackCredentialsWithUsernameAndPassword() throws Exception {
185
180
launcher .setLaunchTimeoutSeconds (5 );
186
181
launcher .setRetryWaitTime (5 );
187
182
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 );
189
185
190
186
Fingerprint fingerprint = CredentialsProvider .getFingerprintOf (credentials );
191
187
assertThat ("No fingerprint created until use" , fingerprint , nullValue ());
@@ -202,7 +198,7 @@ public void trackCredentialsWithUsernameAndPassword() throws Exception {
202
198
203
199
@ Issue ("JENKINS-38832" )
204
200
@ Test
205
- public void trackCredentialsWithUsernameAndPrivateKey () throws Exception {
201
+ void trackCredentialsWithUsernameAndPrivateKey () throws Exception {
206
202
BasicSSHUserPrivateKey credentials =
207
203
new BasicSSHUserPrivateKey (CredentialsScope .SYSTEM , "dummyCredentialId" , "user" , null , "" , "desc" );
208
204
SystemCredentialsProvider .getInstance ()
@@ -212,7 +208,8 @@ public void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
212
208
launcher .setLaunchTimeoutSeconds (5 );
213
209
launcher .setRetryWaitTime (5 );
214
210
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 );
216
213
217
214
Fingerprint fingerprint = CredentialsProvider .getFingerprintOf (credentials );
218
215
assertThat ("No fingerprint created until use" , fingerprint , nullValue ());
@@ -229,7 +226,7 @@ public void trackCredentialsWithUsernameAndPrivateKey() throws Exception {
229
226
230
227
@ Issue ("JENKINS-44111" )
231
228
@ Test
232
- public void workDirTest () {
229
+ void workDirTest () {
233
230
String rootFS = "/home/user" ;
234
231
String anotherWorkDir = "/another/workdir" ;
235
232
@@ -246,7 +243,7 @@ public void workDirTest() {
246
243
15 ,
247
244
new NonVerifyingKeyVerificationStrategy ());
248
245
// use rootFS
249
- Assert . assertEquals (
246
+ assertEquals (
250
247
WORK_DIR_PARAM + rootFS + JAR_CACHE_PARAM + rootFS + JAR_CACHE_DIR , launcher .getWorkDirParam (rootFS ));
251
248
252
249
launcher = new SSHLauncher (
@@ -262,10 +259,10 @@ public void workDirTest() {
262
259
15 ,
263
260
new NonVerifyingKeyVerificationStrategy ());
264
261
// if worDir is in suffix return ""
265
- Assert . assertEquals ("" , launcher .getWorkDirParam (rootFS ));
262
+ assertEquals ("" , launcher .getWorkDirParam (rootFS ));
266
263
// if worDir is in suffix return "", even do you set workDir in configuration
267
264
launcher .setWorkDir (anotherWorkDir );
268
- Assert . assertEquals ("" , launcher .getWorkDirParam (rootFS ));
265
+ assertEquals ("" , launcher .getWorkDirParam (rootFS ));
269
266
270
267
launcher = new SSHLauncher (
271
268
"Hostname" ,
@@ -281,13 +278,13 @@ public void workDirTest() {
281
278
new NonVerifyingKeyVerificationStrategy ());
282
279
// user the workDir set in configuration
283
280
launcher .setWorkDir (anotherWorkDir );
284
- Assert . assertEquals (
281
+ assertEquals (
285
282
WORK_DIR_PARAM + anotherWorkDir + JAR_CACHE_PARAM + anotherWorkDir + JAR_CACHE_DIR ,
286
283
launcher .getWorkDirParam (rootFS ));
287
284
}
288
285
289
286
@ Test
290
- public void timeoutAndRetrySettings () {
287
+ void timeoutAndRetrySettings () {
291
288
final SSHLauncher launcher = new SSHLauncher (
292
289
"Hostname" ,
293
290
22 ,
@@ -307,7 +304,7 @@ public void timeoutAndRetrySettings() {
307
304
308
305
@ Issue ("JENKINS-54934" )
309
306
@ Test
310
- public void timeoutAndRetrySettingsAllowZero () {
307
+ void timeoutAndRetrySettingsAllowZero () {
311
308
final SSHLauncher launcher = new SSHLauncher (
312
309
"Hostname" ,
313
310
22 ,
@@ -325,7 +322,7 @@ public void timeoutAndRetrySettingsAllowZero() {
325
322
}
326
323
327
324
@ Test
328
- public void timeoutAndRetrySettingsSetDefaultsIfOutOfRange () {
325
+ void timeoutAndRetrySettingsSetDefaultsIfOutOfRange () {
329
326
final SSHLauncher launcher = new SSHLauncher (
330
327
"Hostname" ,
331
328
22 ,
@@ -360,41 +357,27 @@ public void timeoutAndRetrySettingsSetDefaultsIfOutOfRange() {
360
357
}
361
358
362
359
@ 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 );
372
364
}
373
365
374
366
@ 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 )) {
382
371
assertTrue (testFile .exists ());
383
- inputStream = new FileInputStream (testFile );
384
372
byte [] bytes = SSHLauncher .readInputStreamIntoByteArrayAndClose (inputStream );
385
373
assertNotNull (bytes );
386
374
assertTrue (bytes .length > 0 );
387
375
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 );
393
376
}
394
377
}
395
378
396
379
@ Test
397
- public void retryTest () throws IOException , InterruptedException , Descriptor .FormException {
380
+ void retryTest () throws IOException , InterruptedException , Descriptor .FormException {
398
381
DumbSlave agent = getPermanentAgentHostNotExist ();
399
382
j .jenkins .addNode (agent );
400
383
String log = "" ;
@@ -411,14 +394,22 @@ public void retryTest() throws IOException, InterruptedException, Descriptor.For
411
394
assertFalse (log .contains ("There are 4 more retries left." ));
412
395
}
413
396
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
+
414
405
private DumbSlave getPermanentAgentHostNotExist () throws Descriptor .FormException , IOException {
415
406
fakeCredentials ("dummyCredentialId" );
416
407
final SSHLauncher launcher = new SSHLauncher ("HostNotExists" , 22 , "dummyCredentialId" );
417
408
launcher .setSshHostKeyVerificationStrategy (new NonVerifyingKeyVerificationStrategy ());
418
409
launcher .setLaunchTimeoutSeconds (5 );
419
410
launcher .setRetryWaitTime (1 );
420
411
launcher .setMaxNumRetries (3 );
421
- return new DumbSlave ("agent" , temporaryFolder . newFolder ().getAbsolutePath (), launcher );
412
+ return new DumbSlave ("agent" , newFolder (temporaryFolder , "junit" ).getAbsolutePath (), launcher );
422
413
}
423
414
424
415
private void fakeCredentials (String id ) {
@@ -429,11 +420,10 @@ private void fakeCredentials(String id) {
429
420
.put (Domain .global (), Collections .singletonList (credentials ));
430
421
}
431
422
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 ;
438
428
}
439
429
}
0 commit comments