mirror of https://github.com/apache/cloudstack.git
Fixing the testGetHostStatsCommand test under LibvirtComputingResourceTest. - Removed the expected value from the Test annotation - Mocking the bash path in order to avoid environment/OS issues
Fixing typo on LibvirtRequestWrapper - Replace linbvirtCommands by libvirtCommands on LibvirtRequestWrapper Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com> This closes #255
This commit is contained in:
parent
9d8a62d0ee
commit
0cdb4b6108
|
|
@ -194,6 +194,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
|
|||
public static final String SSHPRVKEYPATH = SSHKEYSPATH + File.separator + "id_rsa.cloud";
|
||||
public static final String SSHPUBKEYPATH = SSHKEYSPATH + File.separator + "id_rsa.pub.cloud";
|
||||
|
||||
public static final String BASH_SCRIPT_PATH = "/bin/bash";
|
||||
|
||||
private String _mountPoint = "/mnt";
|
||||
private StorageLayer _storage;
|
||||
private KVMStoragePoolManager _storagePoolMgr;
|
||||
|
|
|
|||
|
|
@ -37,7 +37,11 @@ public final class LibvirtGetHostStatsCommandWrapper extends CommandWrapper<GetH
|
|||
|
||||
@Override
|
||||
public Answer execute(final GetHostStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
|
||||
final Script cpuScript = new Script("/bin/bash", s_logger);
|
||||
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
|
||||
final String bashScriptPath = libvirtUtilitiesHelper.retrieveBashScriptPath();
|
||||
|
||||
|
||||
final Script cpuScript = new Script(bashScriptPath, s_logger);
|
||||
cpuScript.add("-c");
|
||||
cpuScript.add("idle=$(top -b -n 1| awk -F, '/^[%]*[Cc]pu/{$0=$4; gsub(/[^0-9.,]+/,\"\"); print }'); echo $idle");
|
||||
|
||||
|
|
@ -50,7 +54,7 @@ public final class LibvirtGetHostStatsCommandWrapper extends CommandWrapper<GetH
|
|||
final double cpuUtil = 100.0D - Double.parseDouble(parser.getLine());
|
||||
|
||||
long freeMem = 0;
|
||||
final Script memScript = new Script("/bin/bash", s_logger);
|
||||
final Script memScript = new Script(bashScriptPath, s_logger);
|
||||
memScript.add("-c");
|
||||
memScript.add("freeMem=$(free|grep cache:|awk '{print $4}');echo $freeMem");
|
||||
final OutputInterpreter.OneLineParser Memparser = new OutputInterpreter.OneLineParser();
|
||||
|
|
@ -61,7 +65,7 @@ public final class LibvirtGetHostStatsCommandWrapper extends CommandWrapper<GetH
|
|||
}
|
||||
freeMem = Long.parseLong(Memparser.getLine());
|
||||
|
||||
final Script totalMem = new Script("/bin/bash", s_logger);
|
||||
final Script totalMem = new Script(bashScriptPath, s_logger);
|
||||
totalMem.add("-c");
|
||||
totalMem.add("free|grep Mem:|awk '{print $2}'");
|
||||
final OutputInterpreter.OneLineParser totMemparser = new OutputInterpreter.OneLineParser();
|
||||
|
|
|
|||
|
|
@ -99,66 +99,66 @@ public class LibvirtRequestWrapper extends RequestWrapper {
|
|||
@SuppressWarnings("rawtypes")
|
||||
private void init() {
|
||||
// LibvirtComputingResource commands
|
||||
final Hashtable<Class<? extends Command>, CommandWrapper> linbvirtCommands = new Hashtable<Class<? extends Command>, CommandWrapper>();
|
||||
final Hashtable<Class<? extends Command>, CommandWrapper> libvirtCommands = new Hashtable<Class<? extends Command>, CommandWrapper>();
|
||||
|
||||
linbvirtCommands.put(StopCommand.class, new LibvirtStopCommandWrapper());
|
||||
linbvirtCommands.put(GetVmStatsCommand.class, new LibvirtGetVmStatsCommandWrapper());
|
||||
linbvirtCommands.put(GetVmDiskStatsCommand.class, new LibvirtGetVmDiskStatsCommandWrapper());
|
||||
linbvirtCommands.put(RebootRouterCommand.class, new LibvirtRebootRouterCommandWrapper());
|
||||
linbvirtCommands.put(RebootCommand.class, new LibvirtRebootCommandWrapper());
|
||||
linbvirtCommands.put(GetHostStatsCommand.class, new LibvirtGetHostStatsCommandWrapper());
|
||||
linbvirtCommands.put(CheckHealthCommand.class, new LibvirtCheckHealthCommandWrapper());
|
||||
linbvirtCommands.put(PrepareForMigrationCommand.class, new LibvirtPrepareForMigrationCommandWrapper());
|
||||
linbvirtCommands.put(MigrateCommand.class, new LibvirtMigrateCommandWrapper());
|
||||
linbvirtCommands.put(PingTestCommand.class, new LibvirtPingTestCommandWrapper());
|
||||
linbvirtCommands.put(CheckVirtualMachineCommand.class, new LibvirtCheckVirtualMachineCommandWrapper());
|
||||
linbvirtCommands.put(ReadyCommand.class, new LibvirtReadyCommandWrapper());
|
||||
linbvirtCommands.put(AttachIsoCommand.class, new LibvirtAttachIsoCommandWrapper());
|
||||
linbvirtCommands.put(AttachVolumeCommand.class, new LibvirtAttachVolumeCommandWrapper());
|
||||
linbvirtCommands.put(WatchConsoleProxyLoadCommand.class, new LibvirtWatchConsoleProxyLoadCommandWrapper());
|
||||
linbvirtCommands.put(CheckConsoleProxyLoadCommand.class, new LibvirtCheckConsoleProxyLoadCommandWrapper());
|
||||
linbvirtCommands.put(GetVncPortCommand.class, new LibvirtGetVncPortCommandWrapper());
|
||||
linbvirtCommands.put(ModifySshKeysCommand.class, new LibvirtModifySshKeysCommandWrapper());
|
||||
linbvirtCommands.put(MaintainCommand.class, new LibvirtMaintainCommandWrapper());
|
||||
linbvirtCommands.put(CreateCommand.class, new LibvirtCreateCommandWrapper());
|
||||
linbvirtCommands.put(DestroyCommand.class, new LibvirtDestroyCommandWrapper());
|
||||
linbvirtCommands.put(PrimaryStorageDownloadCommand.class, new LibvirtPrimaryStorageDownloadCommandWrapper());
|
||||
linbvirtCommands.put(GetStorageStatsCommand.class, new LibvirtGetStorageStatsCommandWrapper());
|
||||
linbvirtCommands.put(UpgradeSnapshotCommand.class, new LibvirtUpgradeSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(DeleteStoragePoolCommand.class, new LibvirtDeleteStoragePoolCommandWrapper());
|
||||
linbvirtCommands.put(OvsSetupBridgeCommand.class, new LibvirtOvsSetupBridgeCommandWrapper());
|
||||
linbvirtCommands.put(OvsDestroyBridgeCommand.class, new LibvirtOvsDestroyBridgeCommandWrapper());
|
||||
linbvirtCommands.put(OvsFetchInterfaceCommand.class, new LibvirtOvsFetchInterfaceCommandWrapper());
|
||||
linbvirtCommands.put(OvsVpcPhysicalTopologyConfigCommand.class, new LibvirtOvsVpcPhysicalTopologyConfigCommandWrapper());
|
||||
linbvirtCommands.put(OvsVpcRoutingPolicyConfigCommand.class, new LibvirtOvsVpcRoutingPolicyConfigCommandWrapper());
|
||||
linbvirtCommands.put(CreateStoragePoolCommand.class, new LibvirtCreateStoragePoolCommandWrapper());
|
||||
linbvirtCommands.put(ModifyStoragePoolCommand.class, new LibvirtModifyStoragePoolCommandWrapper());
|
||||
linbvirtCommands.put(CleanupNetworkRulesCmd.class, new LibvirtCleanupNetworkRulesCommandWrapper());
|
||||
linbvirtCommands.put(NetworkRulesVmSecondaryIpCommand.class, new LibvirtNetworkRulesVmSecondaryIpCommandWrapper());
|
||||
linbvirtCommands.put(NetworkRulesSystemVmCommand.class, new LibvirtNetworkRulesSystemVmCommandWrapper());
|
||||
linbvirtCommands.put(CheckSshCommand.class, new LibvirtCheckSshCommandWrapper());
|
||||
linbvirtCommands.put(CheckNetworkCommand.class, new LibvirtCheckNetworkCommandWrapper());
|
||||
linbvirtCommands.put(OvsDestroyTunnelCommand.class, new LibvirtOvsDestroyTunnelCommandWrapper());
|
||||
linbvirtCommands.put(CheckOnHostCommand.class, new LibvirtCheckOnHostCommandWrapper());
|
||||
linbvirtCommands.put(OvsCreateTunnelCommand.class, new LibvirtOvsCreateTunnelCommandWrapper());
|
||||
linbvirtCommands.put(CreateVolumeFromSnapshotCommand.class, new LibvirtCreateVolumeFromSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(FenceCommand.class, new LibvirtFenceCommandWrapper());
|
||||
linbvirtCommands.put(SecurityGroupRulesCmd.class, new LibvirtSecurityGroupRulesCommandWrapper());
|
||||
linbvirtCommands.put(PlugNicCommand.class, new LibvirtPlugNicCommandWrapper());
|
||||
linbvirtCommands.put(UnPlugNicCommand.class, new LibvirtUnPlugNicCommandWrapper());
|
||||
linbvirtCommands.put(NetworkUsageCommand.class, new LibvirtNetworkUsageCommandWrapper());
|
||||
linbvirtCommands.put(CreatePrivateTemplateFromVolumeCommand.class, new LibvirtCreatePrivateTemplateFromVolumeCommandWrapper());
|
||||
linbvirtCommands.put(ManageSnapshotCommand.class, new LibvirtManageSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(BackupSnapshotCommand.class, new LibvirtBackupSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(CreatePrivateTemplateFromSnapshotCommand.class, new LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper());
|
||||
linbvirtCommands.put(CopyVolumeCommand.class, new LibvirtCopyVolumeCommandWrapper());
|
||||
linbvirtCommands.put(PvlanSetupCommand.class, new LibvirtPvlanSetupCommandWrapper());
|
||||
linbvirtCommands.put(ResizeVolumeCommand.class, new LibvirtResizeVolumeCommandWrapper());
|
||||
linbvirtCommands.put(NetworkElementCommand.class, new LibvirtNetworkElementCommandWrapper());
|
||||
linbvirtCommands.put(StorageSubSystemCommand.class, new LibvirtStorageSubSystemCommandWrapper());
|
||||
linbvirtCommands.put(StartCommand.class, new LibvirtStartCommandWrapper());
|
||||
libvirtCommands.put(StopCommand.class, new LibvirtStopCommandWrapper());
|
||||
libvirtCommands.put(GetVmStatsCommand.class, new LibvirtGetVmStatsCommandWrapper());
|
||||
libvirtCommands.put(GetVmDiskStatsCommand.class, new LibvirtGetVmDiskStatsCommandWrapper());
|
||||
libvirtCommands.put(RebootRouterCommand.class, new LibvirtRebootRouterCommandWrapper());
|
||||
libvirtCommands.put(RebootCommand.class, new LibvirtRebootCommandWrapper());
|
||||
libvirtCommands.put(GetHostStatsCommand.class, new LibvirtGetHostStatsCommandWrapper());
|
||||
libvirtCommands.put(CheckHealthCommand.class, new LibvirtCheckHealthCommandWrapper());
|
||||
libvirtCommands.put(PrepareForMigrationCommand.class, new LibvirtPrepareForMigrationCommandWrapper());
|
||||
libvirtCommands.put(MigrateCommand.class, new LibvirtMigrateCommandWrapper());
|
||||
libvirtCommands.put(PingTestCommand.class, new LibvirtPingTestCommandWrapper());
|
||||
libvirtCommands.put(CheckVirtualMachineCommand.class, new LibvirtCheckVirtualMachineCommandWrapper());
|
||||
libvirtCommands.put(ReadyCommand.class, new LibvirtReadyCommandWrapper());
|
||||
libvirtCommands.put(AttachIsoCommand.class, new LibvirtAttachIsoCommandWrapper());
|
||||
libvirtCommands.put(AttachVolumeCommand.class, new LibvirtAttachVolumeCommandWrapper());
|
||||
libvirtCommands.put(WatchConsoleProxyLoadCommand.class, new LibvirtWatchConsoleProxyLoadCommandWrapper());
|
||||
libvirtCommands.put(CheckConsoleProxyLoadCommand.class, new LibvirtCheckConsoleProxyLoadCommandWrapper());
|
||||
libvirtCommands.put(GetVncPortCommand.class, new LibvirtGetVncPortCommandWrapper());
|
||||
libvirtCommands.put(ModifySshKeysCommand.class, new LibvirtModifySshKeysCommandWrapper());
|
||||
libvirtCommands.put(MaintainCommand.class, new LibvirtMaintainCommandWrapper());
|
||||
libvirtCommands.put(CreateCommand.class, new LibvirtCreateCommandWrapper());
|
||||
libvirtCommands.put(DestroyCommand.class, new LibvirtDestroyCommandWrapper());
|
||||
libvirtCommands.put(PrimaryStorageDownloadCommand.class, new LibvirtPrimaryStorageDownloadCommandWrapper());
|
||||
libvirtCommands.put(GetStorageStatsCommand.class, new LibvirtGetStorageStatsCommandWrapper());
|
||||
libvirtCommands.put(UpgradeSnapshotCommand.class, new LibvirtUpgradeSnapshotCommandWrapper());
|
||||
libvirtCommands.put(DeleteStoragePoolCommand.class, new LibvirtDeleteStoragePoolCommandWrapper());
|
||||
libvirtCommands.put(OvsSetupBridgeCommand.class, new LibvirtOvsSetupBridgeCommandWrapper());
|
||||
libvirtCommands.put(OvsDestroyBridgeCommand.class, new LibvirtOvsDestroyBridgeCommandWrapper());
|
||||
libvirtCommands.put(OvsFetchInterfaceCommand.class, new LibvirtOvsFetchInterfaceCommandWrapper());
|
||||
libvirtCommands.put(OvsVpcPhysicalTopologyConfigCommand.class, new LibvirtOvsVpcPhysicalTopologyConfigCommandWrapper());
|
||||
libvirtCommands.put(OvsVpcRoutingPolicyConfigCommand.class, new LibvirtOvsVpcRoutingPolicyConfigCommandWrapper());
|
||||
libvirtCommands.put(CreateStoragePoolCommand.class, new LibvirtCreateStoragePoolCommandWrapper());
|
||||
libvirtCommands.put(ModifyStoragePoolCommand.class, new LibvirtModifyStoragePoolCommandWrapper());
|
||||
libvirtCommands.put(CleanupNetworkRulesCmd.class, new LibvirtCleanupNetworkRulesCommandWrapper());
|
||||
libvirtCommands.put(NetworkRulesVmSecondaryIpCommand.class, new LibvirtNetworkRulesVmSecondaryIpCommandWrapper());
|
||||
libvirtCommands.put(NetworkRulesSystemVmCommand.class, new LibvirtNetworkRulesSystemVmCommandWrapper());
|
||||
libvirtCommands.put(CheckSshCommand.class, new LibvirtCheckSshCommandWrapper());
|
||||
libvirtCommands.put(CheckNetworkCommand.class, new LibvirtCheckNetworkCommandWrapper());
|
||||
libvirtCommands.put(OvsDestroyTunnelCommand.class, new LibvirtOvsDestroyTunnelCommandWrapper());
|
||||
libvirtCommands.put(CheckOnHostCommand.class, new LibvirtCheckOnHostCommandWrapper());
|
||||
libvirtCommands.put(OvsCreateTunnelCommand.class, new LibvirtOvsCreateTunnelCommandWrapper());
|
||||
libvirtCommands.put(CreateVolumeFromSnapshotCommand.class, new LibvirtCreateVolumeFromSnapshotCommandWrapper());
|
||||
libvirtCommands.put(FenceCommand.class, new LibvirtFenceCommandWrapper());
|
||||
libvirtCommands.put(SecurityGroupRulesCmd.class, new LibvirtSecurityGroupRulesCommandWrapper());
|
||||
libvirtCommands.put(PlugNicCommand.class, new LibvirtPlugNicCommandWrapper());
|
||||
libvirtCommands.put(UnPlugNicCommand.class, new LibvirtUnPlugNicCommandWrapper());
|
||||
libvirtCommands.put(NetworkUsageCommand.class, new LibvirtNetworkUsageCommandWrapper());
|
||||
libvirtCommands.put(CreatePrivateTemplateFromVolumeCommand.class, new LibvirtCreatePrivateTemplateFromVolumeCommandWrapper());
|
||||
libvirtCommands.put(ManageSnapshotCommand.class, new LibvirtManageSnapshotCommandWrapper());
|
||||
libvirtCommands.put(BackupSnapshotCommand.class, new LibvirtBackupSnapshotCommandWrapper());
|
||||
libvirtCommands.put(CreatePrivateTemplateFromSnapshotCommand.class, new LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper());
|
||||
libvirtCommands.put(CopyVolumeCommand.class, new LibvirtCopyVolumeCommandWrapper());
|
||||
libvirtCommands.put(PvlanSetupCommand.class, new LibvirtPvlanSetupCommandWrapper());
|
||||
libvirtCommands.put(ResizeVolumeCommand.class, new LibvirtResizeVolumeCommandWrapper());
|
||||
libvirtCommands.put(NetworkElementCommand.class, new LibvirtNetworkElementCommandWrapper());
|
||||
libvirtCommands.put(StorageSubSystemCommand.class, new LibvirtStorageSubSystemCommandWrapper());
|
||||
libvirtCommands.put(StartCommand.class, new LibvirtStartCommandWrapper());
|
||||
|
||||
resources.put(LibvirtComputingResource.class, linbvirtCommands);
|
||||
resources.put(LibvirtComputingResource.class, libvirtCommands);
|
||||
}
|
||||
|
||||
public static LibvirtRequestWrapper getInstance() {
|
||||
|
|
|
|||
|
|
@ -82,4 +82,8 @@ public class LibvirtUtilitiesHelper {
|
|||
public String retrieveSshPrvKeyPath() {
|
||||
return LibvirtComputingResource.SSHPRVKEYPATH;
|
||||
}
|
||||
|
||||
public String retrieveBashScriptPath() {
|
||||
return LibvirtComputingResource.BASH_SCRIPT_PATH;
|
||||
}
|
||||
}
|
||||
|
|
@ -916,19 +916,28 @@ public class LibvirtComputingResourceTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test(expected = NumberFormatException.class)
|
||||
@Test
|
||||
public void testGetHostStatsCommand() {
|
||||
// A bit difficult to test due to the logger being passed and the parser itself relying on the connection.
|
||||
// Have to spend some more time afterwards in order to refactor the wrapper itself.
|
||||
|
||||
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
|
||||
final String bashScriptPath = "/path";
|
||||
|
||||
final String uuid = "e8d6b4d0-bc6d-4613-b8bb-cb9e0600f3c6";
|
||||
final GetHostStatsCommand command = new GetHostStatsCommand(uuid, "summer", 1l);
|
||||
|
||||
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
|
||||
when(libvirtUtilitiesHelper.retrieveBashScriptPath()).thenReturn(bashScriptPath);
|
||||
|
||||
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
|
||||
assertNotNull(wrapper);
|
||||
|
||||
final Answer answer = wrapper.execute(command, libvirtComputingResource);
|
||||
assertFalse(answer.getResult());
|
||||
|
||||
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
|
||||
verify(libvirtUtilitiesHelper, times(1)).retrieveBashScriptPath();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue