diff options
author | Dan Radez <dradez@redhat.com> | 2017-10-04 11:46:04 -0400 |
---|---|---|
committer | Dan Radez <dradez@redhat.com> | 2017-10-10 10:02:49 -0400 |
commit | 88230c898808ecefffed413c36d3a558dcb7f763 (patch) | |
tree | 4e88a5fe45f47348f663535341614d8899eb1ffe | |
parent | 16fa09bb892289f3997ef14ed6cbe5a278ee342a (diff) |
Increasing clean.py unittest coverage
Change-Id: Iae3e80b399d1431382614449488238475b47b103
Signed-off-by: Dan Radez <dradez@redhat.com>
-rw-r--r-- | apex/tests/test_apex_clean.py | 127 |
1 files changed, 82 insertions, 45 deletions
diff --git a/apex/tests/test_apex_clean.py b/apex/tests/test_apex_clean.py index b3ead6f7..e379f477 100644 --- a/apex/tests/test_apex_clean.py +++ b/apex/tests/test_apex_clean.py @@ -1,5 +1,5 @@ ############################################################################## -# Copyright (c) 2016 Tim Rozet (Red Hat) +# Copyright (c) 2016 Tim Rozet, Dan Radez (Red Hat) # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 @@ -7,51 +7,27 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import libvirt import mock import os import pyipmi import pyipmi.chassis + from mock import patch +from mock import MagicMock + from nose.tools import ( assert_raises, - assert_equal + assert_equal, + assert_is_none ) from apex import clean_nodes from apex import clean +from apex.common.exceptions import ApexCleanException from apex.tests import constants as con -class dummy_domain: - - def isActive(self): - return True - - def destroy(self): - pass - - def undefine(self): - pass - - -class dummy_vol: - - def wipe(self, *args): - pass - - def delete(self, *args): - pass - - -class dummy_pool: - - def storageVolLookupByName(self, *args, **kwargs): - return dummy_vol() - - def refresh(self): - pass - - class TestClean: @classmethod def setup_class(cls): @@ -76,11 +52,51 @@ class TestClean: assert_equal(mock_method.call_count, 5) assert_equal(mock_method2.call_count, 5) + @patch('apex.clean.utils.parse_yaml') + def test_clean_nodes_empty(self, mock_parse_yaml): + mock_parse_yaml.return_value = None + assert_raises(SystemExit, clean_nodes, 'dummy_file') + mock_parse_yaml.return_value = {} + assert_raises(SystemExit, clean_nodes, 'dummy_file') + + @patch('apex.clean.pyipmi.interfaces.create_interface') + @patch('apex.clean.utils.parse_yaml') + def test_clean_nodes_raises(self, mock_parse_yaml, mock_pyipmi): + mock_parse_yaml.return_value = {'nodes': {'node': {}}} + mock_pyipmi.side_effect = Exception() + assert_raises(SystemExit, clean_nodes, 'dummy_file') + @patch('virtualbmc.manager.VirtualBMCManager.list', return_value=[{'domain_name': 'dummy1'}, {'domain_name': 'dummy2'}]) @patch('virtualbmc.manager.VirtualBMCManager.delete') - def test_vmbc_clean(self, vbmc_del_func, vbmc_list_func): - assert clean.clean_vbmcs() is None + def test_clean_vmbcs(self, vbmc_del_func, vbmc_list_func): + assert_is_none(clean.clean_vbmcs()) + + @patch('apex.clean.libvirt.open') + def test_clean_vms(self, mock_libvirt): + ml = mock_libvirt.return_value + ml.storagePoolLookupByName.return_value = MagicMock() + uc = MagicMock() + uc.name.return_value = 'undercloud' + x = MagicMock() + x.name.return_value = 'domain_x' + ml.listAllDomains.return_value = [uc, x] + assert_is_none(clean.clean_vms()) + + @patch('apex.clean.libvirt.open') + def test_clean_vms_skip_vol(self, mock_libvirt): + ml = mock_libvirt.return_value + pool = ml.storagePoolLookupByName.return_value + pool.storageVolLookupByName.side_effect = libvirt.libvirtError('msg') + uc = MagicMock() + uc.name.return_value = 'undercloud' + ml.listAllDomains.return_value = [uc] + clean.clean_vms() + + @patch('apex.clean.libvirt.open') + def test_clean_vms_raises_clean_ex(self, mock_libvirt): + mock_libvirt.return_value = None + assert_raises(ApexCleanException, clean.clean_vms) def test_clean_ssh_keys(self): ssh_file = os.path.join(con.TEST_DUMMY_CONFIG, 'authorized_dummy') @@ -93,17 +109,9 @@ class TestClean: if os.path.isfile(ssh_file): os.remove(ssh_file) - @patch('libvirt.open') - def test_clean_vms(self, mock_libvirt): - ml = mock_libvirt.return_value - ml.storagePoolLookupByName.return_value = dummy_pool() - ml.listDefinedDomains.return_value = ['undercloud'] - ml.lookupByName.return_value = dummy_domain() - assert clean.clean_vms() is None - - @patch('apex.network.jumphost.detach_interface_from_ovs') - @patch('apex.network.jumphost.remove_ovs_bridge') - @patch('libvirt.open') + @patch('apex.clean.jumphost.detach_interface_from_ovs') + @patch('apex.clean.jumphost.remove_ovs_bridge') + @patch('apex.clean.libvirt.open') def test_clean_networks(self, mock_libvirt, mock_jumphost_ovs_remove, mock_jumphost_detach): ml = mock_libvirt.return_value @@ -112,3 +120,32 @@ class TestClean: mock_net.isActive.return_value = True clean.clean_networks() assert_equal(mock_net.destroy.call_count, 3) + + @patch('apex.clean.jumphost.detach_interface_from_ovs') + @patch('apex.clean.jumphost.remove_ovs_bridge') + @patch('apex.clean.libvirt.open') + def test_clean_networks_raises(self, mock_libvirt, + mock_jumphost_ovs_remove, + mock_jumphost_detach): + mock_libvirt.return_value = False + assert_raises(ApexCleanException, clean.clean_networks) + + @patch('apex.clean.clean_ssh_keys') + @patch('apex.clean.clean_networks') + @patch('apex.clean.clean_vbmcs') + @patch('apex.clean.clean_vms') + @patch('apex.clean.clean_nodes') + @patch('apex.clean.os.path.isfile') + @patch('apex.clean.os.makedirs') + @patch('apex.clean.argparse') + def test_main(self, mock_argparse, mock_mkdirs, mock_isfile, + mock_clean_nodes, mock_clean_vms, mock_clean_vbmcs, + mock_clean_networks, mock_clean_ssh_keys): + clean.main() + + @patch('apex.clean.os.path.isfile') + @patch('apex.clean.os.makedirs') + @patch('apex.clean.argparse') + def test_main_no_inv(self, mock_argparse, mock_mkdirs, mock_isfile): + mock_isfile.return_value = False + assert_raises(FileNotFoundError, clean.main) |