root / test / py / ganeti.hypervisor.hv_xen_unittest.py @ 8ef418bb
History | View | Annotate | Download (30.9 kB)
1 | 55cc0a44 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 55cc0a44 | Michael Hanselmann | #
|
3 | 55cc0a44 | Michael Hanselmann | |
4 | b255379d | Michael Hanselmann | # Copyright (C) 2011, 2013 Google Inc.
|
5 | 55cc0a44 | Michael Hanselmann | #
|
6 | 55cc0a44 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 55cc0a44 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 55cc0a44 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 55cc0a44 | Michael Hanselmann | # (at your option) any later version.
|
10 | 55cc0a44 | Michael Hanselmann | #
|
11 | 55cc0a44 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 55cc0a44 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 55cc0a44 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 55cc0a44 | Michael Hanselmann | # General Public License for more details.
|
15 | 55cc0a44 | Michael Hanselmann | #
|
16 | 55cc0a44 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 55cc0a44 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 55cc0a44 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 55cc0a44 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 55cc0a44 | Michael Hanselmann | |
21 | 55cc0a44 | Michael Hanselmann | |
22 | b44af065 | Helga Velroyen | """Script for testing ganeti.hypervisor.hv_xen"""
|
23 | 55cc0a44 | Michael Hanselmann | |
24 | d0bb3f24 | Michael Hanselmann | import string # pylint: disable=W0402 |
25 | 55cc0a44 | Michael Hanselmann | import unittest |
26 | 64a66bd2 | Michael Hanselmann | import tempfile |
27 | 64a66bd2 | Michael Hanselmann | import shutil |
28 | 74a50c46 | Michael Hanselmann | import random |
29 | 7610d884 | Michael Hanselmann | import os |
30 | 58e356a9 | Helga Velroyen | import mock |
31 | 55cc0a44 | Michael Hanselmann | |
32 | 55cc0a44 | Michael Hanselmann | from ganeti import constants |
33 | 55cc0a44 | Michael Hanselmann | from ganeti import objects |
34 | 48bba9de | Balazs Lecz | from ganeti import pathutils |
35 | 55cc0a44 | Michael Hanselmann | from ganeti import hypervisor |
36 | b255379d | Michael Hanselmann | from ganeti import utils |
37 | b255379d | Michael Hanselmann | from ganeti import errors |
38 | b255379d | Michael Hanselmann | from ganeti import compat |
39 | 55cc0a44 | Michael Hanselmann | |
40 | 55cc0a44 | Michael Hanselmann | from ganeti.hypervisor import hv_xen |
41 | 55cc0a44 | Michael Hanselmann | |
42 | 55cc0a44 | Michael Hanselmann | import testutils |
43 | 55cc0a44 | Michael Hanselmann | |
44 | 55cc0a44 | Michael Hanselmann | |
45 | 74a50c46 | Michael Hanselmann | # Map from hypervisor class to hypervisor name
|
46 | 74a50c46 | Michael Hanselmann | HVCLASS_TO_HVNAME = utils.InvertDict(hypervisor._HYPERVISOR_MAP) |
47 | 74a50c46 | Michael Hanselmann | |
48 | 74a50c46 | Michael Hanselmann | |
49 | 55cc0a44 | Michael Hanselmann | class TestConsole(unittest.TestCase): |
50 | 55cc0a44 | Michael Hanselmann | def test(self): |
51 | 8ef418bb | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
52 | 8ef418bb | Helga Velroyen | for cls in [hv_xen.XenPvmHypervisor(), hv_xen.XenHvmHypervisor()]: |
53 | 55cc0a44 | Michael Hanselmann | instance = objects.Instance(name="xen.example.com",
|
54 | 55cc0a44 | Michael Hanselmann | primary_node="node24828")
|
55 | 8ef418bb | Helga Velroyen | cons = cls.GetInstanceConsole(instance, hvparams, {}) |
56 | 55cc0a44 | Michael Hanselmann | self.assertTrue(cons.Validate())
|
57 | 55cc0a44 | Michael Hanselmann | self.assertEqual(cons.kind, constants.CONS_SSH)
|
58 | 55cc0a44 | Michael Hanselmann | self.assertEqual(cons.host, instance.primary_node)
|
59 | 55cc0a44 | Michael Hanselmann | self.assertEqual(cons.command[-1], instance.name) |
60 | 55cc0a44 | Michael Hanselmann | |
61 | 55cc0a44 | Michael Hanselmann | |
62 | 347fa0f1 | Michael Hanselmann | class TestCreateConfigCpus(unittest.TestCase): |
63 | 347fa0f1 | Michael Hanselmann | def testEmpty(self): |
64 | 347fa0f1 | Michael Hanselmann | for cpu_mask in [None, ""]: |
65 | 347fa0f1 | Michael Hanselmann | self.assertEqual(hv_xen._CreateConfigCpus(cpu_mask),
|
66 | 347fa0f1 | Michael Hanselmann | "cpus = [ ]")
|
67 | 347fa0f1 | Michael Hanselmann | |
68 | 347fa0f1 | Michael Hanselmann | def testAll(self): |
69 | 347fa0f1 | Michael Hanselmann | self.assertEqual(hv_xen._CreateConfigCpus(constants.CPU_PINNING_ALL),
|
70 | 347fa0f1 | Michael Hanselmann | None)
|
71 | 347fa0f1 | Michael Hanselmann | |
72 | 347fa0f1 | Michael Hanselmann | def testOne(self): |
73 | 347fa0f1 | Michael Hanselmann | self.assertEqual(hv_xen._CreateConfigCpus("9"), "cpu = \"9\"") |
74 | 347fa0f1 | Michael Hanselmann | |
75 | 347fa0f1 | Michael Hanselmann | def testMultiple(self): |
76 | 347fa0f1 | Michael Hanselmann | self.assertEqual(hv_xen._CreateConfigCpus("0-2,4,5-5:3:all"), |
77 | 347fa0f1 | Michael Hanselmann | ("cpus = [ \"0,1,2,4,5\", \"3\", \"%s\" ]" %
|
78 | 347fa0f1 | Michael Hanselmann | constants.CPU_PINNING_ALL_XEN)) |
79 | 347fa0f1 | Michael Hanselmann | |
80 | 347fa0f1 | Michael Hanselmann | |
81 | 3eb073a6 | Helga Velroyen | class TestGetCommand(testutils.GanetiTestCase): |
82 | 3eb073a6 | Helga Velroyen | def testCommandExplicit(self): |
83 | 3eb073a6 | Helga Velroyen | """Test the case when the command is given as class parameter explicitly.
|
84 | 3eb073a6 | Helga Velroyen |
|
85 | 3eb073a6 | Helga Velroyen | """
|
86 | 3eb073a6 | Helga Velroyen | expected_cmd = "xl"
|
87 | 3eb073a6 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cmd=constants.XEN_CMD_XL) |
88 | 398fd4f6 | Helga Velroyen | self.assertEqual(hv._GetCommand(None), expected_cmd) |
89 | 3eb073a6 | Helga Velroyen | |
90 | 3eb073a6 | Helga Velroyen | def testCommandInvalid(self): |
91 | 3eb073a6 | Helga Velroyen | """Test the case an invalid command is given as class parameter explicitly.
|
92 | 3eb073a6 | Helga Velroyen |
|
93 | 3eb073a6 | Helga Velroyen | """
|
94 | 3eb073a6 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cmd="invalidcommand")
|
95 | 3eb073a6 | Helga Velroyen | self.assertRaises(errors.ProgrammerError, hv._GetCommand, None) |
96 | 3eb073a6 | Helga Velroyen | |
97 | 51a95d00 | Helga Velroyen | def testCommandHvparams(self): |
98 | 51a95d00 | Helga Velroyen | expected_cmd = "xl"
|
99 | 51a95d00 | Helga Velroyen | test_hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
100 | 51a95d00 | Helga Velroyen | hv = hv_xen.XenHypervisor() |
101 | 398fd4f6 | Helga Velroyen | self.assertEqual(hv._GetCommand(test_hvparams), expected_cmd)
|
102 | 51a95d00 | Helga Velroyen | |
103 | 51a95d00 | Helga Velroyen | def testCommandHvparamsInvalid(self): |
104 | 51a95d00 | Helga Velroyen | test_hvparams = {} |
105 | 51a95d00 | Helga Velroyen | hv = hv_xen.XenHypervisor() |
106 | 398fd4f6 | Helga Velroyen | self.assertRaises(errors.HypervisorError, hv._GetCommand, test_hvparams)
|
107 | 51a95d00 | Helga Velroyen | |
108 | 51a95d00 | Helga Velroyen | def testCommandHvparamsCmdInvalid(self): |
109 | 51a95d00 | Helga Velroyen | test_hvparams = {constants.HV_XEN_CMD: "invalidcommand"}
|
110 | 51a95d00 | Helga Velroyen | hv = hv_xen.XenHypervisor() |
111 | 51a95d00 | Helga Velroyen | self.assertRaises(errors.ProgrammerError, hv._GetCommand, test_hvparams)
|
112 | 51a95d00 | Helga Velroyen | |
113 | 3eb073a6 | Helga Velroyen | |
114 | 2609da63 | Helga Velroyen | class TestParseInstanceList(testutils.GanetiTestCase): |
115 | b255379d | Michael Hanselmann | def test(self): |
116 | b255379d | Michael Hanselmann | data = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt")
|
117 | b255379d | Michael Hanselmann | |
118 | b255379d | Michael Hanselmann | # Exclude node
|
119 | 2609da63 | Helga Velroyen | self.assertEqual(hv_xen._ParseInstanceList(data.splitlines(), False), []) |
120 | b255379d | Michael Hanselmann | |
121 | b255379d | Michael Hanselmann | # Include node
|
122 | 2609da63 | Helga Velroyen | result = hv_xen._ParseInstanceList(data.splitlines(), True)
|
123 | b255379d | Michael Hanselmann | self.assertEqual(len(result), 1) |
124 | b255379d | Michael Hanselmann | self.assertEqual(len(result[0]), 6) |
125 | b255379d | Michael Hanselmann | |
126 | b255379d | Michael Hanselmann | # Name
|
127 | b255379d | Michael Hanselmann | self.assertEqual(result[0][0], hv_xen._DOM0_NAME) |
128 | b255379d | Michael Hanselmann | |
129 | b255379d | Michael Hanselmann | # ID
|
130 | b255379d | Michael Hanselmann | self.assertEqual(result[0][1], 0) |
131 | b255379d | Michael Hanselmann | |
132 | b255379d | Michael Hanselmann | # Memory
|
133 | b255379d | Michael Hanselmann | self.assertEqual(result[0][2], 1023) |
134 | b255379d | Michael Hanselmann | |
135 | b255379d | Michael Hanselmann | # VCPUs
|
136 | b255379d | Michael Hanselmann | self.assertEqual(result[0][3], 1) |
137 | b255379d | Michael Hanselmann | |
138 | b255379d | Michael Hanselmann | # State
|
139 | b255379d | Michael Hanselmann | self.assertEqual(result[0][4], "r-----") |
140 | b255379d | Michael Hanselmann | |
141 | b255379d | Michael Hanselmann | # Time
|
142 | b255379d | Michael Hanselmann | self.assertAlmostEqual(result[0][5], 121152.6) |
143 | b255379d | Michael Hanselmann | |
144 | b255379d | Michael Hanselmann | def testWrongLineFormat(self): |
145 | b255379d | Michael Hanselmann | tests = [ |
146 | b255379d | Michael Hanselmann | ["three fields only"],
|
147 | b255379d | Michael Hanselmann | ["name InvalidID 128 1 r----- 12345"],
|
148 | b255379d | Michael Hanselmann | ] |
149 | b255379d | Michael Hanselmann | |
150 | b255379d | Michael Hanselmann | for lines in tests: |
151 | b255379d | Michael Hanselmann | try:
|
152 | 2609da63 | Helga Velroyen | hv_xen._ParseInstanceList(["Header would be here"] + lines, False) |
153 | b255379d | Michael Hanselmann | except errors.HypervisorError, err:
|
154 | 2609da63 | Helga Velroyen | self.assertTrue("Can't parse instance list" in str(err)) |
155 | b255379d | Michael Hanselmann | else:
|
156 | b255379d | Michael Hanselmann | self.fail("Exception was not raised") |
157 | b255379d | Michael Hanselmann | |
158 | b255379d | Michael Hanselmann | |
159 | 36bebc53 | Helga Velroyen | class TestGetInstanceList(testutils.GanetiTestCase): |
160 | b255379d | Michael Hanselmann | def _Fail(self): |
161 | b255379d | Michael Hanselmann | return utils.RunResult(constants.EXIT_FAILURE, None, |
162 | b255379d | Michael Hanselmann | "stdout", "stderr", None, |
163 | b255379d | Michael Hanselmann | NotImplemented, NotImplemented) |
164 | b255379d | Michael Hanselmann | |
165 | b255379d | Michael Hanselmann | def testTimeout(self): |
166 | b255379d | Michael Hanselmann | fn = testutils.CallCounter(self._Fail)
|
167 | b255379d | Michael Hanselmann | try:
|
168 | 36bebc53 | Helga Velroyen | hv_xen._GetInstanceList(fn, False, _timeout=0.1) |
169 | b255379d | Michael Hanselmann | except errors.HypervisorError, err:
|
170 | b255379d | Michael Hanselmann | self.assertTrue("timeout exceeded" in str(err)) |
171 | b255379d | Michael Hanselmann | else:
|
172 | b255379d | Michael Hanselmann | self.fail("Exception was not raised") |
173 | b255379d | Michael Hanselmann | |
174 | b255379d | Michael Hanselmann | self.assertTrue(fn.Count() < 10, |
175 | b255379d | Michael Hanselmann | msg="'xm list' was called too many times")
|
176 | b255379d | Michael Hanselmann | |
177 | b255379d | Michael Hanselmann | def _Success(self, stdout): |
178 | b255379d | Michael Hanselmann | return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None, |
179 | b255379d | Michael Hanselmann | NotImplemented, NotImplemented) |
180 | b255379d | Michael Hanselmann | |
181 | b255379d | Michael Hanselmann | def testSuccess(self): |
182 | b255379d | Michael Hanselmann | data = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
|
183 | b255379d | Michael Hanselmann | |
184 | b255379d | Michael Hanselmann | fn = testutils.CallCounter(compat.partial(self._Success, data))
|
185 | b255379d | Michael Hanselmann | |
186 | 36bebc53 | Helga Velroyen | result = hv_xen._GetInstanceList(fn, True, _timeout=0.1) |
187 | b255379d | Michael Hanselmann | |
188 | b255379d | Michael Hanselmann | self.assertEqual(len(result), 4) |
189 | b255379d | Michael Hanselmann | |
190 | b255379d | Michael Hanselmann | self.assertEqual(map(compat.fst, result), [ |
191 | b255379d | Michael Hanselmann | "Domain-0",
|
192 | b255379d | Michael Hanselmann | "server01.example.com",
|
193 | b255379d | Michael Hanselmann | "web3106215069.example.com",
|
194 | b255379d | Michael Hanselmann | "testinstance.example.com",
|
195 | b255379d | Michael Hanselmann | ]) |
196 | b255379d | Michael Hanselmann | |
197 | b255379d | Michael Hanselmann | self.assertEqual(fn.Count(), 1) |
198 | b255379d | Michael Hanselmann | |
199 | b255379d | Michael Hanselmann | |
200 | 06c9a520 | Michael Hanselmann | class TestParseNodeInfo(testutils.GanetiTestCase): |
201 | 06c9a520 | Michael Hanselmann | def testEmpty(self): |
202 | 06c9a520 | Michael Hanselmann | self.assertEqual(hv_xen._ParseNodeInfo(""), {}) |
203 | 06c9a520 | Michael Hanselmann | |
204 | 06c9a520 | Michael Hanselmann | def testUnknownInput(self): |
205 | 06c9a520 | Michael Hanselmann | data = "\n".join([
|
206 | 06c9a520 | Michael Hanselmann | "foo bar",
|
207 | 06c9a520 | Michael Hanselmann | "something else goes",
|
208 | 06c9a520 | Michael Hanselmann | "here",
|
209 | 06c9a520 | Michael Hanselmann | ]) |
210 | 06c9a520 | Michael Hanselmann | self.assertEqual(hv_xen._ParseNodeInfo(data), {})
|
211 | 06c9a520 | Michael Hanselmann | |
212 | 06c9a520 | Michael Hanselmann | def testBasicInfo(self): |
213 | 06c9a520 | Michael Hanselmann | data = testutils.ReadTestData("xen-xm-info-4.0.1.txt")
|
214 | 06c9a520 | Michael Hanselmann | result = hv_xen._ParseNodeInfo(data) |
215 | 06c9a520 | Michael Hanselmann | self.assertEqual(result, {
|
216 | 06c9a520 | Michael Hanselmann | "cpu_nodes": 1, |
217 | 06c9a520 | Michael Hanselmann | "cpu_sockets": 2, |
218 | 06c9a520 | Michael Hanselmann | "cpu_total": 4, |
219 | 06c9a520 | Michael Hanselmann | "hv_version": (4, 0), |
220 | 06c9a520 | Michael Hanselmann | "memory_free": 8004, |
221 | 06c9a520 | Michael Hanselmann | "memory_total": 16378, |
222 | 06c9a520 | Michael Hanselmann | }) |
223 | 06c9a520 | Michael Hanselmann | |
224 | 06c9a520 | Michael Hanselmann | |
225 | 06c9a520 | Michael Hanselmann | class TestMergeInstanceInfo(testutils.GanetiTestCase): |
226 | 06c9a520 | Michael Hanselmann | def testEmpty(self): |
227 | fac489a5 | Helga Velroyen | self.assertEqual(hv_xen._MergeInstanceInfo({}, []), {})
|
228 | 06c9a520 | Michael Hanselmann | |
229 | 06c9a520 | Michael Hanselmann | def _FakeXmList(self, include_node): |
230 | 06c9a520 | Michael Hanselmann | return [
|
231 | 06c9a520 | Michael Hanselmann | (hv_xen._DOM0_NAME, NotImplemented, 4096, 7, NotImplemented, |
232 | 06c9a520 | Michael Hanselmann | NotImplemented),
|
233 | 06c9a520 | Michael Hanselmann | ("inst1.example.com", NotImplemented, 2048, 4, NotImplemented, |
234 | 06c9a520 | Michael Hanselmann | NotImplemented),
|
235 | 06c9a520 | Michael Hanselmann | ] |
236 | 06c9a520 | Michael Hanselmann | |
237 | 06c9a520 | Michael Hanselmann | def testMissingNodeInfo(self): |
238 | fac489a5 | Helga Velroyen | instance_list = self._FakeXmList(True) |
239 | fac489a5 | Helga Velroyen | result = hv_xen._MergeInstanceInfo({}, instance_list) |
240 | 06c9a520 | Michael Hanselmann | self.assertEqual(result, {
|
241 | 06c9a520 | Michael Hanselmann | "memory_dom0": 4096, |
242 | 06c9a520 | Michael Hanselmann | "dom0_cpus": 7, |
243 | 06c9a520 | Michael Hanselmann | }) |
244 | 06c9a520 | Michael Hanselmann | |
245 | 06c9a520 | Michael Hanselmann | def testWithNodeInfo(self): |
246 | 06c9a520 | Michael Hanselmann | info = testutils.ReadTestData("xen-xm-info-4.0.1.txt")
|
247 | fac489a5 | Helga Velroyen | instance_list = self._FakeXmList(True) |
248 | fac489a5 | Helga Velroyen | result = hv_xen._GetNodeInfo(info, instance_list) |
249 | 06c9a520 | Michael Hanselmann | self.assertEqual(result, {
|
250 | 06c9a520 | Michael Hanselmann | "cpu_nodes": 1, |
251 | 06c9a520 | Michael Hanselmann | "cpu_sockets": 2, |
252 | 06c9a520 | Michael Hanselmann | "cpu_total": 4, |
253 | 06c9a520 | Michael Hanselmann | "dom0_cpus": 7, |
254 | 06c9a520 | Michael Hanselmann | "hv_version": (4, 0), |
255 | 06c9a520 | Michael Hanselmann | "memory_dom0": 4096, |
256 | 06c9a520 | Michael Hanselmann | "memory_free": 8004, |
257 | 06c9a520 | Michael Hanselmann | "memory_hv": 2230, |
258 | 06c9a520 | Michael Hanselmann | "memory_total": 16378, |
259 | 06c9a520 | Michael Hanselmann | }) |
260 | 06c9a520 | Michael Hanselmann | |
261 | 06c9a520 | Michael Hanselmann | |
262 | d0bb3f24 | Michael Hanselmann | class TestGetConfigFileDiskData(unittest.TestCase): |
263 | d0bb3f24 | Michael Hanselmann | def testLetterCount(self): |
264 | d0bb3f24 | Michael Hanselmann | self.assertEqual(len(hv_xen._DISK_LETTERS), 26) |
265 | d0bb3f24 | Michael Hanselmann | |
266 | d0bb3f24 | Michael Hanselmann | def testNoDisks(self): |
267 | d0bb3f24 | Michael Hanselmann | self.assertEqual(hv_xen._GetConfigFileDiskData([], "hd"), []) |
268 | d0bb3f24 | Michael Hanselmann | |
269 | d0bb3f24 | Michael Hanselmann | def testManyDisks(self): |
270 | d0bb3f24 | Michael Hanselmann | for offset in [0, 1, 10]: |
271 | d0bb3f24 | Michael Hanselmann | disks = [(objects.Disk(dev_type=constants.LD_LV), "/tmp/disk/%s" % idx)
|
272 | d0bb3f24 | Michael Hanselmann | for idx in range(len(hv_xen._DISK_LETTERS) + offset)] |
273 | d0bb3f24 | Michael Hanselmann | |
274 | d0bb3f24 | Michael Hanselmann | if offset == 0: |
275 | d0bb3f24 | Michael Hanselmann | result = hv_xen._GetConfigFileDiskData(disks, "hd")
|
276 | d0bb3f24 | Michael Hanselmann | self.assertEqual(result, [
|
277 | d0bb3f24 | Michael Hanselmann | "'phy:/tmp/disk/%s,hd%s,r'" % (idx, string.ascii_lowercase[idx])
|
278 | d0bb3f24 | Michael Hanselmann | for idx in range(len(hv_xen._DISK_LETTERS) + offset) |
279 | d0bb3f24 | Michael Hanselmann | ]) |
280 | d0bb3f24 | Michael Hanselmann | else:
|
281 | d0bb3f24 | Michael Hanselmann | try:
|
282 | d0bb3f24 | Michael Hanselmann | hv_xen._GetConfigFileDiskData(disks, "hd")
|
283 | d0bb3f24 | Michael Hanselmann | except errors.HypervisorError, err:
|
284 | d0bb3f24 | Michael Hanselmann | self.assertEqual(str(err), "Too many disks") |
285 | d0bb3f24 | Michael Hanselmann | else:
|
286 | d0bb3f24 | Michael Hanselmann | self.fail("Exception was not raised") |
287 | d0bb3f24 | Michael Hanselmann | |
288 | d0bb3f24 | Michael Hanselmann | def testTwoLvDisksWithMode(self): |
289 | d0bb3f24 | Michael Hanselmann | disks = [ |
290 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_LV, mode=constants.DISK_RDWR), |
291 | d0bb3f24 | Michael Hanselmann | "/tmp/diskFirst"),
|
292 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_LV, mode=constants.DISK_RDONLY), |
293 | d0bb3f24 | Michael Hanselmann | "/tmp/diskLast"),
|
294 | d0bb3f24 | Michael Hanselmann | ] |
295 | d0bb3f24 | Michael Hanselmann | |
296 | d0bb3f24 | Michael Hanselmann | result = hv_xen._GetConfigFileDiskData(disks, "hd")
|
297 | d0bb3f24 | Michael Hanselmann | self.assertEqual(result, [
|
298 | d0bb3f24 | Michael Hanselmann | "'phy:/tmp/diskFirst,hda,w'",
|
299 | d0bb3f24 | Michael Hanselmann | "'phy:/tmp/diskLast,hdb,r'",
|
300 | d0bb3f24 | Michael Hanselmann | ]) |
301 | d0bb3f24 | Michael Hanselmann | |
302 | d0bb3f24 | Michael Hanselmann | def testFileDisks(self): |
303 | d0bb3f24 | Michael Hanselmann | disks = [ |
304 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_FILE, mode=constants.DISK_RDWR, |
305 | d0bb3f24 | Michael Hanselmann | physical_id=[constants.FD_LOOP]), |
306 | d0bb3f24 | Michael Hanselmann | "/tmp/diskFirst"),
|
307 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_FILE, mode=constants.DISK_RDONLY, |
308 | d0bb3f24 | Michael Hanselmann | physical_id=[constants.FD_BLKTAP]), |
309 | d0bb3f24 | Michael Hanselmann | "/tmp/diskTwo"),
|
310 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_FILE, mode=constants.DISK_RDWR, |
311 | d0bb3f24 | Michael Hanselmann | physical_id=[constants.FD_LOOP]), |
312 | d0bb3f24 | Michael Hanselmann | "/tmp/diskThree"),
|
313 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_FILE, mode=constants.DISK_RDWR, |
314 | d0bb3f24 | Michael Hanselmann | physical_id=[constants.FD_BLKTAP]), |
315 | d0bb3f24 | Michael Hanselmann | "/tmp/diskLast"),
|
316 | d0bb3f24 | Michael Hanselmann | ] |
317 | d0bb3f24 | Michael Hanselmann | |
318 | d0bb3f24 | Michael Hanselmann | result = hv_xen._GetConfigFileDiskData(disks, "sd")
|
319 | d0bb3f24 | Michael Hanselmann | self.assertEqual(result, [
|
320 | d0bb3f24 | Michael Hanselmann | "'file:/tmp/diskFirst,sda,w'",
|
321 | d0bb3f24 | Michael Hanselmann | "'tap:aio:/tmp/diskTwo,sdb,r'",
|
322 | d0bb3f24 | Michael Hanselmann | "'file:/tmp/diskThree,sdc,w'",
|
323 | d0bb3f24 | Michael Hanselmann | "'tap:aio:/tmp/diskLast,sdd,w'",
|
324 | d0bb3f24 | Michael Hanselmann | ]) |
325 | d0bb3f24 | Michael Hanselmann | |
326 | d0bb3f24 | Michael Hanselmann | def testInvalidFileDisk(self): |
327 | d0bb3f24 | Michael Hanselmann | disks = [ |
328 | d0bb3f24 | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_FILE, mode=constants.DISK_RDWR, |
329 | d0bb3f24 | Michael Hanselmann | physical_id=["#unknown#"]),
|
330 | d0bb3f24 | Michael Hanselmann | "/tmp/diskinvalid"),
|
331 | d0bb3f24 | Michael Hanselmann | ] |
332 | d0bb3f24 | Michael Hanselmann | |
333 | d0bb3f24 | Michael Hanselmann | self.assertRaises(KeyError, hv_xen._GetConfigFileDiskData, disks, "sd") |
334 | d0bb3f24 | Michael Hanselmann | |
335 | d0bb3f24 | Michael Hanselmann | |
336 | 58e356a9 | Helga Velroyen | class TestXenHypervisorRunXen(unittest.TestCase): |
337 | 58e356a9 | Helga Velroyen | |
338 | 58e356a9 | Helga Velroyen | XEN_SUB_CMD = "help"
|
339 | 58e356a9 | Helga Velroyen | |
340 | 58e356a9 | Helga Velroyen | def testCommandUnknown(self): |
341 | 1dee2041 | Michael Hanselmann | cmd = "#unknown command#"
|
342 | 1dee2041 | Michael Hanselmann | self.assertFalse(cmd in constants.KNOWN_XEN_COMMANDS) |
343 | 1dee2041 | Michael Hanselmann | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
344 | 1dee2041 | Michael Hanselmann | _run_cmd_fn=NotImplemented,
|
345 | 1dee2041 | Michael Hanselmann | _cmd=cmd) |
346 | 398fd4f6 | Helga Velroyen | self.assertRaises(errors.ProgrammerError, hv._RunXen, [], None) |
347 | 1dee2041 | Michael Hanselmann | |
348 | 398fd4f6 | Helga Velroyen | def testCommandNoHvparams(self): |
349 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
350 | 398fd4f6 | Helga Velroyen | _run_cmd_fn=NotImplemented)
|
351 | 398fd4f6 | Helga Velroyen | hvparams = None
|
352 | 398fd4f6 | Helga Velroyen | self.assertRaises(errors.HypervisorError, hv._RunXen, [self.XEN_SUB_CMD], |
353 | 398fd4f6 | Helga Velroyen | hvparams) |
354 | 58e356a9 | Helga Velroyen | |
355 | 58e356a9 | Helga Velroyen | def testCommandFromHvparams(self): |
356 | 58e356a9 | Helga Velroyen | expected_xen_cmd = "xl"
|
357 | 58e356a9 | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
358 | 58e356a9 | Helga Velroyen | mock_run_cmd = mock.Mock() |
359 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
360 | 58e356a9 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
361 | 58e356a9 | Helga Velroyen | hv._RunXen([self.XEN_SUB_CMD], hvparams=hvparams)
|
362 | 58e356a9 | Helga Velroyen | mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_SUB_CMD])
|
363 | 58e356a9 | Helga Velroyen | |
364 | 58e356a9 | Helga Velroyen | |
365 | 58e356a9 | Helga Velroyen | class TestXenHypervisorGetInstanceList(unittest.TestCase): |
366 | 58e356a9 | Helga Velroyen | |
367 | 58e356a9 | Helga Velroyen | RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) |
368 | 58e356a9 | Helga Velroyen | XEN_LIST = "list"
|
369 | 58e356a9 | Helga Velroyen | |
370 | 398fd4f6 | Helga Velroyen | def testNoHvparams(self): |
371 | 58e356a9 | Helga Velroyen | expected_xen_cmd = "xm"
|
372 | 58e356a9 | Helga Velroyen | mock_run_cmd = mock.Mock( return_value=self.RESULT_OK )
|
373 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
374 | 58e356a9 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
375 | 398fd4f6 | Helga Velroyen | self.assertRaises(errors.HypervisorError, hv._GetInstanceList, True, None) |
376 | 58e356a9 | Helga Velroyen | |
377 | 58e356a9 | Helga Velroyen | def testFromHvparams(self): |
378 | 58e356a9 | Helga Velroyen | expected_xen_cmd = "xl"
|
379 | 58e356a9 | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
380 | 58e356a9 | Helga Velroyen | mock_run_cmd = mock.Mock( return_value=self.RESULT_OK )
|
381 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
382 | 58e356a9 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
383 | 398fd4f6 | Helga Velroyen | hv._GetInstanceList(True, hvparams)
|
384 | 58e356a9 | Helga Velroyen | mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST])
|
385 | 58e356a9 | Helga Velroyen | |
386 | 58e356a9 | Helga Velroyen | |
387 | 58e356a9 | Helga Velroyen | class TestXenHypervisorListInstances(unittest.TestCase): |
388 | 58e356a9 | Helga Velroyen | |
389 | 58e356a9 | Helga Velroyen | RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) |
390 | 58e356a9 | Helga Velroyen | XEN_LIST = "list"
|
391 | 58e356a9 | Helga Velroyen | |
392 | 398fd4f6 | Helga Velroyen | def testNoHvparams(self): |
393 | 58e356a9 | Helga Velroyen | expected_xen_cmd = "xm"
|
394 | 58e356a9 | Helga Velroyen | mock_run_cmd = mock.Mock( return_value=self.RESULT_OK )
|
395 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
396 | 58e356a9 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
397 | 398fd4f6 | Helga Velroyen | self.assertRaises(errors.HypervisorError, hv.ListInstances)
|
398 | 58e356a9 | Helga Velroyen | |
399 | 58e356a9 | Helga Velroyen | def testHvparamsXl(self): |
400 | 58e356a9 | Helga Velroyen | expected_xen_cmd = "xl"
|
401 | 58e356a9 | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
402 | 58e356a9 | Helga Velroyen | mock_run_cmd = mock.Mock( return_value=self.RESULT_OK )
|
403 | 58e356a9 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
404 | 58e356a9 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
405 | 58e356a9 | Helga Velroyen | hv.ListInstances(hvparams=hvparams) |
406 | 58e356a9 | Helga Velroyen | mock_run_cmd.assert_called_with([expected_xen_cmd, self.XEN_LIST])
|
407 | 58e356a9 | Helga Velroyen | |
408 | 1dee2041 | Michael Hanselmann | |
409 | 75bf3149 | Helga Velroyen | class TestXenHypervisorCheckToolstack(unittest.TestCase): |
410 | 75bf3149 | Helga Velroyen | |
411 | 75bf3149 | Helga Velroyen | def setUp(self): |
412 | 75bf3149 | Helga Velroyen | self.tmpdir = tempfile.mkdtemp()
|
413 | 75bf3149 | Helga Velroyen | self.cfg_name = "xen_config" |
414 | 75bf3149 | Helga Velroyen | self.cfg_path = utils.PathJoin(self.tmpdir, self.cfg_name) |
415 | 75bf3149 | Helga Velroyen | self.hv = hv_xen.XenHypervisor()
|
416 | 75bf3149 | Helga Velroyen | |
417 | 75bf3149 | Helga Velroyen | def tearDown(self): |
418 | 75bf3149 | Helga Velroyen | shutil.rmtree(self.tmpdir)
|
419 | 75bf3149 | Helga Velroyen | |
420 | 75bf3149 | Helga Velroyen | def testBinaryNotFound(self): |
421 | 75bf3149 | Helga Velroyen | RESULT_FAILED = utils.RunResult(1, None, "", "", "", None, None) |
422 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) |
423 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
424 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
425 | 75bf3149 | Helga Velroyen | result = hv._CheckToolstackBinary("xl")
|
426 | 75bf3149 | Helga Velroyen | self.assertFalse(result)
|
427 | 75bf3149 | Helga Velroyen | |
428 | 75bf3149 | Helga Velroyen | def testCheckToolstackXlConfigured(self): |
429 | 75bf3149 | Helga Velroyen | RESULT_OK = utils.RunResult(0, None, "", "", "", None, None) |
430 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=RESULT_OK) |
431 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
432 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
433 | 75bf3149 | Helga Velroyen | result = hv._CheckToolstackXlConfigured() |
434 | 75bf3149 | Helga Velroyen | self.assertTrue(result)
|
435 | 75bf3149 | Helga Velroyen | |
436 | 75bf3149 | Helga Velroyen | def testCheckToolstackXlNotConfigured(self): |
437 | 75bf3149 | Helga Velroyen | RESULT_FAILED = utils.RunResult( |
438 | 75bf3149 | Helga Velroyen | 1, None, "", |
439 | 75bf3149 | Helga Velroyen | "ERROR: A different toolstack (xm) have been selected!",
|
440 | 75bf3149 | Helga Velroyen | "", None, None) |
441 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) |
442 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
443 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
444 | 75bf3149 | Helga Velroyen | result = hv._CheckToolstackXlConfigured() |
445 | 75bf3149 | Helga Velroyen | self.assertFalse(result)
|
446 | 75bf3149 | Helga Velroyen | |
447 | 75bf3149 | Helga Velroyen | def testCheckToolstackXlFails(self): |
448 | 75bf3149 | Helga Velroyen | RESULT_FAILED = utils.RunResult( |
449 | 75bf3149 | Helga Velroyen | 1, None, "", |
450 | 75bf3149 | Helga Velroyen | "ERROR: The pink bunny hid the binary.",
|
451 | 75bf3149 | Helga Velroyen | "", None, None) |
452 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=RESULT_FAILED) |
453 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
454 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
455 | 75bf3149 | Helga Velroyen | self.assertRaises(errors.HypervisorError, hv._CheckToolstackXlConfigured)
|
456 | 75bf3149 | Helga Velroyen | |
457 | 75bf3149 | Helga Velroyen | |
458 | 64a66bd2 | Michael Hanselmann | class TestXenHypervisorWriteConfigFile(unittest.TestCase): |
459 | 64a66bd2 | Michael Hanselmann | def setUp(self): |
460 | 64a66bd2 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
461 | 64a66bd2 | Michael Hanselmann | |
462 | 64a66bd2 | Michael Hanselmann | def tearDown(self): |
463 | 64a66bd2 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
464 | 64a66bd2 | Michael Hanselmann | |
465 | 64a66bd2 | Michael Hanselmann | def testWriteError(self): |
466 | 64a66bd2 | Michael Hanselmann | cfgdir = utils.PathJoin(self.tmpdir, "foobar") |
467 | 64a66bd2 | Michael Hanselmann | |
468 | 64a66bd2 | Michael Hanselmann | hv = hv_xen.XenHypervisor(_cfgdir=cfgdir, |
469 | 64a66bd2 | Michael Hanselmann | _run_cmd_fn=NotImplemented,
|
470 | 64a66bd2 | Michael Hanselmann | _cmd=NotImplemented)
|
471 | 64a66bd2 | Michael Hanselmann | |
472 | 64a66bd2 | Michael Hanselmann | self.assertFalse(os.path.exists(cfgdir))
|
473 | 64a66bd2 | Michael Hanselmann | |
474 | 64a66bd2 | Michael Hanselmann | try:
|
475 | 64a66bd2 | Michael Hanselmann | hv._WriteConfigFile("name", "data") |
476 | 64a66bd2 | Michael Hanselmann | except errors.HypervisorError, err:
|
477 | 64a66bd2 | Michael Hanselmann | self.assertTrue(str(err).startswith("Cannot write Xen instance")) |
478 | 64a66bd2 | Michael Hanselmann | else:
|
479 | 64a66bd2 | Michael Hanselmann | self.fail("Exception was not raised") |
480 | 64a66bd2 | Michael Hanselmann | |
481 | 64a66bd2 | Michael Hanselmann | |
482 | 75bf3149 | Helga Velroyen | class TestXenHypervisorVerify(unittest.TestCase): |
483 | 75bf3149 | Helga Velroyen | |
484 | 75bf3149 | Helga Velroyen | def setUp(self): |
485 | 75bf3149 | Helga Velroyen | output = testutils.ReadTestData("xen-xm-info-4.0.1.txt")
|
486 | 75bf3149 | Helga Velroyen | self._result_ok = utils.RunResult(0, None, output, "", "", None, None) |
487 | 75bf3149 | Helga Velroyen | |
488 | 75bf3149 | Helga Velroyen | def testVerify(self): |
489 | 75bf3149 | Helga Velroyen | hvparams = {constants.HV_XEN_CMD : constants.XEN_CMD_XL} |
490 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=self._result_ok)
|
491 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
492 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
493 | 75bf3149 | Helga Velroyen | hv._CheckToolstack = mock.Mock(return_value=True)
|
494 | 75bf3149 | Helga Velroyen | result = hv.Verify(hvparams) |
495 | 75bf3149 | Helga Velroyen | self.assertTrue(result is None) |
496 | 75bf3149 | Helga Velroyen | |
497 | 75bf3149 | Helga Velroyen | def testVerifyToolstackNotOk(self): |
498 | 75bf3149 | Helga Velroyen | hvparams = {constants.HV_XEN_CMD : constants.XEN_CMD_XL} |
499 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=self._result_ok)
|
500 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
501 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
502 | 75bf3149 | Helga Velroyen | hv._CheckToolstack = mock.Mock() |
503 | 75bf3149 | Helga Velroyen | hv._CheckToolstack.side_effect = errors.HypervisorError("foo")
|
504 | 75bf3149 | Helga Velroyen | result = hv.Verify(hvparams) |
505 | 75bf3149 | Helga Velroyen | self.assertTrue(result is not None) |
506 | 75bf3149 | Helga Velroyen | |
507 | 75bf3149 | Helga Velroyen | def testVerifyFailing(self): |
508 | 75bf3149 | Helga Velroyen | result_failed = utils.RunResult(1, None, "", "", "", None, None) |
509 | 75bf3149 | Helga Velroyen | mock_run_cmd = mock.Mock(return_value=result_failed) |
510 | 75bf3149 | Helga Velroyen | hv = hv_xen.XenHypervisor(_cfgdir=NotImplemented,
|
511 | 75bf3149 | Helga Velroyen | _run_cmd_fn=mock_run_cmd) |
512 | 75bf3149 | Helga Velroyen | hv._CheckToolstack = mock.Mock(return_value=True)
|
513 | 75bf3149 | Helga Velroyen | result = hv.Verify() |
514 | 75bf3149 | Helga Velroyen | self.assertTrue(result is not None) |
515 | 75bf3149 | Helga Velroyen | |
516 | 75bf3149 | Helga Velroyen | |
517 | 74a50c46 | Michael Hanselmann | class _TestXenHypervisor(object): |
518 | 74a50c46 | Michael Hanselmann | TARGET = NotImplemented
|
519 | 74a50c46 | Michael Hanselmann | CMD = NotImplemented
|
520 | 74a50c46 | Michael Hanselmann | HVNAME = NotImplemented
|
521 | 8351df2f | Helga Velroyen | VALID_HVPARAMS = {constants.HV_XEN_CMD: constants.XEN_CMD_XL} |
522 | 74a50c46 | Michael Hanselmann | |
523 | 74a50c46 | Michael Hanselmann | def setUp(self): |
524 | 74a50c46 | Michael Hanselmann | super(_TestXenHypervisor, self).setUp() |
525 | 74a50c46 | Michael Hanselmann | |
526 | 74a50c46 | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
527 | 74a50c46 | Michael Hanselmann | |
528 | 74a50c46 | Michael Hanselmann | self.vncpw = "".join(random.sample(string.ascii_letters, 10)) |
529 | 74a50c46 | Michael Hanselmann | |
530 | 74a50c46 | Michael Hanselmann | self.vncpw_path = utils.PathJoin(self.tmpdir, "vncpw") |
531 | 74a50c46 | Michael Hanselmann | utils.WriteFile(self.vncpw_path, data=self.vncpw) |
532 | 74a50c46 | Michael Hanselmann | |
533 | 74a50c46 | Michael Hanselmann | def tearDown(self): |
534 | 74a50c46 | Michael Hanselmann | super(_TestXenHypervisor, self).tearDown() |
535 | 74a50c46 | Michael Hanselmann | |
536 | 74a50c46 | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
537 | 74a50c46 | Michael Hanselmann | |
538 | 74a50c46 | Michael Hanselmann | def _GetHv(self, run_cmd=NotImplemented): |
539 | 74a50c46 | Michael Hanselmann | return self.TARGET(_cfgdir=self.tmpdir, _run_cmd_fn=run_cmd, _cmd=self.CMD) |
540 | 74a50c46 | Michael Hanselmann | |
541 | 74a50c46 | Michael Hanselmann | def _SuccessCommand(self, stdout, cmd): |
542 | 74a50c46 | Michael Hanselmann | self.assertEqual(cmd[0], self.CMD) |
543 | 74a50c46 | Michael Hanselmann | |
544 | 74a50c46 | Michael Hanselmann | return utils.RunResult(constants.EXIT_SUCCESS, None, stdout, "", None, |
545 | 74a50c46 | Michael Hanselmann | NotImplemented, NotImplemented) |
546 | 74a50c46 | Michael Hanselmann | |
547 | 74a50c46 | Michael Hanselmann | def _FailingCommand(self, cmd): |
548 | 74a50c46 | Michael Hanselmann | self.assertEqual(cmd[0], self.CMD) |
549 | 74a50c46 | Michael Hanselmann | |
550 | 74a50c46 | Michael Hanselmann | return utils.RunResult(constants.EXIT_FAILURE, None, |
551 | 74a50c46 | Michael Hanselmann | "", "This command failed", None, |
552 | 74a50c46 | Michael Hanselmann | NotImplemented, NotImplemented) |
553 | 74a50c46 | Michael Hanselmann | |
554 | d8784f7d | Michael Hanselmann | def _FakeTcpPing(self, expected, result, target, port, **kwargs): |
555 | d8784f7d | Michael Hanselmann | self.assertEqual((target, port), expected)
|
556 | d8784f7d | Michael Hanselmann | return result
|
557 | d8784f7d | Michael Hanselmann | |
558 | 57270b2d | Michael Hanselmann | def testReadingNonExistentConfigFile(self): |
559 | 57270b2d | Michael Hanselmann | hv = self._GetHv()
|
560 | 57270b2d | Michael Hanselmann | |
561 | 57270b2d | Michael Hanselmann | try:
|
562 | 57270b2d | Michael Hanselmann | hv._ReadConfigFile("inst15780.example.com")
|
563 | 57270b2d | Michael Hanselmann | except errors.HypervisorError, err:
|
564 | 57270b2d | Michael Hanselmann | self.assertTrue(str(err).startswith("Failed to load Xen config file:")) |
565 | 57270b2d | Michael Hanselmann | else:
|
566 | 57270b2d | Michael Hanselmann | self.fail("Exception was not raised") |
567 | 57270b2d | Michael Hanselmann | |
568 | 7610d884 | Michael Hanselmann | def testRemovingAutoConfigFile(self): |
569 | 7610d884 | Michael Hanselmann | name = "inst8206.example.com"
|
570 | 7610d884 | Michael Hanselmann | cfgfile = utils.PathJoin(self.tmpdir, name)
|
571 | 7610d884 | Michael Hanselmann | autodir = utils.PathJoin(self.tmpdir, "auto") |
572 | 7610d884 | Michael Hanselmann | autocfgfile = utils.PathJoin(autodir, name) |
573 | 7610d884 | Michael Hanselmann | |
574 | 7610d884 | Michael Hanselmann | os.mkdir(autodir) |
575 | 7610d884 | Michael Hanselmann | |
576 | 7610d884 | Michael Hanselmann | utils.WriteFile(autocfgfile, data="")
|
577 | 7610d884 | Michael Hanselmann | |
578 | 7610d884 | Michael Hanselmann | hv = self._GetHv()
|
579 | 7610d884 | Michael Hanselmann | |
580 | 7610d884 | Michael Hanselmann | self.assertTrue(os.path.isfile(autocfgfile))
|
581 | 7610d884 | Michael Hanselmann | hv._WriteConfigFile(name, "content")
|
582 | 7610d884 | Michael Hanselmann | self.assertFalse(os.path.exists(autocfgfile))
|
583 | 7610d884 | Michael Hanselmann | self.assertEqual(utils.ReadFile(cfgfile), "content") |
584 | 7610d884 | Michael Hanselmann | |
585 | 2edc1c79 | Michael Hanselmann | def _XenList(self, cmd): |
586 | 2edc1c79 | Michael Hanselmann | self.assertEqual(cmd, [self.CMD, "list"]) |
587 | 2edc1c79 | Michael Hanselmann | |
588 | 2edc1c79 | Michael Hanselmann | # TODO: Use actual data from "xl" command
|
589 | 2edc1c79 | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
|
590 | 2edc1c79 | Michael Hanselmann | |
591 | 2edc1c79 | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
592 | 2edc1c79 | Michael Hanselmann | |
593 | 2edc1c79 | Michael Hanselmann | def testGetInstanceInfo(self): |
594 | 2edc1c79 | Michael Hanselmann | hv = self._GetHv(run_cmd=self._XenList) |
595 | 2edc1c79 | Michael Hanselmann | |
596 | 2edc1c79 | Michael Hanselmann | (name, instid, memory, vcpus, state, runtime) = \ |
597 | 2edc1c79 | Michael Hanselmann | hv.GetInstanceInfo("server01.example.com")
|
598 | 2edc1c79 | Michael Hanselmann | |
599 | 2edc1c79 | Michael Hanselmann | self.assertEqual(name, "server01.example.com") |
600 | 2edc1c79 | Michael Hanselmann | self.assertEqual(instid, 1) |
601 | 2edc1c79 | Michael Hanselmann | self.assertEqual(memory, 1024) |
602 | 2edc1c79 | Michael Hanselmann | self.assertEqual(vcpus, 1) |
603 | 2edc1c79 | Michael Hanselmann | self.assertEqual(state, "-b----") |
604 | 2edc1c79 | Michael Hanselmann | self.assertAlmostEqual(runtime, 167643.2) |
605 | 2edc1c79 | Michael Hanselmann | |
606 | 2edc1c79 | Michael Hanselmann | def testGetInstanceInfoDom0(self): |
607 | 2edc1c79 | Michael Hanselmann | hv = self._GetHv(run_cmd=self._XenList) |
608 | 2edc1c79 | Michael Hanselmann | |
609 | 2edc1c79 | Michael Hanselmann | # TODO: Not sure if this is actually used anywhere (can't find it), but the
|
610 | 2edc1c79 | Michael Hanselmann | # code supports querying for Dom0
|
611 | 2edc1c79 | Michael Hanselmann | (name, instid, memory, vcpus, state, runtime) = \ |
612 | 2edc1c79 | Michael Hanselmann | hv.GetInstanceInfo(hv_xen._DOM0_NAME) |
613 | 2edc1c79 | Michael Hanselmann | |
614 | 2edc1c79 | Michael Hanselmann | self.assertEqual(name, "Domain-0") |
615 | 2edc1c79 | Michael Hanselmann | self.assertEqual(instid, 0) |
616 | 2edc1c79 | Michael Hanselmann | self.assertEqual(memory, 1023) |
617 | 2edc1c79 | Michael Hanselmann | self.assertEqual(vcpus, 1) |
618 | 2edc1c79 | Michael Hanselmann | self.assertEqual(state, "r-----") |
619 | 2edc1c79 | Michael Hanselmann | self.assertAlmostEqual(runtime, 154706.1) |
620 | 2edc1c79 | Michael Hanselmann | |
621 | 2edc1c79 | Michael Hanselmann | def testGetInstanceInfoUnknown(self): |
622 | 2edc1c79 | Michael Hanselmann | hv = self._GetHv(run_cmd=self._XenList) |
623 | 2edc1c79 | Michael Hanselmann | |
624 | 2edc1c79 | Michael Hanselmann | result = hv.GetInstanceInfo("unknown.example.com")
|
625 | 2edc1c79 | Michael Hanselmann | self.assertTrue(result is None) |
626 | 2edc1c79 | Michael Hanselmann | |
627 | 2edc1c79 | Michael Hanselmann | def testGetAllInstancesInfo(self): |
628 | 2edc1c79 | Michael Hanselmann | hv = self._GetHv(run_cmd=self._XenList) |
629 | 2edc1c79 | Michael Hanselmann | |
630 | 2edc1c79 | Michael Hanselmann | result = hv.GetAllInstancesInfo() |
631 | 2edc1c79 | Michael Hanselmann | |
632 | 2edc1c79 | Michael Hanselmann | self.assertEqual(map(compat.fst, result), [ |
633 | 2edc1c79 | Michael Hanselmann | "server01.example.com",
|
634 | 2edc1c79 | Michael Hanselmann | "web3106215069.example.com",
|
635 | 2edc1c79 | Michael Hanselmann | "testinstance.example.com",
|
636 | 2edc1c79 | Michael Hanselmann | ]) |
637 | 2edc1c79 | Michael Hanselmann | |
638 | 2edc1c79 | Michael Hanselmann | def testListInstances(self): |
639 | 2edc1c79 | Michael Hanselmann | hv = self._GetHv(run_cmd=self._XenList) |
640 | 2edc1c79 | Michael Hanselmann | |
641 | 2edc1c79 | Michael Hanselmann | self.assertEqual(hv.ListInstances(), [
|
642 | 2edc1c79 | Michael Hanselmann | "server01.example.com",
|
643 | 2edc1c79 | Michael Hanselmann | "web3106215069.example.com",
|
644 | 2edc1c79 | Michael Hanselmann | "testinstance.example.com",
|
645 | 2edc1c79 | Michael Hanselmann | ]) |
646 | 2edc1c79 | Michael Hanselmann | |
647 | 396672cd | Michael Hanselmann | def _StartInstanceCommand(self, inst, paused, failcreate, cmd): |
648 | 396672cd | Michael Hanselmann | if cmd == [self.CMD, "info"]: |
649 | 396672cd | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-info-4.0.1.txt")
|
650 | 396672cd | Michael Hanselmann | elif cmd == [self.CMD, "list"]: |
651 | 396672cd | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt")
|
652 | 396672cd | Michael Hanselmann | elif cmd[:2] == [self.CMD, "create"]: |
653 | 396672cd | Michael Hanselmann | args = cmd[2:]
|
654 | 396672cd | Michael Hanselmann | cfgfile = utils.PathJoin(self.tmpdir, inst.name)
|
655 | 396672cd | Michael Hanselmann | |
656 | 396672cd | Michael Hanselmann | if paused:
|
657 | 396672cd | Michael Hanselmann | self.assertEqual(args, ["-p", cfgfile]) |
658 | 396672cd | Michael Hanselmann | else:
|
659 | 396672cd | Michael Hanselmann | self.assertEqual(args, [cfgfile])
|
660 | 396672cd | Michael Hanselmann | |
661 | 396672cd | Michael Hanselmann | if failcreate:
|
662 | 396672cd | Michael Hanselmann | return self._FailingCommand(cmd) |
663 | 396672cd | Michael Hanselmann | |
664 | 396672cd | Michael Hanselmann | output = ""
|
665 | 396672cd | Michael Hanselmann | else:
|
666 | 396672cd | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
667 | 396672cd | Michael Hanselmann | |
668 | 396672cd | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
669 | 396672cd | Michael Hanselmann | |
670 | 396672cd | Michael Hanselmann | def _MakeInstance(self): |
671 | 396672cd | Michael Hanselmann | # Copy default parameters
|
672 | 396672cd | Michael Hanselmann | bep = objects.FillDict(constants.BEC_DEFAULTS, {}) |
673 | 396672cd | Michael Hanselmann | hvp = objects.FillDict(constants.HVC_DEFAULTS[self.HVNAME], {})
|
674 | 396672cd | Michael Hanselmann | |
675 | 396672cd | Michael Hanselmann | # Override default VNC password file path
|
676 | 396672cd | Michael Hanselmann | if constants.HV_VNC_PASSWORD_FILE in hvp: |
677 | 396672cd | Michael Hanselmann | hvp[constants.HV_VNC_PASSWORD_FILE] = self.vncpw_path
|
678 | 396672cd | Michael Hanselmann | |
679 | 396672cd | Michael Hanselmann | disks = [ |
680 | 396672cd | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_LV, mode=constants.DISK_RDWR), |
681 | 396672cd | Michael Hanselmann | utils.PathJoin(self.tmpdir, "disk0")), |
682 | 396672cd | Michael Hanselmann | (objects.Disk(dev_type=constants.LD_LV, mode=constants.DISK_RDONLY), |
683 | 396672cd | Michael Hanselmann | utils.PathJoin(self.tmpdir, "disk1")), |
684 | 396672cd | Michael Hanselmann | ] |
685 | 396672cd | Michael Hanselmann | |
686 | 396672cd | Michael Hanselmann | inst = objects.Instance(name="server01.example.com",
|
687 | 396672cd | Michael Hanselmann | hvparams=hvp, beparams=bep, |
688 | 396672cd | Michael Hanselmann | osparams={}, nics=[], os="deb1",
|
689 | 396672cd | Michael Hanselmann | disks=map(compat.fst, disks))
|
690 | 396672cd | Michael Hanselmann | inst.UpgradeConfig() |
691 | 396672cd | Michael Hanselmann | |
692 | 396672cd | Michael Hanselmann | return (inst, disks)
|
693 | 396672cd | Michael Hanselmann | |
694 | 396672cd | Michael Hanselmann | def testStartInstance(self): |
695 | 396672cd | Michael Hanselmann | (inst, disks) = self._MakeInstance()
|
696 | 48bba9de | Balazs Lecz | pathutils.LOG_XEN_DIR = self.tmpdir
|
697 | 396672cd | Michael Hanselmann | |
698 | 396672cd | Michael Hanselmann | for failcreate in [False, True]: |
699 | 396672cd | Michael Hanselmann | for paused in [False, True]: |
700 | 396672cd | Michael Hanselmann | run_cmd = compat.partial(self._StartInstanceCommand,
|
701 | 396672cd | Michael Hanselmann | inst, paused, failcreate) |
702 | 396672cd | Michael Hanselmann | |
703 | 396672cd | Michael Hanselmann | hv = self._GetHv(run_cmd=run_cmd)
|
704 | 396672cd | Michael Hanselmann | |
705 | 396672cd | Michael Hanselmann | # Ensure instance is not listed
|
706 | 396672cd | Michael Hanselmann | self.assertTrue(inst.name not in hv.ListInstances()) |
707 | 396672cd | Michael Hanselmann | |
708 | 396672cd | Michael Hanselmann | # Remove configuration
|
709 | 396672cd | Michael Hanselmann | cfgfile = utils.PathJoin(self.tmpdir, inst.name)
|
710 | 396672cd | Michael Hanselmann | utils.RemoveFile(cfgfile) |
711 | 396672cd | Michael Hanselmann | |
712 | 396672cd | Michael Hanselmann | if failcreate:
|
713 | 396672cd | Michael Hanselmann | self.assertRaises(errors.HypervisorError, hv.StartInstance,
|
714 | 396672cd | Michael Hanselmann | inst, disks, paused) |
715 | 48bba9de | Balazs Lecz | # Check whether a stale config file is left behind
|
716 | 48bba9de | Balazs Lecz | self.assertFalse(os.path.exists(cfgfile))
|
717 | 396672cd | Michael Hanselmann | else:
|
718 | 396672cd | Michael Hanselmann | hv.StartInstance(inst, disks, paused) |
719 | 48bba9de | Balazs Lecz | # Check if configuration was updated
|
720 | 48bba9de | Balazs Lecz | lines = utils.ReadFile(cfgfile).splitlines() |
721 | 396672cd | Michael Hanselmann | |
722 | 396672cd | Michael Hanselmann | if constants.HV_VNC_PASSWORD_FILE in inst.hvparams: |
723 | 396672cd | Michael Hanselmann | self.assertTrue(("vncpasswd = '%s'" % self.vncpw) in lines) |
724 | 396672cd | Michael Hanselmann | else:
|
725 | 396672cd | Michael Hanselmann | extra = inst.hvparams[constants.HV_KERNEL_ARGS] |
726 | 396672cd | Michael Hanselmann | self.assertTrue(("extra = '%s'" % extra) in lines) |
727 | 396672cd | Michael Hanselmann | |
728 | 664b392d | Michael Hanselmann | def _StopInstanceCommand(self, instance_name, force, fail, cmd): |
729 | 664b392d | Michael Hanselmann | if ((force and cmd[:2] == [self.CMD, "destroy"]) or |
730 | 664b392d | Michael Hanselmann | (not force and cmd[:2] == [self.CMD, "shutdown"])): |
731 | 664b392d | Michael Hanselmann | self.assertEqual(cmd[2:], [instance_name]) |
732 | 664b392d | Michael Hanselmann | output = ""
|
733 | 664b392d | Michael Hanselmann | else:
|
734 | 664b392d | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
735 | 664b392d | Michael Hanselmann | |
736 | 664b392d | Michael Hanselmann | if fail:
|
737 | 664b392d | Michael Hanselmann | # Simulate a failing command
|
738 | 664b392d | Michael Hanselmann | return self._FailingCommand(cmd) |
739 | 664b392d | Michael Hanselmann | else:
|
740 | 664b392d | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
741 | 664b392d | Michael Hanselmann | |
742 | 664b392d | Michael Hanselmann | def testStopInstance(self): |
743 | 664b392d | Michael Hanselmann | name = "inst4284.example.com"
|
744 | 664b392d | Michael Hanselmann | cfgfile = utils.PathJoin(self.tmpdir, name)
|
745 | 664b392d | Michael Hanselmann | cfgdata = "config file content\n"
|
746 | 664b392d | Michael Hanselmann | |
747 | 664b392d | Michael Hanselmann | for force in [False, True]: |
748 | 664b392d | Michael Hanselmann | for fail in [False, True]: |
749 | 664b392d | Michael Hanselmann | utils.WriteFile(cfgfile, data=cfgdata) |
750 | 664b392d | Michael Hanselmann | |
751 | 664b392d | Michael Hanselmann | run_cmd = compat.partial(self._StopInstanceCommand, name, force, fail)
|
752 | 664b392d | Michael Hanselmann | |
753 | 664b392d | Michael Hanselmann | hv = self._GetHv(run_cmd=run_cmd)
|
754 | 664b392d | Michael Hanselmann | |
755 | 664b392d | Michael Hanselmann | self.assertTrue(os.path.isfile(cfgfile))
|
756 | 664b392d | Michael Hanselmann | |
757 | 664b392d | Michael Hanselmann | if fail:
|
758 | 664b392d | Michael Hanselmann | try:
|
759 | 58e356a9 | Helga Velroyen | hv._StopInstance(name, force, None)
|
760 | 664b392d | Michael Hanselmann | except errors.HypervisorError, err:
|
761 | 664b392d | Michael Hanselmann | self.assertTrue(str(err).startswith("Failed to stop instance")) |
762 | 664b392d | Michael Hanselmann | else:
|
763 | 664b392d | Michael Hanselmann | self.fail("Exception was not raised") |
764 | 664b392d | Michael Hanselmann | self.assertEqual(utils.ReadFile(cfgfile), cfgdata,
|
765 | 664b392d | Michael Hanselmann | msg=("Configuration was removed when stopping"
|
766 | 664b392d | Michael Hanselmann | " instance failed"))
|
767 | 664b392d | Michael Hanselmann | else:
|
768 | 58e356a9 | Helga Velroyen | hv._StopInstance(name, force, None)
|
769 | 664b392d | Michael Hanselmann | self.assertFalse(os.path.exists(cfgfile))
|
770 | 664b392d | Michael Hanselmann | |
771 | d8784f7d | Michael Hanselmann | def _MigrateNonRunningInstCmd(self, cmd): |
772 | d8784f7d | Michael Hanselmann | if cmd == [self.CMD, "list"]: |
773 | d8784f7d | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-dom0-only.txt")
|
774 | d8784f7d | Michael Hanselmann | else:
|
775 | d8784f7d | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
776 | d8784f7d | Michael Hanselmann | |
777 | d8784f7d | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
778 | d8784f7d | Michael Hanselmann | |
779 | d8784f7d | Michael Hanselmann | def testMigrateInstanceNotRunning(self): |
780 | d8784f7d | Michael Hanselmann | name = "nonexistinginstance.example.com"
|
781 | d8784f7d | Michael Hanselmann | target = constants.IP4_ADDRESS_LOCALHOST |
782 | d8784f7d | Michael Hanselmann | port = 14618
|
783 | d8784f7d | Michael Hanselmann | |
784 | d8784f7d | Michael Hanselmann | hv = self._GetHv(run_cmd=self._MigrateNonRunningInstCmd) |
785 | d8784f7d | Michael Hanselmann | |
786 | d8784f7d | Michael Hanselmann | for live in [False, True]: |
787 | d8784f7d | Michael Hanselmann | try:
|
788 | d8784f7d | Michael Hanselmann | hv._MigrateInstance(NotImplemented, name, target, port, live,
|
789 | 8351df2f | Helga Velroyen | self.VALID_HVPARAMS, _ping_fn=NotImplemented) |
790 | d8784f7d | Michael Hanselmann | except errors.HypervisorError, err:
|
791 | d8784f7d | Michael Hanselmann | self.assertEqual(str(err), "Instance not running, cannot migrate") |
792 | d8784f7d | Michael Hanselmann | else:
|
793 | d8784f7d | Michael Hanselmann | self.fail("Exception was not raised") |
794 | d8784f7d | Michael Hanselmann | |
795 | d8784f7d | Michael Hanselmann | def _MigrateInstTargetUnreachCmd(self, cmd): |
796 | d8784f7d | Michael Hanselmann | if cmd == [self.CMD, "list"]: |
797 | d8784f7d | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
|
798 | d8784f7d | Michael Hanselmann | else:
|
799 | d8784f7d | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
800 | d8784f7d | Michael Hanselmann | |
801 | d8784f7d | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
802 | d8784f7d | Michael Hanselmann | |
803 | d8784f7d | Michael Hanselmann | def testMigrateTargetUnreachable(self): |
804 | d8784f7d | Michael Hanselmann | name = "server01.example.com"
|
805 | d8784f7d | Michael Hanselmann | target = constants.IP4_ADDRESS_LOCALHOST |
806 | d8784f7d | Michael Hanselmann | port = 28349
|
807 | d8784f7d | Michael Hanselmann | |
808 | d8784f7d | Michael Hanselmann | hv = self._GetHv(run_cmd=self._MigrateInstTargetUnreachCmd) |
809 | 8351df2f | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: self.CMD}
|
810 | d8784f7d | Michael Hanselmann | |
811 | d8784f7d | Michael Hanselmann | for live in [False, True]: |
812 | d8784f7d | Michael Hanselmann | if self.CMD == constants.XEN_CMD_XL: |
813 | d8784f7d | Michael Hanselmann | # TODO: Detect unreachable targets
|
814 | d8784f7d | Michael Hanselmann | pass
|
815 | d8784f7d | Michael Hanselmann | else:
|
816 | d8784f7d | Michael Hanselmann | try:
|
817 | d8784f7d | Michael Hanselmann | hv._MigrateInstance(NotImplemented, name, target, port, live,
|
818 | 8351df2f | Helga Velroyen | hvparams, |
819 | d8784f7d | Michael Hanselmann | _ping_fn=compat.partial(self._FakeTcpPing,
|
820 | d8784f7d | Michael Hanselmann | (target, port), False))
|
821 | d8784f7d | Michael Hanselmann | except errors.HypervisorError, err:
|
822 | d8784f7d | Michael Hanselmann | wanted = "Remote host %s not" % target
|
823 | d8784f7d | Michael Hanselmann | self.assertTrue(str(err).startswith(wanted)) |
824 | d8784f7d | Michael Hanselmann | else:
|
825 | d8784f7d | Michael Hanselmann | self.fail("Exception was not raised") |
826 | d8784f7d | Michael Hanselmann | |
827 | d8784f7d | Michael Hanselmann | def _MigrateInstanceCmd(self, cluster_name, instance_name, target, port, |
828 | d8784f7d | Michael Hanselmann | live, fail, cmd): |
829 | d8784f7d | Michael Hanselmann | if cmd == [self.CMD, "list"]: |
830 | d8784f7d | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
|
831 | d8784f7d | Michael Hanselmann | elif cmd[:2] == [self.CMD, "migrate"]: |
832 | d8784f7d | Michael Hanselmann | if self.CMD == constants.XEN_CMD_XM: |
833 | d8784f7d | Michael Hanselmann | args = ["-p", str(port)] |
834 | d8784f7d | Michael Hanselmann | |
835 | d8784f7d | Michael Hanselmann | if live:
|
836 | d8784f7d | Michael Hanselmann | args.append("-l")
|
837 | d8784f7d | Michael Hanselmann | |
838 | d8784f7d | Michael Hanselmann | elif self.CMD == constants.XEN_CMD_XL: |
839 | d8784f7d | Michael Hanselmann | args = [ |
840 | d8784f7d | Michael Hanselmann | "-s", constants.XL_SSH_CMD % cluster_name,
|
841 | d8784f7d | Michael Hanselmann | "-C", utils.PathJoin(self.tmpdir, instance_name), |
842 | d8784f7d | Michael Hanselmann | ] |
843 | d8784f7d | Michael Hanselmann | |
844 | d8784f7d | Michael Hanselmann | else:
|
845 | d8784f7d | Michael Hanselmann | self.fail("Unknown Xen command '%s'" % self.CMD) |
846 | d8784f7d | Michael Hanselmann | |
847 | d8784f7d | Michael Hanselmann | args.extend([instance_name, target]) |
848 | d8784f7d | Michael Hanselmann | self.assertEqual(cmd[2:], args) |
849 | d8784f7d | Michael Hanselmann | |
850 | d8784f7d | Michael Hanselmann | if fail:
|
851 | d8784f7d | Michael Hanselmann | return self._FailingCommand(cmd) |
852 | d8784f7d | Michael Hanselmann | |
853 | d8784f7d | Michael Hanselmann | output = ""
|
854 | d8784f7d | Michael Hanselmann | else:
|
855 | d8784f7d | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
856 | d8784f7d | Michael Hanselmann | |
857 | d8784f7d | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
858 | d8784f7d | Michael Hanselmann | |
859 | d8784f7d | Michael Hanselmann | def testMigrateInstance(self): |
860 | d8784f7d | Michael Hanselmann | clustername = "cluster.example.com"
|
861 | d8784f7d | Michael Hanselmann | instname = "server01.example.com"
|
862 | d8784f7d | Michael Hanselmann | target = constants.IP4_ADDRESS_LOCALHOST |
863 | d8784f7d | Michael Hanselmann | port = 22364
|
864 | d8784f7d | Michael Hanselmann | |
865 | 8351df2f | Helga Velroyen | hvparams = {constants.HV_XEN_CMD: self.CMD}
|
866 | 8351df2f | Helga Velroyen | |
867 | d8784f7d | Michael Hanselmann | for live in [False, True]: |
868 | d8784f7d | Michael Hanselmann | for fail in [False, True]: |
869 | d8784f7d | Michael Hanselmann | ping_fn = \ |
870 | d8784f7d | Michael Hanselmann | testutils.CallCounter(compat.partial(self._FakeTcpPing,
|
871 | d8784f7d | Michael Hanselmann | (target, port), True))
|
872 | d8784f7d | Michael Hanselmann | |
873 | d8784f7d | Michael Hanselmann | run_cmd = \ |
874 | d8784f7d | Michael Hanselmann | compat.partial(self._MigrateInstanceCmd,
|
875 | d8784f7d | Michael Hanselmann | clustername, instname, target, port, live, |
876 | d8784f7d | Michael Hanselmann | fail) |
877 | d8784f7d | Michael Hanselmann | |
878 | d8784f7d | Michael Hanselmann | hv = self._GetHv(run_cmd=run_cmd)
|
879 | d8784f7d | Michael Hanselmann | |
880 | d8784f7d | Michael Hanselmann | if fail:
|
881 | d8784f7d | Michael Hanselmann | try:
|
882 | d8784f7d | Michael Hanselmann | hv._MigrateInstance(clustername, instname, target, port, live, |
883 | 8351df2f | Helga Velroyen | hvparams, _ping_fn=ping_fn) |
884 | d8784f7d | Michael Hanselmann | except errors.HypervisorError, err:
|
885 | d8784f7d | Michael Hanselmann | self.assertTrue(str(err).startswith("Failed to migrate instance")) |
886 | d8784f7d | Michael Hanselmann | else:
|
887 | d8784f7d | Michael Hanselmann | self.fail("Exception was not raised") |
888 | d8784f7d | Michael Hanselmann | else:
|
889 | d8784f7d | Michael Hanselmann | hv._MigrateInstance(clustername, instname, target, port, live, |
890 | 8351df2f | Helga Velroyen | hvparams, _ping_fn=ping_fn) |
891 | d8784f7d | Michael Hanselmann | |
892 | d8784f7d | Michael Hanselmann | if self.CMD == constants.XEN_CMD_XM: |
893 | d8784f7d | Michael Hanselmann | expected_pings = 1
|
894 | d8784f7d | Michael Hanselmann | else:
|
895 | d8784f7d | Michael Hanselmann | expected_pings = 0
|
896 | d8784f7d | Michael Hanselmann | |
897 | d8784f7d | Michael Hanselmann | self.assertEqual(ping_fn.Count(), expected_pings)
|
898 | d8784f7d | Michael Hanselmann | |
899 | 1b9e1f42 | Michael Hanselmann | def _GetNodeInfoCmd(self, fail, cmd): |
900 | 1b9e1f42 | Michael Hanselmann | if cmd == [self.CMD, "info"]: |
901 | 1b9e1f42 | Michael Hanselmann | if fail:
|
902 | 1b9e1f42 | Michael Hanselmann | return self._FailingCommand(cmd) |
903 | 1b9e1f42 | Michael Hanselmann | else:
|
904 | 1b9e1f42 | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-info-4.0.1.txt")
|
905 | 1b9e1f42 | Michael Hanselmann | elif cmd == [self.CMD, "list"]: |
906 | 1b9e1f42 | Michael Hanselmann | if fail:
|
907 | 1b9e1f42 | Michael Hanselmann | self.fail("'xm list' shouldn't be called when 'xm info' failed") |
908 | 1b9e1f42 | Michael Hanselmann | else:
|
909 | 1b9e1f42 | Michael Hanselmann | output = testutils.ReadTestData("xen-xm-list-4.0.1-four-instances.txt")
|
910 | 1b9e1f42 | Michael Hanselmann | else:
|
911 | 1b9e1f42 | Michael Hanselmann | self.fail("Unhandled command: %s" % (cmd, )) |
912 | 1b9e1f42 | Michael Hanselmann | |
913 | 1b9e1f42 | Michael Hanselmann | return self._SuccessCommand(output, cmd) |
914 | 1b9e1f42 | Michael Hanselmann | |
915 | 1b9e1f42 | Michael Hanselmann | def testGetNodeInfo(self): |
916 | 1b9e1f42 | Michael Hanselmann | run_cmd = compat.partial(self._GetNodeInfoCmd, False) |
917 | 1b9e1f42 | Michael Hanselmann | hv = self._GetHv(run_cmd=run_cmd)
|
918 | 1b9e1f42 | Michael Hanselmann | result = hv.GetNodeInfo() |
919 | 1b9e1f42 | Michael Hanselmann | |
920 | 1b9e1f42 | Michael Hanselmann | self.assertEqual(result["hv_version"], (4, 0)) |
921 | 1b9e1f42 | Michael Hanselmann | self.assertEqual(result["memory_free"], 8004) |
922 | 1b9e1f42 | Michael Hanselmann | |
923 | 1b9e1f42 | Michael Hanselmann | def testGetNodeInfoFailing(self): |
924 | 1b9e1f42 | Michael Hanselmann | run_cmd = compat.partial(self._GetNodeInfoCmd, True) |
925 | 1b9e1f42 | Michael Hanselmann | hv = self._GetHv(run_cmd=run_cmd)
|
926 | 1b9e1f42 | Michael Hanselmann | self.assertTrue(hv.GetNodeInfo() is None) |
927 | 1b9e1f42 | Michael Hanselmann | |
928 | 74a50c46 | Michael Hanselmann | |
929 | 74a50c46 | Michael Hanselmann | def _MakeTestClass(cls, cmd): |
930 | 74a50c46 | Michael Hanselmann | """Makes a class for testing.
|
931 | 74a50c46 | Michael Hanselmann |
|
932 | 74a50c46 | Michael Hanselmann | The returned class has structure as shown in the following pseudo code:
|
933 | 74a50c46 | Michael Hanselmann |
|
934 | 74a50c46 | Michael Hanselmann | class Test{cls.__name__}{cmd}(_TestXenHypervisor, unittest.TestCase):
|
935 | 74a50c46 | Michael Hanselmann | TARGET = {cls}
|
936 | 74a50c46 | Michael Hanselmann | CMD = {cmd}
|
937 | 74a50c46 | Michael Hanselmann | HVNAME = {Hypervisor name retrieved using class}
|
938 | 74a50c46 | Michael Hanselmann |
|
939 | 74a50c46 | Michael Hanselmann | @type cls: class
|
940 | 74a50c46 | Michael Hanselmann | @param cls: Hypervisor class to be tested
|
941 | 74a50c46 | Michael Hanselmann | @type cmd: string
|
942 | 74a50c46 | Michael Hanselmann | @param cmd: Hypervisor command
|
943 | 74a50c46 | Michael Hanselmann | @rtype: tuple
|
944 | 74a50c46 | Michael Hanselmann | @return: Class name and class object (not instance)
|
945 | 74a50c46 | Michael Hanselmann |
|
946 | 74a50c46 | Michael Hanselmann | """
|
947 | 74a50c46 | Michael Hanselmann | name = "Test%sCmd%s" % (cls.__name__, cmd.title())
|
948 | 74a50c46 | Michael Hanselmann | bases = (_TestXenHypervisor, unittest.TestCase) |
949 | 74a50c46 | Michael Hanselmann | hvname = HVCLASS_TO_HVNAME[cls] |
950 | 74a50c46 | Michael Hanselmann | |
951 | 74a50c46 | Michael Hanselmann | return (name, type(name, bases, dict(TARGET=cls, CMD=cmd, HVNAME=hvname))) |
952 | 74a50c46 | Michael Hanselmann | |
953 | 74a50c46 | Michael Hanselmann | |
954 | 74a50c46 | Michael Hanselmann | # Create test classes programmatically instead of manually to reduce the risk
|
955 | 74a50c46 | Michael Hanselmann | # of forgetting some combinations
|
956 | 74a50c46 | Michael Hanselmann | for cls in [hv_xen.XenPvmHypervisor, hv_xen.XenHvmHypervisor]: |
957 | 74a50c46 | Michael Hanselmann | for cmd in constants.KNOWN_XEN_COMMANDS: |
958 | 74a50c46 | Michael Hanselmann | (name, testcls) = _MakeTestClass(cls, cmd) |
959 | 74a50c46 | Michael Hanselmann | |
960 | 74a50c46 | Michael Hanselmann | assert name not in locals() |
961 | 74a50c46 | Michael Hanselmann | |
962 | 74a50c46 | Michael Hanselmann | locals()[name] = testcls
|
963 | 74a50c46 | Michael Hanselmann | |
964 | 74a50c46 | Michael Hanselmann | |
965 | 55cc0a44 | Michael Hanselmann | if __name__ == "__main__": |
966 | 55cc0a44 | Michael Hanselmann | testutils.GanetiTestProgram() |