Increasing clean.py unittest coverage
[apex.git] / apex / tests / test_apex_clean.py
1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet, Dan Radez (Red Hat)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import libvirt
11 import mock
12 import os
13 import pyipmi
14 import pyipmi.chassis
15
16 from mock import patch
17 from mock import MagicMock
18
19 from nose.tools import (
20     assert_raises,
21     assert_equal,
22     assert_is_none
23 )
24
25 from apex import clean_nodes
26 from apex import clean
27 from apex.common.exceptions import ApexCleanException
28 from apex.tests import constants as con
29
30
31 class TestClean:
32     @classmethod
33     def setup_class(cls):
34         """This method is run once for each class before any tests are run"""
35
36     @classmethod
37     def teardown_class(cls):
38         """This method is run once for each class _after_ all tests are run"""
39
40     def setup(self):
41         """This method is run once before _each_ test method is executed"""
42
43     def teardown(self):
44         """This method is run once after _each_ test method is executed"""
45
46     def test_clean_nodes(self):
47         with mock.patch.object(pyipmi.Session, 'establish') as mock_method:
48             with patch.object(pyipmi.chassis.Chassis,
49                               'chassis_control_power_down') as mock_method2:
50                 clean_nodes('apex/tests/config/inventory.yaml')
51
52         assert_equal(mock_method.call_count, 5)
53         assert_equal(mock_method2.call_count, 5)
54
55     @patch('apex.clean.utils.parse_yaml')
56     def test_clean_nodes_empty(self, mock_parse_yaml):
57         mock_parse_yaml.return_value = None
58         assert_raises(SystemExit, clean_nodes, 'dummy_file')
59         mock_parse_yaml.return_value = {}
60         assert_raises(SystemExit, clean_nodes, 'dummy_file')
61
62     @patch('apex.clean.pyipmi.interfaces.create_interface')
63     @patch('apex.clean.utils.parse_yaml')
64     def test_clean_nodes_raises(self, mock_parse_yaml, mock_pyipmi):
65         mock_parse_yaml.return_value = {'nodes': {'node': {}}}
66         mock_pyipmi.side_effect = Exception()
67         assert_raises(SystemExit, clean_nodes, 'dummy_file')
68
69     @patch('virtualbmc.manager.VirtualBMCManager.list',
70            return_value=[{'domain_name': 'dummy1'}, {'domain_name': 'dummy2'}])
71     @patch('virtualbmc.manager.VirtualBMCManager.delete')
72     def test_clean_vmbcs(self, vbmc_del_func, vbmc_list_func):
73         assert_is_none(clean.clean_vbmcs())
74
75     @patch('apex.clean.libvirt.open')
76     def test_clean_vms(self, mock_libvirt):
77         ml = mock_libvirt.return_value
78         ml.storagePoolLookupByName.return_value = MagicMock()
79         uc = MagicMock()
80         uc.name.return_value = 'undercloud'
81         x = MagicMock()
82         x.name.return_value = 'domain_x'
83         ml.listAllDomains.return_value = [uc, x]
84         assert_is_none(clean.clean_vms())
85
86     @patch('apex.clean.libvirt.open')
87     def test_clean_vms_skip_vol(self, mock_libvirt):
88         ml = mock_libvirt.return_value
89         pool = ml.storagePoolLookupByName.return_value
90         pool.storageVolLookupByName.side_effect = libvirt.libvirtError('msg')
91         uc = MagicMock()
92         uc.name.return_value = 'undercloud'
93         ml.listAllDomains.return_value = [uc]
94         clean.clean_vms()
95
96     @patch('apex.clean.libvirt.open')
97     def test_clean_vms_raises_clean_ex(self, mock_libvirt):
98         mock_libvirt.return_value = None
99         assert_raises(ApexCleanException, clean.clean_vms)
100
101     def test_clean_ssh_keys(self):
102         ssh_file = os.path.join(con.TEST_DUMMY_CONFIG, 'authorized_dummy')
103         with open(ssh_file, 'w') as fh:
104             fh.write('ssh-rsa 2LwlofGD8rNUFAlafY2/oUsKOf1mQ1 stack@undercloud')
105         assert clean.clean_ssh_keys(ssh_file) is None
106         with open(ssh_file, 'r') as fh:
107             output = fh.read()
108         assert 'stack@undercloud' not in output
109         if os.path.isfile(ssh_file):
110             os.remove(ssh_file)
111
112     @patch('apex.clean.jumphost.detach_interface_from_ovs')
113     @patch('apex.clean.jumphost.remove_ovs_bridge')
114     @patch('apex.clean.libvirt.open')
115     def test_clean_networks(self, mock_libvirt, mock_jumphost_ovs_remove,
116                             mock_jumphost_detach):
117         ml = mock_libvirt.return_value
118         ml.listNetworks.return_value = ['admin', 'external', 'tenant', 'blah']
119         mock_net = ml.networkLookupByName.return_value
120         mock_net.isActive.return_value = True
121         clean.clean_networks()
122         assert_equal(mock_net.destroy.call_count, 3)
123
124     @patch('apex.clean.jumphost.detach_interface_from_ovs')
125     @patch('apex.clean.jumphost.remove_ovs_bridge')
126     @patch('apex.clean.libvirt.open')
127     def test_clean_networks_raises(self, mock_libvirt,
128                                    mock_jumphost_ovs_remove,
129                                    mock_jumphost_detach):
130         mock_libvirt.return_value = False
131         assert_raises(ApexCleanException, clean.clean_networks)
132
133     @patch('apex.clean.clean_ssh_keys')
134     @patch('apex.clean.clean_networks')
135     @patch('apex.clean.clean_vbmcs')
136     @patch('apex.clean.clean_vms')
137     @patch('apex.clean.clean_nodes')
138     @patch('apex.clean.os.path.isfile')
139     @patch('apex.clean.os.makedirs')
140     @patch('apex.clean.argparse')
141     def test_main(self, mock_argparse, mock_mkdirs, mock_isfile,
142                   mock_clean_nodes, mock_clean_vms, mock_clean_vbmcs,
143                   mock_clean_networks, mock_clean_ssh_keys):
144         clean.main()
145
146     @patch('apex.clean.os.path.isfile')
147     @patch('apex.clean.os.makedirs')
148     @patch('apex.clean.argparse')
149     def test_main_no_inv(self, mock_argparse, mock_mkdirs, mock_isfile):
150         mock_isfile.return_value = False
151         assert_raises(FileNotFoundError, clean.main)