summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Radez <dradez@redhat.com>2017-09-21 17:12:24 -0400
committerTim Rozet <trozet@redhat.com>2017-10-02 15:03:30 +0000
commit1ffe380a91d9751c480d3e9c344d412c49ad72d2 (patch)
tree85d75c35001c707138af9564d0ecbc7f3841d078
parent17c64de8f2d9c86c0f80df8dddf7831210f5234a (diff)
Adding unittests for undercloud.py
Change-Id: I65d73f7a1ad27f319857a72b7d9f738b04318aa6 Signed-off-by: Dan Radez <dradez@redhat.com> (cherry picked from commit 11be8e73043eeaa9ec18508410ec28faecbccc08)
-rw-r--r--apex/tests/test_apex_undercloud.py196
1 files changed, 196 insertions, 0 deletions
diff --git a/apex/tests/test_apex_undercloud.py b/apex/tests/test_apex_undercloud.py
new file mode 100644
index 00000000..9458bf9f
--- /dev/null
+++ b/apex/tests/test_apex_undercloud.py
@@ -0,0 +1,196 @@
+##############################################################################
+# Copyright (c) 2016 Dan Radez (dradez@redhat.com) (Red Hat)
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import libvirt
+import os
+import subprocess
+import unittest
+
+from mock import patch
+from mock import MagicMock
+
+from apex.common import constants
+from apex.undercloud.undercloud import Undercloud
+from apex.undercloud.undercloud import ApexUndercloudException
+
+from nose.tools import (
+ assert_regexp_matches,
+ assert_raises,
+ assert_true,
+ assert_equal)
+
+
+class TestUndercloud(unittest.TestCase):
+ @classmethod
+ def setup_class(cls):
+ """This method is run once for each class before any tests are run"""
+
+ @classmethod
+ def teardown_class(cls):
+ """This method is run once for each class _after_ all tests are run"""
+
+ def setup(self):
+ """This method is run once before _each_ test method is executed"""
+
+ def teardown(self):
+ """This method is run once after _each_ test method is executed"""
+
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_init(self, mock_get_vm, mock_create):
+ Undercloud('img_path', 'tplt_path')
+ mock_create.assert_called()
+
+ @patch.object(Undercloud, '_get_vm', return_value=object())
+ @patch.object(Undercloud, 'create')
+ def test_init_uc_exists(self, mock_get_vm, mock_create):
+ assert_raises(ApexUndercloudException,
+ Undercloud, 'img_path', 'tplt_path')
+
+ @patch('apex.undercloud.undercloud.libvirt.open')
+ @patch.object(Undercloud, 'create')
+ def test_get_vm_exists(self, mock_create, mock_libvirt):
+ assert_raises(ApexUndercloudException,
+ Undercloud, 'img_path', 'tplt_path')
+
+ @patch('apex.undercloud.undercloud.libvirt.open')
+ @patch.object(Undercloud, 'create')
+ def test_get_vm_not_exists(self, mock_create, mock_libvirt):
+ conn = mock_libvirt.return_value
+ conn.lookupByName.side_effect = libvirt.libvirtError('defmsg')
+ Undercloud('img_path', 'tplt_path')
+
+ @patch('apex.undercloud.undercloud.vm_lib')
+ @patch.object(Undercloud, 'inject_auth', return_value=None)
+ @patch.object(Undercloud, 'setup_volumes', return_value=None)
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ def test_create(self, mock_get_vm, mock_setup_vols,
+ mock_inject_auth, mock_vm_lib):
+ Undercloud('img_path', 'tplt_path', external_network=True)
+ mock_inject_auth.assert_called()
+ mock_setup_vols.assert_called()
+ mock_inject_auth.assert_called()
+
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_set_ip(self, mock_get_vm, mock_create):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ uc.vm = MagicMock()
+ if_addrs = {'item1': {'addrs': [{'type': libvirt.VIR_IP_ADDR_TYPE_IPV4,
+ 'addr': 'ipaddress'}]},
+ 'item2': {'addrs': [{'type': libvirt.VIR_IP_ADDR_TYPE_IPV4,
+ 'addr': 'ipaddress'}]}}
+ uc.vm.interfaceAddresses.return_value = if_addrs
+ assert_true(uc._set_ip())
+
+ @patch('apex.undercloud.undercloud.time.sleep')
+ @patch.object(Undercloud, '_set_ip', return_value=False)
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_start(self, mock_create, mock_get_vm,
+ mock_set_ip, mock_time):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ uc.vm = MagicMock()
+ uc.vm.isActive.return_value = False
+ mock_set_ip.return_value = True
+ uc.start()
+
+ @patch('apex.undercloud.undercloud.time.sleep')
+ @patch.object(Undercloud, '_set_ip', return_value=False)
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_start_no_ip(self, mock_create, mock_get_vm,
+ mock_set_ip, mock_time):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ uc.vm = MagicMock()
+ uc.vm.isActive.return_value = True
+ mock_set_ip.return_value = False
+ assert_raises(ApexUndercloudException, uc.start)
+
+ @patch('apex.undercloud.undercloud.utils')
+ @patch.object(Undercloud, 'generate_config', return_value={})
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_configure(self, mock_create, mock_get_vm,
+ mock_generate_config, mock_utils):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ ns = MagicMock()
+ uc.configure(ns, 'playbook', '/tmp/dir')
+
+ @patch('apex.undercloud.undercloud.utils')
+ @patch.object(Undercloud, 'generate_config', return_value={})
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_configure_raises(self, mock_create, mock_get_vm,
+ mock_generate_config, mock_utils):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ ns = MagicMock()
+ subps_err = subprocess.CalledProcessError(1, 'cmd')
+ mock_utils.run_ansible.side_effect = subps_err
+ assert_raises(ApexUndercloudException,
+ uc.configure, ns, 'playbook', '/tmp/dir')
+
+ @patch('apex.undercloud.undercloud.os.remove')
+ @patch('apex.undercloud.undercloud.os.path')
+ @patch('apex.undercloud.undercloud.shutil')
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_setup_vols(self, mock_get_vm, mock_create,
+ mock_shutil, mock_os_path, mock_os_remove):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ mock_os_path.isfile.return_value = True
+ mock_os_path.exists.return_value = True
+ uc.setup_volumes()
+ for img_file in ('overcloud-full.vmlinuz', 'overcloud-full.initrd',
+ 'undercloud.qcow2'):
+ src_img = os.path.join(uc.image_path, img_file)
+ dest_img = os.path.join(constants.LIBVIRT_VOLUME_PATH, img_file)
+ mock_shutil.copyfile.assert_called_with(src_img, dest_img)
+
+ @patch('apex.undercloud.undercloud.os.path')
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_setup_vols_raises(self, mock_get_vm, mock_create, mock_os_path):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ mock_os_path.isfile.return_value = False
+ assert_raises(ApexUndercloudException, uc.setup_volumes)
+
+ @patch('apex.undercloud.undercloud.virt_utils')
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_inject_auth(self, mock_get_vm, mock_create, mock_vutils):
+ uc = Undercloud('img_path', 'tplt_path', external_network=True)
+ uc.root_pw = 'test'
+ uc.inject_auth()
+ test_ops = [{'--root-password': 'password:test'},
+ {'--run-command': 'mkdir -p /root/.ssh'},
+ {'--upload':
+ '/root/.ssh/id_rsa.pub:/root/.ssh/authorized_keys'},
+ {'--run-command': 'chmod 600 /root/.ssh/authorized_keys'},
+ {'--run-command': 'restorecon /root/.ssh/authorized_keys'},
+ {'--run-command':
+ 'cp /root/.ssh/authorized_keys /home/stack/.ssh/'},
+ {'--run-command':
+ 'chown stack:stack /home/stack/.ssh/authorized_keys'},
+ {'--run-command':
+ 'chmod 600 /home/stack/.ssh/authorized_keys'}]
+ mock_vutils.virt_customize.assert_called_with(test_ops, uc.volume)
+
+ @patch.object(Undercloud, '_get_vm', return_value=None)
+ @patch.object(Undercloud, 'create')
+ def test_generate_config(self, mock_get_vm, mock_create):
+ ns_net = MagicMock()
+ ns_net.__getitem__.side_effect = \
+ lambda i: '1234/24' if i is 'cidr' else MagicMock()
+ ns = {'apex': MagicMock(),
+ 'dns-domain': 'dns',
+ 'networks': {'admin': ns_net,
+ 'external': [ns_net]}}
+
+ Undercloud('img_path', 'tplt_path').generate_config(ns)