2525import com .cloud .ha .HighAvailabilityManager ;
2626import com .cloud .host .Host ;
2727import com .cloud .host .HostVO ;
28+ import com .cloud .host .Status ;
2829import com .cloud .host .dao .HostDao ;
2930import com .cloud .hypervisor .Hypervisor ;
3031import com .cloud .storage .StorageManager ;
32+ import com .cloud .utils .Pair ;
33+ import com .cloud .utils .exception .CloudRuntimeException ;
3134import com .cloud .utils .fsm .NoTransitionException ;
35+ import com .cloud .utils .ssh .SSHCmdHelper ;
36+ import com .cloud .utils .ssh .SshException ;
3237import com .cloud .vm .VMInstanceVO ;
3338import com .cloud .vm .dao .UserVmDetailsDao ;
3439import com .cloud .vm .dao .VMInstanceDao ;
40+ import com .trilead .ssh2 .Connection ;
41+ import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
3542import org .junit .Assert ;
3643import org .junit .Before ;
3744import org .junit .Test ;
5663import static org .mockito .Matchers .anyLong ;
5764import static org .mockito .Matchers .anyString ;
5865import static org .mockito .Matchers .eq ;
66+ import static org .mockito .Mockito .never ;
5967import static org .mockito .Mockito .times ;
6068import static org .mockito .Mockito .verify ;
6169import static org .mockito .Mockito .when ;
6270
6371@ RunWith (PowerMockRunner .class )
64- @ PrepareForTest ({ActionEventUtils .class , ResourceManagerImpl .class })
72+ @ PrepareForTest ({ActionEventUtils .class , ResourceManagerImpl .class , SSHCmdHelper . class })
6573public class ResourceManagerImplTest {
6674
6775 @ Mock
@@ -78,6 +86,8 @@ public class ResourceManagerImplTest {
7886 private HostDao hostDao ;
7987 @ Mock
8088 private VMInstanceDao vmInstanceDao ;
89+ @ Mock
90+ private ConfigurationDao configurationDao ;
8191
8292 @ Spy
8393 @ InjectMocks
@@ -99,7 +109,13 @@ public class ResourceManagerImplTest {
99109 @ Mock
100110 private GetVncPortCommand getVncPortCommandVm2 ;
101111
112+ @ Mock
113+ private Connection sshConnection ;
114+
102115 private static long hostId = 1L ;
116+ private static final String hostUsername = "user" ;
117+ private static final String hostPassword = "password" ;
118+ private static final String hostPrivateIp = "192.168.1.10" ;
103119
104120 private static long vm1Id = 1L ;
105121 private static String vm1InstanceName = "i-1-VM" ;
@@ -117,9 +133,13 @@ public void setup() throws Exception {
117133 when (host .getType ()).thenReturn (Host .Type .Routing );
118134 when (host .getId ()).thenReturn (hostId );
119135 when (host .getResourceState ()).thenReturn (ResourceState .Enabled );
120- when (host .getHypervisorType ()).thenReturn (Hypervisor .HypervisorType .VMware );
136+ when (host .getHypervisorType ()).thenReturn (Hypervisor .HypervisorType .KVM );
121137 when (host .getClusterId ()).thenReturn (1L );
122138 when (hostDao .findById (hostId )).thenReturn (host );
139+ when (host .getDetail ("username" )).thenReturn (hostUsername );
140+ when (host .getDetail ("password" )).thenReturn (hostPassword );
141+ when (host .getStatus ()).thenReturn (Status .Up );
142+ when (host .getPrivateIpAddress ()).thenReturn (hostPrivateIp );
123143 when (vm1 .getId ()).thenReturn (vm1Id );
124144 when (vm2 .getId ()).thenReturn (vm2Id );
125145 when (vm1 .getInstanceName ()).thenReturn (vm1InstanceName );
@@ -138,6 +158,15 @@ public void setup() throws Exception {
138158 PowerMockito .whenNew (GetVncPortCommand .class ).withArguments (vm2Id , vm2InstanceName ).thenReturn (getVncPortCommandVm2 );
139159 when (agentManager .easySend (eq (hostId ), eq (getVncPortCommandVm1 ))).thenReturn (getVncPortAnswerVm1 );
140160 when (agentManager .easySend (eq (hostId ), eq (getVncPortCommandVm2 ))).thenReturn (getVncPortAnswerVm2 );
161+
162+ PowerMockito .mockStatic (SSHCmdHelper .class );
163+ BDDMockito .given (SSHCmdHelper .acquireAuthorizedConnection (eq (hostPrivateIp ), eq (22 ),
164+ eq (hostUsername ), eq (hostPassword ))).willReturn (sshConnection );
165+ BDDMockito .given (SSHCmdHelper .sshExecuteCmdOneShot (eq (sshConnection ),
166+ eq ("service cloudstack-agent restart" ))).
167+ willReturn (new SSHCmdHelper .SSHCmdResult (0 ,"" ,"" ));
168+
169+ when (configurationDao .getValue (ResourceManager .KvmSshToAgentEnabled .key ())).thenReturn ("true" );
141170 }
142171
143172 @ Test
@@ -206,4 +235,76 @@ public void testCheckAndMaintainErrorInMaintenanceRetries() throws NoTransitionE
206235 verify (resourceManager , times (retries + 1 )).isHostInMaintenance (host , failedMigrations , new ArrayList <>(), failedMigrations );
207236 verify (resourceManager ).setHostIntoErrorInMaintenance (host , failedMigrations );
208237 }
238+
239+ @ Test (expected = CloudRuntimeException .class )
240+ public void testGetHostCredentialsMissingParameter () {
241+ when (host .getDetail ("password" )).thenReturn (null );
242+ resourceManager .getHostCredentials (host );
243+ }
244+
245+ @ Test
246+ public void testGetHostCredentials () {
247+ Pair <String , String > credentials = resourceManager .getHostCredentials (host );
248+ Assert .assertNotNull (credentials );
249+ Assert .assertEquals (hostUsername , credentials .first ());
250+ Assert .assertEquals (hostPassword , credentials .second ());
251+ }
252+
253+ @ Test (expected = CloudRuntimeException .class )
254+ public void testConnectAndRestartAgentOnHostCannotConnect () {
255+ BDDMockito .given (SSHCmdHelper .acquireAuthorizedConnection (eq (hostPrivateIp ), eq (22 ),
256+ eq (hostUsername ), eq (hostPassword ))).willReturn (null );
257+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
258+ }
259+
260+ @ Test (expected = CloudRuntimeException .class )
261+ public void testConnectAndRestartAgentOnHostCannotRestart () throws Exception {
262+ BDDMockito .given (SSHCmdHelper .sshExecuteCmdOneShot (eq (sshConnection ),
263+ eq ("service cloudstack-agent restart" ))).willThrow (new SshException ("exception" ));
264+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
265+ }
266+
267+ @ Test
268+ public void testConnectAndRestartAgentOnHost () {
269+ resourceManager .connectAndRestartAgentOnHost (host , hostUsername , hostPassword );
270+ }
271+
272+ @ Test
273+ public void testHandleAgentSSHEnabledNotConnectedAgent () {
274+ when (host .getStatus ()).thenReturn (Status .Disconnected );
275+ resourceManager .handleAgentIfNotConnected (host , false );
276+ verify (resourceManager ).getHostCredentials (eq (host ));
277+ verify (resourceManager ).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
278+ }
279+
280+ @ Test
281+ public void testHandleAgentSSHEnabledConnectedAgent () {
282+ when (host .getStatus ()).thenReturn (Status .Up );
283+ resourceManager .handleAgentIfNotConnected (host , false );
284+ verify (resourceManager , never ()).getHostCredentials (eq (host ));
285+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
286+ }
287+
288+ @ Test (expected = CloudRuntimeException .class )
289+ public void testHandleAgentSSHDisabledNotConnectedAgent () {
290+ when (host .getStatus ()).thenReturn (Status .Disconnected );
291+ when (configurationDao .getValue (ResourceManager .KvmSshToAgentEnabled .key ())).thenReturn ("false" );
292+ resourceManager .handleAgentIfNotConnected (host , false );
293+ }
294+
295+ @ Test
296+ public void testHandleAgentSSHDisabledConnectedAgent () {
297+ when (host .getStatus ()).thenReturn (Status .Up );
298+ when (configurationDao .getValue (ResourceManager .KvmSshToAgentEnabled .key ())).thenReturn ("false" );
299+ resourceManager .handleAgentIfNotConnected (host , false );
300+ verify (resourceManager , never ()).getHostCredentials (eq (host ));
301+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
302+ }
303+
304+ @ Test
305+ public void testHandleAgentVMsMigrating () {
306+ resourceManager .handleAgentIfNotConnected (host , true );
307+ verify (resourceManager , never ()).getHostCredentials (eq (host ));
308+ verify (resourceManager , never ()).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ));
309+ }
209310}
0 commit comments