b3ead6f79bd3e386945f9b5a86c493d230a7db1f
[apex.git] / apex / tests / test_apex_clean.py
1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet (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 mock
11 import os
12 import pyipmi
13 import pyipmi.chassis
14 from mock import patch
15 from nose.tools import (
16     assert_raises,
17     assert_equal
18 )
19
20 from apex import clean_nodes
21 from apex import clean
22 from apex.tests import constants as con
23
24
25 class dummy_domain:
26
27     def isActive(self):
28         return True
29
30     def destroy(self):
31         pass
32
33     def undefine(self):
34         pass
35
36
37 class dummy_vol:
38
39     def wipe(self, *args):
40         pass
41
42     def delete(self, *args):
43         pass
44
45
46 class dummy_pool:
47
48     def storageVolLookupByName(self, *args, **kwargs):
49         return dummy_vol()
50
51     def refresh(self):
52         pass
53
54
55 class TestClean:
56     @classmethod
57     def setup_class(cls):
58         """This method is run once for each class before any tests are run"""
59
60     @classmethod
61     def teardown_class(cls):
62         """This method is run once for each class _after_ all tests are run"""
63
64     def setup(self):
65         """This method is run once before _each_ test method is executed"""
66
67     def teardown(self):
68         """This method is run once after _each_ test method is executed"""
69
70     def test_clean_nodes(self):
71         with mock.patch.object(pyipmi.Session, 'establish') as mock_method:
72             with patch.object(pyipmi.chassis.Chassis,
73                               'chassis_control_power_down') as mock_method2:
74                 clean_nodes('apex/tests/config/inventory.yaml')
75
76         assert_equal(mock_method.call_count, 5)
77         assert_equal(mock_method2.call_count, 5)
78
79     @patch('virtualbmc.manager.VirtualBMCManager.list',
80            return_value=[{'domain_name': 'dummy1'}, {'domain_name': 'dummy2'}])
81     @patch('virtualbmc.manager.VirtualBMCManager.delete')
82     def test_vmbc_clean(self, vbmc_del_func, vbmc_list_func):
83         assert clean.clean_vbmcs() is None
84
85     def test_clean_ssh_keys(self):
86         ssh_file = os.path.join(con.TEST_DUMMY_CONFIG, 'authorized_dummy')
87         with open(ssh_file, 'w') as fh:
88             fh.write('ssh-rsa 2LwlofGD8rNUFAlafY2/oUsKOf1mQ1 stack@undercloud')
89         assert clean.clean_ssh_keys(ssh_file) is None
90         with open(ssh_file, 'r') as fh:
91             output = fh.read()
92         assert 'stack@undercloud' not in output
93         if os.path.isfile(ssh_file):
94             os.remove(ssh_file)
95
96     @patch('libvirt.open')
97     def test_clean_vms(self, mock_libvirt):
98         ml = mock_libvirt.return_value
99         ml.storagePoolLookupByName.return_value = dummy_pool()
100         ml.listDefinedDomains.return_value = ['undercloud']
101         ml.lookupByName.return_value = dummy_domain()
102         assert clean.clean_vms() is None
103
104     @patch('apex.network.jumphost.detach_interface_from_ovs')
105     @patch('apex.network.jumphost.remove_ovs_bridge')
106     @patch('libvirt.open')
107     def test_clean_networks(self, mock_libvirt, mock_jumphost_ovs_remove,
108                             mock_jumphost_detach):
109         ml = mock_libvirt.return_value
110         ml.listNetworks.return_value = ['admin', 'external', 'tenant', 'blah']
111         mock_net = ml.networkLookupByName.return_value
112         mock_net.isActive.return_value = True
113         clean.clean_networks()
114         assert_equal(mock_net.destroy.call_count, 3)