diff options
Diffstat (limited to 'tests/unit/post')
-rw-r--r-- | tests/unit/post/test_glance.py | 71 | ||||
-rw-r--r-- | tests/unit/post/test_keystoneauth.py | 10 | ||||
-rw-r--r-- | tests/unit/post/test_neutron.py | 371 | ||||
-rw-r--r-- | tests/unit/post/test_nova.py | 79 | ||||
-rw-r--r-- | tests/unit/post/test_post_execute.py | 165 |
5 files changed, 695 insertions, 1 deletions
diff --git a/tests/unit/post/test_glance.py b/tests/unit/post/test_glance.py new file mode 100644 index 00000000..1a154464 --- /dev/null +++ b/tests/unit/post/test_glance.py @@ -0,0 +1,71 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp 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 os + +import pytest +import mock + +from deploy.post.glance import Glance +from deploy.post import glance + + +@pytest.fixture(scope="module") +def openrc_conf_file_dir(data_root): + return os.path.join(data_root, 'openrc_conf') + + +def test_create_Glance_instance(openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + glance = Glance(openrc=openrc_file) + assert glance.controller == glance.client.images + + +@mock.patch('glanceclient.v2.images.Controller.create') +@mock.patch('glanceclient.v2.images.Controller.upload') +def test_create_in_Glance(mock_upload, mock_create, + openrc_conf_file_dir, tmpdir): + class Test_image(): + def __init__(self, id): + self.id = id + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + file_name = 'test_image.qcow2' + file_path = os.path.join(tmpdir.dirname, tmpdir.basename, file_name) + with open(file_path, 'w') as f: + f.write('test_data') + id = 0x1234 + glance = Glance(openrc=openrc_file) + mock_create.return_value = Test_image(id) + ret = glance.create(file_name, file_path) + assert ret == id + tmpdir.remove() + + +@mock.patch.object(glance.Glance, 'list') +def test_get_by_name_in_Glance(mock_list, openrc_conf_file_dir): + class Test_image(): + def __init__(self, name): + self.name = name + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + image_inst1 = Test_image('test_image1.qcow2') + image_inst2 = Test_image('test_image2.qcow2') + images_list = [image_inst1, image_inst2] + mock_list.return_value = images_list + glance = Glance(openrc=openrc_file) + ret = glance.get_by_name('test_image1.qcow2') + assert ret == image_inst1 + + +@mock.patch('glanceclient.v2.images.Controller.list') +def test_list_in_Glance(mock_list, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + glance_list = ['test1'] + mock_list.return_value = glance_list + glance = Glance(openrc=openrc_file) + ret = glance.list() + assert ret == glance_list diff --git a/tests/unit/post/test_keystoneauth.py b/tests/unit/post/test_keystoneauth.py index 1ae86452..1a3021ee 100644 --- a/tests/unit/post/test_keystoneauth.py +++ b/tests/unit/post/test_keystoneauth.py @@ -102,3 +102,13 @@ def test__parse_openrc(openrc_conf_file_dir, openrc_file_name, expected): KeystoneClient = Keystoneauth(openrc) ret_openrc_dict = KeystoneClient._parse_openrc() assert expected == ret_openrc_dict + + +@pytest.mark.parametrize('openrc_file_name', [ + ( + 'admin-openrc.sh' + )]) +def test__get_auth(openrc_conf_file_dir, openrc_file_name,): + openrc = os.path.join(openrc_conf_file_dir, openrc_file_name) + KeystoneClient = Keystoneauth(openrc) + assert KeystoneClient._get_auth() diff --git a/tests/unit/post/test_neutron.py b/tests/unit/post/test_neutron.py new file mode 100644 index 00000000..94ec3a75 --- /dev/null +++ b/tests/unit/post/test_neutron.py @@ -0,0 +1,371 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp 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 os + +import pytest +import mock + +from deploy.post import neutron +from deploy.post.neutron import Neutron + + +@pytest.fixture(scope="module") +def openrc_conf_file_dir(data_root): + return os.path.join(data_root, 'openrc_conf') + + +def test_create_Neutron_instance(openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + neutron = Neutron(openrc=openrc_file) + assert neutron.openrc == openrc_file + + +@pytest.mark.parametrize('ret_get_network_by_name', [ + (None), + ({'name': 'network_test2'})]) +@mock.patch.object(neutron.Neutron, 'get_network_by_name') +@mock.patch.object(neutron.Neutron, '_create_network') +def test_create_network_in_Neutron(mock__create_network, mock_get_network_by_name, + ret_get_network_by_name, openrc_conf_file_dir): + net_name = 'network_test1' + net_body = {'name': 'network_test1'} + net_id = 0xabcd + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_get_network_by_name.return_value = ret_get_network_by_name + mock__create_network.return_value = net_id + neutron = Neutron(openrc=openrc_file) + ret = neutron.create_network(net_name, net_body) + if ret_get_network_by_name is None: + assert ret == net_id + mock__create_network.asset_called_once_with(net_body) + else: + assert ret is None + mock__create_network.assert_not_called() + + +@pytest.mark.parametrize('ret_get_subnet_by_name', [ + (None), + ({'name': 'subnet_test2'})]) +@mock.patch.object(neutron.Neutron, 'get_subnet_by_name') +@mock.patch.object(neutron.Neutron, '_create_subnet') +def test_create_subnet_in_Neutron_no_exist(mock__create_subnet, mock_get_subnet_by_name, + ret_get_subnet_by_name, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + subnet_body = {'name': 'subnet_test1'} + subnet_id = 0xabcd + mock_get_subnet_by_name.return_value = ret_get_subnet_by_name + mock__create_subnet.return_value = subnet_id + neutron = Neutron(openrc=openrc_file) + ret = neutron.create_subnet(subnet_body) + if ret_get_subnet_by_name is None: + assert ret == subnet_id + mock__create_subnet.asset_called_once_with(subnet_body) + else: + assert ret is None + mock__create_subnet.assert_not_called() + + +@mock.patch('neutronclient.v2_0.client.Client.list_networks') +def test_list_networks_in_Neutron(mock_list_networks, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + network_list = [{'name': 'network_test1'}] + mock_list_networks.return_value = {'networks': network_list} + neutron = Neutron(openrc=openrc_file) + ret = neutron.list_networks() + assert ret == network_list + mock_list_networks.assert_called_once() + + +@mock.patch('neutronclient.v2_0.client.Client.list_subnets') +def test_list_subnets_in_Neutron(mock_list_subnets, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + subnet_list = [{'name': 'subnet_test1'}] + mock_list_subnets.return_value = {'subnets': subnet_list} + neutron = Neutron(openrc=openrc_file) + ret = neutron.list_subnets() + assert ret == subnet_list + mock_list_subnets.assert_called_once() + + +@mock.patch.object(neutron.Neutron, 'list_networks') +def test_get_network_by_name_in_Neutron(mock_list_networks, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + network_list = [{'name': 'network_test1'}, {'name': 'network_test2'}] + network_name = 'network_test1' + mock_list_networks.return_value = network_list + neutron = Neutron(openrc=openrc_file) + ret = neutron.get_network_by_name(network_name) + assert ret == {'name': 'network_test1'} + mock_list_networks.assert_called_once() + + +@pytest.mark.parametrize('body, ret_list_subnets, expeced', [ + ( + { + 'subnets': [ + { + 'name': 'ext_subnet', + 'ip_version': 4, + 'network_id': 0x1234 + } + ] + }, + [ + {'name': 'ext_subnet', 'network_id': 0x1234}, + {'name': 'inter_subnet', 'network_id': 0x2345} + ], + {'name': 'ext_subnet', 'network_id': 0x1234} + ), + ( + { + 'subnets': [ + { + 'name': 'ext_subnet', + 'ip_version': 4, + 'network_id': 0x1234 + } + ] + }, + [ + {'name': 'admin_subnet', 'network_id': 0x1234}, + {'name': 'inter_subnet', 'network_id': 0x2345} + ], None + )]) +@mock.patch.object(neutron.Neutron, 'list_subnets') +def test_get_subnet_by_name_in_Neutron(mock_list_subnets, body, + ret_list_subnets, expeced, + openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_list_subnets.return_value = ret_list_subnets + neutron = Neutron(openrc=openrc_file) + ret = neutron.get_subnet_by_name(body) + assert ret == expeced + mock_list_subnets.assert_called_once() + + +@mock.patch('neutronclient.v2_0.client.Client.create_network') +def test__create_network_in_Neutron_no_exist(mock_create_network, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + nid = 0x1234 + name = 'ext' + body = { + 'network': { + 'name': 'ext', + 'admin_state_up': True, + 'shared': False, + 'provider:network_type': 'flat', + 'provider:physical_network': 'physnet1', + 'router:external': True + } + } + ret_net_info = { + 'network': + { + 'id': 0x1234 + } + } + mock_create_network.return_value = ret_net_info + neutron = Neutron(openrc=openrc_file) + ret = neutron._create_network(name, body) + assert ret == nid + mock_create_network.assert_called_once_with(body=body) + + +@mock.patch('neutronclient.v2_0.client.Client.create_subnet') +def test__create_subnet_in_Neutron_no_exist(mock_create_subnet, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + snid = 0xabcd + body = { + 'subnets': { + 'name': 'admin_external_subnet', + 'cidr': '172.70.0.0/24', + 'gateway_ip': '172.70.0.1', + 'allocation_pools': [{'start': '172.70.0.2', 'end': '172.70.0.100'}], + 'enable_dhcp': False + } + } + ret_subnet_info = { + 'subnets': [ + { + 'name': 'admin_external_subnet', + 'cidr': '172.70.0.0/24', + 'ip_version': 4, + 'network_id': 0x1234, + 'gateway_ip': '172.70.0.1', + 'allocation_pools': [{'start': '172.70.0.2', 'end': '172.70.0.100'}], + 'enable_dhcp': False, + 'id': 0xabcd + } + ] + } + mock_create_subnet.return_value = ret_subnet_info + neutron = Neutron(openrc=openrc_file) + ret = neutron._create_subnet(body) + assert ret == snid + mock_create_subnet.assert_called_once_with(body) + + +@mock.patch('neutronclient.v2_0.client.Client.list_security_groups') +def test__list_security_groups_in_Neutron(mock_list_security_groups, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + ret_security_groups = { + 'security_groups': + [ + { + 'name': 'default' + } + ] + } + security_groups_list = [ + { + 'name': 'default' + } + ] + mock_list_security_groups.return_value = ret_security_groups + neutron = Neutron(openrc=openrc_file) + ret = neutron._list_security_groups() + assert ret == security_groups_list + mock_list_security_groups.assert_called_once_with() + + +@mock.patch.object(neutron.Neutron, '_list_security_groups') +def test_get_security_group_by_name_in_Neutron(mock__list_security_groups, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + security_group_1 = {'name': 'default', 'security_group_id': 0x1234} + security_group_2 = {'name': 'security_1', 'security_group_id': 0x2345} + security_group_list = [security_group_1, security_group_2] + mock__list_security_groups.return_value = security_group_list + neutron = Neutron(openrc=openrc_file) + ret = neutron.get_security_group_by_name(security_group_1['name']) + assert ret == security_group_1 + mock__list_security_groups.assert_called_once_with() + + +@pytest.mark.parametrize('security_group, body, expected', [ + ( + { + 'security_group_rules': + [ + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'tcp', + 'port_range_min': 22, + 'port_range_max': 22, + 'remote_ip_prefix': '0.0.0.0/0', + }, + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'icmp', + 'remote_ip_prefix': '0.0.0.0/0', + } + ], + 'id': 0x1234 + }, + { + 'security_group_rule': + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'tcp', + 'port_range_min': 22, + 'port_range_max': 22, + 'remote_ip_prefix': '0.0.0.0/0', + } + }, True + ), + ( + { + 'security_group_rules': + [ + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'icmp', + 'remote_ip_prefix': '0.0.0.0/0', + } + ], + 'id': 0x1234 + }, + { + 'security_group_rule': + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'tcp', + 'port_range_min': 22, + 'port_range_max': 22, + 'remote_ip_prefix': '0.0.0.0/0', + } + }, False + )]) +def test__check_security_group_rule_conflict(security_group, body, + expected, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + neutron = Neutron(openrc=openrc_file) + ret = neutron._check_security_group_rule_conflict(security_group, body) + assert ret == expected + + +@pytest.mark.parametrize('ret__check_security_group_rule_conflict', [ + (True), + (False)]) +@mock.patch('neutronclient.v2_0.client.Client.create_security_group_rule') +@mock.patch.object(neutron.Neutron, '_check_security_group_rule_conflict') +def test_create_security_group_rule_in_Neutron(mock__check_security_group_rule_conflict, + mock_create_security_group_rule, + ret__check_security_group_rule_conflict, + openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + security_group = { + 'security_group_rules': + [ + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'tcp', + 'port_range_min': 22, + 'port_range_max': 22, + 'remote_ip_prefix': '0.0.0.0/0', + }, + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'icmp', + 'remote_ip_prefix': '0.0.0.0/0', + } + ], + 'id': 0x1234 + } + body = { + 'security_group_rule': + { + 'direction': 'ingress', + 'ethertype': 'IPv4', + 'protocol': 'tcp', + 'port_range_min': 22, + 'port_range_max': 22, + 'remote_ip_prefix': '0.0.0.0/0', + } + } + rule = { + 'security_group_rule': + { + 'id': 0x1234 + } + } + mock__check_security_group_rule_conflict.return_value = ret__check_security_group_rule_conflict + mock_create_security_group_rule.return_value = rule + neutron = Neutron(openrc=openrc_file) + neutron.create_security_group_rule(security_group, body) + if ret__check_security_group_rule_conflict is False: + mock_create_security_group_rule.assert_called_once_with(body=body) + else: + mock_create_security_group_rule.assert_not_called() diff --git a/tests/unit/post/test_nova.py b/tests/unit/post/test_nova.py new file mode 100644 index 00000000..c2f1d1fc --- /dev/null +++ b/tests/unit/post/test_nova.py @@ -0,0 +1,79 @@ +############################################################################## +# Copyright (c) 2017 ZTE Corp 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 os + +import pytest +import mock + +from deploy.post import nova +from deploy.post.nova import Nova + + +@pytest.fixture(scope="module") +def openrc_conf_file_dir(data_root): + return os.path.join(data_root, 'openrc_conf') + + +def test_create_Nova_instance(openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + nova = Nova(openrc=openrc_file) + assert nova.flavors == nova.client.flavors + + +@mock.patch('novaclient.v2.flavors.FlavorManager.create') +def test_create_flavor_in_Glance(mock_create, openrc_conf_file_dir): + class Test_flavor(): + def __init__(self, id): + self.id = id + flavor_conf = { + 'name': 'flavor_test', + 'ram': 64, + 'vcpus': 1, + 'disk': 1, + 'is_public': True + } + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + id = 0x1234 + nova = Nova(openrc=openrc_file) + mock_create.return_value = Test_flavor(id) + ret = nova.create_flavor(flavor_conf['name'], flavor_conf['ram'], + flavor_conf['vcpus'], flavor_conf['disk'], + is_public=flavor_conf['is_public']) + assert ret == id + mock_create.assert_called_once_with(flavor_conf['name'], flavor_conf['ram'], + flavor_conf['vcpus'], flavor_conf['disk'], + is_public=flavor_conf['is_public']) + + +@mock.patch.object(nova.Nova, 'list_flavors') +def test_get_flavor_by_name_in_Nova(mock_list_flavors, openrc_conf_file_dir): + class Test_flavor(): + def __init__(self, name): + self.name = name + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + flavor_inst1 = Test_flavor('test_flavor1') + flavor_inst2 = Test_flavor('test_flavor2') + flavors_list = [flavor_inst1, flavor_inst2] + flavor_name = 'test_flavor2' + mock_list_flavors.return_value = flavors_list + nova = Nova(openrc=openrc_file) + ret = nova.get_flavor_by_name(flavor_name) + assert ret == flavor_inst2 + mock_list_flavors.assert_called_once_with() + + +@mock.patch('novaclient.v2.flavors.FlavorManager.list') +def test_list_flavors_in_Nova(mock_list, openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + flavor_list = ['test1'] + mock_list.return_value = flavor_list + nova = Nova(openrc=openrc_file) + ret = nova.list_flavors() + assert ret == flavor_list + mock_list.assert_called_once_with(detailed=True) diff --git a/tests/unit/post/test_post_execute.py b/tests/unit/post/test_post_execute.py index c06bc085..55e345a2 100644 --- a/tests/unit/post/test_post_execute.py +++ b/tests/unit/post/test_post_execute.py @@ -11,12 +11,20 @@ import os from deepdiff import DeepDiff import pytest +import mock +from deploy.post import execute from deploy.post.execute import ( _config_external_network, + _config_external_subnet, _config_icmp_security_group_rule, _config_ssh_security_group_rule, - _config_kolla_admin_openrc + _config_kolla_admin_openrc, + _create_external_network, + _create_flavor_m1_micro, + _prepare_cirros, + _create_image_TestVM, + _create_security_group_rules ) @@ -47,6 +55,42 @@ def test__config_external_network(ext_name, physnet, expected): assert _config_external_network(ext_name, physnet) == expected +@pytest.mark.parametrize('ext_id, network_conf, expected', [ + (0x1234, + { + 'ext_network_name': 'admin_external', + 'ext_cidr': '172.70.0.0/24', + 'ext_gateway': '172.70.0.1', + 'ext_ip_ranges': [{'start': '172.70.0.2', 'end': '172.70.0.100'}] + }, + { + 'subnets': [ + { + 'name': 'admin_external_subnet', + 'cidr': '172.70.0.0/24', + 'ip_version': 4, + 'network_id': 0x1234, + 'gateway_ip': '172.70.0.1', + 'allocation_pools': [{'start': '172.70.0.2', 'end': '172.70.0.100'}], + 'enable_dhcp': False + } + ] + })]) +def test__config_external_subnet(ext_id, network_conf, expected): + class Network_conf(): + def __init__(self, network_name, cidr, gateway, ip_ranges): + self.ext_network_name = network_name + self.ext_cidr = cidr + self.ext_gateway = gateway + self.ext_ip_ranges = ip_ranges + + network_conf_inst = Network_conf(network_conf['ext_network_name'], + network_conf['ext_cidr'], + network_conf['ext_gateway'], + network_conf['ext_ip_ranges']) + assert _config_external_subnet(ext_id, network_conf_inst) == expected + + @pytest.mark.parametrize('security_group_id, expected', [ ('0x1111', { @@ -129,3 +173,122 @@ def test__config_kolla_admin_openrc(globals_file_name, openrc_conf_file_dir, tmp for val in diff['iterable_item_added'].values(): assert 'export SDN_CONTROLLER_IP' in val tmpdir.remove() + + +@pytest.fixture(scope="module") +def net_conf_file_dir(data_root): + return os.path.join(data_root, 'lab_conf') + + +@mock.patch.object(execute.neutron.Neutron, 'create_network') +@mock.patch.object(execute.neutron.Neutron, 'create_subnet') +def test__create_external_network_with_openrc(mock_create_subnet, mock_create_network, + net_conf_file_dir, openrc_conf_file_dir): + external_network_info = { + 'network': { + 'name': 'admin_external', + 'admin_state_up': True, + 'shared': False, + 'provider:network_type': 'flat', + 'provider:physical_network': 'physnet1', + 'router:external': True + } + } + external_subnet_info = { + 'subnets': [ + { + 'name': 'admin_external_subnet', + 'cidr': '172.70.0.0/24', + 'ip_version': 4, + 'network_id': 0x1234, + 'gateway_ip': '172.70.0.1', + 'allocation_pools': [{'start': '172.70.0.2', 'end': '172.70.0.100'}], + 'enable_dhcp': False + } + ] + } + network_file = os.path.join(net_conf_file_dir, 'network_baremetal.yml') + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_create_network.return_value = 0x1234 + mock_create_subnet.return_value = 0xabcd + _create_external_network(network_file, openrc_file) + mock_create_network.assert_called_once_with('admin_external', external_network_info) + mock_create_subnet.assert_called_once_with(external_subnet_info) + + +@mock.patch.object(execute.neutron.Neutron, 'create_network') +@mock.patch.object(execute.neutron.Neutron, 'create_subnet') +@mock.patch('deploy.post.execute.neutron.Neutron') +def test__create_external_network_without_openrc(mock_Neutron, mock_create_subnet, mock_create_network, + net_conf_file_dir): + network_file = os.path.join(net_conf_file_dir, 'network_baremetal.yml') + mock_create_network.return_value = 0x1234 + mock_create_subnet.return_value = 0xabcd + _create_external_network(network_file) + mock_create_network.assert_not_called() + mock_create_subnet.assert_not_called() + + +@pytest.mark.parametrize('flavor,', [ + (None), + ({'name': 'm1.micro', 'ram': 64})]) +@mock.patch.object(execute.nova.Nova, 'get_flavor_by_name') +@mock.patch.object(execute.nova.Nova, 'create_flavor') +def test__create_flavor_m1_micro(mock_create_flavor, mock_get_flavor_by_name, + openrc_conf_file_dir, flavor): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_create_flavor.return_value = True + mock_get_flavor_by_name.return_value = flavor + _create_flavor_m1_micro(openrc_file) + mock_get_flavor_by_name.assert_called_once_with('m1.micro') + if flavor is None: + mock_create_flavor.assert_called_once_with('m1.micro', ram=64, vcpus=1, disk=0) + else: + mock_create_flavor.assert_not_called() + + +@mock.patch('deploy.post.execute.os.system') +def test__prepare_cirros(mock_system): + mock_system.return_value = 0 + _prepare_cirros() + assert mock_system.call_count == 3 + + +@pytest.mark.parametrize('image_path, image_info', [ + (None, None), + ('/var/lib/daisy/images/cirros-0.3.5-x86_64-disk.img', None), + (None, {'name': 'TestVM'}), + ('/var/lib/daisy/images/cirros-0.3.5-x86_64-disk.img', {'name': 'TestVM'})]) +@mock.patch('deploy.post.execute._prepare_cirros') +@mock.patch('deploy.post.execute.err_exit') +@mock.patch.object(execute.glance.Glance, 'create') +@mock.patch.object(execute.glance.Glance, 'get_by_name') +def test__create_image_TestVM(mock_get_by_name, mock_create, + mock_err_exit, mock___prepare_cirros, + image_path, image_info, + openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_get_by_name.return_value = image_info + mock_create.return_value = 0x1234 + mock___prepare_cirros.return_value = image_path + _create_image_TestVM(openrc_file) + mock_get_by_name.assert_called_once_with('TestVM') + if image_info: + mock___prepare_cirros.assert_not_called() + else: + mock___prepare_cirros.assert_called_once_with() + if image_path: + mock_create.assert_called_once_with('TestVM', image_path) + else: + mock_err_exit.assert_called_once_with("Test image preparation failed") + + +@mock.patch.object(execute.neutron.Neutron, 'create_security_group_rule') +@mock.patch.object(execute.neutron.Neutron, 'get_security_group_by_name') +def test__create_security_group_rules(mock_get_security_group_by_name, mock_create_security_group_rule, + openrc_conf_file_dir): + openrc_file = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh') + mock_get_security_group_by_name.return_value = {'id': 0xaaaa} + _create_security_group_rules(openrc_file) + mock_get_security_group_by_name.assert_called_once_with('default') + assert mock_create_security_group_rule.call_count == 2 |