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