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