1 ##############################################################################
2 # Copyright (c) 2016 Tim Rozet (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 ##############################################################################
14 from mock import patch
15 from nose.tools import (
20 from apex import clean_nodes
21 from apex import clean
22 from apex.tests import constants as con
39 def wipe(self, *args):
42 def delete(self, *args):
48 def storageVolLookupByName(self, *args, **kwargs):
58 """This method is run once for each class before any tests are run"""
61 def teardown_class(cls):
62 """This method is run once for each class _after_ all tests are run"""
65 """This method is run once before _each_ test method is executed"""
68 """This method is run once after _each_ test method is executed"""
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')
76 assert_equal(mock_method.call_count, 5)
77 assert_equal(mock_method2.call_count, 5)
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
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:
92 assert 'stack@undercloud' not in output
93 if os.path.isfile(ssh_file):
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
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)