From 9d24ddc56a76175a4b7514cea5c789e2612b2528 Mon Sep 17 00:00:00 2001 From: Dan Radez Date: Tue, 19 Sep 2017 15:57:01 -0400 Subject: Adding python unittests for apex/virtual/* Change-Id: I13dd395cd6270cbf0a02855b1d29794ecca06d76 Signed-off-by: Dan Radez --- apex/deploy.py | 2 +- apex/overcloud/overcloud_deploy.py | 2 +- apex/tests/test_apex_virtual_configure_vm.py | 102 ++++++++++++++++ apex/tests/test_apex_virtual_utils.py | 101 ++++++++++++++++ apex/undercloud/undercloud.py | 2 +- apex/virtual/utils.py | 167 +++++++++++++++++++++++++++ apex/virtual/virtual_utils.py | 167 --------------------------- tox.ini | 2 +- 8 files changed, 374 insertions(+), 171 deletions(-) create mode 100644 apex/tests/test_apex_virtual_configure_vm.py create mode 100644 apex/tests/test_apex_virtual_utils.py create mode 100644 apex/virtual/utils.py delete mode 100644 apex/virtual/virtual_utils.py diff --git a/apex/deploy.py b/apex/deploy.py index a0561384..55b1092a 100644 --- a/apex/deploy.py +++ b/apex/deploy.py @@ -20,7 +20,7 @@ import sys import tempfile import apex.virtual.configure_vm as vm_lib -import apex.virtual.virtual_utils as virt_utils +import apex.virtual.utils as virt_utils from apex import DeploySettings from apex import Inventory from apex import NetworkEnvironment diff --git a/apex/overcloud/overcloud_deploy.py b/apex/overcloud/overcloud_deploy.py index 20fb4a60..3b79ec49 100644 --- a/apex/overcloud/overcloud_deploy.py +++ b/apex/overcloud/overcloud_deploy.py @@ -20,7 +20,7 @@ import time from apex.common import constants as con from apex.common.exceptions import ApexDeployException from apex.common import parsers -from apex.virtual import virtual_utils as virt_utils +from apex.virtual import utils as virt_utils from cryptography.hazmat.primitives import serialization as \ crypto_serialization from cryptography.hazmat.primitives.asymmetric import rsa diff --git a/apex/tests/test_apex_virtual_configure_vm.py b/apex/tests/test_apex_virtual_configure_vm.py new file mode 100644 index 00000000..228e06d6 --- /dev/null +++ b/apex/tests/test_apex_virtual_configure_vm.py @@ -0,0 +1,102 @@ +############################################################################## +# 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 unittest + +from mock import patch + +from apex.virtual.configure_vm import generate_baremetal_macs +from apex.virtual.configure_vm import create_vm_storage +from apex.virtual.configure_vm import create_vm + +from nose.tools import ( + assert_regexp_matches, + assert_raises, + assert_equal) + + +class TestVirtualConfigureVM(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""" + + def test_generate_baremetal_macs(self): + assert_regexp_matches(generate_baremetal_macs()[0], + '^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$') + + def test_generate_baremetal_macs_alot(self): + assert_equal(len(generate_baremetal_macs(127)), 127) + + def test_generate_baremetal_macs_too_many(self): + assert_raises(ValueError, generate_baremetal_macs, 128) + + @patch('apex.virtual.configure_vm.libvirt.open') + def test_create_vm_storage(self, mock_libvirt_open): + # setup mock + conn = mock_libvirt_open.return_value + pool = conn.storagePoolLookupByName.return_value + pool.isActive.return_value = 0 + # execute + create_vm_storage('test') + + @patch('apex.virtual.configure_vm.libvirt.open') + def test_create_vm_storage_pool_none(self, mock_libvirt_open): + # setup mock + conn = mock_libvirt_open.return_value + conn.storagePoolLookupByName.return_value = None + # execute + assert_raises(Exception, create_vm_storage, 'test') + + @patch('apex.virtual.configure_vm.libvirt.open') + def test_create_vm_storage_libvirt_error(self, mock_libvirt_open): + # setup mock + conn = mock_libvirt_open.return_value + pool = conn.storagePoolLookupByName.return_value + pool.storageVolLookupByName.side_effect = libvirt.libvirtError('ermsg') + # execute + assert_raises(libvirt.libvirtError, create_vm_storage, 'test') + + @patch('apex.virtual.configure_vm.libvirt.open') + def test_create_vm_storage_new_vol_none(self, mock_libvirt_open): + # setup mock + conn = mock_libvirt_open.return_value + pool = conn.storagePoolLookupByName.return_value + pool.createXML.return_value = None + # execute + assert_raises(Exception, create_vm_storage, 'test') + + @patch('apex.virtual.configure_vm.libvirt.open') + @patch('apex.virtual.configure_vm.create_vm_storage') + def test_create_vm(self, mock_create_vm_storage, + mock_libvirt_open): + create_vm('test', 'image', default_network=True, + direct_boot=True, kernel_args='test', template_dir='./build') + + @patch('apex.virtual.configure_vm.libvirt.open') + @patch('apex.virtual.configure_vm.create_vm_storage') + def test_create_vm_x86_64(self, mock_create_vm_storage, + mock_libvirt_open): + create_vm('test', 'image', arch='x86_64', template_dir='./build') + + @patch('apex.virtual.configure_vm.libvirt.open') + @patch('apex.virtual.configure_vm.create_vm_storage') + def test_create_vm_aarch64(self, mock_create_vm_storage, + mock_libvirt_open): + create_vm('test', 'image', arch='aarch64', template_dir='./build') diff --git a/apex/tests/test_apex_virtual_utils.py b/apex/tests/test_apex_virtual_utils.py new file mode 100644 index 00000000..643069f3 --- /dev/null +++ b/apex/tests/test_apex_virtual_utils.py @@ -0,0 +1,101 @@ +############################################################################## +# 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 subprocess +import unittest + +from mock import patch + +from apex.virtual.utils import DEFAULT_VIRT_IP +from apex.virtual.utils import get_virt_ip +from apex.virtual.utils import generate_inventory +from apex.virtual.utils import host_setup +from apex.virtual.utils import virt_customize + +from nose.tools import ( + assert_is_instance, + assert_regexp_matches, + assert_raises, + assert_equal) + + +class TestVirtualUtils(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('apex.virtual.utils.subprocess.check_output') + def test_get_virt_ip(self, mock_subprocess): + mock_subprocess.return_value = '' + assert_equal(get_virt_ip(), DEFAULT_VIRT_IP) + + @patch('apex.virtual.utils.subprocess.check_output') + def test_get_virt_ip_not_default(self, mock_subprocess): + mock_subprocess.return_value = ''' + +''' + assert_equal(get_virt_ip(), '1.2.3.4') + + @patch('apex.virtual.utils.subprocess.check_output') + def test_get_virt_ip_raises(self, mock_subprocess): + mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd') + assert_equal(get_virt_ip(), DEFAULT_VIRT_IP) + + @patch('apex.virtual.utils.common_utils') + def test_generate_inventory(self, mock_common_utils): + assert_is_instance(generate_inventory('target_file'), dict) + + @patch('apex.virtual.utils.common_utils') + def test_generate_inventory_ha_enabled(self, mock_common_utils): + assert_is_instance(generate_inventory('target_file', ha_enabled=True), + dict) + + @patch('apex.virtual.utils.iptc') + @patch('apex.virtual.utils.subprocess.check_call') + @patch('apex.virtual.utils.vbmc_lib') + def test_host_setup(self, mock_vbmc_lib, mock_subprocess, mock_iptc): + host_setup({'test': 2468}) + mock_subprocess.assert_called_with(['vbmc', 'start', 'test']) + + @patch('apex.virtual.utils.iptc') + @patch('apex.virtual.utils.subprocess.check_call') + @patch('apex.virtual.utils.vbmc_lib') + def test_host_setup_raise_called_process_error(self, mock_vbmc_lib, + mock_subprocess, mock_iptc): + mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd') + assert_raises(subprocess.CalledProcessError, host_setup, {'tst': 2468}) + + @patch('apex.virtual.utils.os.path') + @patch('apex.virtual.utils.subprocess.check_output') + def test_virt_customize(self, mock_subprocess, mock_os_path): + virt_customize([{'--operation': 'arg'}], 'target') + + @patch('apex.virtual.utils.subprocess.check_output') + def test_virt_customize_file_not_found(self, mock_subprocess): + assert_raises(FileNotFoundError, + virt_customize, + [{'--operation': 'arg'}], 'target') + + @patch('apex.virtual.utils.os.path') + @patch('apex.virtual.utils.subprocess.check_output') + def test_virt_customize_raises(self, mock_subprocess, mock_os_path): + mock_subprocess.side_effect = subprocess.CalledProcessError(1, 'cmd') + assert_raises(subprocess.CalledProcessError, + virt_customize, + [{'--operation': 'arg'}], 'target') diff --git a/apex/undercloud/undercloud.py b/apex/undercloud/undercloud.py index 7b7c35f0..50035638 100644 --- a/apex/undercloud/undercloud.py +++ b/apex/undercloud/undercloud.py @@ -15,7 +15,7 @@ import shutil import subprocess import time -from apex.virtual import virtual_utils as virt_utils +from apex.virtual import utils as virt_utils from apex.virtual import configure_vm as vm_lib from apex.common import constants from apex.common import utils diff --git a/apex/virtual/utils.py b/apex/virtual/utils.py new file mode 100644 index 00000000..226af1b5 --- /dev/null +++ b/apex/virtual/utils.py @@ -0,0 +1,167 @@ +############################################################################## +# Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others. +# +# 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 copy +import iptc +import logging +import os +import platform +import pprint +import subprocess +import xml.etree.ElementTree as ET + +from apex.common import utils as common_utils +from apex.virtual import configure_vm as vm_lib +from virtualbmc import manager as vbmc_lib + +DEFAULT_RAM = 8192 +DEFAULT_PM_PORT = 6230 +DEFAULT_USER = 'admin' +DEFAULT_PASS = 'password' +DEFAULT_VIRT_IP = '192.168.122.1' + + +def get_virt_ip(): + try: + virsh_net_xml = subprocess.check_output(['virsh', 'net-dumpxml', + 'default'], + stderr=subprocess.STDOUT) + except subprocess.CalledProcessError: + logging.warning('Unable to detect default virsh network IP. Will ' + 'use 192.168.122.1') + return DEFAULT_VIRT_IP + + tree = ET.fromstring(virsh_net_xml) + ip_tag = tree.find('ip') + if ip_tag is not None: + virsh_ip = ip_tag.get('address') + if virsh_ip: + logging.debug("Detected virsh default network ip: " + "{}".format(virsh_ip)) + return virsh_ip + + return DEFAULT_VIRT_IP + + +def generate_inventory(target_file, ha_enabled=False, num_computes=1, + controller_ram=DEFAULT_RAM, arch=platform.machine(), + compute_ram=DEFAULT_RAM, vcpus=4): + """ + Generates inventory file for virtual deployments + :param target_file: + :param ha_enabled: + :param num_computes: + :param controller_ram: + :param arch: + :param compute_ram: + :param vcpus: + :return: + """ + + node = {'mac_address': '', + 'ipmi_ip': get_virt_ip(), + 'ipmi_user': DEFAULT_USER, + 'ipmi_pass': DEFAULT_PASS, + 'pm_type': 'pxe_ipmitool', + 'pm_port': '', + 'cpu': vcpus, + 'memory': DEFAULT_RAM, + 'disk': 41, + 'arch': arch, + 'capabilities': '' + } + + inv_output = {'nodes': {}} + if ha_enabled: + num_ctrlrs = 3 + else: + num_ctrlrs = 1 + + for idx in range(num_ctrlrs + num_computes): + tmp_node = copy.deepcopy(node) + tmp_node['mac_address'] = vm_lib.generate_baremetal_macs(1)[0] + tmp_node['pm_port'] = DEFAULT_PM_PORT + idx + if idx < num_ctrlrs: + tmp_node['capabilities'] = 'profile:control' + tmp_node['memory'] = controller_ram + else: + tmp_node['capabilities'] = 'profile:compute' + tmp_node['memory'] = compute_ram + inv_output['nodes']['node{}'.format(idx)] = copy.deepcopy(tmp_node) + + common_utils.dump_yaml(inv_output, target_file) + logging.info('Virtual environment file created: {}'.format(target_file)) + return inv_output + + +def host_setup(node): + """ + Handles configuring vmbc and firewalld/iptables + :param node: dictionary of domain names and ports for ipmi + :return: + """ + vbmc_manager = vbmc_lib.VirtualBMCManager() + for name, port in node.items(): + vbmc_manager.add(username=DEFAULT_USER, password=DEFAULT_PASS, + port=port, address=get_virt_ip(), domain_name=name, + libvirt_uri='qemu:///system', + libvirt_sasl_password=False, + libvirt_sasl_username=False) + + # TODO(trozet): add support for firewalld + try: + subprocess.check_call(['systemctl', 'stop', 'firewalld']) + subprocess.check_call(['systemctl', 'restart', 'libvirtd']) + except subprocess.CalledProcessError: + logging.warning('Failed to stop firewalld and restart libvirtd') + # iptables rule + rule = iptc.Rule() + rule.protocol = 'udp' + match = rule.create_match('udp') + match.dport = str(port) + rule.add_match(match) + rule.target = iptc.Target(rule, "ACCEPT") + chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT") + chain.insert_rule(rule) + try: + subprocess.check_call(['vbmc', 'start', name]) + logging.debug("Started vbmc for domain {}".format(name)) + except subprocess.CalledProcessError: + logging.error("Failed to start vbmc for {}".format(name)) + raise + logging.debug('vmbcs setup: {}'.format(vbmc_manager.list())) + + +def virt_customize(ops, target): + """ + Helper function to virt customize disks + :param ops: list of of operations and arguments + :param target: target disk to modify + :return: None + """ + logging.info("Virt customizing target disk: {}".format(target)) + virt_cmd = ['virt-customize'] + for op in ops: + for op_cmd, op_arg in op.items(): + virt_cmd.append(op_cmd) + virt_cmd.append(op_arg) + virt_cmd.append('-a') + virt_cmd.append(target) + if not os.path.isfile(target): + raise FileNotFoundError + my_env = os.environ.copy() + my_env['LIBGUESTFS_BACKEND'] = 'direct' + logging.debug("Virt-customizing with: \n{}".format(virt_cmd)) + try: + logging.debug(subprocess.check_output(virt_cmd, env=my_env, + stderr=subprocess.STDOUT)) + except subprocess.CalledProcessError as e: + logging.error("Error executing virt-customize: {}".format( + pprint.pformat(e.output))) + raise diff --git a/apex/virtual/virtual_utils.py b/apex/virtual/virtual_utils.py deleted file mode 100644 index 1fe2c399..00000000 --- a/apex/virtual/virtual_utils.py +++ /dev/null @@ -1,167 +0,0 @@ -############################################################################## -# Copyright (c) 2017 Tim Rozet (trozet@redhat.com) and others. -# -# 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 copy -import iptc -import logging -import os -import platform -import pprint -import subprocess -import xml.etree.ElementTree as ET - -from apex.common import utils -from apex.virtual import configure_vm as vm_lib -from virtualbmc import manager as vbmc_lib - -DEFAULT_RAM = 8192 -DEFAULT_PM_PORT = 6230 -DEFAULT_USER = 'admin' -DEFAULT_PASS = 'password' -DEFAULT_VIRT_IP = '192.168.122.1' - - -def get_virt_ip(): - try: - virsh_net_xml = subprocess.check_output(['virsh', 'net-dumpxml', - 'default'], - stderr=subprocess.STDOUT) - except subprocess.CalledProcessError: - logging.warning('Unable to detect default virsh network IP. Will ' - 'use 192.168.122.1') - return DEFAULT_VIRT_IP - - tree = ET.fromstring(virsh_net_xml) - ip_tag = tree.find('ip') - if ip_tag: - virsh_ip = ip_tag.get('address') - if virsh_ip: - logging.debug("Detected virsh default network ip: " - "{}".format(virsh_ip)) - return virsh_ip - - return DEFAULT_VIRT_IP - - -def generate_inventory(target_file, ha_enabled=False, num_computes=1, - controller_ram=DEFAULT_RAM, arch=platform.machine(), - compute_ram=DEFAULT_RAM, vcpus=4): - """ - Generates inventory file for virtual deployments - :param target_file: - :param ha_enabled: - :param num_computes: - :param controller_ram: - :param arch: - :param compute_ram: - :param vcpus: - :return: - """ - - node = {'mac_address': '', - 'ipmi_ip': get_virt_ip(), - 'ipmi_user': DEFAULT_USER, - 'ipmi_pass': DEFAULT_PASS, - 'pm_type': 'pxe_ipmitool', - 'pm_port': '', - 'cpu': vcpus, - 'memory': DEFAULT_RAM, - 'disk': 41, - 'arch': arch, - 'capabilities': '' - } - - inv_output = {'nodes': {}} - if ha_enabled: - num_ctrlrs = 3 - else: - num_ctrlrs = 1 - - for idx in range(num_ctrlrs + num_computes): - tmp_node = copy.deepcopy(node) - tmp_node['mac_address'] = vm_lib.generate_baremetal_macs(1)[0] - tmp_node['pm_port'] = DEFAULT_PM_PORT + idx - if idx < num_ctrlrs: - tmp_node['capabilities'] = 'profile:control' - tmp_node['memory'] = controller_ram - else: - tmp_node['capabilities'] = 'profile:compute' - tmp_node['memory'] = compute_ram - inv_output['nodes']['node{}'.format(idx)] = copy.deepcopy(tmp_node) - - utils.dump_yaml(inv_output, target_file) - - logging.info('Virtual environment file created: {}'.format(target_file)) - - -def host_setup(node): - """ - Handles configuring vmbc and firewalld/iptables - :param node: dictionary of domain names and ports for ipmi - :return: - """ - vbmc_manager = vbmc_lib.VirtualBMCManager() - for name, port in node.items(): - vbmc_manager.add(username=DEFAULT_USER, password=DEFAULT_PASS, - port=port, address=get_virt_ip(), domain_name=name, - libvirt_uri='qemu:///system', - libvirt_sasl_password=False, - libvirt_sasl_username=False) - - # TODO(trozet): add support for firewalld - try: - subprocess.check_call(['systemctl', 'stop', 'firewalld']) - subprocess.check_call(['systemctl', 'restart', 'libvirtd']) - except subprocess.CalledProcessError: - logging.warning('Failed to stop firewalld and restart libvirtd') - # iptables rule - rule = iptc.Rule() - rule.protocol = 'udp' - match = rule.create_match('udp') - match.dport = str(port) - rule.add_match(match) - rule.target = iptc.Target(rule, "ACCEPT") - chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT") - chain.insert_rule(rule) - try: - subprocess.check_call(['vbmc', 'start', name]) - logging.debug("Started vbmc for domain {}".format(name)) - except subprocess.CalledProcessError: - logging.error("Failed to start vbmc for {}".format(name)) - raise - logging.debug('vmbcs setup: {}'.format(vbmc_manager.list())) - - -def virt_customize(ops, target): - """ - Helper function to virt customize disks - :param ops: list of of operations and arguments - :param target: target disk to modify - :return: None - """ - logging.info("Virt customizing target disk: {}".format(target)) - virt_cmd = ['virt-customize'] - for op in ops: - for op_cmd, op_arg in op.items(): - virt_cmd.append(op_cmd) - virt_cmd.append(op_arg) - virt_cmd.append('-a') - virt_cmd.append(target) - if not os.path.isfile(target): - raise FileNotFoundError - my_env = os.environ.copy() - my_env['LIBGUESTFS_BACKEND'] = 'direct' - logging.debug("Virt-customizing with: \n{}".format(virt_cmd)) - try: - logging.debug(subprocess.check_output(virt_cmd, env=my_env, - stderr=subprocess.STDOUT)) - except subprocess.CalledProcessError as e: - logging.error("Error executing virt-customize: {}".format( - pprint.pformat(e.output))) - raise diff --git a/tox.ini b/tox.ini index 87b6c035..cde191cb 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ commands = --cover-tests \ --cover-package=apex \ --cover-xml \ - --cover-min-percentage 90 \ + --cover-min-percentage 94 \ apex/tests coverage report -- cgit 1.2.3-korg