root / test / py / ganeti.client.gnt_instance_unittest.py @ b21d488b
History | View | Annotate | Download (8.2 kB)
1 | 25ce3ec4 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 25ce3ec4 | Michael Hanselmann | #
|
3 | 25ce3ec4 | Michael Hanselmann | |
4 | 25ce3ec4 | Michael Hanselmann | # Copyright (C) 2011 Google Inc.
|
5 | 25ce3ec4 | Michael Hanselmann | #
|
6 | 25ce3ec4 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | 25ce3ec4 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | 25ce3ec4 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | 25ce3ec4 | Michael Hanselmann | # (at your option) any later version.
|
10 | 25ce3ec4 | Michael Hanselmann | #
|
11 | 25ce3ec4 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | 25ce3ec4 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 25ce3ec4 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 25ce3ec4 | Michael Hanselmann | # General Public License for more details.
|
15 | 25ce3ec4 | Michael Hanselmann | #
|
16 | 25ce3ec4 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | 25ce3ec4 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | 25ce3ec4 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 25ce3ec4 | Michael Hanselmann | # 02110-1301, USA.
|
20 | 25ce3ec4 | Michael Hanselmann | |
21 | 25ce3ec4 | Michael Hanselmann | |
22 | 25ce3ec4 | Michael Hanselmann | """Script for testing ganeti.client.gnt_instance"""
|
23 | 25ce3ec4 | Michael Hanselmann | |
24 | 25ce3ec4 | Michael Hanselmann | import unittest |
25 | 25ce3ec4 | Michael Hanselmann | |
26 | 25ce3ec4 | Michael Hanselmann | from ganeti import constants |
27 | 25ce3ec4 | Michael Hanselmann | from ganeti import utils |
28 | 25ce3ec4 | Michael Hanselmann | from ganeti import errors |
29 | 25ce3ec4 | Michael Hanselmann | from ganeti import objects |
30 | 25ce3ec4 | Michael Hanselmann | from ganeti.client import gnt_instance |
31 | 25ce3ec4 | Michael Hanselmann | |
32 | 25ce3ec4 | Michael Hanselmann | import testutils |
33 | 25ce3ec4 | Michael Hanselmann | |
34 | 25ce3ec4 | Michael Hanselmann | |
35 | 25ce3ec4 | Michael Hanselmann | class TestConsole(unittest.TestCase): |
36 | 25ce3ec4 | Michael Hanselmann | def setUp(self): |
37 | 25ce3ec4 | Michael Hanselmann | self._output = []
|
38 | 25ce3ec4 | Michael Hanselmann | self._cmds = []
|
39 | 25ce3ec4 | Michael Hanselmann | self._next_cmd_exitcode = 0 |
40 | 25ce3ec4 | Michael Hanselmann | |
41 | abd42b0a | Michael Hanselmann | def _Test(self, console, show_command, cluster_name): |
42 | abd42b0a | Michael Hanselmann | return gnt_instance._DoConsole(console, show_command, cluster_name,
|
43 | 25ce3ec4 | Michael Hanselmann | feedback_fn=self._Feedback,
|
44 | 25ce3ec4 | Michael Hanselmann | _runcmd_fn=self._FakeRunCmd)
|
45 | 25ce3ec4 | Michael Hanselmann | |
46 | 25ce3ec4 | Michael Hanselmann | def _Feedback(self, msg, *args): |
47 | 25ce3ec4 | Michael Hanselmann | if args:
|
48 | 25ce3ec4 | Michael Hanselmann | msg = msg % args |
49 | 25ce3ec4 | Michael Hanselmann | self._output.append(msg)
|
50 | 25ce3ec4 | Michael Hanselmann | |
51 | 25ce3ec4 | Michael Hanselmann | def _FakeRunCmd(self, cmd, interactive=None): |
52 | 25ce3ec4 | Michael Hanselmann | self.assertTrue(interactive)
|
53 | 25ce3ec4 | Michael Hanselmann | self.assertTrue(isinstance(cmd, list)) |
54 | 25ce3ec4 | Michael Hanselmann | self._cmds.append(cmd)
|
55 | 25ce3ec4 | Michael Hanselmann | return utils.RunResult(self._next_cmd_exitcode, None, "", "", "cmd", |
56 | a4ccecf6 | Michael Hanselmann | utils.process._TIMEOUT_NONE, 5)
|
57 | 25ce3ec4 | Michael Hanselmann | |
58 | 25ce3ec4 | Michael Hanselmann | def testMessage(self): |
59 | 25ce3ec4 | Michael Hanselmann | cons = objects.InstanceConsole(instance="inst98.example.com",
|
60 | 25ce3ec4 | Michael Hanselmann | kind=constants.CONS_MESSAGE, |
61 | 25ce3ec4 | Michael Hanselmann | message="Hello World")
|
62 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(self._Test(cons, False, "cluster.example.com"), |
63 | 25ce3ec4 | Michael Hanselmann | constants.EXIT_SUCCESS) |
64 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._cmds), 0) |
65 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(self._output, ["Hello World"]) |
66 | 25ce3ec4 | Michael Hanselmann | |
67 | 25ce3ec4 | Michael Hanselmann | def testVnc(self): |
68 | 25ce3ec4 | Michael Hanselmann | cons = objects.InstanceConsole(instance="inst1.example.com",
|
69 | 25ce3ec4 | Michael Hanselmann | kind=constants.CONS_VNC, |
70 | 25ce3ec4 | Michael Hanselmann | host="node1.example.com",
|
71 | 25ce3ec4 | Michael Hanselmann | port=5901,
|
72 | 25ce3ec4 | Michael Hanselmann | display=1)
|
73 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(self._Test(cons, False, "cluster.example.com"), |
74 | 25ce3ec4 | Michael Hanselmann | constants.EXIT_SUCCESS) |
75 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._cmds), 0) |
76 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._output), 1) |
77 | 25ce3ec4 | Michael Hanselmann | self.assertTrue(" inst1.example.com " in self._output[0]) |
78 | 25ce3ec4 | Michael Hanselmann | self.assertTrue(" node1.example.com:5901 " in self._output[0]) |
79 | 25ce3ec4 | Michael Hanselmann | self.assertTrue("vnc://node1.example.com:5901/" in self._output[0]) |
80 | 25ce3ec4 | Michael Hanselmann | |
81 | 25ce3ec4 | Michael Hanselmann | def testSshShow(self): |
82 | 25ce3ec4 | Michael Hanselmann | cons = objects.InstanceConsole(instance="inst31.example.com",
|
83 | 25ce3ec4 | Michael Hanselmann | kind=constants.CONS_SSH, |
84 | 25ce3ec4 | Michael Hanselmann | host="node93.example.com",
|
85 | 25ce3ec4 | Michael Hanselmann | user="user_abc",
|
86 | 25ce3ec4 | Michael Hanselmann | command="xm console x.y.z")
|
87 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(self._Test(cons, True, "cluster.example.com"), |
88 | 25ce3ec4 | Michael Hanselmann | constants.EXIT_SUCCESS) |
89 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._cmds), 0) |
90 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._output), 1) |
91 | 25ce3ec4 | Michael Hanselmann | self.assertTrue(" user_abc@node93.example.com " in self._output[0]) |
92 | 25ce3ec4 | Michael Hanselmann | self.assertTrue("'xm console x.y.z'" in self._output[0]) |
93 | 25ce3ec4 | Michael Hanselmann | |
94 | 25ce3ec4 | Michael Hanselmann | def testSshRun(self): |
95 | 25ce3ec4 | Michael Hanselmann | cons = objects.InstanceConsole(instance="inst31.example.com",
|
96 | 25ce3ec4 | Michael Hanselmann | kind=constants.CONS_SSH, |
97 | 25ce3ec4 | Michael Hanselmann | host="node93.example.com",
|
98 | 25ce3ec4 | Michael Hanselmann | user="user_abc",
|
99 | 25ce3ec4 | Michael Hanselmann | command=["xm", "console", "x.y.z"]) |
100 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(self._Test(cons, False, "cluster.example.com"), |
101 | 25ce3ec4 | Michael Hanselmann | constants.EXIT_SUCCESS) |
102 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._cmds), 1) |
103 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._output), 0) |
104 | 25ce3ec4 | Michael Hanselmann | |
105 | 25ce3ec4 | Michael Hanselmann | # This is very important to prevent escapes from the console
|
106 | 25ce3ec4 | Michael Hanselmann | self.assertTrue("-oEscapeChar=none" in self._cmds[0]) |
107 | 25ce3ec4 | Michael Hanselmann | |
108 | 25ce3ec4 | Michael Hanselmann | def testSshRunFail(self): |
109 | 25ce3ec4 | Michael Hanselmann | cons = objects.InstanceConsole(instance="inst31.example.com",
|
110 | 25ce3ec4 | Michael Hanselmann | kind=constants.CONS_SSH, |
111 | 25ce3ec4 | Michael Hanselmann | host="node93.example.com",
|
112 | 25ce3ec4 | Michael Hanselmann | user="user_abc",
|
113 | 25ce3ec4 | Michael Hanselmann | command=["xm", "console", "x.y.z"]) |
114 | 25ce3ec4 | Michael Hanselmann | |
115 | 25ce3ec4 | Michael Hanselmann | self._next_cmd_exitcode = 100 |
116 | 25ce3ec4 | Michael Hanselmann | self.assertRaises(errors.OpExecError, self._Test, |
117 | 25ce3ec4 | Michael Hanselmann | cons, False, "cluster.example.com") |
118 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._cmds), 1) |
119 | 25ce3ec4 | Michael Hanselmann | self.assertEqual(len(self._output), 0) |
120 | 25ce3ec4 | Michael Hanselmann | |
121 | 25ce3ec4 | Michael Hanselmann | |
122 | a71f835e | Michael Hanselmann | class TestConvertNicDiskModifications(unittest.TestCase): |
123 | a71f835e | Michael Hanselmann | def test(self): |
124 | a71f835e | Michael Hanselmann | fn = gnt_instance._ConvertNicDiskModifications |
125 | a71f835e | Michael Hanselmann | |
126 | a71f835e | Michael Hanselmann | self.assertEqual(fn([]), [])
|
127 | a71f835e | Michael Hanselmann | |
128 | a71f835e | Michael Hanselmann | # Error cases
|
129 | a71f835e | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, fn, [
|
130 | a71f835e | Michael Hanselmann | (constants.DDM_REMOVE, { "param": "value", }), |
131 | a71f835e | Michael Hanselmann | ]) |
132 | a71f835e | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, fn, [
|
133 | a71f835e | Michael Hanselmann | (0, { constants.DDM_REMOVE: True, "param": "value", }), |
134 | a71f835e | Michael Hanselmann | ]) |
135 | a71f835e | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, fn, [
|
136 | a71f835e | Michael Hanselmann | (0, {
|
137 | a71f835e | Michael Hanselmann | constants.DDM_REMOVE: True,
|
138 | a71f835e | Michael Hanselmann | constants.DDM_ADD: True,
|
139 | a71f835e | Michael Hanselmann | }), |
140 | a71f835e | Michael Hanselmann | ]) |
141 | a71f835e | Michael Hanselmann | |
142 | a71f835e | Michael Hanselmann | # Legacy calls
|
143 | a71f835e | Michael Hanselmann | for action in constants.DDMS_VALUES: |
144 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
145 | a71f835e | Michael Hanselmann | (action, {}), |
146 | a71f835e | Michael Hanselmann | ]), [ |
147 | a71f835e | Michael Hanselmann | (action, -1, {}),
|
148 | a71f835e | Michael Hanselmann | ]) |
149 | 7a70541e | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, fn, [
|
150 | 7a70541e | Michael Hanselmann | (0, {
|
151 | 7a70541e | Michael Hanselmann | action: True,
|
152 | 7a70541e | Michael Hanselmann | constants.DDM_MODIFY: True,
|
153 | 7a70541e | Michael Hanselmann | }), |
154 | 7a70541e | Michael Hanselmann | ]) |
155 | a71f835e | Michael Hanselmann | |
156 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
157 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, { |
158 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: 1024,
|
159 | a71f835e | Michael Hanselmann | }), |
160 | a71f835e | Michael Hanselmann | ]), [ |
161 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, -1, {
|
162 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: 1024,
|
163 | a71f835e | Michael Hanselmann | }), |
164 | a71f835e | Michael Hanselmann | ]) |
165 | a71f835e | Michael Hanselmann | |
166 | a71f835e | Michael Hanselmann | # New-style calls
|
167 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
168 | a71f835e | Michael Hanselmann | (2, {
|
169 | a71f835e | Michael Hanselmann | constants.IDISK_MODE: constants.DISK_RDWR, |
170 | a71f835e | Michael Hanselmann | }), |
171 | a71f835e | Michael Hanselmann | ]), [ |
172 | a71f835e | Michael Hanselmann | (constants.DDM_MODIFY, 2, {
|
173 | a71f835e | Michael Hanselmann | constants.IDISK_MODE: constants.DISK_RDWR, |
174 | a71f835e | Michael Hanselmann | }), |
175 | a71f835e | Michael Hanselmann | ]) |
176 | a71f835e | Michael Hanselmann | |
177 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
178 | a71f835e | Michael Hanselmann | (0, {
|
179 | a71f835e | Michael Hanselmann | constants.DDM_ADD: True,
|
180 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: 4096,
|
181 | a71f835e | Michael Hanselmann | }), |
182 | a71f835e | Michael Hanselmann | ]), [ |
183 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, 0, {
|
184 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: 4096,
|
185 | a71f835e | Michael Hanselmann | }), |
186 | a71f835e | Michael Hanselmann | ]) |
187 | a71f835e | Michael Hanselmann | |
188 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
189 | a71f835e | Michael Hanselmann | (-1, {
|
190 | a71f835e | Michael Hanselmann | constants.DDM_REMOVE: True,
|
191 | a71f835e | Michael Hanselmann | }), |
192 | a71f835e | Michael Hanselmann | ]), [ |
193 | a71f835e | Michael Hanselmann | (constants.DDM_REMOVE, -1, {}),
|
194 | a71f835e | Michael Hanselmann | ]) |
195 | a71f835e | Michael Hanselmann | |
196 | 7a70541e | Michael Hanselmann | self.assertEqual(fn([
|
197 | 7a70541e | Michael Hanselmann | (-1, {
|
198 | 7a70541e | Michael Hanselmann | constants.DDM_MODIFY: True,
|
199 | 7a70541e | Michael Hanselmann | constants.IDISK_SIZE: 1024,
|
200 | 7a70541e | Michael Hanselmann | }), |
201 | 7a70541e | Michael Hanselmann | ]), [ |
202 | 7a70541e | Michael Hanselmann | (constants.DDM_MODIFY, -1, {
|
203 | 7a70541e | Michael Hanselmann | constants.IDISK_SIZE: 1024,
|
204 | 7a70541e | Michael Hanselmann | }), |
205 | 7a70541e | Michael Hanselmann | ]) |
206 | 7a70541e | Michael Hanselmann | |
207 | b21d488b | Christos Stavrakakis | # Names and UUIDs
|
208 | b21d488b | Christos Stavrakakis | self.assertEqual(fn([
|
209 | b21d488b | Christos Stavrakakis | ('name', {
|
210 | b21d488b | Christos Stavrakakis | constants.IDISK_MODE: constants.DISK_RDWR, |
211 | b21d488b | Christos Stavrakakis | constants.IDISK_NAME: "rename",
|
212 | b21d488b | Christos Stavrakakis | }), |
213 | b21d488b | Christos Stavrakakis | ]), [ |
214 | b21d488b | Christos Stavrakakis | (constants.DDM_MODIFY, 'name', {
|
215 | b21d488b | Christos Stavrakakis | constants.IDISK_MODE: constants.DISK_RDWR, |
216 | b21d488b | Christos Stavrakakis | constants.IDISK_NAME: "rename",
|
217 | b21d488b | Christos Stavrakakis | }), |
218 | b21d488b | Christos Stavrakakis | ]) |
219 | b21d488b | Christos Stavrakakis | self.assertEqual(fn([
|
220 | b21d488b | Christos Stavrakakis | ('024ef14d-4879-400e-8767-d61c051950bf', {
|
221 | b21d488b | Christos Stavrakakis | constants.DDM_MODIFY: True,
|
222 | b21d488b | Christos Stavrakakis | constants.IDISK_SIZE: 1024,
|
223 | b21d488b | Christos Stavrakakis | constants.IDISK_NAME: "name",
|
224 | b21d488b | Christos Stavrakakis | }), |
225 | b21d488b | Christos Stavrakakis | ]), [ |
226 | b21d488b | Christos Stavrakakis | (constants.DDM_MODIFY, '024ef14d-4879-400e-8767-d61c051950bf', {
|
227 | b21d488b | Christos Stavrakakis | constants.IDISK_SIZE: 1024,
|
228 | b21d488b | Christos Stavrakakis | constants.IDISK_NAME: "name",
|
229 | b21d488b | Christos Stavrakakis | }), |
230 | b21d488b | Christos Stavrakakis | ]) |
231 | b21d488b | Christos Stavrakakis | self.assertEqual(fn([
|
232 | b21d488b | Christos Stavrakakis | ('name', {
|
233 | b21d488b | Christos Stavrakakis | constants.DDM_REMOVE: True,
|
234 | b21d488b | Christos Stavrakakis | }), |
235 | b21d488b | Christos Stavrakakis | ]), [ |
236 | b21d488b | Christos Stavrakakis | (constants.DDM_REMOVE, 'name', {}),
|
237 | b21d488b | Christos Stavrakakis | ]) |
238 | b21d488b | Christos Stavrakakis | |
239 | a71f835e | Michael Hanselmann | |
240 | a71f835e | Michael Hanselmann | class TestParseDiskSizes(unittest.TestCase): |
241 | a71f835e | Michael Hanselmann | def test(self): |
242 | a71f835e | Michael Hanselmann | fn = gnt_instance._ParseDiskSizes |
243 | a71f835e | Michael Hanselmann | |
244 | a71f835e | Michael Hanselmann | self.assertEqual(fn([]), [])
|
245 | a71f835e | Michael Hanselmann | |
246 | a71f835e | Michael Hanselmann | # Missing size parameter
|
247 | a71f835e | Michael Hanselmann | self.assertRaises(errors.OpPrereqError, fn, [
|
248 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, 0, {}),
|
249 | a71f835e | Michael Hanselmann | ]) |
250 | a71f835e | Michael Hanselmann | |
251 | a71f835e | Michael Hanselmann | # Converting disk size
|
252 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
253 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, 11, {
|
254 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: "9G",
|
255 | a71f835e | Michael Hanselmann | }), |
256 | a71f835e | Michael Hanselmann | ]), [ |
257 | a71f835e | Michael Hanselmann | (constants.DDM_ADD, 11, {
|
258 | a71f835e | Michael Hanselmann | constants.IDISK_SIZE: 9216,
|
259 | a71f835e | Michael Hanselmann | }), |
260 | a71f835e | Michael Hanselmann | ]) |
261 | a71f835e | Michael Hanselmann | |
262 | a71f835e | Michael Hanselmann | # No size parameter
|
263 | a71f835e | Michael Hanselmann | self.assertEqual(fn([
|
264 | a71f835e | Michael Hanselmann | (constants.DDM_REMOVE, 11, {
|
265 | a71f835e | Michael Hanselmann | "other": "24M", |
266 | a71f835e | Michael Hanselmann | }), |
267 | a71f835e | Michael Hanselmann | ]), [ |
268 | a71f835e | Michael Hanselmann | (constants.DDM_REMOVE, 11, {
|
269 | a71f835e | Michael Hanselmann | "other": "24M", |
270 | a71f835e | Michael Hanselmann | }), |
271 | a71f835e | Michael Hanselmann | ]) |
272 | a71f835e | Michael Hanselmann | |
273 | a71f835e | Michael Hanselmann | |
274 | 25ce3ec4 | Michael Hanselmann | if __name__ == "__main__": |
275 | 25ce3ec4 | Michael Hanselmann | testutils.GanetiTestProgram() |