67
67
import com .cloud .host .dao .HostDao ;
68
68
import com .cloud .hypervisor .Hypervisor ;
69
69
import com .cloud .storage .StorageManager ;
70
- import com .cloud .utils .Pair ;
70
+ import com .cloud .utils .Ternary ;
71
71
import com .cloud .utils .exception .CloudRuntimeException ;
72
72
import com .cloud .utils .fsm .NoTransitionException ;
73
73
import com .cloud .utils .ssh .SSHCmdHelper ;
@@ -125,6 +125,7 @@ public class ResourceManagerImplTest {
125
125
private static long hostId = 1L ;
126
126
private static final String hostUsername = "user" ;
127
127
private static final String hostPassword = "password" ;
128
+ private static final String hostPrivateKey = "privatekey" ;
128
129
private static final String hostPrivateIp = "192.168.1.10" ;
129
130
130
131
private static long vm1Id = 1L ;
@@ -148,6 +149,7 @@ public void setup() throws Exception {
148
149
when (hostDao .findById (hostId )).thenReturn (host );
149
150
when (host .getDetail ("username" )).thenReturn (hostUsername );
150
151
when (host .getDetail ("password" )).thenReturn (hostPassword );
152
+ when (configurationDao .getValue ("ssh.privatekey" )).thenReturn (hostPrivateKey );
151
153
when (host .getStatus ()).thenReturn (Status .Up );
152
154
when (host .getPrivateIpAddress ()).thenReturn (hostPrivateIp );
153
155
when (vm1 .getId ()).thenReturn (vm1Id );
@@ -171,7 +173,7 @@ public void setup() throws Exception {
171
173
172
174
PowerMockito .mockStatic (SSHCmdHelper .class );
173
175
BDDMockito .given (SSHCmdHelper .acquireAuthorizedConnection (eq (hostPrivateIp ), eq (22 ),
174
- eq (hostUsername ), eq (hostPassword ))).willReturn (sshConnection );
176
+ eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) )).willReturn (sshConnection );
175
177
BDDMockito .given (SSHCmdHelper .sshExecuteCmdOneShot (eq (sshConnection ),
176
178
eq ("service cloudstack-agent restart" ))).
177
179
willReturn (new SSHCmdHelper .SSHCmdResult (0 ,"" ,"" ));
@@ -292,50 +294,52 @@ public void testConfigureVncAccessForKVMHostFailedMigrations() {
292
294
@ Test (expected = CloudRuntimeException .class )
293
295
public void testGetHostCredentialsMissingParameter () {
294
296
when (host .getDetail ("password" )).thenReturn (null );
297
+ when (configurationDao .getValue ("ssh.privatekey" )).thenReturn (null );
295
298
resourceManager .getHostCredentials (host );
296
299
}
297
300
298
301
@ Test
299
302
public void testGetHostCredentials () {
300
- Pair < String , String > credentials = resourceManager .getHostCredentials (host );
303
+ Ternary < String , String , String > credentials = resourceManager .getHostCredentials (host );
301
304
Assert .assertNotNull (credentials );
302
305
Assert .assertEquals (hostUsername , credentials .first ());
303
306
Assert .assertEquals (hostPassword , credentials .second ());
307
+ Assert .assertEquals (hostPrivateKey , credentials .third ());
304
308
}
305
309
306
310
@ Test (expected = CloudRuntimeException .class )
307
311
public void testConnectAndRestartAgentOnHostCannotConnect () {
308
312
BDDMockito .given (SSHCmdHelper .acquireAuthorizedConnection (eq (hostPrivateIp ), eq (22 ),
309
- eq (hostUsername ), eq (hostPassword ))).willReturn (null );
310
- resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
313
+ eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) )).willReturn (null );
314
+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword , hostPrivateKey );
311
315
}
312
316
313
317
@ Test (expected = CloudRuntimeException .class )
314
318
public void testConnectAndRestartAgentOnHostCannotRestart () throws Exception {
315
319
BDDMockito .given (SSHCmdHelper .sshExecuteCmdOneShot (eq (sshConnection ),
316
320
eq ("service cloudstack-agent restart" ))).willThrow (new SshException ("exception" ));
317
- resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
321
+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword , hostPrivateKey );
318
322
}
319
323
320
324
@ Test
321
325
public void testConnectAndRestartAgentOnHost () {
322
- resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
326
+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword , hostPrivateKey );
323
327
}
324
328
325
329
@ Test
326
330
public void testHandleAgentSSHEnabledNotConnectedAgent () {
327
331
when (host .getStatus ()).thenReturn (Status .Disconnected );
328
332
resourceManager .handleAgentIfNotConnected (host , false );
329
333
verify (resourceManager ).getHostCredentials (eq (host ));
330
- verify (resourceManager ).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
334
+ verify (resourceManager ).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) );
331
335
}
332
336
333
337
@ Test
334
338
public void testHandleAgentSSHEnabledConnectedAgent () {
335
339
when (host .getStatus ()).thenReturn (Status .Up );
336
340
resourceManager .handleAgentIfNotConnected (host , false );
337
341
verify (resourceManager , never ()).getHostCredentials (eq (host ));
338
- verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
342
+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) );
339
343
}
340
344
341
345
@ Test (expected = CloudRuntimeException .class )
@@ -351,14 +355,14 @@ public void testHandleAgentSSHDisabledConnectedAgent() {
351
355
when (configurationDao .getValue (ResourceManager .KvmSshToAgentEnabled .key ())).thenReturn ("false" );
352
356
resourceManager .handleAgentIfNotConnected (host , false );
353
357
verify (resourceManager , never ()).getHostCredentials (eq (host ));
354
- verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
358
+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) );
355
359
}
356
360
357
361
@ Test
358
362
public void testHandleAgentVMsMigrating () {
359
363
resourceManager .handleAgentIfNotConnected (host , true );
360
364
verify (resourceManager , never ()).getHostCredentials (eq (host ));
361
- verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
365
+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ), eq ( hostPrivateKey ) );
362
366
}
363
367
364
368
private void setupNoPendingMigrationRetries () {
0 commit comments