Merge lp:~rohitk/nova/libvirt_unittests into lp:~hudson-openstack/nova/trunk
- libvirt_unittests
- Merge into trunk
Status: | Work in progress | ||||
---|---|---|---|---|---|
Proposed branch: | lp:~rohitk/nova/libvirt_unittests | ||||
Merge into: | lp:~hudson-openstack/nova/trunk | ||||
Diff against target: |
2005 lines (+1769/-59) 3 files modified
Authors (+1/-0) nova/tests/fake_network.py (+3/-0) nova/tests/test_libvirt.py (+1765/-59) |
||||
To merge this branch: | bzr merge lp:~rohitk/nova/libvirt_unittests | ||||
Related bugs: |
|
||||
Related blueprints: |
Diablo Testing
(Medium)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Brian Lamar (community) | Needs Fixing | ||
Matt Dietz (community) | Approve | ||
William Wolf (community) | Approve | ||
Jay Pipes (community) | Approve | ||
Review via email: mp+68144@code.launchpad.net |
Commit message
Description of the change
I have added the following test methods for LibvirtConnTestCase class in tests/test_
This also fixes bugs in the test_spawn_
Code coverage for libvirt/
Some tests (#72,73) have been skipped due to Bug:807764.
This effort is towards the Diablo-testing blueprint (https:/
1 test_live_
2 test_live_
3 test_spawn_
4 test_spawn_
5 test_destroy
6 test_destroy_
7 test_destroy_
8 test_destroy_
9 test_destroy_
10 test_reboot
11 test_reboot_
12 test_list_
13 test_list_
14 test_pause
15 test_unpause
16 test_suspend
17 test_resume
18 test_rescue
19 test_rescue_
20 test_unrescue
21 test_attach_volume
22 test_attach_
23 test_detach_volume
24 test_detach_
25 test_get_disks
26 test_get_
27 test_get_interfaces
28 test_get_
29 test_get_info
30 test_get_cpu_info
31 test_get_
32 test_get_
33 test_compare_cpu
34 test_compare_
35 test_compare_
36 test_init_
37 test_init_
38 test_init_
39 test_get_
40 test_get_
41 test_get_
42 test_get_
43 test_get_
44 test_get_
45 test_get_
46 test_get_
47 test_block_stats
48 test_interface_
49 test_get_
50 test_refresh_
51 test_refresh_
52 test_refresh_
53 test_unfilter_
54 test_get_vcpu_used
55 test_cache_
56 test_cache_
57 test_fetch_
58 test_fetch_
59 test_create_local
60 test_lookup_by_name
61 test_lookup_
62 test_lookup_
63 test_get_disk_xml
64 test_get_
65 test_flush_
66 test_flush_
67 test_append_to_file
68 test_dump_file
69 test_volume_
70 test_volume_
71 test_create_
72 test_create_
73 test_create_
74 test_get_connection
75 test_test_
76 test_test_
77 test_test_
78 test_connect_
79 test_connect_
80 test_map_
81 test_cleanup
Rohit Karajgi (rohitk) wrote : | # |
Fixed, however, please use latest pep8 (0.6.1). The code was validated for pep8 0.6.1. Thanks!
Brian Lamar (blamar) wrote : | # |
22 +from StringIO import StringIO
Although other imports in the file don't follow this, we should only be importing modules, not classes. So technically this should be just "import StringIO"
44 + 'injected': 1}
Minor nitpick, this indent should be consistent with the others.
116 + except_msg = "Test failed with an unknown exception"
This is an interesting strategy, but I would suggest when something happens and you don't know what went wrong to just do a self.fail(
For example:
524 + try:
525 + conn.spawn(
526 + except Exception, e:
527 + fail = 1
528 + time.sleep(1)
529 +
530 + self.assertFals
Would give me a generic failure message, but:
524 + try:
525 + conn.spawn(
526 + except Exception as err:
527 + self.fail(err)
Would give the reason, while still making sure the test failed.
Lines 300 - 307, you shouldn't need those backslashes and if you could, where applicable, line up parameters under the space after the method's opening "(" like this:
300 + conn.firewall_
301 + self.fake_none)
In all of the tests, FLAGS are auto-magically reset, so feel free to just edit them directly:
348 + fake_flag = 'VIR_MIGRATE_
349 + self.mox.
350 + connection.
Could safely become:
348 + migration_flag = 'VIR_MIGRATE_
349 + FLAGS.live_
There are existing structures for Mocks and Stubs, so this:
451 + self.mox.
452 + connection.
could just be:
451 + self.stubs.
if you wanted to do it in a single line.
As a general comment, if you find yourself repeating test "setup", such as creating a connection and setting some test parameters, you might consider breaking similar tests into their own TestCase classes and putting the common code in setUp().
I'm going to keep looking at this, as it's a good number of tests...but hopefully this will kick off some discussion.
Thanks for the tests!
Brian Lamar (blamar) wrote : | # |
test_get_cpu_info
test_get_
test_get_
test_get_disks
test_get_interfaces
test_get_
These tests are failing when libvirt isn't installed on the testing machine. Can you skip these tests if libvirt isn't installed?
Rohit Karajgi (rohitk) wrote : | # |
Thanks Brian for the comments till now. I'll make those fixes wherever applicable.
Rohit Karajgi (rohitk) wrote : | # |
Brian, request you to please review the branch for merging. I have implemented all the review comments by you. Thanks.
Jay Pipes (jaypipes) wrote : | # |
Hi Rohit!
Awesome work on these tests :)
One little nit from me...
import shutil
import sys
+import time
import tempfile
+import StringIO
from xml.etree.
That actually needs to be in alphabetical order, and StringIO is a standard lib module and so should be above tempfile import..., so like this:
import shutil
import StringIO
import sys
import tempfile
import time
from xml.etree.
Cheers!
jay
Rohit Karajgi (rohitk) wrote : | # |
Thanks Jay. Fixed the import sequence and other regressions cropped due to trunk changes. All tests have passed at my end.
Brian Lamar (blamar) wrote : | # |
Getting 3 test errors: http://
Rohit Karajgi (rohitk) wrote : | # |
The first two errors are fixed. I am not able to reproduce the third error, from test_virt_
Please review branch for modified files. Thanks!
William Wolf (throughnothing) wrote : | # |
I'm getting the third error with your most recent changes as well from a fresh, unmodified checkout. Not sure why you're not getting it.
William Wolf (throughnothing) wrote : | # |
One thought, make sure you run the entire test suite, not just that one file or class of tests....it may be conflicting with something run in other tests...which would be bad.
Rohit Karajgi (rohitk) wrote : | # |
Hi William,
The issue was with the method test_map_
I have run the full suite and tests have passed. Please review.
Rohit Karajgi (rohitk) wrote : | # |
Run tests output at http://
William Wolf (throughnothing) wrote : | # |
Everything passes for me now Rohit. Thanks, looks good.
Matt Dietz (cerberus) wrote : | # |
Awesome to see a new collection of unit tests in the project! Everything passes for me.
Brian Lamar (blamar) wrote : | # |
Hey, I gave some bad advice earlier, which is 100% my fault. Unfortunately I was wrong when I said that you should set flags like "FLAGS.
self.flags(
So just to be safe I think that these tests should follow that principle. Have you spoken with Soren? He also has a major branch for libvirt tests (lp:~soren/nova/virt-test-improvements), although I don't know how much they will conflict.
Sorry for taking so long to get back to you on this branch!
Rohit Karajgi (rohitk) wrote : | # |
> self.flags(
>
> So just to be safe I think that these tests should follow that principle.
>
Changed wherever applicable.
> Have you spoken with Soren? He also has a major branch for libvirt tests
> (lp:~soren/nova/virt-test-improvements), although I don't know how much they
> will conflict.
I just checked Soren's branch. Although there isn't any conflicting code or tests, I think the fake images library he has added will help me with not having to create the fakes in test_libvirt. I am putting the branch push on hold till I sync with Soren.
Thanks for checking my branch.
Unmerged revisions
- 1258. By Rohit Karajgi
-
Removed failing skipped tests till Bugs 853602 and 854009 are fixed
- 1257. By Rohit Karajgi
-
Merge Trunk
- 1256. By Rohit Karajgi
-
post merge
- 1255. By Rohit Karajgi
-
added fake gateway_v6
- 1254. By Rohit Karajgi
-
Fixed import sequence and some regressions
- 1253. By Rohit Karajgi
-
Merge trunk
- 1252. By Rohit Karajgi
-
Resolved conflicts on merge
- 1251. By Rohit Karajgi
-
Merge Trunk
- 1250. By Rohit Karajgi
-
Modified affected tests after changes to virt.connection due to Midokura's network refactoring branch
- 1249. By Rohit Karajgi
-
Merge trunk
Preview Diff
1 | === modified file 'Authors' |
2 | --- Authors 2011-09-08 18:47:56 +0000 |
3 | +++ Authors 2011-09-19 17:37:28 +0000 |
4 | @@ -94,6 +94,7 @@ |
5 | Rick Clark <rick@openstack.org> |
6 | Rick Harris <rconradharris@gmail.com> |
7 | Rob Kost <kost@isi.edu> |
8 | +Rohit Karajgi <rohit.karajgi@vertex.co.in> |
9 | Ryan Lane <rlane@wikimedia.org> |
10 | Ryan Lucio <rlucio@internap.com> |
11 | Ryu Ishimoto <ryu@midokura.jp> |
12 | |
13 | === modified file 'nova/tests/fake_network.py' |
14 | --- nova/tests/fake_network.py 2011-09-13 19:48:10 +0000 |
15 | +++ nova/tests/fake_network.py 2011-09-19 17:37:28 +0000 |
16 | @@ -54,6 +54,9 @@ |
17 | 'extra_params': 'fake', |
18 | } |
19 | |
20 | + def unplug(self, *args): |
21 | + return True |
22 | + |
23 | |
24 | class FakeModel(dict): |
25 | """Represent a model from the db""" |
26 | |
27 | === modified file 'nova/tests/test_libvirt.py' |
28 | --- nova/tests/test_libvirt.py 2011-09-19 14:22:34 +0000 |
29 | +++ nova/tests/test_libvirt.py 2011-09-19 17:37:28 +0000 |
30 | @@ -20,8 +20,10 @@ |
31 | import os |
32 | import re |
33 | import shutil |
34 | +import StringIO |
35 | import sys |
36 | import tempfile |
37 | +import time |
38 | |
39 | from xml.etree.ElementTree import fromstring as xml_to_tree |
40 | from xml.dom.minidom import parseString as xml_to_dom |
41 | @@ -39,6 +41,8 @@ |
42 | from nova.virt.libvirt import connection |
43 | from nova.virt.libvirt import firewall |
44 | from nova.tests import fake_network |
45 | +from nova.tests import fake_utils |
46 | + |
47 | |
48 | libvirt = None |
49 | FLAGS = flags.FLAGS |
50 | @@ -68,14 +72,25 @@ |
51 | self._fake_dom_xml = fake_xml |
52 | else: |
53 | self._fake_dom_xml = """ |
54 | - <domain type='kvm'> |
55 | - <devices> |
56 | - <disk type='file'> |
57 | - <source file='filename'/> |
58 | - </disk> |
59 | - </devices> |
60 | - </domain> |
61 | - """ |
62 | + <domain type='kvm'> |
63 | + <devices> |
64 | + <disk type='file'> |
65 | + <source file='filename'/> |
66 | + <target dev='vdb' bus='ide'/> |
67 | + </disk> |
68 | + <interface type='bridge'> |
69 | + <source bridge='br0'/> |
70 | + <target dev='vnet'/> |
71 | + <mac address="00:11:22:33:44:55"/> |
72 | + </interface> |
73 | + <serial type='pty'> |
74 | + <source path='/dev/pts/3'/> |
75 | + <target port='0'/> |
76 | + </serial> |
77 | + <graphics type='vnc' port='5904'/> |
78 | + </devices> |
79 | + </domain> |
80 | + """ |
81 | |
82 | def snapshotCreateXML(self, *args): |
83 | return FakeVirDomainSnapshot(self) |
84 | @@ -86,6 +101,64 @@ |
85 | def XMLDesc(self, *args): |
86 | return self._fake_dom_xml |
87 | |
88 | + def create(self): |
89 | + pass |
90 | + |
91 | + def managedSave(self, *args): |
92 | + pass |
93 | + |
94 | + def destroy(self, *args): |
95 | + pass |
96 | + |
97 | + def undefine(self, *args): |
98 | + pass |
99 | + |
100 | + def reboot(self, *args): |
101 | + pass |
102 | + |
103 | + def pause(self, *args): |
104 | + pass |
105 | + |
106 | + def unpause(self, *args): |
107 | + pass |
108 | + |
109 | + def suspend(self, *args): |
110 | + pass |
111 | + |
112 | + def resume(self, *args): |
113 | + pass |
114 | + |
115 | + def info(self, *args): |
116 | + return {'state': 1, |
117 | + 'max_mem': 10, |
118 | + 'mem': 10, |
119 | + 'num_cpu': 1, |
120 | + 'cpu_time': 1} |
121 | + |
122 | + def setAutostart(self, *args): |
123 | + pass |
124 | + |
125 | + def attachDevice(self, *args): |
126 | + pass |
127 | + |
128 | + def detachDevice(self, *args): |
129 | + pass |
130 | + |
131 | + def compareCPU(self, *args): |
132 | + return 1 |
133 | + |
134 | + def blockStats(self, *args): |
135 | + pass |
136 | + |
137 | + def interfaceStats(self, *args): |
138 | + pass |
139 | + |
140 | + def migrateToURI(self, *args): |
141 | + pass |
142 | + |
143 | + def name(self, *args): |
144 | + return 'fake' |
145 | + |
146 | |
147 | class CacheConcurrencyTestCase(test.TestCase): |
148 | def setUp(self): |
149 | @@ -98,11 +171,8 @@ |
150 | return True |
151 | return False |
152 | |
153 | - def fake_execute(*args, **kwargs): |
154 | - pass |
155 | - |
156 | self.stubs.Set(os.path, 'exists', fake_exists) |
157 | - self.stubs.Set(utils, 'execute', fake_execute) |
158 | + fake_utils.stub_out_utils_execute(self.stubs) |
159 | |
160 | def test_same_fname_concurrency(self): |
161 | """Ensures that the same fname cache runs at a sequentially""" |
162 | @@ -146,7 +216,6 @@ |
163 | |
164 | |
165 | class LibvirtConnTestCase(test.TestCase): |
166 | - |
167 | def setUp(self): |
168 | super(LibvirtConnTestCase, self).setUp() |
169 | connection._late_load_cheetah() |
170 | @@ -159,6 +228,7 @@ |
171 | self.flags(instances_path='') |
172 | self.call_libvirt_dependant_setup = False |
173 | |
174 | + test_ip = '10.11.12.13' |
175 | test_instance = {'memory_kb': '1024000', |
176 | 'basepath': '/some/path', |
177 | 'bridge_name': 'br100', |
178 | @@ -186,12 +256,13 @@ |
179 | def create_fake_libvirt_mock(self, **kwargs): |
180 | """Defining mocks for LibvirtConnection(libvirt is not used).""" |
181 | |
182 | - # A fake libvirt.virConnect |
183 | class FakeLibvirtConnection(object): |
184 | + def createXML(self, *args): |
185 | + pass |
186 | + |
187 | def defineXML(self, xml): |
188 | return FakeVirtDomain() |
189 | |
190 | - # Creating mocks |
191 | fake = FakeLibvirtConnection() |
192 | # Customizing above fake if necessary |
193 | for key, val in kwargs.items(): |
194 | @@ -201,11 +272,13 @@ |
195 | fw_driver = "nova.tests.fake_network.FakeIptablesFirewallDriver" |
196 | self.flags(firewall_driver=fw_driver) |
197 | self.flags(libvirt_vif_driver="nova.tests.fake_network.FakeVIFDriver") |
198 | - |
199 | self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
200 | connection.LibvirtConnection._conn = fake |
201 | |
202 | - def fake_lookup(self, instance_name): |
203 | + def fake_none(self, *args, **kwargs): |
204 | + return |
205 | + |
206 | + def fake_lookup(self, *args): |
207 | return FakeVirtDomain() |
208 | |
209 | def fake_execute(self, *args): |
210 | @@ -490,6 +563,7 @@ |
211 | (lambda t: _ipv4_like(t.find(parameter).get('value'), '192.168'), |
212 | True), |
213 | (lambda t: t.findall(parameter)[1].get('name'), 'DHCPSERVER'), |
214 | + (lambda t: t.findall(parameter)[1].get('value'), '192.168.0.1'), |
215 | (lambda t: _ipv4_like(t.findall(parameter)[1].get('value'), |
216 | '192.168.*.1'), True), |
217 | (lambda t: t.find('./devices/serial/source').get( |
218 | @@ -609,17 +683,12 @@ |
219 | |
220 | def test_ensure_filtering_rules_for_instance_timeout(self): |
221 | """ensure_filtering_fules_for_instance() finishes with timeout.""" |
222 | - # Skip if non-libvirt environment |
223 | if not self.lazy_load_library_exists(): |
224 | return |
225 | |
226 | - # Preparing mocks |
227 | def fake_none(self, *args): |
228 | return |
229 | |
230 | - def fake_raise(self): |
231 | - raise libvirt.libvirtError('ERR') |
232 | - |
233 | class FakeTime(object): |
234 | def __init__(self): |
235 | self.counter = 0 |
236 | @@ -633,13 +702,15 @@ |
237 | instance_ref = db.instance_create(self.context, self.test_instance) |
238 | network_info = _fake_network_info(self.stubs, 1) |
239 | |
240 | - # Start test |
241 | self.mox.ReplayAll() |
242 | try: |
243 | conn = connection.LibvirtConnection(False) |
244 | - conn.firewall_driver.setattr('setup_basic_filtering', fake_none) |
245 | - conn.firewall_driver.setattr('prepare_instance_filter', fake_none) |
246 | - conn.firewall_driver.setattr('instance_filter_exists', fake_none) |
247 | + conn.firewall_driver.setattr('setup_basic_filtering', |
248 | + self.fake_none) |
249 | + conn.firewall_driver.setattr('prepare_instance_filter', |
250 | + self.fake_none) |
251 | + conn.firewall_driver.setattr('instance_filter_exists', |
252 | + self.fake_none) |
253 | conn.ensure_filtering_rules_for_instance(instance_ref, |
254 | network_info, |
255 | time=fake_timer) |
256 | @@ -652,13 +723,57 @@ |
257 | |
258 | db.instance_destroy(self.context, instance_ref['id']) |
259 | |
260 | + def test_live_migration_readonly_connection(self): |
261 | + if not self.lazy_load_library_exists(): |
262 | + return |
263 | + |
264 | + self.compute = utils.import_object(FLAGS.compute_manager) |
265 | + instance_dict = {'host': 'fake', 'state': power_state.RUNNING, |
266 | + 'state_description': 'running'} |
267 | + instance_ref = db.instance_create(self.context, self.test_instance) |
268 | + instance_ref = db.instance_update(self.context, instance_ref['id'], |
269 | + instance_dict) |
270 | + |
271 | + migration_flag = 'VIR_MIGRATE_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER' |
272 | + FLAGS.live_migration_flag = migration_flag |
273 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
274 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
275 | + self.mox.ReplayAll() |
276 | + conn = connection.LibvirtConnection(True) |
277 | + try: |
278 | + conn._live_migration(self.context, instance_ref, 'dest', |
279 | + self.fake_none, self.compute.rollback_live_migration) |
280 | + except Exception, err: |
281 | + self.fail(err) |
282 | + |
283 | + def test_live_migration_authorized_connection(self): |
284 | + if not self.lazy_load_library_exists(): |
285 | + return |
286 | + |
287 | + self.compute = utils.import_object(FLAGS.compute_manager) |
288 | + instance_dict = {'host': 'fake', 'state': power_state.RUNNING, |
289 | + 'state_description': 'running'} |
290 | + instance_ref = db.instance_create(self.context, self.test_instance) |
291 | + instance_ref = db.instance_update(self.context, instance_ref['id'], |
292 | + instance_dict) |
293 | + |
294 | + migration_flag = 'VIR_MIGRATE_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER' |
295 | + FLAGS.live_migration_flag = migration_flag |
296 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
297 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
298 | + self.mox.ReplayAll() |
299 | + conn = connection.LibvirtConnection(False) |
300 | + try: |
301 | + conn._live_migration(self.context, instance_ref, 'dest', |
302 | + self.fake_none, self.compute.rollback_live_migration) |
303 | + except Exception, err: |
304 | + self.fail(err) |
305 | + |
306 | def test_live_migration_raises_exception(self): |
307 | """Confirms recover method is called when exceptions are raised.""" |
308 | - # Skip if non-libvirt environment |
309 | if not self.lazy_load_library_exists(): |
310 | return |
311 | |
312 | - # Preparing data |
313 | self.compute = utils.import_object(FLAGS.compute_manager) |
314 | instance_dict = {'host': 'fake', |
315 | 'power_state': power_state.RUNNING, |
316 | @@ -671,13 +786,12 @@ |
317 | db.volume_attached(self.context, volume_ref['id'], instance_ref['id'], |
318 | '/dev/fake') |
319 | |
320 | - # Preparing mocks |
321 | vdmock = self.mox.CreateMock(libvirt.virDomain) |
322 | self.mox.StubOutWithMock(vdmock, "migrateToURI") |
323 | vdmock.migrateToURI(FLAGS.live_migration_uri % 'dest', |
324 | mox.IgnoreArg(), |
325 | - None, FLAGS.live_migration_bandwidth).\ |
326 | - AndRaise(libvirt.libvirtError('ERR')) |
327 | + None, FLAGS.live_migration_bandwidth).AndRaise( |
328 | + libvirt.libvirtError('ERR')) |
329 | |
330 | def fake_lookup(instance_name): |
331 | if instance_name == instance_ref.name: |
332 | @@ -691,7 +805,6 @@ |
333 | self.compute.rollback_live_migration(self.context, instance_ref, |
334 | 'dest', False) |
335 | |
336 | - #start test |
337 | self.mox.ReplayAll() |
338 | conn = connection.LibvirtConnection(False) |
339 | self.assertRaises(libvirt.libvirtError, |
340 | @@ -798,39 +911,1633 @@ |
341 | db.instance_destroy(self.context, instance_ref['id']) |
342 | |
343 | def test_spawn_with_network_info(self): |
344 | - # Skip if non-libvirt environment |
345 | - if not self.lazy_load_library_exists(): |
346 | - return |
347 | - |
348 | - # Preparing mocks |
349 | - def fake_none(self, instance): |
350 | - return |
351 | - |
352 | - self.create_fake_libvirt_mock() |
353 | - instance = db.instance_create(self.context, self.test_instance) |
354 | - |
355 | - # Start test |
356 | - self.mox.ReplayAll() |
357 | - conn = connection.LibvirtConnection(False) |
358 | - conn.firewall_driver.setattr('setup_basic_filtering', fake_none) |
359 | - conn.firewall_driver.setattr('prepare_instance_filter', fake_none) |
360 | - |
361 | - network_info = _fake_network_info(self.stubs, 1) |
362 | - |
363 | - try: |
364 | - conn.spawn(self.context, instance, network_info) |
365 | - except Exception, e: |
366 | - count = (0 <= str(e.message).find('Unexpected method call')) |
367 | - |
368 | - shutil.rmtree(os.path.join(FLAGS.instances_path, instance.name)) |
369 | - shutil.rmtree(os.path.join(FLAGS.instances_path, '_base')) |
370 | + '''Test spawn method with network info passed''' |
371 | + if not self.lazy_load_library_exists(): |
372 | + return |
373 | + |
374 | + instance = db.instance_create(self.context, self.test_instance) |
375 | + |
376 | + def fake_get_info(*args): |
377 | + return {'state': 1} |
378 | + |
379 | + def fake_method(*args, **kwargs): |
380 | + return |
381 | + |
382 | + self.create_fake_libvirt_mock() |
383 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
384 | + self.stubs.Set(connection.LibvirtConnection, 'to_xml', fake_method) |
385 | + network_info = _fake_network_info(self.stubs, 1) |
386 | + self.mox.ReplayAll() |
387 | + |
388 | + conn = connection.LibvirtConnection(False) |
389 | + conn._create_image = self.fake_none |
390 | + conn._create_new_domain = self.fake_lookup |
391 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
392 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
393 | + conn.firewall_driver.setattr('apply_instance_filter', self.fake_none) |
394 | + FLAGS.start_guests_on_host_boot = 1 |
395 | + |
396 | + try: |
397 | + conn.spawn(self.context, instance, network_info) |
398 | + except Exception, err: |
399 | + self.fail(err) |
400 | + time.sleep(1) |
401 | + |
402 | + def test_spawn_with_network_info_instance_not_found_error(self): |
403 | + '''Test spawn method when no instance state info is returned''' |
404 | + if not self.lazy_load_library_exists(): |
405 | + return |
406 | + |
407 | + instance = db.instance_create(self.context, self.test_instance) |
408 | + |
409 | + def fake_get_info(*args): |
410 | + raise exception.NotFound |
411 | + |
412 | + def fake_method(*args, **kwargs): |
413 | + return |
414 | + |
415 | + self.create_fake_libvirt_mock() |
416 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
417 | + self.stubs.Set(connection.LibvirtConnection, 'to_xml', fake_method) |
418 | + network_info = _fake_network_info(self.stubs, 1) |
419 | + self.mox.ReplayAll() |
420 | + |
421 | + conn = connection.LibvirtConnection(False) |
422 | + conn._create_image = self.fake_none |
423 | + conn._create_new_domain = self.fake_lookup |
424 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
425 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
426 | + conn.firewall_driver.setattr('apply_instance_filter', self.fake_none) |
427 | + |
428 | + FLAGS.start_guests_on_host_boot = 1 |
429 | + try: |
430 | + conn.spawn(self.context, instance, network_info) |
431 | + except Exception, err: |
432 | + self.fail(err) |
433 | + time.sleep(1) |
434 | |
435 | def test_get_host_ip_addr(self): |
436 | + '''Test correct ip is returned''' |
437 | conn = connection.LibvirtConnection(False) |
438 | ip = conn.get_host_ip_addr() |
439 | self.assertEquals(ip, FLAGS.my_ip) |
440 | |
441 | + def test_destroy(self): |
442 | + '''Test destroy method for a valid instance''' |
443 | + if not self.lazy_load_library_exists(): |
444 | + return |
445 | + instance_ref = db.instance_create(self.context, self.test_instance) |
446 | + network_info = _fake_network_info(self.stubs, 1) |
447 | + |
448 | + def fake_get_info(*args): |
449 | + return {'state': '5'} |
450 | + |
451 | + self.create_fake_libvirt_mock(lookupByName=self.fake_lookup) |
452 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
453 | + self.mox.ReplayAll() |
454 | + conn = connection.LibvirtConnection(False) |
455 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
456 | + try: |
457 | + result = conn.destroy(instance_ref, network_info) |
458 | + except Exception, err: |
459 | + self.fail(err) |
460 | + |
461 | + time.sleep(1) |
462 | + self.assertTrue(result) |
463 | + |
464 | + def test_destroy_raises_loopingcall_done(self): |
465 | + '''Test destroy method when instance is destroyed properly''' |
466 | + if not self.lazy_load_library_exists(): |
467 | + return |
468 | + instance_ref = db.instance_create(self.context, self.test_instance) |
469 | + network_info = _fake_network_info(self.stubs, 1) |
470 | + |
471 | + def fake_get_info(*args): |
472 | + raise exception.NotFound |
473 | + |
474 | + self.create_fake_libvirt_mock(lookupByName=self.fake_lookup) |
475 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
476 | + self.mox.ReplayAll() |
477 | + conn = connection.LibvirtConnection(False) |
478 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
479 | + try: |
480 | + result = conn.destroy(instance_ref, network_info) |
481 | + except Exception, err: |
482 | + self.fail(err) |
483 | + |
484 | + time.sleep(1) |
485 | + self.assertTrue(result) |
486 | + |
487 | + def test_destroy_no_domain_exception(self): |
488 | + '''Negative test to check exception if invalid instance is passed''' |
489 | + if not self.lazy_load_library_exists(): |
490 | + return |
491 | + |
492 | + instance_ref = {'name': 'Invalid'} |
493 | + network_info = _fake_network_info(self.stubs, 1) |
494 | + |
495 | + self.create_fake_libvirt_mock() |
496 | + self.mox.StubOutWithMock(connection.LibvirtConnection, |
497 | + '_lookup_by_name') |
498 | + connection.LibvirtConnection._lookup_by_name(mox.IgnoreArg() |
499 | + ).AndRaise(exception.NotFound) |
500 | + self.mox.ReplayAll() |
501 | + conn = connection.LibvirtConnection(False) |
502 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
503 | + try: |
504 | + result = conn.destroy(instance_ref, network_info) |
505 | + except Exception, err: |
506 | + self.fail(err) |
507 | + |
508 | + self.assertTrue(result) |
509 | + |
510 | + def test_destroy_undefine_raises_libvirt_error_already_shutoff(self): |
511 | + '''Test destroy raises libvirt error during undefine''' |
512 | + if not self.lazy_load_library_exists(): |
513 | + return |
514 | + instance_ref = db.instance_create(self.context, self.test_instance) |
515 | + network_info = _fake_network_info(self.stubs, 1) |
516 | + |
517 | + def fake_get_last_error(): |
518 | + return [55, 0, 'fake message'] |
519 | + |
520 | + def fake_lookup(self, *args): |
521 | + |
522 | + class FakeVirtDomain: |
523 | + def __init__(self): |
524 | + pass |
525 | + |
526 | + def destroy(self): |
527 | + raise libvirt.libvirtError('virDomainDestroy() failed', |
528 | + conn=self) |
529 | + |
530 | + def info(self): |
531 | + return (5, 10, 10, 1, 1) |
532 | + |
533 | + def undefine(self): |
534 | + raise libvirt.libvirtError('virDomainDestroy() failed', |
535 | + conn=self) |
536 | + |
537 | + return FakeVirtDomain() |
538 | + |
539 | + self.stubs.Set(connection.LibvirtConnection, '_lookup_by_name', |
540 | + fake_lookup) |
541 | + self.stubs.Set(connection.libvirt, 'virGetLastError', |
542 | + fake_get_last_error) |
543 | + self.mox.ReplayAll() |
544 | + conn = connection.LibvirtConnection(False) |
545 | + self.assertRaises(libvirt.libvirtError, conn.destroy, instance_ref, |
546 | + network_info) |
547 | + |
548 | + def test_destroy_raises_libvirt_error_running_state(self): |
549 | + '''Test destroy raises libvirt error, instance is still running''' |
550 | + if not self.lazy_load_library_exists(): |
551 | + return |
552 | + instance_ref = db.instance_create(self.context, self.test_instance) |
553 | + network_info = _fake_network_info(self.stubs, 1) |
554 | + |
555 | + def fake_get_last_error(): |
556 | + return [55, 0, 'fake message'] |
557 | + |
558 | + def fake_lookup(self, *args): |
559 | + |
560 | + class FakeVirtDomain: |
561 | + def __init__(self): |
562 | + pass |
563 | + |
564 | + def destroy(self): |
565 | + raise libvirt.libvirtError('virDomainDestroy() failed,', |
566 | + conn=self) |
567 | + |
568 | + def info(self): |
569 | + return (1, 10, 10, 1, 1) |
570 | + |
571 | + return FakeVirtDomain() |
572 | + |
573 | + self.stubs.Set(connection.LibvirtConnection, '_lookup_by_name', |
574 | + fake_lookup) |
575 | + self.stubs.Set(connection.libvirt, 'virGetLastError', |
576 | + fake_get_last_error) |
577 | + self.mox.ReplayAll() |
578 | + conn = connection.LibvirtConnection(False) |
579 | + self.assertRaises(libvirt.libvirtError, conn.destroy, instance_ref, |
580 | + network_info) |
581 | + |
582 | + def test_reboot(self): |
583 | + '''Test reboot method insrance rebooted successfully''' |
584 | + if not self.lazy_load_library_exists(): |
585 | + return |
586 | + instance_ref = db.instance_create(self.context, self.test_instance) |
587 | + network_info = _fake_network_info(self.stubs, 1) |
588 | + |
589 | + def fake_get_info(*args): |
590 | + return {'state': 1} |
591 | + |
592 | + self.create_fake_libvirt_mock(lookupByName=self.fake_lookup) |
593 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
594 | + self.mox.ReplayAll() |
595 | + |
596 | + conn = connection.LibvirtConnection(False) |
597 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
598 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
599 | + conn.firewall_driver.setattr('apply_instance_filter', self.fake_none) |
600 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
601 | + try: |
602 | + conn.reboot(instance_ref, network_info, 'fake') |
603 | + except Exception, err: |
604 | + self.fail(err) |
605 | + time.sleep(1) |
606 | + |
607 | + def test_reboot_fails_instance_not_found(self): |
608 | + '''Test reboot method instance disappears''' |
609 | + if not self.lazy_load_library_exists(): |
610 | + return |
611 | + instance_ref = db.instance_create(self.context, self.test_instance) |
612 | + network_info = _fake_network_info(self.stubs, 1) |
613 | + |
614 | + def fake_get_info(*args): |
615 | + raise exception.NotFound |
616 | + |
617 | + self.create_fake_libvirt_mock(lookupByName=self.fake_lookup) |
618 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
619 | + self.mox.ReplayAll() |
620 | + |
621 | + conn = connection.LibvirtConnection(False) |
622 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
623 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
624 | + conn.firewall_driver.setattr('apply_instance_filter', self.fake_none) |
625 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
626 | + try: |
627 | + conn.reboot(instance_ref, network_info, 'fake') |
628 | + except Exception, err: |
629 | + self.fail(err) |
630 | + time.sleep(1) |
631 | + |
632 | + def test_list_instances_blank_list(self): |
633 | + '''Test returns blank list if no domains are available ''' |
634 | + |
635 | + def listDomainsID(): |
636 | + return [] |
637 | + |
638 | + self.create_fake_libvirt_mock(listDomainsID=listDomainsID, |
639 | + lookupByID=self.fake_lookup) |
640 | + |
641 | + self.mox.ReplayAll() |
642 | + conn = connection.LibvirtConnection(False) |
643 | + try: |
644 | + inst = conn.list_instances() |
645 | + except Exception, err: |
646 | + self.fail(err) |
647 | + |
648 | + self.assertEquals(len(inst), 0) |
649 | + |
650 | + def test_list_instances_detail(self): |
651 | + '''Test list instances detail returns correct list''' |
652 | + |
653 | + def listDomainsID(): |
654 | + return [1, 2, 3, 4] |
655 | + |
656 | + def lookupByID(*args): |
657 | + return 'test' |
658 | + |
659 | + def map_instance_info(*args): |
660 | + return 'test_info' |
661 | + |
662 | + class FakeLibvirtConnection(object): |
663 | + pass |
664 | + |
665 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
666 | + connection.LibvirtConnection._conn = FakeLibvirtConnection() |
667 | + connection.LibvirtConnection._conn.listDomainsID = listDomainsID |
668 | + connection.LibvirtConnection._conn.lookupByID = lookupByID |
669 | + self.stubs.Set(connection.LibvirtConnection, '_map_to_instance_info', |
670 | + map_instance_info) |
671 | + |
672 | + self.create_fake_libvirt_mock(listDomainsID=listDomainsID, |
673 | + lookupByID=self.fake_lookup) |
674 | + |
675 | + self.mox.ReplayAll() |
676 | + conn = connection.LibvirtConnection(False) |
677 | + try: |
678 | + inst = conn.list_instances_detail() |
679 | + except Exception, err: |
680 | + self.fail(err) |
681 | + |
682 | + self.assertEquals(len(inst), 4) |
683 | + |
684 | + def test_pause(self): |
685 | + '''Test pause method suspends an instance''' |
686 | + if not self.lazy_load_library_exists(): |
687 | + return |
688 | + instance_ref = db.instance_create(self.context, self.test_instance) |
689 | + |
690 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
691 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
692 | + self.mox.ReplayAll() |
693 | + |
694 | + conn = connection.LibvirtConnection(False) |
695 | + callback = self.fake_none() |
696 | + try: |
697 | + conn.pause(instance_ref, callback) |
698 | + except Exception, err: |
699 | + self.fail(err) |
700 | + |
701 | + def test_unpause(self): |
702 | + '''Test unpause resumes an instance''' |
703 | + if not self.lazy_load_library_exists(): |
704 | + return |
705 | + instance_ref = db.instance_create(self.context, self.test_instance) |
706 | + |
707 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
708 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
709 | + self.mox.ReplayAll() |
710 | + |
711 | + conn = connection.LibvirtConnection(False) |
712 | + callback = self.fake_none() |
713 | + try: |
714 | + conn.unpause(instance_ref, callback) |
715 | + except Exception, err: |
716 | + self.fail(err) |
717 | + |
718 | + def test_suspend(self): |
719 | + '''Test suspend suspends an instance''' |
720 | + if not self.lazy_load_library_exists(): |
721 | + return |
722 | + instance_ref = db.instance_create(self.context, self.test_instance) |
723 | + |
724 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
725 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
726 | + self.mox.ReplayAll() |
727 | + conn = connection.LibvirtConnection(False) |
728 | + callback = self.fake_none() |
729 | + try: |
730 | + conn.suspend(instance_ref, callback) |
731 | + except Exception, err: |
732 | + self.fail(err) |
733 | + |
734 | + def test_resume(self): |
735 | + '''Test resume resumes an instance''' |
736 | + if not self.lazy_load_library_exists(): |
737 | + return |
738 | + instance_ref = db.instance_create(self.context, self.test_instance) |
739 | + |
740 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
741 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
742 | + self.mox.ReplayAll() |
743 | + conn = connection.LibvirtConnection(False) |
744 | + callback = self.fake_none() |
745 | + try: |
746 | + conn.resume(instance_ref, callback) |
747 | + except Exception, err: |
748 | + self.fail(err) |
749 | + |
750 | + def test_rescue(self): |
751 | + '''Test rescue brings the instance state back to running''' |
752 | + if not self.lazy_load_library_exists(): |
753 | + return |
754 | + |
755 | + def fake_to_xml(*args, **kwargs): |
756 | + pass |
757 | + |
758 | + def fake_get_info(*args): |
759 | + return {'state': 1} |
760 | + |
761 | + def fake_open(*args): |
762 | + return StringIO.StringIO("one,two,three\n1,2,3") |
763 | + |
764 | + import __builtin__ |
765 | + self.stubs.Set(__builtin__, 'open', fake_open) |
766 | + network_info = _fake_network_info(self.stubs, 1) |
767 | + self.create_fake_libvirt_mock() |
768 | + instance = db.instance_create(self.context, self.test_instance) |
769 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
770 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
771 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
772 | + self.stubs.Set(connection.LibvirtConnection, 'to_xml', fake_to_xml) |
773 | + self.mox.ReplayAll() |
774 | + conn = connection.LibvirtConnection(False) |
775 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
776 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
777 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
778 | + conn._create_image = self.fake_none |
779 | + conn._create_new_domain = self.fake_lookup |
780 | + try: |
781 | + conn.rescue(self.context, instance, 'fake_callback', network_info) |
782 | + except Exception, err: |
783 | + self.fail(err) |
784 | + time.sleep(1) |
785 | + |
786 | + def test_unrescue(self): |
787 | + '''Test reboot by unrescue method''' |
788 | + if not self.lazy_load_library_exists(): |
789 | + return |
790 | + instance_ref = db.instance_create(self.context, self.test_instance) |
791 | + network_info = _fake_network_info(self.stubs, 1) |
792 | + |
793 | + def fake_reboot(*args, **kwargs): |
794 | + pass |
795 | + |
796 | + def fake_open(*args): |
797 | + return StringIO.StringIO("one,two,three\n1,2,3") |
798 | + |
799 | + def fake_remove(*args): |
800 | + pass |
801 | + |
802 | + import __builtin__ |
803 | + self.stubs.Set(__builtin__, 'open', fake_open) |
804 | + self.stubs.Set(os, 'remove', fake_remove) |
805 | + self.stubs.Set(connection.LibvirtConnection, 'reboot', fake_reboot) |
806 | + callback = self.fake_none |
807 | + self.mox.ReplayAll() |
808 | + |
809 | + conn = connection.LibvirtConnection(False) |
810 | + try: |
811 | + conn.unrescue(instance_ref, callback, network_info) |
812 | + except Exception, err: |
813 | + self.fail(err) |
814 | + |
815 | + def test_attach_volume(self): |
816 | + '''Test attach volume method attaches device to instance using xml ''' |
817 | + instance = db.instance_create(self.context, self.test_instance) |
818 | + instance_name = instance['name'] |
819 | + test_device_path = ["/dev/vdb", "testproto:testname"] |
820 | + test_mountpoint = "/mnt/testvol" |
821 | + self.create_fake_libvirt_mock() |
822 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
823 | + self.mox.ReplayAll() |
824 | + |
825 | + conn = connection.LibvirtConnection(False) |
826 | + for device_path in test_device_path: |
827 | + try: |
828 | + conn.attach_volume(instance_name, device_path, test_mountpoint) |
829 | + except Exception, err: |
830 | + self.fail(err) |
831 | + |
832 | + def test_attach_volume_raises_invalid_device_path_exception(self): |
833 | + '''Test attach volume raises exception for an invalid device path''' |
834 | + instance = db.instance_create(self.context, self.test_instance) |
835 | + instance_name = instance['name'] |
836 | + test_device_path = "testPath" |
837 | + test_mountpoint = "/mnt/testvol" |
838 | + self.create_fake_libvirt_mock() |
839 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
840 | + self.mox.ReplayAll() |
841 | + conn = connection.LibvirtConnection(False) |
842 | + expected_msg = ('The supplied device path (%s) is invalid.' % |
843 | + test_device_path) |
844 | + try: |
845 | + conn.attach_volume(instance_name, test_device_path, |
846 | + test_mountpoint) |
847 | + except Exception, msg: |
848 | + self.assertTrue(str(msg).find(expected_msg) != -1) |
849 | + |
850 | + def test_detach_volume(self): |
851 | + '''Test detach volume detaches device from instance''' |
852 | + instance = db.instance_create(self.context, self.test_instance) |
853 | + instance_name = instance['name'] |
854 | + test_mountpoint = "/mnt/testvol" |
855 | + |
856 | + def fake_xml(self, *args): |
857 | + test_xml = """<disk type='block'> |
858 | + <driver name='qemu' type='raw'/> |
859 | + <source dev='test'/> |
860 | + <target dev='test' bus='virtio'/> |
861 | + </disk>""" |
862 | + return test_xml |
863 | + |
864 | + self.create_fake_libvirt_mock() |
865 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
866 | + self.stubs.Set(connection.LibvirtConnection, '_get_disk_xml', fake_xml) |
867 | + self.mox.ReplayAll() |
868 | + conn = connection.LibvirtConnection(False) |
869 | + try: |
870 | + conn.detach_volume(instance_name, test_mountpoint) |
871 | + except Exception, err: |
872 | + self.fail(err) |
873 | + |
874 | + def test_detach_volume_raises_disk_not_found_exception(self): |
875 | + '''Test detach volume raises DiskNotFound if xml is not found''' |
876 | + instance = db.instance_create(self.context, self.test_instance) |
877 | + instance_name = instance['name'] |
878 | + test_mountpoint = "/mnt/testvol" |
879 | + expected_msg = 'No disk at testvol' |
880 | + |
881 | + def fake_xml(self, *args): |
882 | + return None |
883 | + |
884 | + self.create_fake_libvirt_mock() |
885 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
886 | + self.stubs.Set(connection.LibvirtConnection, '_get_disk_xml', fake_xml) |
887 | + self.mox.ReplayAll() |
888 | + conn = connection.LibvirtConnection(False) |
889 | + try: |
890 | + conn.detach_volume(instance_name, test_mountpoint) |
891 | + except Exception, msg: |
892 | + self.assertTrue(str(msg).find(expected_msg) != -1) |
893 | + |
894 | + def test_get_disks(self): |
895 | + '''Test get disks returns single list of all block devices''' |
896 | + if not self.lazy_load_library_exists(): |
897 | + return |
898 | + |
899 | + instance = db.instance_create(self.context, self.test_instance) |
900 | + instance_name = instance['name'] |
901 | + |
902 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
903 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
904 | + self.mox.ReplayAll() |
905 | + conn = connection.LibvirtConnection(False) |
906 | + try: |
907 | + disk_list = conn.get_disks(instance_name) |
908 | + except Exception, err: |
909 | + self.fail(err) |
910 | + |
911 | + self.assertEquals(disk_list[0], 'vdb') |
912 | + |
913 | + def test_get_disks_libxml_exception_returns_blank_list(self): |
914 | + '''Test blank list returned for unloaded libxml for xml is incorrect''' |
915 | + if not self.lazy_load_library_exists(): |
916 | + return |
917 | + |
918 | + instance = db.instance_create(self.context, self.test_instance) |
919 | + instance_name = instance['name'] |
920 | + |
921 | + def parseDoc(*args): |
922 | + raise |
923 | + |
924 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
925 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
926 | + self.stubs.Set(connection.libxml2, 'parseDoc', parseDoc) |
927 | + self.mox.ReplayAll() |
928 | + |
929 | + conn = connection.LibvirtConnection(False) |
930 | + try: |
931 | + disk_list = conn.get_disks(instance_name) |
932 | + except Exception, err: |
933 | + self.fail(err) |
934 | + |
935 | + self.assertEquals(len(disk_list), 0) |
936 | + |
937 | + def test_get_interfaces(self): |
938 | + '''Test correct list of interfaces is returned''' |
939 | + if not self.lazy_load_library_exists(): |
940 | + return |
941 | + |
942 | + instance = db.instance_create(self.context, self.test_instance) |
943 | + instance_name = instance['name'] |
944 | + |
945 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
946 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
947 | + self.mox.ReplayAll() |
948 | + conn = connection.LibvirtConnection(False) |
949 | + try: |
950 | + interface_list = conn.get_interfaces(instance_name) |
951 | + except Exception, err: |
952 | + self.fail(err) |
953 | + |
954 | + self.assertEquals(interface_list[0], 'vnet') |
955 | + |
956 | + def test_get_interfaces_libxml_exception(self): |
957 | + '''Test blank list returned for unloaded libxml2''' |
958 | + if not self.lazy_load_library_exists(): |
959 | + return |
960 | + |
961 | + instance = db.instance_create(self.context, self.test_instance) |
962 | + instance_name = instance['name'] |
963 | + |
964 | + def parseDoc(*args): |
965 | + raise |
966 | + |
967 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
968 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
969 | + self.stubs.Set(connection.libxml2, 'parseDoc', parseDoc) |
970 | + self.mox.ReplayAll() |
971 | + |
972 | + conn = connection.LibvirtConnection(False) |
973 | + try: |
974 | + interface_list = conn.get_interfaces(instance_name) |
975 | + except Exception, err: |
976 | + self.fail(err) |
977 | + |
978 | + self.assertEquals(len(interface_list), 0) |
979 | + |
980 | + def test_get_info(self): |
981 | + instance = db.instance_create(self.context, self.test_instance) |
982 | + instance_name = instance['name'] |
983 | + |
984 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
985 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
986 | + self.mox.ReplayAll() |
987 | + conn = connection.LibvirtConnection(False) |
988 | + try: |
989 | + conn.get_info(instance_name) |
990 | + except Exception, err: |
991 | + self.fail(err) |
992 | + |
993 | + def test_get_cpu_info(self): |
994 | + if not self.lazy_load_library_exists(): |
995 | + return |
996 | + |
997 | + def fake_get_capabs(): |
998 | + xml = '''<capabilities> |
999 | + <host> |
1000 | + <cpu> |
1001 | + <arch>x86_64</arch> |
1002 | + <model>coreduo</model> |
1003 | + <vendor>AMD</vendor> |
1004 | + <topology sockets='1' cores='2' threads='1'> |
1005 | + <cells num='1'> |
1006 | + <cell id='0'> |
1007 | + <cpus num='2'> |
1008 | + <cpu id='0'/> |
1009 | + <cpu id='1'/> |
1010 | + </cpus> |
1011 | + </cell> |
1012 | + </cells> |
1013 | + </topology> |
1014 | + <feature policy='disable' name='lahf_lm'/> |
1015 | + </cpu> |
1016 | + </host> |
1017 | + </capabilities>''' |
1018 | + return xml |
1019 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1020 | + connection.LibvirtConnection._conn.getCapabilities = fake_get_capabs |
1021 | + self.mox.ReplayAll() |
1022 | + conn = connection.LibvirtConnection(False) |
1023 | + try: |
1024 | + cpu_info = conn.get_cpu_info() |
1025 | + except Exception, err: |
1026 | + self.fail(err) |
1027 | + |
1028 | + self.assertEquals(len(cpu_info), 140, msg="Invalid CPU Info returned") |
1029 | + |
1030 | + def test_get_cpu_info_nocpus(self): |
1031 | + if not self.lazy_load_library_exists(): |
1032 | + return |
1033 | + |
1034 | + def fake_get_capabs(): |
1035 | + return '''<capabilities> |
1036 | + <host> |
1037 | + </host> |
1038 | + </capabilities>''' |
1039 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1040 | + connection.LibvirtConnection._conn.getCapabilities = fake_get_capabs |
1041 | + self.mox.ReplayAll() |
1042 | + conn = connection.LibvirtConnection(False) |
1043 | + self.assertRaises(exception.InvalidCPUInfo, conn.get_cpu_info) |
1044 | + |
1045 | + def test_get_cpu_info_nokeys(self): |
1046 | + if not self.lazy_load_library_exists(): |
1047 | + return |
1048 | + |
1049 | + def fake_get_capabs(): |
1050 | + xml = '''<capabilities> |
1051 | + <host> |
1052 | + <cpu> |
1053 | + <arch>x86_64</arch> |
1054 | + <model>coreduo</model> |
1055 | + <vendor>AMD</vendor> |
1056 | + <topology> |
1057 | + <cells num='1'> |
1058 | + <cell id='0'> |
1059 | + <cpus num='2'> |
1060 | + <cpu id='0'/> |
1061 | + <cpu id='1'/> |
1062 | + </cpus> |
1063 | + </cell> |
1064 | + </cells> |
1065 | + </topology> |
1066 | + <feature policy='disable' name='lahf_lm'/> |
1067 | + </cpu> |
1068 | + </host> |
1069 | + </capabilities>''' |
1070 | + return xml |
1071 | + |
1072 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1073 | + connection.LibvirtConnection._conn.getCapabilities = fake_get_capabs |
1074 | + self.mox.ReplayAll() |
1075 | + conn = connection.LibvirtConnection(False) |
1076 | + self.assertRaises(exception.InvalidCPUInfo, conn.get_cpu_info) |
1077 | + |
1078 | + def test_compare_cpu(self): |
1079 | + cpu_info_json = '''{"vendor": "AMD", "model": "coreduo", "arch": |
1080 | + "x86_64", "features": ["disable"], "topology": |
1081 | + {"cores": "2", "threads": "1", "sockets": "1"}}''' |
1082 | + |
1083 | + def compareCPU(self, *args): |
1084 | + return 1 |
1085 | + |
1086 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1087 | + connection.LibvirtConnection._conn.compareCPU = compareCPU |
1088 | + self.mox.ReplayAll() |
1089 | + conn = connection.LibvirtConnection(False) |
1090 | + try: |
1091 | + conn.compare_cpu(cpu_info_json) |
1092 | + except Exception, err: |
1093 | + self.fail(err) |
1094 | + |
1095 | + def test_compare_cpu_invalid_exception(self): |
1096 | + cpu_info_json = '''{"vendor": "AMD", "model": "coreduo", "arch": |
1097 | + "x86_64", "features": ["disable"], "topology": |
1098 | + {"cores": "2", "threads": "1", "sockets": "1"}}''' |
1099 | + |
1100 | + def compareCPU(self, *args): |
1101 | + return 0 |
1102 | + |
1103 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1104 | + connection.LibvirtConnection._conn.compareCPU = compareCPU |
1105 | + self.mox.ReplayAll() |
1106 | + conn = connection.LibvirtConnection(False) |
1107 | + self.assertRaises(exception.InvalidCPUInfo, conn.compare_cpu, |
1108 | + cpu_info_json) |
1109 | + |
1110 | + def test_compare_cpu_libvirt_error(self): |
1111 | + if not self.lazy_load_library_exists(): |
1112 | + return |
1113 | + |
1114 | + cpu_info_json = '''{"vendor": "AMD", "model": "coreduo", "arch": |
1115 | + "x86_64", "features": ["disable"], "topology": |
1116 | + {"cores": "2", "threads": "1", "sockets": "1"}}''' |
1117 | + |
1118 | + def compareCPU(self, *args): |
1119 | + raise libvirt.libvirtError('virDomainCompareCPU Failed', conn=self) |
1120 | + |
1121 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1122 | + connection.LibvirtConnection._conn.compareCPU = compareCPU |
1123 | + |
1124 | + self.mox.ReplayAll() |
1125 | + conn = connection.LibvirtConnection(False) |
1126 | + self.assertRaises(libvirt.libvirtError, conn.compare_cpu, |
1127 | + cpu_info_json) |
1128 | + |
1129 | + def test_init_host_running(self): |
1130 | + instance = db.instance_create(self.context, self.test_instance) |
1131 | + |
1132 | + def fake_get_info(*args): |
1133 | + return {'state': 1} |
1134 | + |
1135 | + def fake_db_instance_list(*args): |
1136 | + return [instance] |
1137 | + |
1138 | + def fake_db_inst_set_state(*args): |
1139 | + pass |
1140 | + |
1141 | + self.create_fake_libvirt_mock() |
1142 | + |
1143 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1144 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1145 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
1146 | + self.stubs.Set(db, 'instance_get_all_by_host', fake_db_instance_list) |
1147 | + self.stubs.Set(db, 'instance_set_state', fake_db_inst_set_state) |
1148 | + self.mox.ReplayAll() |
1149 | + |
1150 | + conn = connection.LibvirtConnection(False) |
1151 | + conn.firewall_driver.setattr('setup_basic_filtering', self.fake_none) |
1152 | + conn.firewall_driver.setattr('prepare_instance_filter', self.fake_none) |
1153 | + conn.firewall_driver.setattr('apply_instance_filter', self.fake_none) |
1154 | + try: |
1155 | + conn.init_host(self.test_ip) |
1156 | + except Exception, err: |
1157 | + self.fail(err) |
1158 | + |
1159 | + def test_init_host_not_running(self): |
1160 | + instance = db.instance_create(self.context, self.test_instance) |
1161 | + |
1162 | + def fake_get_info(*args): |
1163 | + return {'state': 3} |
1164 | + |
1165 | + def fake_db_instance_list(*args): |
1166 | + return [instance] |
1167 | + |
1168 | + def fake_db_inst_set_state(*args): |
1169 | + pass |
1170 | + |
1171 | + self.create_fake_libvirt_mock() |
1172 | + |
1173 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1174 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1175 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
1176 | + self.stubs.Set(db, 'instance_get_all_by_host', fake_db_instance_list) |
1177 | + self.stubs.Set(db, 'instance_set_state', fake_db_inst_set_state) |
1178 | + |
1179 | + self.mox.ReplayAll() |
1180 | + conn = connection.LibvirtConnection(False) |
1181 | + try: |
1182 | + conn.init_host(self.test_ip) |
1183 | + except Exception, err: |
1184 | + self.fail(err) |
1185 | + |
1186 | + def test_init_host_instance_not_found(self): |
1187 | + instance = db.instance_create(self.context, self.test_instance) |
1188 | + |
1189 | + def fake_get_info(*args): |
1190 | + raise exception.NotFound |
1191 | + |
1192 | + def fake_db_instance_list(*args): |
1193 | + return [instance] |
1194 | + |
1195 | + def fake_db_inst_set_state(*args): |
1196 | + pass |
1197 | + |
1198 | + self.create_fake_libvirt_mock() |
1199 | + |
1200 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1201 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1202 | + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info) |
1203 | + self.stubs.Set(db, 'instance_get_all_by_host', fake_db_instance_list) |
1204 | + self.stubs.Set(db, 'instance_set_state', fake_db_inst_set_state) |
1205 | + self.mox.ReplayAll() |
1206 | + |
1207 | + conn = connection.LibvirtConnection(False) |
1208 | + try: |
1209 | + conn.init_host(self.test_ip) |
1210 | + except Exception, err: |
1211 | + self.fail(err) |
1212 | + |
1213 | + def test_get_console_output(self): |
1214 | + instance = db.instance_create(self.context, self.test_instance) |
1215 | + instance_name = instance['name'] |
1216 | + |
1217 | + test_contents = "Test contents dumped to file" |
1218 | + |
1219 | + def fake_get_path(*args): |
1220 | + return '/test/path/' |
1221 | + |
1222 | + def fake_flush(*args): |
1223 | + pass |
1224 | + |
1225 | + def fake_append_to_file(*args): |
1226 | + pass |
1227 | + |
1228 | + def fake_dump_file(*args): |
1229 | + return test_contents |
1230 | + |
1231 | + fake_utils.stub_out_utils_execute(self.stubs) |
1232 | + self.stubs.Set(os.path, 'join', fake_get_path) |
1233 | + FLAGS.libvirt_type = 'xen' |
1234 | + self.stubs.Set(connection.LibvirtConnection, '_flush_xen_console', |
1235 | + fake_flush) |
1236 | + self.stubs.Set(connection.LibvirtConnection, '_append_to_file', |
1237 | + fake_append_to_file) |
1238 | + self.stubs.Set(connection.LibvirtConnection, '_dump_file', |
1239 | + fake_dump_file) |
1240 | + self.mox.ReplayAll() |
1241 | + |
1242 | + conn = connection.LibvirtConnection(False) |
1243 | + try: |
1244 | + ret = conn.get_console_output(instance) |
1245 | + except Exception, err: |
1246 | + self.fail(err) |
1247 | + |
1248 | + self.assertEquals(ret, test_contents) |
1249 | + |
1250 | + def test_get_ajax_console_raises_exception(self): |
1251 | + instance = db.instance_create(self.context, self.test_instance) |
1252 | + instance_name = instance['name'] |
1253 | + |
1254 | + fake_utils.stub_out_utils_execute(self.stubs) |
1255 | + FLAGS.ajaxterm_portrange = '1200-45320' |
1256 | + self.mox.ReplayAll() |
1257 | + |
1258 | + conn = connection.LibvirtConnection(False) |
1259 | + self.assertRaises(Exception, |
1260 | + conn.get_ajax_console, instance) |
1261 | + |
1262 | + def test_get_vnc_console(self): |
1263 | + instance = db.instance_create(self.context, self.test_instance) |
1264 | + instance_name = instance['name'] |
1265 | + instance['host'] = 'test_host001' |
1266 | + test_uuid = 'abc12345-6789-0123-a1b2-a123456789z' |
1267 | + port = u'5904' |
1268 | + test_dict = {'host': instance['host'], 'token': test_uuid, |
1269 | + 'port': port} |
1270 | + |
1271 | + def fake_uuid(): |
1272 | + return test_uuid |
1273 | + |
1274 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1275 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1276 | + self.stubs.Set(connection.uuid, 'uuid4', fake_uuid) |
1277 | + self.mox.ReplayAll() |
1278 | + |
1279 | + conn = connection.LibvirtConnection(False) |
1280 | + ret = {} |
1281 | + try: |
1282 | + ret = conn.get_vnc_console(instance) |
1283 | + except Exception, err: |
1284 | + self.fail(err) |
1285 | + |
1286 | + self.assertDictMatch(ret, test_dict) |
1287 | + |
1288 | + def test_get_diagnostics_not_supported(self): |
1289 | + conn = connection.LibvirtConnection(False) |
1290 | + self.assertRaises(exception.ApiError, conn.get_diagnostics, 'test-001') |
1291 | + |
1292 | + def test_get_vcpu_total_not_implemented_exception(self): |
1293 | + |
1294 | + def fake_cpu_count(): |
1295 | + raise NotImplementedError |
1296 | + |
1297 | + self.stubs.Set(connection.multiprocessing, 'cpu_count', fake_cpu_count) |
1298 | + self.mox.ReplayAll() |
1299 | + conn = connection.LibvirtConnection(False) |
1300 | + ret = conn.get_vcpu_total() |
1301 | + self.assertEquals(ret, 0) |
1302 | + |
1303 | + def test_block_stats(self): |
1304 | + instance = db.instance_create(self.context, self.test_instance) |
1305 | + instance_name = instance['name'] |
1306 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1307 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1308 | + self.mox.ReplayAll() |
1309 | + |
1310 | + conn = connection.LibvirtConnection(False) |
1311 | + try: |
1312 | + conn.block_stats(instance, 'test_disk') |
1313 | + except Exception, err: |
1314 | + self.fail(err) |
1315 | + |
1316 | + def test_interface_stats(self): |
1317 | + instance = db.instance_create(self.context, self.test_instance) |
1318 | + instance_name = instance['name'] |
1319 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1320 | + connection.LibvirtConnection._conn.lookupByName = self.fake_lookup |
1321 | + self.mox.ReplayAll() |
1322 | + |
1323 | + conn = connection.LibvirtConnection(False) |
1324 | + try: |
1325 | + conn.interface_stats(instance, 'test_interface') |
1326 | + except Exception, err: |
1327 | + self.fail(err) |
1328 | + |
1329 | + def test_get_console_pool_info(self): |
1330 | + match_dict = {'address': '127.0.0.1', |
1331 | + 'username': 'fakeuser', |
1332 | + 'password': 'fakepassword'} |
1333 | + conn = connection.LibvirtConnection(False) |
1334 | + try: |
1335 | + ret = conn.get_console_pool_info('test_tty') |
1336 | + except Exception, err: |
1337 | + self.fail(err) |
1338 | + |
1339 | + self.assertDictMatch(ret, match_dict) |
1340 | + |
1341 | + def test_refresh_security_group_rules(self): |
1342 | + self.create_fake_libvirt_mock() |
1343 | + self.mox.ReplayAll() |
1344 | + conn = connection.LibvirtConnection(False) |
1345 | + conn.firewall_driver.setattr('refresh_security_group_rules', |
1346 | + self.fake_none) |
1347 | + try: |
1348 | + conn.refresh_security_group_rules('test_gid') |
1349 | + except Exception, err: |
1350 | + self.fail(err) |
1351 | + |
1352 | + def test_refresh_security_group_members(self): |
1353 | + self.create_fake_libvirt_mock() |
1354 | + self.mox.ReplayAll() |
1355 | + conn = connection.LibvirtConnection(False) |
1356 | + conn.firewall_driver.setattr('refresh_security_group_members', |
1357 | + self.fake_none) |
1358 | + try: |
1359 | + conn.refresh_security_group_members('test_gid') |
1360 | + except Exception, err: |
1361 | + self.fail(err) |
1362 | + |
1363 | + def test_refresh_provider_fw_rules(self): |
1364 | + self.create_fake_libvirt_mock() |
1365 | + self.mox.ReplayAll() |
1366 | + conn = connection.LibvirtConnection(False) |
1367 | + conn.firewall_driver.setattr('refresh_provider_fw_rules', |
1368 | + self.fake_none) |
1369 | + try: |
1370 | + conn.refresh_provider_fw_rules() |
1371 | + except Exception, err: |
1372 | + self.fail(err) |
1373 | + |
1374 | + def test_unfilter_instance(self): |
1375 | + network_info = _fake_network_info(self.stubs, 1) |
1376 | + self.create_fake_libvirt_mock() |
1377 | + self.mox.ReplayAll() |
1378 | + conn = connection.LibvirtConnection(False) |
1379 | + conn.firewall_driver.setattr('unfilter_instance', self.fake_none) |
1380 | + try: |
1381 | + conn.unfilter_instance('test_inst_ref', network_info) |
1382 | + except Exception, err: |
1383 | + self.fail(err) |
1384 | + |
1385 | + def test_get_vcpu_used(self): |
1386 | + |
1387 | + def listDomainsID(): |
1388 | + return [1, 2, 3, 4] |
1389 | + |
1390 | + def lookupByID(*args): |
1391 | + |
1392 | + class Vcpu: |
1393 | + def __init__(self): |
1394 | + pass |
1395 | + |
1396 | + def vcpus(self): |
1397 | + return [99, 'vcpu'] |
1398 | + |
1399 | + return Vcpu() |
1400 | + |
1401 | + class FakeLibvirtConnection(object): |
1402 | + pass |
1403 | + |
1404 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1405 | + connection.LibvirtConnection._conn = FakeLibvirtConnection() |
1406 | + connection.LibvirtConnection._conn.listDomainsID = listDomainsID |
1407 | + connection.LibvirtConnection._conn.lookupByID = lookupByID |
1408 | + self.mox.ReplayAll() |
1409 | + total = 16 |
1410 | + conn = connection.LibvirtConnection(False) |
1411 | + try: |
1412 | + ret = conn.get_vcpu_used() |
1413 | + except Exception, err: |
1414 | + self.fail(err) |
1415 | + |
1416 | + self.assertEquals(ret, total) |
1417 | + |
1418 | + def test_cache_image_with_cow(self): |
1419 | + def fake_path_exists(*args): |
1420 | + return 0 |
1421 | + |
1422 | + def fake_path_join(*args): |
1423 | + return '/test/base/dir' |
1424 | + |
1425 | + def fake_fn(*args, **kwargs): |
1426 | + pass |
1427 | + |
1428 | + self.stubs.Set(os.path, 'exists', fake_path_exists) |
1429 | + self.stubs.Set(os.path, 'join', fake_path_join) |
1430 | + self.stubs.Set(os, 'mkdir', fake_fn) |
1431 | + self.stubs.Set(connection.utils, 'execute', fake_fn) |
1432 | + |
1433 | + self.mox.ReplayAll() |
1434 | + conn = connection.LibvirtConnection(False) |
1435 | + try: |
1436 | + conn._cache_image(fake_fn, 'test_target', 'test.ami', cow=True) |
1437 | + except Exception, err: |
1438 | + self.fail(err) |
1439 | + |
1440 | + def test_cache_image_no_cow(self): |
1441 | + def fake_path_exists(*args): |
1442 | + return 0 |
1443 | + |
1444 | + def fake_path_join(*args): |
1445 | + return '/test/base/dir' |
1446 | + |
1447 | + def fake_fn(*args, **kwargs): |
1448 | + pass |
1449 | + |
1450 | + self.stubs.Set(os.path, 'exists', fake_path_exists) |
1451 | + self.stubs.Set(os.path, 'join', fake_path_join) |
1452 | + self.stubs.Set(os, 'mkdir', fake_fn) |
1453 | + self.stubs.Set(connection.utils, 'execute', fake_fn) |
1454 | + |
1455 | + self.mox.ReplayAll() |
1456 | + conn = connection.LibvirtConnection(False) |
1457 | + try: |
1458 | + conn._cache_image(fake_fn, 'test_target', 'test.ami', cow=False) |
1459 | + except Exception, err: |
1460 | + self.fail(err) |
1461 | + |
1462 | + def test_fetch_image_with_size(self): |
1463 | + def fake_fn(*args): |
1464 | + pass |
1465 | + |
1466 | + self.stubs.Set(connection.images, 'fetch', fake_fn) |
1467 | + self.stubs.Set(connection.disk, 'extend', fake_fn) |
1468 | + self.mox.ReplayAll() |
1469 | + |
1470 | + conn = connection.LibvirtConnection(False) |
1471 | + try: |
1472 | + conn._fetch_image(self.context, 'test_target', 'test-001', |
1473 | + self.user_id, self.project_id, size='2G') |
1474 | + except Exception, err: |
1475 | + self.fail(err) |
1476 | + |
1477 | + def test_fetch_image_no_size(self): |
1478 | + def fake_fetch(*args): |
1479 | + pass |
1480 | + |
1481 | + self.stubs.Set(connection.images, 'fetch', fake_fetch) |
1482 | + self.mox.ReplayAll() |
1483 | + |
1484 | + conn = connection.LibvirtConnection(False) |
1485 | + try: |
1486 | + conn._fetch_image(self.context, 'test_target', 'test-001', |
1487 | + self.user_id, self.project_id, size=None) |
1488 | + except Exception, err: |
1489 | + self.fail(err) |
1490 | + |
1491 | + def test_create_local(self): |
1492 | + fake_utils.stub_out_utils_execute(self.stubs) |
1493 | + self.mox.ReplayAll() |
1494 | + |
1495 | + conn = connection.LibvirtConnection(False) |
1496 | + try: |
1497 | + conn._create_local('test_target', 1) |
1498 | + except Exception, err: |
1499 | + self.fail(err) |
1500 | + |
1501 | + def test_lookup_by_name(self): |
1502 | + if not self.lazy_load_library_exists(): |
1503 | + return |
1504 | + |
1505 | + instance = db.instance_create(self.context, self.test_instance) |
1506 | + instance_name = instance['name'] |
1507 | + |
1508 | + class FakeLibvirtConnection(object): |
1509 | + def __init__(self): |
1510 | + pass |
1511 | + |
1512 | + def lookupByName(self, *args): |
1513 | + return True |
1514 | + |
1515 | + fake = FakeLibvirtConnection() |
1516 | + self.stubs.Set(connection.LibvirtConnection, '_conn', fake) |
1517 | + self.mox.ReplayAll() |
1518 | + |
1519 | + conn = connection.LibvirtConnection(False) |
1520 | + try: |
1521 | + ret = conn._lookup_by_name(instance_name) |
1522 | + except Exception, err: |
1523 | + self.fail(err) |
1524 | + |
1525 | + self.assertTrue(ret) |
1526 | + |
1527 | + def test_lookup_by_name_raises_instance_not_found_exception(self): |
1528 | + '''Test instance not found exception is raised''' |
1529 | + if not self.lazy_load_library_exists(): |
1530 | + return |
1531 | + |
1532 | + instance = db.instance_create(self.context, self.test_instance) |
1533 | + instance_name = instance['name'] |
1534 | + |
1535 | + def fake_get_last_error(): |
1536 | + return [42, 13, 'fake message'] |
1537 | + |
1538 | + class FakeLibvirtConnection(object): |
1539 | + def __init__(self): |
1540 | + pass |
1541 | + |
1542 | + def lookupByName(self, *args): |
1543 | + raise libvirt.libvirtError('ERR') |
1544 | + |
1545 | + fake = FakeLibvirtConnection() |
1546 | + |
1547 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1548 | + connection.LibvirtConnection._conn = fake |
1549 | + connection.LibvirtConnection._conn.lookupByName = lookupByName |
1550 | + self.stubs.Set(connection.libvirt, 'virGetLastError', |
1551 | + fake_get_last_error) |
1552 | + self.mox.ReplayAll() |
1553 | + |
1554 | + conn = connection.LibvirtConnection(False) |
1555 | + self.assertRaises(exception.InstanceNotFound, conn._lookup_by_name, |
1556 | + instance_name) |
1557 | + |
1558 | + def test_lookup_by_name_raises_lookup_error(self): |
1559 | + '''Test lookup error is received''' |
1560 | + if not self.lazy_load_library_exists(): |
1561 | + return |
1562 | + |
1563 | + instance = db.instance_create(self.context, self.test_instance) |
1564 | + instance_name = instance['name'] |
1565 | + expected_msg = ('Error from libvirt while looking up %s' % |
1566 | + instance_name) |
1567 | + |
1568 | + class FakeLibvirtConnection(object): |
1569 | + def __init__(self): |
1570 | + pass |
1571 | + |
1572 | + def lookupByName(self, *args): |
1573 | + raise libvirt.libvirtError('ERR') |
1574 | + |
1575 | + fake = FakeLibvirtConnection() |
1576 | + self.mox.StubOutWithMock(connection.LibvirtConnection, '_conn') |
1577 | + connection.LibvirtConnection._conn = fake |
1578 | + connection.LibvirtConnection._conn.lookupByName = lookupByName |
1579 | + |
1580 | + self.mox.ReplayAll() |
1581 | + |
1582 | + conn = connection.LibvirtConnection(False) |
1583 | + try: |
1584 | + conn._lookup_by_name(instance_name) |
1585 | + except Exception, err: |
1586 | + self.assertTrue(str(err).find(expected_msg) != -1) |
1587 | + |
1588 | + def test_get_disk_xml(self): |
1589 | + '''Test method returns xml for disk mounted at device''' |
1590 | + if not self.lazy_load_library_exists(): |
1591 | + return |
1592 | + |
1593 | + xml = """ |
1594 | + <domain type='kvm'> |
1595 | + <devices> |
1596 | + <disk type='file'> |
1597 | + <source file='filename'/> |
1598 | + <target dev='vdb' bus='ide'/> |
1599 | + </disk> |
1600 | + <interface type='bridge'> |
1601 | + <source bridge='br0'/> |
1602 | + <target dev='vnet'/> |
1603 | + <mac address="00:11:22:33:44:55"/> |
1604 | + </interface> |
1605 | + <serial type='pty'> |
1606 | + <source path='/dev/pts/3'/> |
1607 | + <target port='0'/> |
1608 | + </serial> |
1609 | + <graphics type='vnc' port='5904'/> |
1610 | + </devices> |
1611 | + </domain> |
1612 | + """ |
1613 | + expected_node = """<disk type="file"> |
1614 | + <source file="filename"/> |
1615 | + <target dev="vdb" bus="ide"/> |
1616 | + </disk>""" |
1617 | + test_device = 'vdb' |
1618 | + conn = connection.LibvirtConnection(False) |
1619 | + try: |
1620 | + node = conn._get_disk_xml(xml, test_device) |
1621 | + except Exception, err: |
1622 | + self.fail(err) |
1623 | + |
1624 | + self.assertEquals(node, expected_node) |
1625 | + |
1626 | + def test_get_disk_xml_returns_none(self): |
1627 | + if not self.lazy_load_library_exists(): |
1628 | + return |
1629 | + |
1630 | + xml = 'fake' |
1631 | + test_device = 'vdb' |
1632 | + |
1633 | + def fake_parseDoc(*args): |
1634 | + raise |
1635 | + |
1636 | + self.stubs.Set(connection.libxml2, 'parseDoc', fake_parseDoc) |
1637 | + self.mox.ReplayAll() |
1638 | + |
1639 | + conn = connection.LibvirtConnection(False) |
1640 | + try: |
1641 | + node = conn._get_disk_xml(xml, test_device) |
1642 | + except Exception, err: |
1643 | + self.fail(err) |
1644 | + |
1645 | + self.assertEquals(node, None) |
1646 | + |
1647 | + def test_flush_xen_console(self): |
1648 | + test_virsh_output = ['/dev/tty0', 'test'] |
1649 | + |
1650 | + def fake_execute(*args, **kwatgs): |
1651 | + return 200, None |
1652 | + |
1653 | + self.stubs.Set(connection.utils, 'execute', fake_execute) |
1654 | + self.mox.ReplayAll() |
1655 | + conn = connection.LibvirtConnection(False) |
1656 | + try: |
1657 | + out = conn._flush_xen_console(test_virsh_output) |
1658 | + except Exception, err: |
1659 | + self.fail(err) |
1660 | + |
1661 | + self.assertEquals(out, 200) |
1662 | + |
1663 | + def test_flush_xen_console_no_device(self): |
1664 | + test_virsh_output = ['test', 'test'] |
1665 | + |
1666 | + def fake_execute(*args, **kwatgs): |
1667 | + return 200, None |
1668 | + |
1669 | + self.stubs.Set(connection.utils, 'execute', fake_execute) |
1670 | + self.mox.ReplayAll() |
1671 | + conn = connection.LibvirtConnection(False) |
1672 | + try: |
1673 | + out = conn._flush_xen_console(test_virsh_output) |
1674 | + except Exception, err: |
1675 | + self.fail(err) |
1676 | + |
1677 | + self.assertEquals(out, '') |
1678 | + |
1679 | + def test_append_to_file(self): |
1680 | + test_data = "Test Data" |
1681 | + test_file_path = "/test/file/path" |
1682 | + |
1683 | + def fake_open(*args): |
1684 | + return StringIO.StringIO("one,two,three\n1,2,3") |
1685 | + |
1686 | + import __builtin__ |
1687 | + self.stubs.Set(__builtin__, 'open', fake_open) |
1688 | + |
1689 | + self.mox.ReplayAll() |
1690 | + conn = connection.LibvirtConnection(False) |
1691 | + try: |
1692 | + out = conn._append_to_file(test_data, test_file_path) |
1693 | + except Exception, err: |
1694 | + self.fail(err) |
1695 | + |
1696 | + self.assertEquals(out, test_file_path) |
1697 | + |
1698 | + def test_dump_file(self): |
1699 | + test_contents = "Test Data" |
1700 | + test_file_path = "/test/file/path" |
1701 | + fake_contents = 'one,two,three\n1,2,3' |
1702 | + |
1703 | + def fake_open(*args): |
1704 | + return StringIO.StringIO(fake_contents) |
1705 | + |
1706 | + import __builtin__ |
1707 | + self.stubs.Set(__builtin__, 'open', fake_open) |
1708 | + self.mox.ReplayAll() |
1709 | + |
1710 | + conn = connection.LibvirtConnection(False) |
1711 | + try: |
1712 | + out = conn._dump_file(test_file_path) |
1713 | + except Exception, err: |
1714 | + self.fail(err) |
1715 | + |
1716 | + self.assertEquals(out, fake_contents) |
1717 | + |
1718 | + def test_create_image_lxc_no_network_info_no_v6(self): |
1719 | + '''Test private method creates image when no network info is passed''' |
1720 | + instance = db.instance_create(self.context, self.test_instance) |
1721 | + instance_name = instance['name'] |
1722 | + instance['key_data'] = 'abcdef' |
1723 | + instance['image_ref'] = '123456' |
1724 | + |
1725 | + network_info = [] |
1726 | + import __builtin__ |
1727 | + |
1728 | + def fake_get_path(*args): |
1729 | + return 'fake/test/path' |
1730 | + |
1731 | + def fake_method(*args, **kwargs): |
1732 | + pass |
1733 | + |
1734 | + def fake_open(*args): |
1735 | + return StringIO.StringIO("one,two,three\n1,2,3") |
1736 | + |
1737 | + def fake_user(*args): |
1738 | + return self.user |
1739 | + |
1740 | + def fake_project(*args): |
1741 | + return self.project |
1742 | + |
1743 | + def fake_mapping(*args): |
1744 | + return False |
1745 | + |
1746 | + self.stubs.Set(__builtin__, 'open', fake_open) |
1747 | + self.stubs.Set(os.path, 'join', fake_get_path) |
1748 | + self.stubs.Set(connection.utils, 'execute', fake_method) |
1749 | + FLAGS.libvirt_type = 'lxc' |
1750 | + self.stubs.Set(os, 'open', fake_open) |
1751 | + self.stubs.Set(os, 'close', fake_method) |
1752 | + self.stubs.Set(connection.manager.AuthManager, 'get_user', fake_user) |
1753 | + self.stubs.Set(connection.manager.AuthManager, 'get_project', |
1754 | + fake_project) |
1755 | + self.stubs.Set(connection.LibvirtConnection, '_volume_in_mapping', |
1756 | + fake_mapping) |
1757 | + self.stubs.Set(connection.LibvirtConnection, '_cache_image', |
1758 | + fake_method) |
1759 | + self.stubs.Set(connection.disk, 'inject_data', fake_method) |
1760 | + self.stubs.Set(connection.disk, 'setup_container', fake_method) |
1761 | + self.mox.ReplayAll() |
1762 | + conn = connection.LibvirtConnection(False) |
1763 | + try: |
1764 | + conn._create_image(self.context, instance, 'test', |
1765 | + network_info=network_info) |
1766 | + except Exception, err: |
1767 | + self.fail(err) |
1768 | + |
1769 | + def test_create_image_lxc_tiny_instance_has_kernel_ramdisk(self): |
1770 | + '''Test private method creates image with kernel and ramdisk image''' |
1771 | + test_instance = {'memory_kb': '1024000', |
1772 | + 'basepath': '/some/path', |
1773 | + 'bridge_name': 'br100', |
1774 | + 'vcpus': 2, |
1775 | + 'project_id': 'fake', |
1776 | + 'bridge': 'br101', |
1777 | + 'key_data': 'abcdef', |
1778 | + 'image_ref': '123456', |
1779 | + 'kernel_id': '123456', |
1780 | + 'ramdisk_id': '123456', |
1781 | + 'instance_type_id': '2'} # m1.tiny |
1782 | + |
1783 | + instance = db.instance_create(self.context, test_instance) |
1784 | + instance_name = instance['name'] |
1785 | + network_info = _fake_network_info(self.stubs, 1) |
1786 | + |
1787 | + import __builtin__ |
1788 | + |
1789 | + def fake_get_path(*args): |
1790 | + return 'fake/test/path' |
1791 | + |
1792 | + def fake_method(*args, **kwargs): |
1793 | + pass |
1794 | + |
1795 | + def fake_open(*args): |
1796 | + return StringIO.StringIO("one,two,three\n1,2,3") |
1797 | + |
1798 | + def fake_user(*args): |
1799 | + return self.user |
1800 | + |
1801 | + def fake_project(*args): |
1802 | + return self.project |
1803 | + |
1804 | + def fake_mapping(*args): |
1805 | + return False |
1806 | + |
1807 | + self.stubs.Set(__builtin__, 'open', fake_open) |
1808 | + self.stubs.Set(os.path, 'join', fake_get_path) |
1809 | + self.stubs.Set(connection.utils, 'execute', fake_method) |
1810 | + FLAGS.libvirt_type = 'lxc' |
1811 | + self.stubs.Set(os, 'open', fake_open) |
1812 | + self.stubs.Set(os, 'close', fake_method) |
1813 | + self.stubs.Set(connection.manager.AuthManager, 'get_user', fake_user) |
1814 | + self.stubs.Set(connection.manager.AuthManager, 'get_project', |
1815 | + fake_project) |
1816 | + self.stubs.Set(connection.LibvirtConnection, '_volume_in_mapping', |
1817 | + fake_mapping) |
1818 | + |
1819 | + self.stubs.Set(connection.LibvirtConnection, '_cache_image', |
1820 | + fake_method) |
1821 | + FLAGS.use_ipv6 = False |
1822 | + self.stubs.Set(connection.disk, 'inject_data', fake_method) |
1823 | + self.stubs.Set(connection.disk, 'setup_container', fake_method) |
1824 | + self.mox.ReplayAll() |
1825 | + conn = connection.LibvirtConnection(False) |
1826 | + try: |
1827 | + conn._create_image(self.context, instance, 'test', |
1828 | + network_info=network_info) |
1829 | + except Exception, err: |
1830 | + self.fail(err) |
1831 | + |
1832 | + def test_get_connection(self): |
1833 | + '''Test private method that creates the libvirt connection object''' |
1834 | + |
1835 | + def fake_method(*args): |
1836 | + return 'Fake' |
1837 | + |
1838 | + self.stubs.Set(connection.LibvirtConnection, '_test_connection', |
1839 | + None) |
1840 | + self.stubs.Set(connection.LibvirtConnection, '_connect', fake_method) |
1841 | + self.mox.ReplayAll() |
1842 | + conn = connection.LibvirtConnection(False) |
1843 | + ret = conn._get_connection() |
1844 | + self.assertTrue(ret, 'Fake') |
1845 | + |
1846 | + def test_test_connection(self): |
1847 | + '''Test test connection returns true of connection object is created''' |
1848 | + |
1849 | + class fake_wrapped_conn: |
1850 | + def __init__(self): |
1851 | + pass |
1852 | + |
1853 | + def getCapabilities(self): |
1854 | + pass |
1855 | + |
1856 | + conn = connection.LibvirtConnection(False) |
1857 | + conn._wrapped_conn = fake_wrapped_conn() |
1858 | + try: |
1859 | + ret = conn._test_connection() |
1860 | + except Exception, err: |
1861 | + self.fail(err) |
1862 | + |
1863 | + self.assertTrue(ret) |
1864 | + |
1865 | + def test_test_connection_broken(self): |
1866 | + '''Test test connection returns false if system error is recevied''' |
1867 | + if not self.lazy_load_library_exists(): |
1868 | + return |
1869 | + |
1870 | + def fake_get_last_error(): |
1871 | + return [38, 13, 'fake message'] |
1872 | + |
1873 | + class fake_wrapped_conn: |
1874 | + def __init__(self): |
1875 | + pass |
1876 | + |
1877 | + def getCapabilities(self): |
1878 | + raise libvirt.libvirtError("Broken connection to libvirt") |
1879 | + |
1880 | + self.stubs.Set(connection.libvirt, 'virGetLastError', |
1881 | + fake_get_last_error) |
1882 | + self.mox.ReplayAll() |
1883 | + conn = connection.LibvirtConnection(False) |
1884 | + conn._wrapped_conn = fake_wrapped_conn() |
1885 | + try: |
1886 | + ret = conn._test_connection() |
1887 | + except Exception, err: |
1888 | + self.fail(err) |
1889 | + self.assertFalse(ret) |
1890 | + |
1891 | + def test_test_connection_raises_exception_for_unknown_error(self): |
1892 | + '''Test test connection raises exception for unknown libvirt error ''' |
1893 | + if not self.lazy_load_library_exists(): |
1894 | + return |
1895 | + |
1896 | + def fake_get_last_error(): |
1897 | + return [10, 0, 'fake message'] |
1898 | + |
1899 | + class fake_wrapped_conn: |
1900 | + def __init__(self): |
1901 | + pass |
1902 | + |
1903 | + def getCapabilities(self): |
1904 | + raise libvirt.libvirtError("Broken connection to libvirt") |
1905 | + |
1906 | + self.stubs.Set(connection.libvirt, 'virGetLastError', |
1907 | + fake_get_last_error) |
1908 | + self.mox.ReplayAll() |
1909 | + conn = connection.LibvirtConnection(False) |
1910 | + conn._wrapped_conn = fake_wrapped_conn() |
1911 | + self.assertRaises(libvirt.libvirtError, conn._test_connection) |
1912 | + |
1913 | + def test_connect_read_only(self): |
1914 | + '''Test connect returns connection object for read only access''' |
1915 | + if not self.lazy_load_library_exists(): |
1916 | + return |
1917 | + |
1918 | + def fake_method(*args): |
1919 | + return True |
1920 | + |
1921 | + self.stubs.Set(connection.libvirt, 'openReadOnly', fake_method) |
1922 | + self.mox.ReplayAll() |
1923 | + conn = connection.LibvirtConnection(False) |
1924 | + try: |
1925 | + ret = conn._connect('fake', 1) |
1926 | + except Exception, err: |
1927 | + self.fail(err) |
1928 | + |
1929 | + self.assertTrue(ret) |
1930 | + |
1931 | + def test_connect_with_auth(self): |
1932 | + '''Test connect returns connection object for authorized access''' |
1933 | + if not self.lazy_load_library_exists(): |
1934 | + return |
1935 | + |
1936 | + def fake_method(*args): |
1937 | + return True |
1938 | + |
1939 | + self.stubs.Set(connection.libvirt, 'openAuth', fake_method) |
1940 | + self.mox.ReplayAll() |
1941 | + conn = connection.LibvirtConnection(False) |
1942 | + try: |
1943 | + ret = conn._connect('fake_url', 0) |
1944 | + except Exception, err: |
1945 | + self.fail(err) |
1946 | + self.assertTrue(ret) |
1947 | + |
1948 | + #@test.skip_test("SKIP") |
1949 | + def test_map_to_instance_info(self): |
1950 | + '''Test method gets info from virsh domain into an InstanceInfo''' |
1951 | + |
1952 | + class FakeVirtDomain(object): |
1953 | + def __init__(self): |
1954 | + pass |
1955 | + |
1956 | + def info(self): |
1957 | + return (1, '20', '10', '1', '1') |
1958 | + |
1959 | + def name(self): |
1960 | + return 'fake' |
1961 | + |
1962 | + conn = connection.LibvirtConnection(False) |
1963 | + domain = FakeVirtDomain() |
1964 | + try: |
1965 | + ret = conn._map_to_instance_info(domain) |
1966 | + self.assertEquals(ret.name, 'fake') |
1967 | + self.assertEquals(ret.state, 1) |
1968 | + except Exception, err: |
1969 | + self.fail(err) |
1970 | + |
1971 | + def test_cleanup(self): |
1972 | + '''Test instance is deleted as part of cleanup call''' |
1973 | + instance = db.instance_create(self.context, self.test_instance) |
1974 | + |
1975 | + def fake_join(*args): |
1976 | + return 'fake' |
1977 | + |
1978 | + def fake_exists(*args): |
1979 | + return True |
1980 | + |
1981 | + def fake_method(*args, **kwargs): |
1982 | + pass |
1983 | + |
1984 | + self.stubs.Set(os.path, 'join', fake_join) |
1985 | + FLAGS.libvirt_type = 'lxc' |
1986 | + self.stubs.Set(os.path, 'exists', fake_exists) |
1987 | + self.stubs.Set(connection.shutil, 'rmtree', fake_method) |
1988 | + self.stubs.Set(connection.disk, 'destroy_container', fake_method) |
1989 | + conn = connection.LibvirtConnection(False) |
1990 | + try: |
1991 | + conn._cleanup(instance) |
1992 | + except Exception, err: |
1993 | + self.fail(err) |
1994 | + |
1995 | def test_volume_in_mapping(self): |
1996 | + |
1997 | conn = connection.LibvirtConnection(False) |
1998 | swap = {'device_name': '/dev/sdb', |
1999 | 'swap_size': 1} |
2000 | @@ -1468,5 +3175,4 @@ |
2001 | |
2002 | # should undefine 2 filters: instance and instance-secgroup |
2003 | self.assertEqual(original_filter_count - len(fakefilter.filters), 2) |
2004 | - |
2005 | db.instance_destroy(admin_ctxt, instance_ref['id']) |
Only two minor pep8 violations (lines 309, and 2915 - adding a comma will make them go away). Also, a nit about the whitespace in the XML (lines 321 to 332). Other than that, it looks good.