def testParseIpCommandOutput(self): # IPv4-only, fake loopback interface tests = [ "ip-addr-show-lo-ipv4.txt", "ip-addr-show-lo-oneline-ipv4.txt" ] for test_file in tests: data = testutils.ReadTestData(test_file) addr = netutils._GetIpAddressesFromIpOutput(data) self.failUnless( len(addr[4]) == 1 and addr[4][0] == "127.0.0.1" and not addr[6]) # IPv6-only, fake loopback interface tests = ["ip-addr-show-lo-ipv6.txt", "ip-addr-show-lo-ipv6.txt"] for test_file in tests: data = testutils.ReadTestData(test_file) addr = netutils._GetIpAddressesFromIpOutput(data) self.failUnless( len(addr[6]) == 1 and addr[6][0] == "::1" and not addr[4]) # IPv4 and IPv6, fake loopback interface tests = ["ip-addr-show-lo.txt", "ip-addr-show-lo-oneline.txt"] for test_file in tests: data = testutils.ReadTestData(test_file) addr = netutils._GetIpAddressesFromIpOutput(data) self.failUnless( len(addr[6]) == 1 and addr[6][0] == "::1" and len(addr[4]) == 1 and addr[4][0] == "127.0.0.1") # IPv4 and IPv6, dummy interface data = testutils.ReadTestData("ip-addr-show-dummy0.txt") addr = netutils._GetIpAddressesFromIpOutput(data) self.failUnless( len(addr[6]) == 1 and addr[6][0] == "2001:db8:85a3::8a2e:370:7334" and len(addr[4]) == 1 and addr[4][0] == "192.0.2.1")
def testInvalidSpiceParameters(self): kvm_help_too_old = testutils.ReadTestData("kvm_0.9.1_help.txt") kvm_help_working = testutils.ReadTestData("kvm_1.1.2_help.txt") invalid_data = { constants.HV_KVM_SPICE_BIND: "0.0.0.0", constants.HV_VNC_BIND_ADDRESS: "0.0.0.0" } self.assertRaises(errors.HypervisorError, validation.validate_spice_parameters, invalid_data, kvm_help_working) invalid_data = { constants.HV_KVM_SPICE_BIND: "0.0.0.0", constants.HV_VNC_BIND_ADDRESS: None } self.assertRaises(errors.HypervisorError, validation.validate_spice_parameters, invalid_data, kvm_help_too_old) invalid_data = { constants.HV_KVM_SPICE_BIND: "invalid-interface0", constants.HV_VNC_BIND_ADDRESS: None } self.assertRaises(errors.HypervisorError, validation.validate_spice_parameters, invalid_data, kvm_help_working)
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 testParseVersion(self): parse = hv_kvm.KVMHypervisor._ParseKVMVersion help_112 = testutils.ReadTestData("kvm_1.1.2_help.txt") help_10 = testutils.ReadTestData("kvm_1.0_help.txt") help_01590 = testutils.ReadTestData("kvm_0.15.90_help.txt") help_0125 = testutils.ReadTestData("kvm_0.12.5_help.txt") help_091 = testutils.ReadTestData("kvm_0.9.1_help.txt") self.assertEqual(parse(help_112), ("1.1.2", 1, 1, 2)) self.assertEqual(parse(help_10), ("1.0", 1, 0, 0)) self.assertEqual(parse(help_01590), ("0.15.90", 0, 15, 90)) self.assertEqual(parse(help_0125), ("0.12.5", 0, 12, 5)) self.assertEqual(parse(help_091), ("0.9.1", 0, 9, 1))
def _GetNodeInfoCmd(self, fail, cmd): if cmd == [self.CMD, "info"]: if fail: return self._FailingCommand(cmd) else: output = testutils.ReadTestData("xen-xm-info-4.0.1.txt") elif cmd == [self.CMD, "list"]: if fail: self.fail("'xm list' shouldn't be called when 'xm info' failed") else: output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt") else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def testValidSpiceParameters(self): kvm_help_working = testutils.ReadTestData("kvm_1.1.2_help.txt") valid_data = { constants.HV_KVM_SPICE_BIND: "0.0.0.0", constants.HV_VNC_BIND_ADDRESS: None } self.assertTrue( validation.validate_spice_parameters(valid_data, kvm_help_working)) valid_data = { constants.HV_KVM_SPICE_BIND: "::", constants.HV_VNC_BIND_ADDRESS: None } self.assertTrue( validation.validate_spice_parameters(valid_data, kvm_help_working)) valid_data = { constants.HV_KVM_SPICE_BIND: "lo", constants.HV_VNC_BIND_ADDRESS: None } self.assertTrue( validation.validate_spice_parameters(valid_data, kvm_help_working))
def testVerifyCertificate(self): cert_pem = testutils.ReadTestData("cert1.pem") cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert_pem) # Not checking return value as this certificate is expired utils.VerifyX509Certificate(cert, 30, 7)
def _MigrateNonRunningInstCmd(self, cmd): if cmd == [self.CMD, "list"]: output = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt") else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def _XenList(self, cmd): self.assertEqual(cmd, [self.CMD, "list"]) # TODO: Use actual data from "xl" command output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt") return self._SuccessCommand(output, cmd)
def _MigrateInstanceCmd(self, cluster_name, instance_name, target, port, live, fail, cmd): if cmd == [self.CMD, "list"]: output = testutils.ReadTestData( "xen-xm-list-4.0.1-four-instances.txt") elif cmd[:2] == [self.CMD, "migrate"]: if self.CMD == constants.XEN_CMD_XM: args = ["-p", str(port)] if live: args.append("-l") elif self.CMD == constants.XEN_CMD_XL: args = [ "-s", constants.XL_SSH_CMD % cluster_name, "-C", utils.PathJoin(self.tmpdir, instance_name), ] else: self.fail("Unknown Xen command '%s'" % self.CMD) args.extend([instance_name, target]) self.assertEqual(cmd[2:], args) if fail: return self._FailingCommand(cmd) output = "" else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def test(self): data = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt") # Exclude node self.assertEqual(hv_xen._ParseInstanceList(data.splitlines(), False), []) # Include node result = hv_xen._ParseInstanceList(data.splitlines(), True) self.assertEqual(len(result), 1) self.assertEqual(len(result[0]), 6) # Name self.assertEqual(result[0][0], hv_xen._DOM0_NAME) # ID self.assertEqual(result[0][1], 0) # Memory self.assertEqual(result[0][2], 1023) # VCPUs self.assertEqual(result[0][3], 1) # State self.assertEqual(result[0][4], hv_base.HvInstanceState.RUNNING) # Time self.assertAlmostEqual(result[0][5], 121152.6)
def _MigrateInstTargetUnreachCmd(self, cmd): if cmd == [self.CMD, "list"]: output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt") else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def testInvalidMachineVersion(self): kvm_machine_output = testutils.ReadTestData("kvm_6.0.0_machine.txt") invalid_data = { constants.HV_KVM_MACHINE_VERSION: "some-invalid-machine-type" } self.assertRaises(errors.HypervisorError, validation.validate_machine_version, invalid_data, kvm_machine_output)
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 testParserDisk(self): """Test drbdsetup show parser for disk""" data = testutils.ReadTestData("bdev-drbd-disk.txt") result = drbd_info.DRBD83ShowInfo.GetDevInfo(data) self.failUnless( self._has_disk(result, "/dev/xenvg/test.data", "/dev/xenvg/test.meta"), "Wrong local disk info") self.failUnless( ("local_addr" not in result and "remote_addr" not in result), "Should not find network info")
def testParserNetIP4(self): """Test drbdsetup show parser for IPv4 network""" data = testutils.ReadTestData("bdev-drbd-net-ip4.txt") result = drbd_info.DRBD83ShowInfo.GetDevInfo(data) self.failUnless(("local_dev" not in result and "meta_dev" not in result and "meta_index" not in result), "Should not find local disk info") self.failUnless( self._has_net(result, ("192.0.2.1", 11002), ("192.0.2.2", 11002)), "Wrong network info (IPv4)")
def testParser84(self): """Test drbdsetup show parser for disk and network version 8.4""" data = testutils.ReadTestData("bdev-drbd-8.4.txt") result = drbd_info.DRBD84ShowInfo.GetDevInfo(data) self.failUnless( self._has_disk(result, "/dev/xenvg/test.data", "/dev/xenvg/test.meta"), "Wrong local disk info") self.failUnless( self._has_net(result, ("192.0.2.1", 11000), ("192.0.2.2", 11000)), "Wrong network info (8.4.x)")
def testPrepareRemoteExport(self, utils): utils.ReadOneLineFile.return_value = "cluster_secret" inst = self.cfg.AddNewInstance() self.rpc.call_x509_cert_create.return_value = \ self.RpcResultsBuilder() \ .CreateSuccessfulNodeResult(inst.primary_node, ("key_name", testutils.ReadTestData("cert1.pem"))) op = opcodes.OpBackupPrepare(instance_name=inst.name, mode=constants.EXPORT_MODE_REMOTE) self.ExecOpCode(op)
def testBasicInfo(self): data = testutils.ReadTestData("xen-xm-info-4.0.1.txt") result = hv_xen._ParseNodeInfo(data) self.assertEqual(result, { "cpu_nodes": 1, "cpu_sockets": 2, "cpu_total": 4, "hv_version": (4, 0), "memory_free": 8004, "memory_total": 16378, })
def testParserNetIP6(self): """Test drbdsetup show parser for IPv6 network""" data = testutils.ReadTestData("bdev-drbd-net-ip6.txt") result = drbd_info.DRBD83ShowInfo.GetDevInfo(data) self.failUnless(("local_dev" not in result and "meta_dev" not in result and "meta_index" not in result), "Should not find local disk info") self.failUnless( self._has_net(result, ("2001:db8:65::1", 11048), ("2001:db8:66::1", 11048)), "Wrong network info (IPv6)")
def _StartInstanceCommand(self, inst, paused, failcreate, cmd): if cmd == [self.CMD, "info"]: output = testutils.ReadTestData("xen-xm-info-4.0.1.txt") elif cmd == [self.CMD, "list"]: output = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt") elif cmd[:2] == [self.CMD, "create"]: args = cmd[2:] cfgfile = utils.PathJoin(self.tmpdir, inst.name) if paused: self.assertEqual(args, ["-p", cfgfile]) else: self.assertEqual(args, [cfgfile]) if failcreate: return self._FailingCommand(cmd) output = "" else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def RunCmd(cmd, **kwargs): if '--help' in cmd: return mock.Mock( failed=False, output=testutils.ReadTestData("kvm_1.1.2_help.txt")) if '-S' in cmd: self.mocks['pid_alive'].return_value = ('file', -1, True) return mock.Mock(failed=False) elif '-M' in cmd: return mock.Mock(failed=False, output='') elif '-device' in cmd: return mock.Mock(failed=False, output='name "virtio-blk-pci"') else: raise errors.ProgrammerError('Unexpected command: %s' % cmd)
def testParser84NoDiskParams(self): """Test drbdsetup show parser for 8.4 without disk params The missing disk parameters occur after re-attaching a local disk but before setting the disk params. """ data = testutils.ReadTestData("bdev-drbd-8.4-no-disk-params.txt") result = drbd_info.DRBD84ShowInfo.GetDevInfo(data) self.failUnless(self._has_disk(result, "/dev/xenvg/test.data", "/dev/xenvg/test.meta", meta_index=None), "Wrong local disk info") self.failUnless(self._has_net(result, ("192.0.2.1", 11000), ("192.0.2.2", 11000)), "Wrong network info (8.4.x)")
def testWithNodeInfo(self): info = testutils.ReadTestData("xen-xm-info-4.0.1.txt") instance_list = self._FakeXmList(True) result = hv_xen._GetNodeInfo(info, instance_list) self.assertEqual(result, { "cpu_nodes": 1, "cpu_sockets": 2, "cpu_total": 4, "cpu_dom0": 7, "hv_version": (4, 0), "memory_dom0": 4096, "memory_free": 8004, "memory_hv": 2230, "memory_total": 16378, })
def testOmitCrashed(self): data = testutils.ReadTestData("xen-xl-list-4.4-crashed-instances.txt") fn = testutils.CallCounter(compat.partial(self._Success, data)) result = hv_xen._GetAllInstanceList(fn, True, delays=(0.02, 1.0, 0.03), timeout=0.1) self.assertEqual(len(result), 2) self.assertEqual(map(compat.fst, result), [ "Domain-0", "server01.example.com", ]) self.assertEqual(fn.Count(), 1)
def testSuccess(self): data = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt") fn = testutils.CallCounter(compat.partial(self._Success, data)) result = hv_xen._GetRunningInstanceList(fn, True, _timeout=0.1) self.assertEqual(len(result), 4) self.assertEqual(map(compat.fst, result), [ "Domain-0", "server01.example.com", "web3106215069.example.com", "testinstance.example.com", ]) self.assertEqual(fn.Count(), 1)
def _MigrateInstanceCmd(self, instance_name, target, port, fail, cmd): if cmd == [self.CMD, "list"]: output = testutils.ReadTestData( "xen-xl-list-4.0.1-four-instances.txt") elif cmd[:2] == [self.CMD, "migrate"]: args = [ "-s", constants.XL_SOCAT_CMD % (target, port), "-C", utils.PathJoin(self.tmpdir, instance_name), ] args.extend([instance_name, target]) self.assertEqual(cmd[2:], args) if fail: return self._FailingCommand(cmd) output = "" else: self.fail("Unhandled command: %s" % (cmd, )) return self._SuccessCommand(output, cmd)
def testBootRe(self): """Check _BOOT_RE It has too match -drive.*boot=on|off except if there is another dash-option at the beginning of the line. """ boot_re = hv_kvm.KVMHypervisor._BOOT_RE help_112 = testutils.ReadTestData("kvm_1.1.2_help.txt") help_10 = testutils.ReadTestData("kvm_1.0_help.txt") help_01590 = testutils.ReadTestData("kvm_0.15.90_help.txt") help_0125 = testutils.ReadTestData("kvm_0.12.5_help.txt") help_091 = testutils.ReadTestData("kvm_0.9.1_help.txt") help_091_fake = testutils.ReadTestData("kvm_0.9.1_help_boot_test.txt") self.assertTrue(boot_re.search(help_091)) self.assertTrue(boot_re.search(help_0125)) self.assertFalse(boot_re.search(help_091_fake)) self.assertFalse(boot_re.search(help_112)) self.assertFalse(boot_re.search(help_10)) self.assertFalse(boot_re.search(help_01590))
def _GetRuntime(cls): data = testutils.ReadTestData("kvm_runtime.json") return hv_kvm._AnalyzeSerializedRuntime(data)
def setUp(self): """Set up input data""" testutils.GanetiTestCase.setUp(self) self.plain_output_old_ok = \ testutils.ReadTestData("bdev-rbd/plain_output_old_ok.txt") self.plain_output_old_no_matches = \ testutils.ReadTestData("bdev-rbd/plain_output_old_no_matches.txt") self.plain_output_old_extra_matches = \ testutils.ReadTestData("bdev-rbd/plain_output_old_extra_matches.txt") self.plain_output_old_empty = \ testutils.ReadTestData("bdev-rbd/plain_output_old_empty.txt") self.plain_output_new_ok = \ testutils.ReadTestData("bdev-rbd/plain_output_new_ok.txt") self.plain_output_new_no_matches = \ testutils.ReadTestData("bdev-rbd/plain_output_new_no_matches.txt") self.plain_output_new_extra_matches = \ testutils.ReadTestData("bdev-rbd/plain_output_new_extra_matches.txt") # This file is completely empty, and as such it's not shipped. self.plain_output_new_empty = "" self.json_output_ok = testutils.ReadTestData( "bdev-rbd/json_output_ok.txt") self.json_output_no_matches = \ testutils.ReadTestData("bdev-rbd/json_output_no_matches.txt") self.json_output_extra_matches = \ testutils.ReadTestData("bdev-rbd/json_output_extra_matches.txt") self.json_output_empty = \ testutils.ReadTestData("bdev-rbd/json_output_empty.txt") self.output_invalid = testutils.ReadTestData( "bdev-rbd/output_invalid.txt") self.volume_name = "d7ab910a-4933-4ffe-88d0-faf2ce31390a.rbd.disk0" self.test_unique_id = ("rbd", self.volume_name) self.test_params = {constants.LDP_POOL: "fake_pool"}