def testOldVersion(self): lvmvg = container.LvmVgStorage() stdout = testutils.ReadTestData("vgreduce-removemissing-2.02.02.txt") vgs_fail = testutils.ReadTestData("vgs-missing-pvs-2.02.02.txt") self.run_history = [ ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME], utils.RunResult(0, None, stdout, "", "", None, None)), ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME], utils.RunResult(0, None, "", "", "", None, None)), ] lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd) self.assertEqual(self.run_history, []) for ecode, out in [(1, ""), (0, vgs_fail)]: self.run_history = [ ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME], utils.RunResult(0, None, stdout, "", "", None, None)), ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME], utils.RunResult(ecode, None, out, "", "", None, None)), ] self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME, _runcmd_fn=self._runCmd) self.assertEqual(self.run_history, [])
def testBinaryNotFound(self): RESULT_FAILED = utils.RunResult(1, None, "", "", "", None, None) mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) result = hv._CheckToolstackBinary("xl") self.assertFalse(result)
def _FakeRunCmd(success, stdout): if success: exit_code = 0 else: exit_code = 1 return lambda cmd: utils.RunResult(exit_code, None, stdout, "", cmd, utils.process._TIMEOUT_NONE, 5)
def fn(args, env=NotImplemented, reset_env=NotImplemented, postfork_fn=NotImplemented): self.assertEqual(args, [utils.PathJoin(self.tmpdir, "test3079")]) self.assertEqual(env, {}) self.assertTrue(reset_env) self.assertTrue(callable(postfork_fn)) trylock = utils.FileLock.Open(lockfile) try: # See if lockfile is still held self.assertRaises(EnvironmentError, trylock.Exclusive, blocking=False) # Call back to release lock postfork_fn(NotImplemented) # See if lockfile can be acquired trylock.Exclusive(blocking=False) finally: trylock.Close() # Simulate a failed command return utils.RunResult(constants.EXIT_FAILURE, None, "stdout", "stderr406328567", utils.ShellQuoteArgs(args), NotImplemented, NotImplemented)
def testCheckToolstackXlConfigured(self): RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) mock_run_cmd = mock.Mock(return_value=RESULT_OK) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) result = hv._CheckToolstackXlConfigured() self.assertTrue(result)
def testVerifyFailing(self): result_failed = utils.RunResult(1, None, "", "", "", None, None) mock_run_cmd = mock.Mock(return_value=result_failed) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) hv._CheckToolstack = mock.Mock(return_value=True) result = hv.Verify() self.assertTrue(result is not None)
def testCheckToolstackXlFails(self): RESULT_FAILED = utils.RunResult( 1, None, "", "ERROR: The pink bunny hid the binary.", "", None, None) mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) self.assertRaises(errors.HypervisorError, hv._CheckToolstackXlConfigured)
def _RunCmd(self, fail, cmd, interactive=NotImplemented): self.assertTrue(interactive) self.assertEqual(cmd, [pathutils.DAEMON_UTIL, "reload-ssh-keys"]) if fail: exit_code = constants.EXIT_FAILURE else: exit_code = constants.EXIT_SUCCESS return utils.RunResult(exit_code, None, "stdout", "stderr", utils.ShellQuoteArgs(cmd), NotImplemented, NotImplemented)
def testCheckToolstackXlNotConfigured(self): RESULT_FAILED = utils.RunResult( 1, None, "", "ERROR: A different toolstack (xm) has been selected!", "", None, None) mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) result = hv._CheckToolstackXlConfigured() self.assertFalse(result)
class TestXenHypervisorListInstances(unittest.TestCase): RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) XEN_LIST = "list" def testHvparamsXl(self): expected_xen_cmd = "xl" hvparams = {} mock_run_cmd = mock.Mock(return_value=self.RESULT_OK) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) hv.ListInstances(hvparams=hvparams) mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST])
def fn(args, env=NotImplemented, reset_env=NotImplemented, postfork_fn=NotImplemented): self.assertEqual(args, [utils.PathJoin(self.tmpdir, "test5667")]) self.assertEqual(env, {}) self.assertTrue(reset_env) # Call back to release lock postfork_fn(NotImplemented) # Simulate a successful command return utils.RunResult(constants.EXIT_SUCCESS, None, "stdout14463", "", utils.ShellQuoteArgs(args), NotImplemented, NotImplemented)
def testNewVersion(self): lvmvg = container.LvmVgStorage() stdout1 = testutils.ReadTestData( "vgreduce-removemissing-2.02.66-fail.txt") stdout2 = testutils.ReadTestData( "vgreduce-removemissing-2.02.66-ok.txt") vgs_fail = testutils.ReadTestData("vgs-missing-pvs-2.02.66.txt") # first: require --fail, check that it's used self.run_history = [ ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME], utils.RunResult(0, None, stdout1, "", "", None, None)), ([self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME], utils.RunResult(0, None, stdout2, "", "", None, None)), ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME], utils.RunResult(0, None, "", "", "", None, None)), ] lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd) self.assertEqual(self.run_history, []) # second: make sure --fail is not used if not needed self.run_history = [ ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME], utils.RunResult(0, None, stdout2, "", "", None, None)), ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME], utils.RunResult(0, None, "", "", "", None, None)), ] lvmvg._RemoveMissing(self.VGNAME, _runcmd_fn=self._runCmd) self.assertEqual(self.run_history, []) # third: make sure we error out if vgs doesn't find the volume for ecode, out in [(1, ""), (0, vgs_fail)]: self.run_history = [ ([self.VGREDUCE_CMD, "--removemissing", self.VGNAME], utils.RunResult(0, None, stdout1, "", "", None, None)), ([ self.VGREDUCE_CMD, "--removemissing", "--force", self.VGNAME ], utils.RunResult(0, None, stdout2, "", "", None, None)), ([self.LIST_CMD, "--noheadings", "--nosuffix", self.VGNAME], utils.RunResult(ecode, None, out, "", "", None, None)), ] self.assertRaises(errors.StorageError, lvmvg._RemoveMissing, self.VGNAME, _runcmd_fn=self._runCmd) self.assertEqual(self.run_history, [])
def testRADOSBlockDeviceImport(self, attach_mock, unmap_mock, run_cmd_mock): """Test for bdev.RADOSBlockDevice.Import()""" # Set up the mock objects return values attach_mock.return_value = True run_cmd_mock.return_value = \ utils.RunResult(0, None, "", "", "", utils.process._TIMEOUT_NONE, 0) # Create a fake rbd volume inst = bdev.RADOSBlockDevice(self.test_unique_id, [], 1024, self.test_params, {}) # Desired output command import_cmd = [ constants.RBD_CMD, "import", "-p", inst.rbd_pool, "-", inst.rbd_name ] self.assertEqual(inst.Import(), import_cmd)
class TestXenHypervisorGetInstanceList(unittest.TestCase): RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) XEN_LIST = "list" def testNoHvparams(self): expected_xen_cmd = "xm" mock_run_cmd = mock.Mock(return_value=self.RESULT_OK) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) self.assertRaises(errors.HypervisorError, hv._GetInstanceList, True, None) def testFromHvparams(self): expected_xen_cmd = "xl" hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} mock_run_cmd = mock.Mock(return_value=self.RESULT_OK) hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented, _run_cmd_fn=mock_run_cmd) hv._GetInstanceList(True, hvparams) mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST])
def _FakeRunCmd(self, cmd, interactive=None): self.assertTrue(interactive) self.assertTrue(isinstance(cmd, list)) self._cmds.append(cmd) return utils.RunResult(self._next_cmd_exitcode, None, "", "", "cmd", utils.process._TIMEOUT_NONE, 5)
def _FailingCommand(self, cmd): self.assertEqual(cmd[0], self.CMD) return utils.RunResult(constants.EXIT_FAILURE, None, "", "This command failed", None, NotImplemented, NotImplemented)
def _SuccessCommand(self, stdout, cmd): self.assertEqual(cmd[0], self.CMD) return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None, NotImplemented, NotImplemented)
def setUp(self): output = testutils.ReadTestData("xen-xm-info-4.0.1.txt") self._result_ok = utils.RunResult(0, None, output, "", "", None, None)
def testCommandVersionInvalid(self): utils.RunCmd.return_value = utils.RunResult(1, None, "", "", [], None, None) self.assertTrue(self.hvc._VerifyLXCCommands()) utils.RunCmd.return_value = RunResultOk("1.2a.0\n") self.assertTrue(self.hvc._VerifyLXCCommands())
def RunResultOk(stdout): return utils.RunResult(0, None, stdout, "", [], None, None)
def _Fail(self): return utils.RunResult(constants.EXIT_FAILURE, None, "stdout", "stderr", None, NotImplemented, NotImplemented)
def _Success(self, stdout): return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None, NotImplemented, NotImplemented)