5353import static org .hamcrest .Matchers .iterableWithSize ;
5454import static org .hamcrest .Matchers .not ;
5555import static org .hamcrest .Matchers .startsWith ;
56- import static org .junit .Assert .assertEquals ;
57- import static org .junit .Assert .assertFalse ;
58- import static org .junit .Assert .assertNotNull ;
59- import static org .junit .Assert .assertNull ;
60- import static org .junit .Assert .fail ;
56+ import static org .junit .Assert .*;
6157import static org .mockito .Matchers .any ;
6258import static org .mockito .Matchers .anyInt ;
6359import static org .mockito .Matchers .eq ;
@@ -303,7 +299,7 @@ public void correctMetadataSet() throws Exception {
303299
304300 assertThat (cloud .getOpenstack ().instanceUrl (), not (emptyString ()));
305301 assertThat (cloud .getOpenstack ().instanceFingerprint (), not (emptyString ()));
306- System . out . println ( cloud . getOpenstack (). instanceFingerprint ());
302+
307303 Server server = template .provisionServer (null , null );
308304 Map <String , String > m = server .getMetadata ();
309305 assertEquals (cloud .getOpenstack ().instanceUrl (), m .get (Openstack .FINGERPRINT_KEY_URL ));
@@ -340,7 +336,7 @@ public void timeoutProvisioning() throws Exception {
340336 }
341337
342338 @ Test
343- public void timeoutLaunching () throws Exception {
339+ public void timeoutLaunchingJnlp () throws Exception {
344340 final SlaveOptions opts = j .defaultSlaveOptions ().getBuilder ().startTimeout (1000 ).build ();
345341 final JCloudsCloud cloud = j .configureSlaveProvisioningWithFloatingIP (j .dummyCloud (opts , j .dummySlaveTemplate ("asdf" )));
346342 final Iterable <NodeProvisioner .PlannedNode > pns = cloud .provision (Label .get ("asdf" ), 1 );
@@ -359,7 +355,48 @@ public void timeoutLaunching() throws Exception {
359355 assertThat ("Server details are printed" , msg , containsString ("Server state: Mock for " ));
360356 }
361357
362- // Wait for the server to be dereflleted
358+ // Wait for the server to be disposed
359+ AsyncResourceDisposer disposer = AsyncResourceDisposer .get ();
360+ while (!disposer .getBacklog ().isEmpty ()) {
361+ Thread .sleep (1000 );
362+ }
363+ verify (cloud .getOpenstack ()).destroyServer (any (Server .class ));
364+ }
365+
366+ @ Test @ Issue ("https://github.com/jenkinsci/openstack-cloud-plugin/issues/310" )
367+ public void timeoutLaunchingSsh () throws Exception {
368+ LauncherFactory .SSH sshLaunch = new LauncherFactory .SSH ("no-such-creds" );
369+ final SlaveOptions opts = j .defaultSlaveOptions ().getBuilder ().startTimeout (3000 ).launcherFactory (sshLaunch ).build ();
370+ final JCloudsCloud cloud = j .configureSlaveProvisioningWithFloatingIP (j .dummyCloud (opts , j .dummySlaveTemplate ("asdf" )));
371+ JCloudsSlave agent = j .provision (cloud , "asdf" );
372+ JCloudsComputer computer = agent .getComputer ();
373+ assertFalse (agent .isLaunchTimedOut ());
374+
375+ OfflineCause ofc = null ;
376+ for (int i = 0 ; i < 3 ; i ++){
377+ ofc = computer .getOfflineCause ();
378+ if (ofc != null ) break ;
379+ Thread .sleep (500 );
380+ }
381+ assertThat (ofc , instanceOf (OfflineCause .LaunchFailed .class ));
382+ // OfflineCause.LaunchFailed is NOT fatal until the stat timeout is up
383+ assertNull (computer .getFatalOfflineCause ());
384+ assertFalse (agent .isLaunchTimedOut ());
385+
386+ for (int i = 0 ; i < 4 ; i ++){
387+ ofc = computer .getFatalOfflineCause ();
388+ if (ofc != null ) break ;
389+ Thread .sleep (1000 );
390+ }
391+ // OfflineCause.LaunchFailed will become fatal when the stat timeout is up
392+ long aliveFor = System .currentTimeMillis () - agent .getCreatedTime ();
393+ assertTrue ("Not timed out after ms " + aliveFor , agent .isLaunchTimedOut ());
394+ //assertThat("Cause not fatal after ms " + aliveFor, computer.getFatalOfflineCause(), instanceOf(OfflineCause.LaunchFailed.class));
395+ assertThat ("Cause not fatal after ms " + aliveFor , ofc , instanceOf (OfflineCause .LaunchFailed .class ));
396+
397+ j .triggerOpenstackSlaveCleanup ();
398+
399+ // Wait for the server to be disposed
363400 AsyncResourceDisposer disposer = AsyncResourceDisposer .get ();
364401 while (!disposer .getBacklog ().isEmpty ()) {
365402 Thread .sleep (1000 );
0 commit comments