2121import com .cloud .agent .api .GetVncPortAnswer ;
2222import com .cloud .agent .api .GetVncPortCommand ;
2323import com .cloud .capacity .dao .CapacityDao ;
24+ import com .cloud .configuration .ConfigurationManagerImpl ;
2425import com .cloud .dc .ClusterVO ;
2526import com .cloud .dc .DataCenterVO ;
2627import com .cloud .dc .HostPodVO ;
5758import org .apache .cloudstack .api .command .admin .host .DeclareHostAsDegradedCmd ;
5859import org .apache .cloudstack .engine .subsystem .api .storage .DataStore ;
5960import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStoreInfo ;
61+ import org .apache .cloudstack .framework .config .ConfigKey ;
6062import org .apache .cloudstack .framework .config .dao .ConfigurationDao ;
6163import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
6264import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
7577import org .mockito .Spy ;
7678import org .mockito .junit .MockitoJUnitRunner ;
7779
80+ import java .lang .reflect .Field ;
7881import java .util .ArrayList ;
7982import java .util .Arrays ;
8083import java .util .Collections ;
@@ -179,6 +182,12 @@ public class ResourceManagerImplTest {
179182 private MockedConstruction <GetVncPortCommand > getVncPortCommandMockedConstruction ;
180183 private AutoCloseable closeable ;
181184
185+ private void overrideDefaultConfigValue (final ConfigKey configKey , final String name , final Object o ) throws IllegalAccessException , NoSuchFieldException {
186+ Field f = ConfigKey .class .getDeclaredField (name );
187+ f .setAccessible (true );
188+ f .set (configKey , o );
189+ }
190+
182191 @ Before
183192 public void setup () throws Exception {
184193 closeable = MockitoAnnotations .openMocks (this );
@@ -221,7 +230,7 @@ public void setup() throws Exception {
221230 eq ("service cloudstack-agent restart" ))).
222231 willReturn (new SSHCmdHelper .SSHCmdResult (0 ,"" ,"" ));
223232
224- when ( configurationDao . getValue ( ResourceManager .KvmSshToAgentEnabled . key ())). thenReturn ( "true" );
233+ overrideDefaultConfigValue ( ResourceManager .KvmSshToAgentEnabled , "_defaultValue" , "true" );
225234
226235 rootDisks = Arrays .asList (rootDisk1 , rootDisk2 );
227236 dataDisks = Collections .singletonList (dataDisk );
@@ -383,8 +392,9 @@ public void testConnectAndRestartAgentOnHost() {
383392 }
384393
385394 @ Test
386- public void testHandleAgentSSHEnabledNotConnectedAgent () {
395+ public void testHandleAgentSSHEnabledNotConnectedAgent () throws NoSuchFieldException , IllegalAccessException {
387396 when (host .getStatus ()).thenReturn (Status .Disconnected );
397+ overrideDefaultConfigValue (ResourceManager .KvmSshToAgentEnabled , "_defaultValue" , "true" );
388398 resourceManager .handleAgentIfNotConnected (host , false );
389399 verify (resourceManager ).getHostCredentials (eq (host ));
390400 verify (resourceManager ).connectAndRestartAgentOnHost (eq (host ), eq (hostUsername ), eq (hostPassword ), eq (hostPrivateKey ));
@@ -399,9 +409,9 @@ public void testHandleAgentSSHEnabledConnectedAgent() {
399409 }
400410
401411 @ Test (expected = CloudRuntimeException .class )
402- public void testHandleAgentSSHDisabledNotConnectedAgent () {
412+ public void testHandleAgentSSHDisabledNotConnectedAgent () throws NoSuchFieldException , IllegalAccessException {
403413 when (host .getStatus ()).thenReturn (Status .Disconnected );
404- when ( configurationDao . getValue ( ResourceManager .KvmSshToAgentEnabled . key ())). thenReturn ( "false" );
414+ overrideDefaultConfigValue ( ResourceManager .KvmSshToAgentEnabled , "_defaultValue" , "false" );
405415 resourceManager .handleAgentIfNotConnected (host , false );
406416 }
407417
0 commit comments