1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (dradez@redhat.com) (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 apex.common import constants
20 from apex.undercloud.undercloud import Undercloud
21 from apex.undercloud.undercloud import ApexUndercloudException
23 from nose.tools import (
24 assert_regexp_matches,
30 class TestUndercloud(unittest.TestCase):
33 """This method is run once for each class before any tests are run"""
36 def teardown_class(cls):
37 """This method is run once for each class _after_ all tests are run"""
40 """This method is run once before _each_ test method is executed"""
43 """This method is run once after _each_ test method is executed"""
45 @patch.object(Undercloud, '_get_vm', return_value=None)
46 @patch.object(Undercloud, 'create')
47 def test_init(self, mock_get_vm, mock_create):
48 Undercloud('img_path', 'tplt_path')
49 mock_create.assert_called()
51 @patch.object(Undercloud, '_get_vm', return_value=object())
52 @patch.object(Undercloud, 'create')
53 def test_init_uc_exists(self, mock_get_vm, mock_create):
54 assert_raises(ApexUndercloudException,
55 Undercloud, 'img_path', 'tplt_path')
57 @patch('apex.undercloud.undercloud.libvirt.open')
58 @patch.object(Undercloud, 'create')
59 def test_get_vm_exists(self, mock_create, mock_libvirt):
60 assert_raises(ApexUndercloudException,
61 Undercloud, 'img_path', 'tplt_path')
63 @patch('apex.undercloud.undercloud.libvirt.open')
64 @patch.object(Undercloud, 'create')
65 def test_get_vm_not_exists(self, mock_create, mock_libvirt):
66 conn = mock_libvirt.return_value
67 conn.lookupByName.side_effect = libvirt.libvirtError('defmsg')
68 Undercloud('img_path', 'tplt_path')
70 @patch('apex.undercloud.undercloud.vm_lib')
71 @patch.object(Undercloud, 'inject_auth', return_value=None)
72 @patch.object(Undercloud, 'setup_volumes', return_value=None)
73 @patch.object(Undercloud, '_get_vm', return_value=None)
74 def test_create(self, mock_get_vm, mock_setup_vols,
75 mock_inject_auth, mock_vm_lib):
76 Undercloud('img_path', 'tplt_path', external_network=True)
77 mock_inject_auth.assert_called()
78 mock_setup_vols.assert_called()
79 mock_inject_auth.assert_called()
81 @patch.object(Undercloud, '_get_vm', return_value=None)
82 @patch.object(Undercloud, 'create')
83 def test_set_ip(self, mock_get_vm, mock_create):
84 uc = Undercloud('img_path', 'tplt_path', external_network=True)
86 if_addrs = {'item1': {'addrs': [{'type': libvirt.VIR_IP_ADDR_TYPE_IPV4,
87 'addr': 'ipaddress'}]},
88 'item2': {'addrs': [{'type': libvirt.VIR_IP_ADDR_TYPE_IPV4,
89 'addr': 'ipaddress'}]}}
90 uc.vm.interfaceAddresses.return_value = if_addrs
91 assert_true(uc._set_ip())
93 @patch('apex.undercloud.undercloud.time.sleep')
94 @patch.object(Undercloud, '_set_ip', return_value=False)
95 @patch.object(Undercloud, '_get_vm', return_value=None)
96 @patch.object(Undercloud, 'create')
97 def test_start(self, mock_create, mock_get_vm,
98 mock_set_ip, mock_time):
99 uc = Undercloud('img_path', 'tplt_path', external_network=True)
101 uc.vm.isActive.return_value = False
102 mock_set_ip.return_value = True
105 @patch('apex.undercloud.undercloud.time.sleep')
106 @patch.object(Undercloud, '_set_ip', return_value=False)
107 @patch.object(Undercloud, '_get_vm', return_value=None)
108 @patch.object(Undercloud, 'create')
109 def test_start_no_ip(self, mock_create, mock_get_vm,
110 mock_set_ip, mock_time):
111 uc = Undercloud('img_path', 'tplt_path', external_network=True)
113 uc.vm.isActive.return_value = True
114 mock_set_ip.return_value = False
115 assert_raises(ApexUndercloudException, uc.start)
117 @patch('apex.undercloud.undercloud.utils')
118 @patch.object(Undercloud, 'generate_config', return_value={})
119 @patch.object(Undercloud, '_get_vm', return_value=None)
120 @patch.object(Undercloud, 'create')
121 def test_configure(self, mock_create, mock_get_vm,
122 mock_generate_config, mock_utils):
123 uc = Undercloud('img_path', 'tplt_path', external_network=True)
126 uc.configure(ns, ds, 'playbook', '/tmp/dir')
128 @patch('apex.undercloud.undercloud.utils')
129 @patch.object(Undercloud, 'generate_config', return_value={})
130 @patch.object(Undercloud, '_get_vm', return_value=None)
131 @patch.object(Undercloud, 'create')
132 def test_configure_raises(self, mock_create, mock_get_vm,
133 mock_generate_config, mock_utils):
134 uc = Undercloud('img_path', 'tplt_path', external_network=True)
137 subps_err = subprocess.CalledProcessError(1, 'cmd')
138 mock_utils.run_ansible.side_effect = subps_err
139 assert_raises(ApexUndercloudException,
140 uc.configure, ns, ds, 'playbook', '/tmp/dir')
142 @patch('apex.undercloud.undercloud.os.remove')
143 @patch('apex.undercloud.undercloud.os.path')
144 @patch('apex.undercloud.undercloud.shutil')
145 @patch.object(Undercloud, '_get_vm', return_value=None)
146 @patch.object(Undercloud, 'create')
147 def test_setup_vols(self, mock_get_vm, mock_create,
148 mock_shutil, mock_os_path, mock_os_remove):
149 uc = Undercloud('img_path', 'tplt_path', external_network=True)
150 mock_os_path.isfile.return_value = True
151 mock_os_path.exists.return_value = True
153 for img_file in ('overcloud-full.vmlinuz', 'overcloud-full.initrd',
155 src_img = os.path.join(uc.image_path, img_file)
156 dest_img = os.path.join(constants.LIBVIRT_VOLUME_PATH, img_file)
157 mock_shutil.copyfile.assert_called_with(src_img, dest_img)
159 @patch('apex.undercloud.undercloud.os.path')
160 @patch.object(Undercloud, '_get_vm', return_value=None)
161 @patch.object(Undercloud, 'create')
162 def test_setup_vols_raises(self, mock_get_vm, mock_create, mock_os_path):
163 uc = Undercloud('img_path', 'tplt_path', external_network=True)
164 mock_os_path.isfile.return_value = False
165 assert_raises(ApexUndercloudException, uc.setup_volumes)
167 @patch('apex.undercloud.undercloud.virt_utils')
168 @patch.object(Undercloud, '_get_vm', return_value=None)
169 @patch.object(Undercloud, 'create')
170 def test_inject_auth(self, mock_get_vm, mock_create, mock_vutils):
171 uc = Undercloud('img_path', 'tplt_path', external_network=True)
174 test_ops = [{'--root-password': 'password:test'},
175 {'--run-command': 'mkdir -p /root/.ssh'},
177 '/root/.ssh/id_rsa.pub:/root/.ssh/authorized_keys'},
178 {'--run-command': 'chmod 600 /root/.ssh/authorized_keys'},
179 {'--run-command': 'restorecon /root/.ssh/authorized_keys'},
181 'cp /root/.ssh/authorized_keys /home/stack/.ssh/'},
183 'chown stack:stack /home/stack/.ssh/authorized_keys'},
185 'chmod 600 /home/stack/.ssh/authorized_keys'}]
186 mock_vutils.virt_customize.assert_called_with(test_ops, uc.volume)
188 @patch.object(Undercloud, '_get_vm', return_value=None)
189 @patch.object(Undercloud, 'create')
190 def test_generate_config(self, mock_get_vm, mock_create):
192 ns.enabled_network_list = ['admin', 'external']
196 'networks': {'admin':
197 {'cidr': ipaddress.ip_network('192.0.2.0/24'),
198 'installer_vm': {'ip': '192.0.2.1',
200 'dhcp_range': ['192.0.2.15', '192.0.2.30']
204 'cidr': ipaddress.ip_network('192.168.0.0/24'),
205 'installer_vm': {'ip': '192.168.0.1',
210 ns.__getitem__.side_effect = ns_dict.__getitem__
211 ns.__contains__.side_effect = ns_dict.__contains__
212 ds = {'global_params': {}}
214 Undercloud('img_path', 'tplt_path').generate_config(ns, ds)
216 @patch.object(Undercloud, '_get_vm', return_value=None)
217 @patch.object(Undercloud, 'create')
218 @patch('apex.undercloud.undercloud.virt_utils')
219 def test_update_delorean(self, mock_vutils, mock_uc_create, mock_get_vm):
220 uc = Undercloud('img_path', 'tmplt_path', external_network=True)
221 uc._update_delorean_repo()
224 "/etc/yum.repos.d/deloran.repo "
225 "https://trunk.rdoproject.org/centos7-{}"
226 "/current-tripleo/delorean.repo".format(
227 constants.DEFAULT_OS_VERSION))
228 test_ops = [{'--run-command': download_cmd}]
229 mock_vutils.virt_customize.assert_called_with(test_ops, uc.volume)