1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet, Dan Radez (Red Hat)
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 ##############################################################################
16 from mock import patch
17 from mock import MagicMock
19 from nose.tools import (
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
34 """This method is run once for each class before any tests are run"""
37 def teardown_class(cls):
38 """This method is run once for each class _after_ all tests are run"""
41 """This method is run once before _each_ test method is executed"""
44 """This method is run once after _each_ test method is executed"""
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')
52 assert_equal(mock_method.call_count, 5)
53 assert_equal(mock_method2.call_count, 5)
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')
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')
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())
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()
80 uc.name.return_value = 'undercloud'
82 x.name.return_value = 'domain_x'
83 ml.listAllDomains.return_value = [uc, x]
84 assert_is_none(clean.clean_vms())
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')
92 uc.name.return_value = 'undercloud'
93 ml.listAllDomains.return_value = [uc]
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)
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:
108 assert 'stack@undercloud' not in output
109 if os.path.isfile(ssh_file):
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)
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)
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):
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)