Merge lp://qastaging/~rohitk/nova/libvirt_unittests into lp://qastaging/~hudson-openstack/nova/trunk

Proposed by Rohit Karajgi
Status: Work in progress
Proposed branch: lp://qastaging/~rohitk/nova/libvirt_unittests
Merge into: lp://qastaging/~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://qastaging/~rohitk/nova/libvirt_unittests
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.qastaging.launchpad.net

Description of the change

I have added the following test methods for LibvirtConnTestCase class in tests/test_libvirt.py to cover and test code in connection.py. These are pure unit-test cases, and not integration unit-test cases. Most of the tests use mocks, stubs and fakes to eliminate dependencies on third party modules, db, file system and network.
This also fixes bugs in the test_spawn_with_network_info module.
Code coverage for libvirt/connection.py has increased to about 96% post this change.
Some tests (#72,73) have been skipped due to Bug:807764.
This effort is towards the Diablo-testing blueprint (https://blueprints.launchpad.net/nova/+spec/diablo-testing).

1 test_live_migration_readonly_connection
2 test_live_migration_authorized_connection
3 test_spawn_with_network_info
4 test_spawn_with_network_info_instance_not_found_error
5 test_destroy
6 test_destroy_raises_loopingcall_done
7 test_destroy_no_domain_exception
8 test_destroy_undefine_raises_libvirt_error_already_shutoff
9 test_destroy_raises_libvirt_error_running_state
10 test_reboot
11 test_reboot_fails_instance_not_found
12 test_list_instances_blank_list
13 test_list_instances_detail
14 test_pause
15 test_unpause
16 test_suspend
17 test_resume
18 test_rescue
19 test_rescue_instance_not_found_error
20 test_unrescue
21 test_attach_volume
22 test_attach_volume_raises_invalid_device_path_exception
23 test_detach_volume
24 test_detach_volume_raises_disk_not_found_exception
25 test_get_disks
26 test_get_disks_libxml_exception_returns_blank_list
27 test_get_interfaces
28 test_get_interfaces_libxml_exception
29 test_get_info
30 test_get_cpu_info
31 test_get_cpu_info_nocpus
32 test_get_cpu_info_nokeys
33 test_compare_cpu
34 test_compare_cpu_invalid_exception
35 test_compare_cpu_libvirt_error
36 test_init_host_running
37 test_init_host_not_running
38 test_init_host_instance_not_found
39 test_get_console_output
40 test_get_console_output_for_xen
41 test_get_console_output_for_lxc
42 test_get_ajax_console
43 test_get_ajax_console_raises_exception
44 test_get_vnc_console
45 test_get_diagnostics_not_supported
46 test_get_vcpu_total_not_implemented_exception
47 test_block_stats
48 test_interface_stats
49 test_get_console_pool_info
50 test_refresh_security_group_rules
51 test_refresh_security_group_members
52 test_refresh_provider_fw_rules
53 test_unfilter_instance
54 test_get_vcpu_used
55 test_cache_image_with_cow
56 test_cache_image_no_cow
57 test_fetch_image_with_size
58 test_fetch_image_no_size
59 test_create_local
60 test_lookup_by_name
61 test_lookup_by_name_raises_instance_not_found_exception
62 test_lookup_by_name_raises_lookuo_error
63 test_get_disk_xml
64 test_get_disk_xml_returns_none
65 test_flush_xen_console
66 test_flush_xen_console_no_device
67 test_append_to_file
68 test_dump_file
69 test_volume_in_mapping_returns_true
70 test_volume_in_mapping_returns_false
71 test_create_image_lxc_no_network_info_no_v6
72 test_create_image_lxc_tiny_instance_has_kernel_ramdisk
73 test_create_image_lxc_unknown_disk_image_ipv6
74 test_get_connection
75 test_test_connection
76 test_test_connection_broken
77 test_test_connection_raises_exception_for_unknown_error
78 test_connect_read_only
79 test_connect_with_auth
80 test_map_to_instance_info
81 test_cleanup

To post a comment you must log in.
Revision history for this message
Mandell (mdegerne) wrote :

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.

Revision history for this message
Rohit Karajgi (rohitk) wrote :

Fixed, however, please use latest pep8 (0.6.1). The code was validated for pep8 0.6.1. Thanks!

Revision history for this message
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(error_message), where 'error_message' is the exception contents. Just saying "unknown exception" isn't very helpful when trying to debug, but the failure message will help out greatly.

For example:

524 + try:
525 + conn.spawn(instance, network_info)
526 + except Exception, e:
527 + fail = 1
528 + time.sleep(1)
529 +
530 + self.assertFalse(fail, msg=self.except_msg)

Would give me a generic failure message, but:

524 + try:
525 + conn.spawn(instance, network_info)
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_driver.setattr('setup_basic_filtering',
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_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER'
349 + self.mox.StubOutWithMock(connection.FLAGS, 'live_migration_flag')
350 + connection.FLAGS.live_migration_flag = fake_flag

Could safely become:

348 + migration_flag = 'VIR_MIGRATE_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER'
349 + FLAGS.live_migration_flag = migration_flag

There are existing structures for Mocks and Stubs, so this:

451 + self.mox.StubOutWithMock(connection.LibvirtConnection, 'get_info')
452 + connection.LibvirtConnection.get_info = fake_get_info

could just be:

451 + self.stubs.Set(connection.LibvirtConnection, 'get_info', fake_get_info)

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!

review: Needs Fixing
Revision history for this message
Brian Lamar (blamar) wrote :

test_get_cpu_info
test_get_cpu_info_nocpus
test_get_cpu_info_nokeys
test_get_disks
test_get_interfaces
test_get_interfaces_libxml_exception

These tests are failing when libvirt isn't installed on the testing machine. Can you skip these tests if libvirt isn't installed?

Revision history for this message
Rohit Karajgi (rohitk) wrote :

Thanks Brian for the comments till now. I'll make those fixes wherever applicable.

Revision history for this message
Rohit Karajgi (rohitk) wrote :

Brian, request you to please review the branch for merging. I have implemented all the review comments by you. Thanks.

Revision history for this message
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.ElementTree import fromstring as xml_to_tree

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.ElementTree import fromstring as xml_to_tree

Cheers!
jay

review: Needs Fixing
Revision history for this message
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.

Revision history for this message
Jay Pipes (jaypipes) wrote :

Looks great!

review: Approve
Revision history for this message
Brian Lamar (blamar) wrote :
review: Needs Fixing
Revision history for this message
Rohit Karajgi (rohitk) wrote :

The first two errors are fixed. I am not able to reproduce the third error, from test_virt_drivers.py, and this file(or it's dependencies) was not modified by this branch. Also noticed large test failures in current trunk due to which full cobertura report is not being generated.
Please review branch for modified files. Thanks!

Revision history for this message
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.

Revision history for this message
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.

Revision history for this message
Rohit Karajgi (rohitk) wrote :

Hi William,

The issue was with the method test_map_to_instance_info that directly set the driver.InstanceInfo object to invalid test values and failed to unset during teardown. I have removed that code and modified the test to use only FakeVirtDomain object, and now the tests are passing without affecting test_virt_drivers tests. Thank you for pointing it out.

I have run the full suite and tests have passed. Please review.

Revision history for this message
Rohit Karajgi (rohitk) wrote :
Revision history for this message
William Wolf (throughnothing) wrote :

Everything passes for me now Rohit. Thanks, looks good.

review: Approve
Revision history for this message
Matt Dietz (cerberus) wrote :

Awesome to see a new collection of unit tests in the project! Everything passes for me.

review: Approve
Revision history for this message
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.live_migration_flag = migration_flag". In tests flags seem to be reset only when you set them like this:

self.flags(live_migration_flag=migration_flag)

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!

review: Needs Fixing
Revision history for this message
Rohit Karajgi (rohitk) wrote :

> self.flags(live_migration_flag=migration_flag)
>
> 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

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
The diff is not available at this time. You can reload the page or download it.