From: Gulsum Atici Date: Thu, 27 Oct 2022 12:18:27 +0000 (+0300) Subject: Refactor openstack new_vminstance method X-Git-Tag: release-v13.0-start~4 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=26f7366ed0ab69a4c7c3e0bc2fdd51f35b36c396;p=osm%2FRO.git Refactor openstack new_vminstance method Change-Id: I33bba6d4ce911e6429835d93705f9b7f4704ebae Signed-off-by: Gulsum Atici --- diff --git a/RO-VIM-openstack/osm_rovim_openstack/tests/test_vimconn_openstack.py b/RO-VIM-openstack/osm_rovim_openstack/tests/test_vimconn_openstack.py index 09991244..c48d1843 100644 --- a/RO-VIM-openstack/osm_rovim_openstack/tests/test_vimconn_openstack.py +++ b/RO-VIM-openstack/osm_rovim_openstack/tests/test_vimconn_openstack.py @@ -23,19 +23,50 @@ This module contains unit tests for the OpenStack VIM connector Run this directly with python2 or python3. """ - import copy +from copy import deepcopy import logging import unittest import mock +from mock import MagicMock, patch from neutronclient.v2_0.client import Client from osm_ro_plugin import vimconn +from osm_ro_plugin.vimconn import VimConnConnectionException, VimConnException from osm_rovim_openstack.vimconn_openstack import vimconnector __author__ = "Igor D.C." __date__ = "$23-aug-2017 23:59:59$" +# Variables Used in TestNewVmInstance Class +name = "basicvm" +description = "my firewall" +start = True +image_id = "408b73-e9cc-5a6a-t270-82cc4811bd4a" +flavor_id = "208b73-e9cc-5a6a-t270-82cc4811bd4a" +affinity_group_list = [] +net_list = [] +cloud_config = {} +disk_list = [] +disk_list2 = [ + {"size": 10, "image_id": image_id}, + {"size": 20}, +] +availability_zone_index = 0 +availability_zone_list = ["nova"] +floating_network_vim_id = "108b73-e9cc-5a6a-t270-82cc4811bd4a" +net_id = "83372685-f67f-49fd-8722-eabb7692fc22" +net2_id = "46472685-f67f-49fd-8722-eabb7692fc22" +mac_address = "00:00:5e:00:53:af" +port_id = "03372685-f67f-49fd-8722-eabb7692fc22" +time_return_value = 156570000 +port2_id = "17472685-f67f-49fd-8722-eabb7692fc22" +root_vol_id = "tc408b73-r9cc-5a6a-a270-82cc4811bd4a" +ip_addr1 = "20.3.4.5" +volume_id = "ac408b73-b9cc-4a6a-a270-82cc4811bd4a" +volume_id2 = "o4e0e83-b9uu-4akk-a234-89cc4811bd4a" +volume_id3 = "44e0e83-t9uu-4akk-a234-p9cc4811bd4a" + class TestSfcOperations(unittest.TestCase): @mock.patch("logging.getLogger", autospec=True) @@ -1057,5 +1088,3363 @@ class TestSfcOperations(unittest.TestCase): self.assertEqual(result, "638f957c-82df-11e7-b7c8-132706021464") +class Status: + def __init__(self, s): + self.status = s + + def __str__(self): + return self.status + + +class CopyingMock(MagicMock): + def __call__(self, *args, **kwargs): + args = deepcopy(args) + kwargs = deepcopy(kwargs) + return super(CopyingMock, self).__call__(*args, **kwargs) + + +class TestNewVmInstance(unittest.TestCase): + @patch("logging.getLogger", autospec=True) + def setUp(self, mock_logger): + # Instantiate dummy VIM connector so we can test it + # It throws exception because of dummy parameters, + # We are disabling the logging of exception not to print them to console. + mock_logger = logging.getLogger() + mock_logger.disabled = True + self.vimconn = vimconnector( + "123", + "openstackvim", + "456", + "789", + "http://dummy.url", + None, + "user", + "pass", + ) + self.vimconn.neutron = CopyingMock() + self.vimconn.nova = CopyingMock() + self.vimconn.cinder = CopyingMock() + self.server = MagicMock(object, autospec=True) + self.server.tenant_id = "408b73-r9cc-5a6a-a270-82cc4811bd4a" + self.server.id = "908b73-e9cc-5a6a-t270-82cc4811bd4a" + self.vimconn.config["security_groups"] = "default" + self.vimconn.config["keypair"] = "my_keypair" + self.vimconn.security_groups_id = "12345" + self.vimconn.nova.api_version.get_string.return_value = "2.32" + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_security_groups_exists_in_config( + self, mock_get_ids + ): + """In VIM config security_groups exists, net port_security is True + no_port_security_extension does not exist. + """ + self.vimconn.config = {"security_groups": "example_security_group"} + net = {"port_security": True} + port_dict = {} + result_dict = {"security_groups": "12345"} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_security_groups_exists_in_config_no_security_groups_id( + self, mock_get_ids + ): + """In VIM config Security_groups exists, net port_security is True, vim security_groups_id does not exist, + no_port_security_extension does not exist. + """ + self.vimconn.config = {"security_groups": "example_security_group"} + self.vimconn.security_groups_id = None + net = {"port_security": True} + port_dict = {} + result_dict = {"security_groups": None} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_security_groups_exists_security_extension_true_in_config( + self, mock_get_ids + ): + """In VIM config security_groups exists, net port_security is True, in VIM security_groups_id exists, + no_port_security_extension set to True. + """ + self.vimconn.config = { + "security_groups": "example_security_group", + "no_port_security_extension": True, + } + net = {"port_security": True} + port_dict = {} + result_dict = {} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_no_security_groups_in_config( + self, mock_get_ids + ): + """In VIM config security_group does not exist, net port_security True, in VIM security_groups_id exists, + no_port_security_extension does not exist.""" + self.vimconn.config = {} + net = {"port_security": True} + port_dict = {} + result_dict = {} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_no_security_groups_security_extension_true_in_config( + self, mock_get_ids + ): + """Security_group does not exist, net port_security is True, in VIM security_groups_id exists, + no_port_security_extension set to True.""" + self.vimconn.config = {"no_port_security_extension": True} + net = {"port_security": True} + port_dict = {} + result_dict = {} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_security_groups_exists_net_port_security_false( + self, mock_get_ids + ): + """In VIM config security_group exists, net port_security False, security_groups_id exists, + no_port_security_extension does not exist.""" + self.vimconn.config = {"security_groups": "example_security_group"} + net = {"port_security": False} + port_dict = {} + result_dict = {} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + @patch.object(vimconnector, "_get_ids_from_name") + def test_prepare_port_dict_security_net_port_security_false_port_security_extension_true( + self, mock_get_ids + ): + """In VIM config security_group exists, net port_security False, security_groups_id exists, + no_port_security_extension set to True.""" + self.vimconn.config = { + "security_groups": "example_security_group", + "no_port_security_extension": True, + } + net = {"port_security": False} + port_dict = {} + result_dict = {} + + self.vimconn._prepare_port_dict_security_groups(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + mock_get_ids.assert_not_called() + + def test_prepare_port_dict_binding_net_type_virtual(self): + """net type is virtual.""" + net = {"type": "virtual"} + port_dict = {} + result_dict = {} + self.vimconn._prepare_port_dict_binding(net, port_dict) + self.assertDictEqual(result_dict, port_dict) + + def test_prepare_port_dict_binding_net_type_vf(self): + """net type is VF, vim_type is not VIO.""" + net = {"type": "VF"} + self.vimconn.vim_type = None + port_dict = {} + result_dict = {"binding:vnic_type": "direct"} + self.vimconn._prepare_port_dict_binding(net, port_dict) + self.assertDictEqual(port_dict, result_dict) + + def test_prepare_port_dict_binding_net_type_sriov_vim_type_vio(self): + """net type is SR-IOV, vim_type is VIO.""" + net = {"type": "SR-IOV"} + self.vimconn.vim_type = "VIO" + port_dict = {} + result_dict = { + "binding:vnic_type": "direct", + "port_security_enabled": False, + "provider_security_groups": [], + "security_groups": [], + } + self.vimconn._prepare_port_dict_binding(net, port_dict) + self.assertDictEqual(port_dict, result_dict) + + def test_prepare_port_dict_binding_net_type_passthrough(self): + """net type is pci-passthrough.""" + net = {"type": "PCI-PASSTHROUGH"} + port_dict = {} + result_dict = { + "binding:vnic_type": "direct-physical", + } + self.vimconn._prepare_port_dict_binding(net, port_dict) + self.assertDictEqual(port_dict, result_dict) + + def test_prepare_port_dict_binding_no_net_type(self): + """net type is missing.""" + net = {} + port_dict = {} + with self.assertRaises(VimConnException) as err: + self.vimconn._prepare_port_dict_binding(net, port_dict) + self.assertEqual(str(err.exception), "Type is missing in the network details.") + + def test_set_fixed_ip(self): + """new_port has fixed ip.""" + net = {} + new_port = { + "port": { + "fixed_ips": [{"ip_address": "10.1.2.3"}, {"ip_address": "20.1.2.3"}] + } + } + result = {"ip": "10.1.2.3"} + self.vimconn._set_fixed_ip(new_port, net) + self.assertDictEqual(net, result) + + def test_set_fixed_ip_no_fixed_ip(self): + """new_port does not have fixed ip.""" + net = {} + new_port = {"port": {}} + result = {"ip": None} + self.vimconn._set_fixed_ip(new_port, net) + self.assertDictEqual(net, result) + + def test_set_fixed_ip_raise_exception(self): + """new_port does not have port details.""" + net = {} + new_port = {} + with self.assertRaises(Exception) as err: + self.vimconn._set_fixed_ip(new_port, net) + self.assertEqual(type(err.exception), KeyError) + + def test_prepare_port_dict_mac_ip_addr(self): + """mac address and ip address exist.""" + net = { + "mac_address": mac_address, + "ip_address": "10.0.1.5", + } + port_dict = {} + result_dict = { + "mac_address": mac_address, + "fixed_ips": [{"ip_address": "10.0.1.5"}], + } + self.vimconn._prepare_port_dict_mac_ip_addr(net, port_dict) + self.assertDictEqual(port_dict, result_dict) + + def test_prepare_port_dict_mac_ip_addr_no_mac_and_ip(self): + """mac address and ip address does not exist.""" + net = {} + port_dict = {} + result_dict = {} + self.vimconn._prepare_port_dict_mac_ip_addr(net, port_dict) + self.assertDictEqual(port_dict, result_dict) + + def test_create_new_port(self): + """new port has id and mac address.""" + new_port = { + "port": { + "id": port_id, + "mac_address": mac_address, + }, + } + self.vimconn.neutron.create_port.return_value = new_port + net, port_dict, created_items = {}, {}, {} + expected_result = new_port + expected_net = { + "mac_adress": mac_address, + "vim_id": port_id, + } + expected_created_items = {f"port:{port_id}": True} + result = self.vimconn._create_new_port(port_dict, created_items, net) + self.assertDictEqual(result, expected_result) + self.assertEqual(net, expected_net) + self.assertEqual(created_items, expected_created_items) + self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict}) + + def test_create_new_port_without_mac_or_id(self): + """new port does not have mac address or ID.""" + new_port = {} + self.vimconn.neutron.create_port.return_value = new_port + net, port_dict, created_items = {}, {}, {} + with self.assertRaises(KeyError): + self.vimconn._create_new_port(port_dict, created_items, net) + self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict}) + + def test_create_new_port_neutron_create_port_raises_exception(self): + """Neutron create port raises exception.""" + self.vimconn.neutron.create_port.side_effect = VimConnException( + "New port is not created." + ) + net, port_dict, created_items = {}, {}, {} + with self.assertRaises(VimConnException): + self.vimconn._create_new_port(port_dict, created_items, net) + self.vimconn.neutron.create_port.assert_called_once_with({"port": port_dict}) + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """Net has name, type, net-id.""" + + net = { + "net_id": net_id, + "name": "management", + "type": "virtual", + } + created_items = {} + new_port = { + "port": { + "id": net_id, + "mac_address": mac_address, + "name": "management", + "fixed_ips": [{"ip_address": ip_addr1}], + }, + } + mock_create_new_port.return_value = new_port + expected_port = { + "port-id": net_id, + "tag": "management", + } + port_dict = { + "network_id": net_id, + "name": "management", + "admin_state_up": True, + } + + new_port_result, port_result = self.vimconn._create_port( + net, name, created_items + ) + + self.assertDictEqual(new_port_result, new_port) + self.assertDictEqual(port_result, expected_port) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + mock_create_new_port.assert_called_once_with(port_dict, created_items, net) + mock_set_fixed_ip.assert_called_once_with(new_port, net) + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_no_port_name( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """Net has no name.""" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + new_port = { + "port": { + "id": net_id, + "mac_address": mac_address, + "name": name, + "fixed_ips": [{"ip_address": ip_addr1}], + }, + } + mock_create_new_port.return_value = new_port + expected_port = { + "port-id": net_id, + "tag": name, + } + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + new_port_result, port_result = self.vimconn._create_port( + net, name, created_items + ) + + self.assertDictEqual(new_port_result, new_port) + self.assertDictEqual(port_result, expected_port) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + mock_create_new_port.assert_called_once_with(port_dict, created_items, net) + mock_set_fixed_ip.assert_called_once_with(new_port, net) + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_nova_api_version_smaller_than_232( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """Nova api version is smaller than 2.32.""" + self.vimconn.nova.api_version.get_string.return_value = "2.30" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + new_port = { + "port": { + "id": net_id, + "mac_address": mac_address, + "name": name, + "fixed_ips": [{"ip_address": ip_addr1}], + }, + } + mock_create_new_port.return_value = new_port + expected_port = { + "port-id": net_id, + } + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + new_port_result, port_result = self.vimconn._create_port( + net, name, created_items + ) + + self.assertDictEqual(new_port_result, new_port) + self.assertDictEqual(port_result, expected_port) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + mock_create_new_port.assert_called_once_with(port_dict, created_items, net) + mock_set_fixed_ip.assert_called_once_with(new_port, net) + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_create_new_port_raise_exception( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """_create_new_port method raises exception.""" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + mock_create_new_port.side_effect = Exception + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + with self.assertRaises(Exception): + self.vimconn._create_port(net, name, created_items) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + mock_create_new_port.assert_called_once_with(port_dict, created_items, net) + mock_set_fixed_ip.assert_not_called() + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_create_sec_groups_raises_exception( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """_prepare_port_dict_security_groups method raises exception.""" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + mock_prepare_port_dict_security_groups.side_effect = Exception + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + with self.assertRaises(Exception): + self.vimconn._create_port(net, name, created_items) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + + mock_prepare_port_dict_binding.assert_not_called() + mock_prepare_port_dict_mac_ip_addr.assert_not_called() + mock_create_new_port.assert_not_called() + mock_set_fixed_ip.assert_not_called() + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_create_port_dict_binding_raise_exception( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """_prepare_port_dict_binding method raises exception.""" + + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + mock_prepare_port_dict_binding.side_effect = Exception + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + with self.assertRaises(Exception): + self.vimconn._create_port(net, name, created_items) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + + mock_prepare_port_dict_mac_ip_addr.assert_not_called() + mock_create_new_port.assert_not_called() + mock_set_fixed_ip.assert_not_called() + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_create_port_mac_ip_addr_raise_exception( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """prepare_port_dict_mac_ip_addr method raises exception.""" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + mock_prepare_port_dict_mac_ip_addr.side_effect = Exception + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + + with self.assertRaises(Exception): + self.vimconn._create_port(net, name, created_items) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + + mock_create_new_port.assert_not_called() + mock_set_fixed_ip.assert_not_called() + + @patch.object(vimconnector, "_prepare_port_dict_security_groups") + @patch.object(vimconnector, "_prepare_port_dict_binding") + @patch.object(vimconnector, "_prepare_port_dict_mac_ip_addr") + @patch.object(vimconnector, "_create_new_port") + @patch.object(vimconnector, "_set_fixed_ip") + def test_create_port_create_port_set_fixed_ip_raise_exception( + self, + mock_set_fixed_ip, + mock_create_new_port, + mock_prepare_port_dict_mac_ip_addr, + mock_prepare_port_dict_binding, + mock_prepare_port_dict_security_groups, + ): + """_set_fixed_ip method raises exception.""" + net = { + "net_id": net_id, + "type": "virtual", + } + created_items = {} + mock_set_fixed_ip.side_effect = VimConnException( + "Port detail is missing in new_port." + ) + port_dict = { + "network_id": net_id, + "admin_state_up": True, + "name": name, + } + new_port = { + "port": { + "id": net_id, + "mac_address": mac_address, + "name": name, + "fixed_ips": [{"ip_address": ip_addr1}], + }, + } + mock_create_new_port.return_value = new_port + + with self.assertRaises(VimConnException): + self.vimconn._create_port(net, name, created_items) + + mock_prepare_port_dict_security_groups.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_binding.assert_called_once_with(net, port_dict) + mock_prepare_port_dict_mac_ip_addr.assert_called_once_with(net, port_dict) + mock_create_new_port.assert_called_once_with(port_dict, created_items, net) + mock_set_fixed_ip.assert_called_once_with(new_port, net) + + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_create_port") + def test_prepare_network_for_vm_instance_no_net_id( + self, mock_create_port, mock_reload_connection + ): + """Nets do not have net_id""" + mock_reload_connection.side_effect = None + created_items = {} + net_list = [ + { + "use": "mgmt", + "port_security": False, + "exit_on_floating_ip_error": False, + "port_security_disable_strategy": "full", + }, + { + "port_security": True, + "exit_on_floating_ip_error": False, + "floating_ip": True, + }, + ] + net_list_vim = [] + external_network, no_secured_ports = [], [] + expected_external_network, expected_no_secured_ports = [], [] + expected_net_list_vim = [] + + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + mock_create_port.assert_not_called() + + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_create_port") + def test_prepare_network_for_vm_instance_empty_net_list( + self, mock_create_port, mock_reload_connection + ): + """Net list is empty.""" + mock_reload_connection.side_effect = None + created_items = {} + net_list_vim = [] + external_network, no_secured_ports = [], [] + expected_external_network, expected_no_secured_ports = [], [] + expected_net_list_vim = [] + + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + mock_create_port.assert_not_called() + + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_create_port") + def test_prepare_network_for_vm_instance_use_floating_ip_false_mgmt_net( + self, mock_create_port, mock_reload_connection + ): + """Nets have net-id, floating_ip False, mgmt network.""" + mock_reload_connection.side_effect = None + created_items = {} + net_list = [ + { + "net_id": net2_id, + "floating_ip": False, + "use": "mgmt", + } + ] + net_list_vim = [] + mock_create_port.side_effect = [ + ( + { + "port": { + "id": port2_id, + "mac_address": mac_address, + "name": name, + }, + }, + {"port-dict": port2_id}, + ), + ] + external_network, no_secured_ports = [], [] + expected_external_network, expected_no_secured_ports = [], [] + expected_net_list_vim = [{"port-dict": port2_id}] + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "floating_ip": False, + "use": "mgmt", + }, + name, + created_items, + ) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_mgmt_net_net_port_security_and_floating_ip_true( + self, mock_reload_connection + ): + """Nets have net-id, use_floating_ip False in VIM config, mgmt network, net floating_ip is True.""" + self.vimconn.config["use_floating_ip"] = False + mock_create_port = CopyingMock() + mock_reload_connection.side_effect = None + created_items = {} + net_list = [ + { + "net_id": net2_id, + "floating_ip": True, + "use": "mgmt", + } + ] + net_list_vim = [] + mock_create_port.side_effect = [ + ( + { + "port": { + "id": port2_id, + "mac_address": mac_address, + "name": name, + }, + }, + {"port-dict": port2_id}, + ), + ] + external_network, no_secured_ports = [], [] + expected_external_network = [ + { + "net_id": net2_id, + "floating_ip": True, + "use": "mgmt", + "exit_on_floating_ip_error": True, + }, + ] + expected_no_secured_ports = [] + expected_net_list_vim = [{"port-dict": port2_id}] + with patch.object(vimconnector, "_create_port", mock_create_port): + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "floating_ip": True, + "use": "mgmt", + }, + name, + created_items, + ) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_use_floating_ip_true_mgmt_net_port_security_false( + self, mock_reload_connection + ): + """Nets have net-id, use_floating_ip is True in VIM config, mgmt network, net port security is False.""" + mock_create_port = CopyingMock() + self.vimconn.config["use_floating_ip"] = True + self.vimconn.config["no_port_security_extension"] = False + mock_reload_connection.side_effect = None + created_items = {} + + net_list = [ + { + "net_id": net2_id, + "use": "mgmt", + "port_security": False, + "exit_on_floating_ip_error": False, + "port_security_disable_strategy": "full", + } + ] + net_list_vim = [] + mock_create_port.side_effect = [ + ( + { + "port": { + "id": port2_id, + "mac_address": mac_address, + "name": name, + }, + }, + {"port-dict": port2_id}, + ), + ] + external_network, no_secured_ports = [], [] + expected_external_network = [ + { + "net_id": net2_id, + "use": "mgmt", + "port_security": False, + "exit_on_floating_ip_error": False, + "port_security_disable_strategy": "full", + "floating_ip": True, + }, + ] + expected_no_secured_ports = [(port2_id, "full")] + expected_net_list_vim = [{"port-dict": port2_id}] + with patch.object(vimconnector, "_create_port", mock_create_port): + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "use": "mgmt", + "port_security": False, + "exit_on_floating_ip_error": False, + "port_security_disable_strategy": "full", + }, + name, + created_items, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_use_fip_true_non_mgmt_net_port_security_false( + self, mock_reload_connection + ): + """Nets have net-id, use_floating_ip True in VIM config, non-mgmt network, port security is False.""" + mock_create_port = CopyingMock() + self.vimconn.config["use_floating_ip"] = True + self.vimconn.config["no_port_security_extension"] = False + mock_reload_connection.side_effect = None + created_items = {} + + net_list = [ + { + "net_id": net2_id, + "use": "other", + "port_security": False, + "port_security_disable_strategy": "full", + } + ] + net_list_vim = [] + mock_create_port.side_effect = [ + ( + { + "port": { + "id": port2_id, + "mac_address": mac_address, + "name": name, + }, + }, + {"port-dict": port2_id}, + ), + ] + external_network, no_secured_ports = [], [] + expected_external_network = [] + expected_no_secured_ports = [(port2_id, "full")] + expected_net_list_vim = [{"port-dict": port2_id}] + with patch.object(vimconnector, "_create_port", mock_create_port): + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "use": "other", + "port_security": False, + "port_security_disable_strategy": "full", + }, + name, + created_items, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_use_fip_true_non_mgmt_net_port_security_true( + self, mock_reload_connection + ): + """Nets have net-id, use_floating_ip is True in VIM config, non-mgmt network, net port security is True.""" + mock_create_port = CopyingMock() + self.vimconn.config["use_floating_ip"] = True + self.vimconn.config["no_port_security_extension"] = True + mock_reload_connection.side_effect = None + created_items = {} + + net_list = [ + { + "net_id": net2_id, + "use": "other", + "port_security": True, + "port_security_disable_strategy": "full", + } + ] + net_list_vim = [] + mock_create_port.side_effect = [ + ( + { + "port": { + "id": port2_id, + "mac_address": mac_address, + "name": name, + }, + }, + {"port-dict": port2_id}, + ), + ] + external_network, no_secured_ports = [], [] + expected_external_network = [] + expected_no_secured_ports = [] + expected_net_list_vim = [{"port-dict": port2_id}] + with patch.object(vimconnector, "_create_port", mock_create_port): + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "use": "other", + "port_security": True, + "port_security_disable_strategy": "full", + }, + name, + created_items, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_create_port_raise_exception( + self, mock_reload_connection + ): + """_create_port method raise exception.""" + mock_create_port = CopyingMock() + self.vimconn.config["use_floating_ip"] = True + self.vimconn.config["no_port_security_extension"] = True + mock_reload_connection.side_effect = None + created_items = {} + + net_list = [ + { + "net_id": net2_id, + "use": "other", + "port_security": True, + "port_security_disable_strategy": "full", + } + ] + net_list_vim = [] + mock_create_port.side_effect = KeyError + external_network, no_secured_ports = [], [] + expected_external_network = [] + expected_no_secured_ports = [] + expected_net_list_vim = [] + with patch.object(vimconnector, "_create_port", mock_create_port): + with self.assertRaises(Exception) as err: + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + + self.assertEqual(type(err.exception), KeyError) + + mock_create_port.assert_called_once_with( + { + "net_id": net2_id, + "use": "other", + "port_security": True, + "port_security_disable_strategy": "full", + }, + name, + created_items, + ) + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + @patch.object(vimconnector, "_reload_connection") + def test_prepare_network_for_vm_instance_reload_connection_raise_exception( + self, mock_reload_connection + ): + """_reload_connection method raises exception.""" + mock_create_port = CopyingMock() + mock_reload_connection.side_effect = VimConnConnectionException( + "Connection failed." + ) + self.vimconn.config["use_floating_ip"] = True + self.vimconn.config["no_port_security_extension"] = True + created_items = {} + + net_list = [ + { + "net_id": net2_id, + "use": "other", + "port_security": True, + "port_security_disable_strategy": "full", + } + ] + net_list_vim = [] + mock_create_port.side_effect = None + external_network, no_secured_ports = [], [] + expected_external_network = [] + expected_no_secured_ports = [] + expected_net_list_vim = [] + with patch.object(vimconnector, "_create_port", mock_create_port): + with self.assertRaises(Exception) as err: + self.vimconn._prepare_network_for_vminstance( + name, + net_list, + created_items, + net_list_vim, + external_network, + no_secured_ports, + ) + + self.assertEqual(type(err.exception), VimConnConnectionException) + self.assertEqual(str(err.exception), "Connection failed.") + mock_reload_connection.assert_called_once() + mock_create_port.assert_not_called() + self.assertEqual(expected_net_list_vim, net_list_vim) + self.assertEqual(external_network, expected_external_network) + self.assertEqual(expected_no_secured_ports, no_secured_ports) + + def test_prepare_persistent_root_volumes_vim_using_volume_id(self): + """Existing persistent root volume with vim_volume_id.""" + vm_av_zone = ["nova"] + base_disk_index = ord("a") + disk = {"vim_volume_id": volume_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + expected_boot_vol_id = None + expected_block_device_mapping = {"vda": volume_id} + expected_existing_vim_volumes = [{"id": volume_id}] + boot_volume_id = self.vimconn._prepare_persistent_root_volumes( + name, + vm_av_zone, + disk, + base_disk_index, + block_device_mapping, + existing_vim_volumes, + created_items, + ) + self.assertEqual(boot_volume_id, expected_boot_vol_id) + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_not_called() + + def test_prepare_persistent_non_root_volumes_vim_using_volume_id(self): + """Existing persistent non root volume with vim_volume_id.""" + vm_av_zone = ["nova"] + base_disk_index = ord("b") + disk = {"vim_volume_id": volume_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + expected_block_device_mapping = {"vdb": volume_id} + expected_existing_vim_volumes = [{"id": volume_id}] + self.vimconn._prepare_non_root_persistent_volumes( + name, + disk, + vm_av_zone, + block_device_mapping, + base_disk_index, + existing_vim_volumes, + created_items, + ) + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_not_called() + + def test_prepare_persistent_root_volumes_using_vim_id(self): + """Existing persistent root volume with vim_id.""" + vm_av_zone = ["nova"] + base_disk_index = ord("a") + disk = {"vim_id": volume_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + expected_boot_vol_id = None + expected_block_device_mapping = {"vda": volume_id} + expected_existing_vim_volumes = [{"id": volume_id}] + boot_volume_id = self.vimconn._prepare_persistent_root_volumes( + name, + vm_av_zone, + disk, + base_disk_index, + block_device_mapping, + existing_vim_volumes, + created_items, + ) + self.assertEqual(boot_volume_id, expected_boot_vol_id) + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_not_called() + + def test_prepare_persistent_non_root_volumes_using_vim_id(self): + """Existing persistent root volume with vim_id.""" + vm_av_zone = ["nova"] + base_disk_index = ord("b") + disk = {"vim_id": volume_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + + expected_block_device_mapping = {"vdb": volume_id} + expected_existing_vim_volumes = [{"id": volume_id}] + self.vimconn._prepare_non_root_persistent_volumes( + name, + disk, + vm_av_zone, + block_device_mapping, + base_disk_index, + existing_vim_volumes, + created_items, + ) + + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_not_called() + + def test_prepare_persistent_root_volumes_create(self): + """Create persistent root volume.""" + self.vimconn.cinder.volumes.create.return_value.id = volume_id2 + vm_av_zone = ["nova"] + base_disk_index = ord("a") + disk = {"size": 10, "image_id": image_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + expected_boot_vol_id = volume_id2 + expected_block_device_mapping = {"vda": volume_id2} + expected_existing_vim_volumes = [] + boot_volume_id = self.vimconn._prepare_persistent_root_volumes( + name, + vm_av_zone, + disk, + base_disk_index, + block_device_mapping, + existing_vim_volumes, + created_items, + ) + self.assertEqual(boot_volume_id, expected_boot_vol_id) + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_called_once_with( + size=10, + name="basicvmvda", + imageRef=image_id, + availability_zone=["nova"], + ) + self.assertEqual(created_items, {f"volume:{volume_id2}": True}) + + def test_prepare_persistent_non_root_volumes_create(self): + """Create persistent non-root volume.""" + self.vimconn.cinder = CopyingMock() + self.vimconn.cinder.volumes.create.return_value.id = volume_id2 + vm_av_zone = ["nova"] + base_disk_index = ord("a") + disk = {"size": 10} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + expected_block_device_mapping = {"vda": volume_id2} + expected_existing_vim_volumes = [] + self.vimconn._prepare_non_root_persistent_volumes( + name, + disk, + vm_av_zone, + block_device_mapping, + base_disk_index, + existing_vim_volumes, + created_items, + ) + + self.assertDictEqual(block_device_mapping, expected_block_device_mapping) + self.assertEqual(existing_vim_volumes, expected_existing_vim_volumes) + self.vimconn.cinder.volumes.create.assert_called_once_with( + size=10, name="basicvmvda", availability_zone=["nova"] + ) + self.assertEqual(created_items, {f"volume:{volume_id2}": True}) + + def test_prepare_persistent_root_volumes_create_raise_exception(self): + """Create persistent root volume raise exception.""" + self.vimconn.cinder.volumes.create.side_effect = Exception + vm_av_zone = ["nova"] + base_disk_index = ord("a") + disk = {"size": 10, "image_id": image_id} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + + with self.assertRaises(Exception): + result = self.vimconn._prepare_persistent_root_volumes( + name, + vm_av_zone, + disk, + base_disk_index, + block_device_mapping, + existing_vim_volumes, + created_items, + ) + + self.assertEqual(result, None) + + self.vimconn.cinder.volumes.create.assert_called_once_with( + size=10, + name="basicvmvda", + imageRef=image_id, + availability_zone=["nova"], + ) + self.assertEqual(existing_vim_volumes, []) + self.assertEqual(block_device_mapping, {}) + self.assertEqual(created_items, {}) + + def test_prepare_persistent_non_root_volumes_create_raise_exception(self): + """Create persistent non-root volume raise exception.""" + self.vimconn.cinder.volumes.create.side_effect = Exception + vm_av_zone = ["nova"] + base_disk_index = ord("b") + disk = {"size": 10} + block_device_mapping = {} + existing_vim_volumes = [] + created_items = {} + + with self.assertRaises(Exception): + self.vimconn._prepare_non_root_persistent_volumes( + name, + disk, + vm_av_zone, + block_device_mapping, + base_disk_index, + existing_vim_volumes, + created_items, + ) + + self.vimconn.cinder.volumes.create.assert_called_once_with( + size=10, name="basicvmvdb", availability_zone=["nova"] + ) + self.assertEqual(existing_vim_volumes, []) + self.assertEqual(block_device_mapping, {}) + self.assertEqual(created_items, {}) + + @patch("time.sleep") + def test_wait_for_created_volumes_availability_volume_status_available( + self, mock_sleep + ): + """Created volume status is available.""" + elapsed_time = 5 + created_items = {f"volume:{volume_id2}": True} + self.vimconn.cinder.volumes.get.return_value.status = "available" + + result = self.vimconn._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + self.assertEqual(result, elapsed_time) + self.vimconn.cinder.volumes.get.assert_called_with(volume_id2) + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_existing_volumes_availability_volume_status_available( + self, mock_sleep + ): + """Existing volume status is available.""" + elapsed_time = 5 + existing_vim_volumes = [{"id": volume_id2}] + self.vimconn.cinder.volumes.get.return_value.status = "available" + + result = self.vimconn._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + self.assertEqual(result, elapsed_time) + self.vimconn.cinder.volumes.get.assert_called_with(volume_id2) + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_created_volumes_availability_status_processing_multiple_volumes( + self, mock_sleep + ): + """Created volume status is processing.""" + elapsed_time = 5 + created_items = { + f"volume:{volume_id2}": True, + f"volume:{volume_id3}": True, + } + self.vimconn.cinder.volumes.get.side_effect = [ + Status("processing"), + Status("available"), + Status("available"), + ] + + result = self.vimconn._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + self.assertEqual(result, 10) + _call_mock_get_volumes = self.vimconn.cinder.volumes.get.call_args_list + self.assertEqual(_call_mock_get_volumes[0][0], (volume_id2,)) + self.assertEqual(_call_mock_get_volumes[1][0], (volume_id2,)) + self.assertEqual(_call_mock_get_volumes[2][0], (volume_id3,)) + mock_sleep.assert_called_with(5) + self.assertEqual(1, mock_sleep.call_count) + + @patch("time.sleep") + def test_wait_for_existing_volumes_availability_status_processing_multiple_volumes( + self, mock_sleep + ): + """Existing volume status is processing.""" + elapsed_time = 5 + existing_vim_volumes = [ + {"id": volume_id2}, + {"id": "44e0e83-b9uu-4akk-t234-p9cc4811bd4a"}, + ] + self.vimconn.cinder.volumes.get.side_effect = [ + Status("processing"), + Status("available"), + Status("available"), + ] + + result = self.vimconn._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + self.assertEqual(result, 10) + _call_mock_get_volumes = self.vimconn.cinder.volumes.get.call_args_list + self.assertEqual(_call_mock_get_volumes[0][0], (volume_id2,)) + self.assertEqual(_call_mock_get_volumes[1][0], (volume_id2,)) + self.assertEqual( + _call_mock_get_volumes[2][0], ("44e0e83-b9uu-4akk-t234-p9cc4811bd4a",) + ) + mock_sleep.assert_called_with(5) + self.assertEqual(1, mock_sleep.call_count) + + @patch("time.sleep") + def test_wait_for_created_volumes_availability_volume_status_processing_timeout( + self, mock_sleep + ): + """Created volume status is processing, elapsed time greater than timeout (1800).""" + elapsed_time = 1805 + created_items = {f"volume:{volume_id2}": True} + self.vimconn.cinder.volumes.get.side_effect = [ + Status("processing"), + Status("processing"), + ] + with patch("time.sleep", mock_sleep): + result = self.vimconn._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + self.assertEqual(result, 1805) + self.vimconn.cinder.volumes.get.assert_not_called() + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_existing_volumes_availability_volume_status_processing_timeout( + self, mock_sleep + ): + """Exsiting volume status is processing, elapsed time greater than timeout (1800).""" + elapsed_time = 1805 + existing_vim_volumes = [{"id": volume_id2}] + self.vimconn.cinder.volumes.get.side_effect = [ + Status("processing"), + Status("processing"), + ] + + result = self.vimconn._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + self.assertEqual(result, 1805) + self.vimconn.cinder.volumes.get.assert_not_called() + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_created_volumes_availability_cinder_raise_exception( + self, mock_sleep + ): + """Cinder get volumes raises exception for created volumes.""" + elapsed_time = 1000 + created_items = {f"volume:{volume_id2}": True} + self.vimconn.cinder.volumes.get.side_effect = Exception + with self.assertRaises(Exception): + result = self.vimconn._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + self.assertEqual(result, 1000) + self.vimconn.cinder.volumes.get.assert_called_with(volume_id2) + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_existing_volumes_availability_cinder_raise_exception( + self, mock_sleep + ): + """Cinder get volumes raises exception for existing volumes.""" + elapsed_time = 1000 + existing_vim_volumes = [{"id": volume_id2}] + self.vimconn.cinder.volumes.get.side_effect = Exception + with self.assertRaises(Exception): + result = self.vimconn._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + self.assertEqual(result, 1000) + self.vimconn.cinder.volumes.get.assert_called_with(volume_id2) + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_created_volumes_availability_no_volume_in_created_items( + self, mock_sleep + ): + """Created_items dict does not have volume-id.""" + elapsed_time = 10 + created_items = {} + + self.vimconn.cinder.volumes.get.side_effect = [None] + + result = self.vimconn._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + self.assertEqual(result, 10) + self.vimconn.cinder.volumes.get.assert_not_called() + mock_sleep.assert_not_called() + + @patch("time.sleep") + def test_wait_for_existing_volumes_availability_no_volume_in_existing_vim_volumes( + self, mock_sleep + ): + """Existing_vim_volumes list does not have volume.""" + elapsed_time = 10 + existing_vim_volumes = [] + + self.vimconn.cinder.volumes.get.side_effect = [None] + + result = self.vimconn._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + self.assertEqual(result, 10) + self.vimconn.cinder.volumes.get.assert_not_called() + mock_sleep.assert_not_called() + + @patch.object(vimconnector, "_prepare_persistent_root_volumes") + @patch.object(vimconnector, "_prepare_non_root_persistent_volumes") + @patch.object(vimconnector, "_wait_for_created_volumes_availability") + @patch.object(vimconnector, "_wait_for_existing_volumes_availability") + def test_prepare_disk_for_vm_instance( + self, + mock_existing_vol_availability, + mock_created_vol_availability, + mock_non_root_volumes, + mock_root_volumes, + ): + """Prepare disks for VM instance successfully.""" + existing_vim_volumes = [] + created_items = {} + vm_av_zone = ["nova"] + + mock_root_volumes.return_value = root_vol_id + mock_created_vol_availability.return_value = 10 + mock_existing_vol_availability.return_value = 15 + self.vimconn.cinder = CopyingMock() + + self.vimconn._prepare_disk_for_vminstance( + name, existing_vim_volumes, created_items, vm_av_zone, disk_list2 + ) + self.vimconn.cinder.volumes.set_bootable.assert_called_once_with( + root_vol_id, True + ) + mock_created_vol_availability.assert_called_once_with(0, created_items) + mock_existing_vol_availability.assert_called_once_with(10, existing_vim_volumes) + self.assertEqual(mock_root_volumes.call_count, 1) + self.assertEqual(mock_non_root_volumes.call_count, 1) + mock_root_volumes.assert_called_once_with( + name="basicvm", + vm_av_zone=["nova"], + disk={"size": 10, "image_id": image_id}, + base_disk_index=97, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + mock_non_root_volumes.assert_called_once_with( + name="basicvm", + disk={"size": 20}, + vm_av_zone=["nova"], + base_disk_index=98, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + + @patch.object(vimconnector, "_prepare_persistent_root_volumes") + @patch.object(vimconnector, "_prepare_non_root_persistent_volumes") + @patch.object(vimconnector, "_wait_for_created_volumes_availability") + @patch.object(vimconnector, "_wait_for_existing_volumes_availability") + def test_prepare_disk_for_vm_instance_timeout_exceeded( + self, + mock_existing_vol_availability, + mock_created_vol_availability, + mock_non_root_volumes, + mock_root_volumes, + ): + """Timeout exceeded while waiting for disks.""" + existing_vim_volumes = [] + created_items = {} + vm_av_zone = ["nova"] + + mock_root_volumes.return_value = root_vol_id + mock_created_vol_availability.return_value = 1700 + mock_existing_vol_availability.return_value = 1900 + + with self.assertRaises(VimConnException) as err: + self.vimconn._prepare_disk_for_vminstance( + name, existing_vim_volumes, created_items, vm_av_zone, disk_list2 + ) + self.assertEqual( + str(err.exception), "Timeout creating volumes for instance basicvm" + ) + self.vimconn.cinder.volumes.set_bootable.assert_not_called() + mock_created_vol_availability.assert_called_once_with(0, created_items) + mock_existing_vol_availability.assert_called_once_with( + 1700, existing_vim_volumes + ) + self.assertEqual(mock_root_volumes.call_count, 1) + self.assertEqual(mock_non_root_volumes.call_count, 1) + mock_root_volumes.assert_called_once_with( + name="basicvm", + vm_av_zone=["nova"], + disk={"size": 10, "image_id": image_id}, + base_disk_index=97, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + mock_non_root_volumes.assert_called_once_with( + name="basicvm", + disk={"size": 20}, + vm_av_zone=["nova"], + base_disk_index=98, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + + @patch.object(vimconnector, "_prepare_persistent_root_volumes") + @patch.object(vimconnector, "_prepare_non_root_persistent_volumes") + @patch.object(vimconnector, "_wait_for_created_volumes_availability") + @patch.object(vimconnector, "_wait_for_existing_volumes_availability") + def test_prepare_disk_for_vm_instance_empty_disk_list( + self, + mock_existing_vol_availability, + mock_created_vol_availability, + mock_non_root_volumes, + mock_root_volumes, + ): + """Disk list is empty.""" + existing_vim_volumes = [] + created_items = {} + vm_av_zone = ["nova"] + mock_created_vol_availability.return_value = 2 + mock_existing_vol_availability.return_value = 3 + + self.vimconn._prepare_disk_for_vminstance( + name, existing_vim_volumes, created_items, vm_av_zone, disk_list + ) + self.vimconn.cinder.volumes.set_bootable.assert_not_called() + mock_created_vol_availability.assert_called_once_with(0, created_items) + mock_existing_vol_availability.assert_called_once_with(2, existing_vim_volumes) + mock_root_volumes.assert_not_called() + mock_non_root_volumes.assert_not_called() + + @patch.object(vimconnector, "_prepare_persistent_root_volumes") + @patch.object(vimconnector, "_prepare_non_root_persistent_volumes") + @patch.object(vimconnector, "_wait_for_created_volumes_availability") + @patch.object(vimconnector, "_wait_for_existing_volumes_availability") + def test_prepare_disk_for_vm_instance_persistent_root_volume_error( + self, + mock_existing_vol_availability, + mock_created_vol_availability, + mock_non_root_volumes, + mock_root_volumes, + ): + """Persistent root volumes preparation raises error.""" + existing_vim_volumes = [] + created_items = {} + vm_av_zone = ["nova"] + + mock_root_volumes.side_effect = Exception() + mock_created_vol_availability.return_value = 10 + mock_existing_vol_availability.return_value = 15 + + with self.assertRaises(Exception): + self.vimconn._prepare_disk_for_vminstance( + name, existing_vim_volumes, created_items, vm_av_zone, disk_list2 + ) + self.vimconn.cinder.volumes.set_bootable.assert_not_called() + mock_created_vol_availability.assert_not_called() + mock_existing_vol_availability.assert_not_called() + mock_root_volumes.assert_called_once_with( + name="basicvm", + vm_av_zone=["nova"], + disk={"size": 10, "image_id": image_id}, + base_disk_index=97, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + mock_non_root_volumes.assert_not_called() + + @patch.object(vimconnector, "_prepare_persistent_root_volumes") + @patch.object(vimconnector, "_prepare_non_root_persistent_volumes") + @patch.object(vimconnector, "_wait_for_created_volumes_availability") + @patch.object(vimconnector, "_wait_for_existing_volumes_availability") + def test_prepare_disk_for_vm_instance_non_root_volume_error( + self, + mock_existing_vol_availability, + mock_created_vol_availability, + mock_non_root_volumes, + mock_root_volumes, + ): + """Non-root volumes preparation raises error.""" + existing_vim_volumes = [] + created_items = {} + vm_av_zone = ["nova"] + + mock_root_volumes.return_value = root_vol_id + mock_non_root_volumes.side_effect = Exception + + with self.assertRaises(Exception): + self.vimconn._prepare_disk_for_vminstance( + name, existing_vim_volumes, created_items, vm_av_zone, disk_list2 + ) + self.vimconn.cinder.volumes.set_bootable.assert_not_called() + mock_created_vol_availability.assert_not_called() + mock_existing_vol_availability.assert_not_called() + self.assertEqual(mock_root_volumes.call_count, 1) + self.assertEqual(mock_non_root_volumes.call_count, 1) + mock_root_volumes.assert_called_once_with( + name="basicvm", + vm_av_zone=["nova"], + disk={"size": 10, "image_id": image_id}, + base_disk_index=97, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + mock_non_root_volumes.assert_called_once_with( + name="basicvm", + disk={"size": 20}, + vm_av_zone=["nova"], + base_disk_index=98, + block_device_mapping={}, + existing_vim_volumes=[], + created_items={}, + ) + + def test_find_external_network_for_floating_ip_no_external_network(self): + """External network could not be found.""" + self.vimconn.neutron.list_networks.return_value = { + "networks": [ + {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": False} + ] + } + with self.assertRaises(VimConnException) as err: + self.vimconn._find_the_external_network_for_floating_ip() + self.assertEqual( + str(err.exception), + "Cannot create floating_ip automatically since no external network is present", + ) + + def test_find_external_network_for_floating_one_external_network(self): + """One external network has been found.""" + self.vimconn.neutron.list_networks.return_value = { + "networks": [ + {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True} + ] + } + expected_result = "408b73-r9cc-5a6a-a270-82cc4811bd4a" + result = self.vimconn._find_the_external_network_for_floating_ip() + self.assertEqual(result, expected_result) + + def test_find_external_network_for_floating_neutron_raises_exception(self): + """Neutron list networks raises exception.""" + self.vimconn.neutron.list_networks.side_effect = Exception + with self.assertRaises(Exception): + self.vimconn._find_the_external_network_for_floating_ip() + + def test_find_external_network_for_floating_several_external_network(self): + """Several exernal networks has been found.""" + self.vimconn.neutron.list_networks.return_value = { + "networks": [ + {"id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", "router:external": True}, + {"id": "608b73-y9cc-5a6a-a270-12cc4811bd4a", "router:external": True}, + ] + } + with self.assertRaises(VimConnException) as err: + self.vimconn._find_the_external_network_for_floating_ip() + self.assertEqual( + str(err.exception), + "Cannot create floating_ip automatically since multiple external networks are present", + ) + + def test_neutron_create_float_ip(self): + """Floating ip creation is successful.""" + param = {"net_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a"} + created_items = {} + self.vimconn.neutron.create_floatingip.return_value = { + "floatingip": {"id": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + } + expected_created_items = { + "floating_ip:308b73-t9cc-1a6a-a270-12cc4811bd4a": True + } + self.vimconn._neutron_create_float_ip(param, created_items) + self.assertEqual(created_items, expected_created_items) + + def test_neutron_create_float_ip_exception_occured(self): + """Floating ip could not be created.""" + param = { + "floatingip": { + "floating_network_id": "408b73-r9cc-5a6a-a270-p2cc4811bd9a", + "tenant_id": "308b73-19cc-8a6a-a270-02cc4811bd9a", + } + } + created_items = {} + self.vimconn.neutron = CopyingMock() + self.vimconn.neutron.create_floatingip.side_effect = Exception( + "Neutron floating ip create exception occured." + ) + with self.assertRaises(VimConnException) as err: + self.vimconn._neutron_create_float_ip(param, created_items) + self.assertEqual(created_items, {}) + self.assertEqual( + str(err.exception), + "Exception: Cannot create new floating_ip Neutron floating ip create exception occured.", + ) + + @patch.object(vimconnector, "_neutron_create_float_ip") + @patch.object(vimconnector, "_find_the_external_network_for_floating_ip") + def test_create_floating_ip_pool_id_available( + self, mock_find_ext_network, mock_create_float_ip + ): + """Floating ip creation, ip pool is available.""" + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + created_items = {} + expected_param = { + "floatingip": { + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", + } + } + self.vimconn._create_floating_ip(floating_network, self.server, created_items) + mock_find_ext_network.assert_not_called() + mock_create_float_ip.assert_called_once_with(expected_param, {}) + + @patch.object(vimconnector, "_neutron_create_float_ip") + @patch.object(vimconnector, "_find_the_external_network_for_floating_ip") + def test_create_floating_ip_finding_pool_id( + self, mock_find_ext_network, mock_create_float_ip + ): + """Floating ip creation, pool id need to be found.""" + floating_network = {"floating_ip": True} + created_items = {} + mock_find_ext_network.return_value = "308b73-t9cc-1a6a-a270-12cc4811bd4a" + expected_param = { + "floatingip": { + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", + } + } + self.vimconn._create_floating_ip(floating_network, self.server, created_items) + mock_find_ext_network.assert_called_once() + mock_create_float_ip.assert_called_once_with(expected_param, {}) + + @patch.object(vimconnector, "_neutron_create_float_ip") + @patch.object(vimconnector, "_find_the_external_network_for_floating_ip") + def test_create_floating_ip_neutron_create_floating_ip_exception( + self, mock_find_ext_network, mock_create_float_ip + ): + """Neutron creat floating ip raises error.""" + floating_network = {"floating_ip": True} + created_items = {} + mock_create_float_ip.side_effect = VimConnException( + "Can not create floating ip." + ) + mock_find_ext_network.return_value = "308b73-t9cc-1a6a-a270-12cc4811bd4a" + expected_param = { + "floatingip": { + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", + } + } + + with self.assertRaises(VimConnException) as err: + self.vimconn._create_floating_ip( + floating_network, self.server, created_items + ) + self.assertEqual(str(err.exception), "Can not create floating ip.") + mock_find_ext_network.assert_called_once() + mock_create_float_ip.assert_called_once_with(expected_param, {}) + + @patch.object(vimconnector, "_neutron_create_float_ip") + @patch.object(vimconnector, "_find_the_external_network_for_floating_ip") + def test_create_floating_ip_can_not_find_pool_id( + self, mock_find_ext_network, mock_create_float_ip + ): + """Floating ip creation, pool id could not be found.""" + floating_network = {"floating_ip": True} + created_items = {} + mock_find_ext_network.side_effect = VimConnException( + "Cannot create floating_ip automatically since no external network is present" + ) + with self.assertRaises(VimConnException) as err: + self.vimconn._create_floating_ip( + floating_network, self.server, created_items + ) + self.assertEqual( + str(err.exception), + "Cannot create floating_ip automatically since no external network is present", + ) + mock_find_ext_network.assert_called_once() + mock_create_float_ip.assert_not_called() + + def test_find_floating_ip_get_free_floating_ip(self): + """Get free floating ips successfully.""" + floating_ips = [ + { + "tenant_id": "408b73-r9cc-5a6a-a270-82cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + } + ] + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + expected_result = "508b73-o9cc-5a6a-a270-72cc4811bd8" + + result = self.vimconn._find_floating_ip( + self.server, floating_ips, floating_network + ) + self.assertEqual(result, expected_result) + + def test_find_floating_ip_different_floating_network_id(self): + """Floating network id is different with floating_ip of floating network.""" + floating_ips = [ + { + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + } + ] + floating_network = {"floating_ip": "508b73-t9cc-1a6a-a270-12cc4811bd4a"} + + result = self.vimconn._find_floating_ip( + self.server, floating_ips, floating_network + ) + self.assertEqual(result, None) + + def test_find_floating_ip_different_fip_tenant(self): + """Items in floating_ips has port_id, tenant_is is not same with server tenant id.""" + floating_ips = [ + { + "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + "tenant_id": self.server.id, + } + ] + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + mock_create_floating_ip = CopyingMock() + with patch.object(vimconnector, "_create_floating_ip", mock_create_floating_ip): + result = self.vimconn._find_floating_ip( + self.server, floating_ips, floating_network + ) + self.assertEqual(result, None) + + @patch("time.sleep") + def test_assign_floating_ip(self, mock_sleep): + """Assign floating ip successfully.""" + free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8" + floating_network = {"vim_id": floating_network_vim_id} + fip = { + "port_id": floating_network_vim_id, + "floating_network_id": "p08b73-e9cc-5a6a-t270-82cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + "tenant_id": "k08b73-e9cc-5a6a-t270-82cc4811bd4a", + } + self.vimconn.neutron.update_floatingip.side_effect = None + self.vimconn.neutron.show_floatingip.return_value = fip + expected_result = fip + + result = self.vimconn._assign_floating_ip(free_floating_ip, floating_network) + self.assertEqual(result, expected_result) + self.vimconn.neutron.update_floatingip.assert_called_once_with( + free_floating_ip, + {"floatingip": {"port_id": floating_network_vim_id}}, + ) + mock_sleep.assert_called_once_with(5) + self.vimconn.neutron.show_floatingip.assert_called_once_with(free_floating_ip) + + @patch("time.sleep") + def test_assign_floating_ip_update_floating_ip_exception(self, mock_sleep): + """Neutron update floating ip raises exception.""" + free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8" + floating_network = {"vim_id": floating_network_vim_id} + self.vimconn.neutron = CopyingMock() + self.vimconn.neutron.update_floatingip.side_effect = Exception( + "Floating ip is not updated." + ) + + with self.assertRaises(Exception) as err: + result = self.vimconn._assign_floating_ip( + free_floating_ip, floating_network + ) + self.assertEqual(result, None) + self.assertEqual(str(err.exception), "Floating ip is not updated.") + + self.vimconn.neutron.update_floatingip.assert_called_once_with( + free_floating_ip, + {"floatingip": {"port_id": floating_network_vim_id}}, + ) + mock_sleep.assert_not_called() + self.vimconn.neutron.show_floatingip.assert_not_called() + + @patch("time.sleep") + def test_assign_floating_ip_show_floating_ip_exception(self, mock_sleep): + """Neutron show floating ip raises exception.""" + free_floating_ip = "508b73-o9cc-5a6a-a270-72cc4811bd8" + floating_network = {"vim_id": floating_network_vim_id} + self.vimconn.neutron.update_floatingip.side_effect = None + self.vimconn.neutron.show_floatingip.side_effect = Exception( + "Floating ip could not be shown." + ) + + with self.assertRaises(Exception) as err: + result = self.vimconn._assign_floating_ip( + free_floating_ip, floating_network + ) + self.assertEqual(result, None) + self.assertEqual(str(err.exception), "Floating ip could not be shown.") + self.vimconn.neutron.update_floatingip.assert_called_once_with( + free_floating_ip, + {"floatingip": {"port_id": floating_network_vim_id}}, + ) + mock_sleep.assert_called_once_with(5) + self.vimconn.neutron.show_floatingip.assert_called_once_with(free_floating_ip) + + @patch("random.shuffle") + @patch.object(vimconnector, "_find_floating_ip") + def test_get_free_floating_ip(self, mock_find_floating_ip, mock_shuffle): + """Get free floating ip successfully.""" + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + created_items = {} + floating_ips = [ + { + "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a", + }, + { + "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "208b73-o9cc-5a6a-a270-52cc4811bd8", + "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a", + }, + ] + self.vimconn.neutron.list_floatingips.return_value = { + "floatingips": floating_ips + } + mock_find_floating_ip.return_value = "508b73-o9cc-5a6a-a270-72cc4811bd8" + expected_result = "508b73-o9cc-5a6a-a270-72cc4811bd8" + + result = self.vimconn._get_free_floating_ip( + self.server, floating_network, created_items + ) + self.assertEqual(result, expected_result) + mock_shuffle.assert_called_once_with(floating_ips) + mock_find_floating_ip.assert_called_once_with( + self.server, floating_ips, floating_network, created_items + ) + + @patch("random.shuffle") + @patch.object(vimconnector, "_find_floating_ip") + def test_get_free_floating_ip_list_floating_ip_exception( + self, mock_find_floating_ip, mock_shuffle + ): + """Neutron list floating IPs raises exception.""" + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + created_items = {} + self.vimconn.neutron = CopyingMock() + self.vimconn.neutron.list_floatingips.side_effect = Exception( + "Floating ips could not be listed." + ) + with self.assertRaises(Exception) as err: + result = self.vimconn._get_free_floating_ip( + self.server, floating_network, created_items + ) + self.assertEqual(result, None) + self.assertEqual(str(err.exception), "Floating ips could not be listed.") + mock_shuffle.assert_not_called() + mock_find_floating_ip.assert_not_called() + + @patch("random.shuffle") + @patch.object(vimconnector, "_find_floating_ip") + def test_get_free_floating_ip_find_floating_ip_exception( + self, mock_find_floating_ip, mock_shuffle + ): + """_find_floating_ip method raises exception.""" + floating_network = {"floating_ip": "308b73-t9cc-1a6a-a270-12cc4811bd4a"} + created_items = {} + floating_ips = [ + { + "port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "508b73-o9cc-5a6a-a270-72cc4811bd8", + "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a", + }, + { + "port_id": "508b73-r9cc-5a6a-5270-o2cc4811bd4a", + "floating_network_id": "308b73-t9cc-1a6a-a270-12cc4811bd4a", + "id": "208b73-o9cc-5a6a-a270-52cc4811bd8", + "tenant_id": "208b73-e9cc-5a6a-t270-82cc4811bd4a", + }, + ] + self.vimconn.neutron = CopyingMock() + self.vimconn.neutron.list_floatingips.return_value = { + "floatingips": floating_ips + } + mock_find_floating_ip.side_effect = Exception( + "Free floating ip could not be found." + ) + + with self.assertRaises(Exception) as err: + result = self.vimconn._get_free_floating_ip( + self.server, floating_network, created_items + ) + self.assertEqual(result, None) + self.assertEqual(str(err.exception), "Free floating ip could not be found.") + mock_shuffle.assert_called_once_with(floating_ips) + mock_find_floating_ip.assert_called_once_with( + self.server, floating_ips, floating_network, created_items + ) + + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + ): + """Prepare external network successfully.""" + external_network = [ + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + }, + ] + created_items = {} + vm_start_time = time_return_value + mock_get_free_floating_ip.side_effect = ["y08b73-o9cc-1a6a-a270-12cc4811bd4u"] + mock_assign_floating_ip.return_value = { + "floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"} + } + self.vimconn.neutron = CopyingMock() + self.vimconn.nova = CopyingMock() + self.vimconn.neutron.show_floatingip.return_value = { + "floatingip": {"port_id": ""} + } + + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + + self.assertEqual(mock_get_free_floating_ip.call_count, 1) + mock_get_free_floating_ip.assert_called_once_with( + self.server, + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + }, + created_items, + ) + self.vimconn.neutron.show_floatingip.assert_called_once_with( + "y08b73-o9cc-1a6a-a270-12cc4811bd4u" + ) + self.vimconn.nova.servers.get.assert_not_called() + mock_create_floating_ip.assert_not_called() + mock_assign_floating_ip.assert_called_once_with( + "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + }, + ) + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_no_free_floating_ip( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """There is not any free floating ip.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + } + external_network = [floating_network] + + created_items = {} + vm_start_time = time_return_value + mock_get_free_floating_ip.return_value = None + mock_assign_floating_ip.return_value = {} + self.vimconn.nova.servers.get.return_value.status = "ERROR" + self.vimconn.neutron.show_floatingip.return_value = {} + + with self.assertRaises(KeyError): + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + + self.assertEqual(mock_get_free_floating_ip.call_count, 4) + mock_get_free_floating_ip.assert_called_with( + self.server, + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + }, + created_items, + ) + self.vimconn.neutron.show_floatingip.assert_called_with(None) + mock_sleep.assert_not_called() + mock_time.assert_not_called() + self.assertEqual(self.vimconn.nova.servers.get.call_count, 4) + mock_create_floating_ip.assert_called_with( + floating_network, self.server, created_items + ) + self.assertEqual(mock_create_floating_ip.call_count, 4) + mock_assign_floating_ip.assert_not_called() + self.vimconn.nova.servers.get.assert_called_with(self.server.id) + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_false( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """There is not any free floating ip, create_floating ip method raise exception + exit_on_floating_ip_error set to False.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": False, + } + external_network = [floating_network] + + created_items = {} + vm_start_time = time_return_value + mock_get_free_floating_ip.return_value = None + mock_assign_floating_ip.return_value = {} + mock_create_floating_ip.side_effect = VimConnException( + "Can not create floating ip." + ) + self.vimconn.nova.servers.get.return_value.status = "ERROR" + self.vimconn.neutron.show_floatingip.return_value = {} + + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual(mock_get_free_floating_ip.call_count, 1) + mock_get_free_floating_ip.assert_called_with( + self.server, + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": False, + }, + created_items, + ) + self.vimconn.neutron.show_floatingip.assert_not_called() + mock_sleep.assert_not_called() + mock_time.assert_not_called() + self.vimconn.nova.servers.get.assert_not_called() + mock_create_floating_ip.assert_called_with( + floating_network, self.server, created_items + ) + self.assertEqual(mock_create_floating_ip.call_count, 1) + mock_assign_floating_ip.assert_not_called() + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_no_free_fip_can_not_create_fip_exit_on_error_true( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """There is not any free floating ip, create_floating ip method raise exception + exit_on_floating_ip_error set to False.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": True, + } + external_network = [floating_network] + + created_items = {} + vm_start_time = time_return_value + mock_get_free_floating_ip.return_value = None + mock_assign_floating_ip.return_value = {} + mock_create_floating_ip.side_effect = VimConnException( + "Can not create floating ip." + ) + self.vimconn.nova.servers.get.return_value.status = "ERROR" + self.vimconn.neutron.show_floatingip.return_value = {} + with self.assertRaises(VimConnException): + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual(mock_get_free_floating_ip.call_count, 1) + mock_get_free_floating_ip.assert_called_with( + self.server, + { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": True, + }, + created_items, + ) + self.vimconn.neutron.show_floatingip.assert_not_called() + mock_sleep.assert_not_called() + mock_time.assert_not_called() + self.vimconn.nova.servers.get.assert_not_called() + mock_create_floating_ip.assert_called_with( + floating_network, self.server, created_items + ) + self.assertEqual(mock_create_floating_ip.call_count, 1) + mock_assign_floating_ip.assert_not_called() + + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_fip_has_port_id( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + ): + """Neutron show floating ip return the fip with port_id and floating network vim_id + is different from port_id.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + } + external_network = [floating_network] + created_items = {} + vm_start_time = 150 + mock_get_free_floating_ip.side_effect = [ + "t08b73-o9cc-1a6a-a270-12cc4811bd4u", + "r08b73-o9cc-1a6a-a270-12cc4811bd4u", + "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + ] + mock_assign_floating_ip.side_effect = [ + {"floatingip": {"port_id": "k08b73-r9cc-5a6a-a270-82cc4811bd4a"}}, + {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}}, + ] + self.vimconn.neutron = CopyingMock() + self.vimconn.nova = CopyingMock() + self.vimconn.neutron.show_floatingip.side_effect = [ + {"floatingip": {"port_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a"}}, + {"floatingip": {"port_id": ""}}, + {"floatingip": {"port_id": ""}}, + ] + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual(mock_get_free_floating_ip.call_count, 3) + _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list + self.assertEqual( + _call_mock_get_free_floating_ip[0][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[1][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[2][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 3) + self.vimconn.nova.servers.get.assert_not_called() + mock_create_floating_ip.assert_not_called() + self.assertEqual(mock_assign_floating_ip.call_count, 2) + _call_mock_assign_floating_ip = mock_assign_floating_ip.call_args_list + self.assertEqual( + _call_mock_assign_floating_ip[0][0], + ("r08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network), + ) + self.assertEqual( + _call_mock_assign_floating_ip[1][0], + ("y08b73-o9cc-1a6a-a270-12cc4811bd4u", floating_network), + ) + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_error( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """Neutron show floating ip gives exception, exit_on_floating_ip_error set to True, + VM status is in error.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": True, + } + external_network = [floating_network] + created_items = {} + vm_start_time = time_return_value + + mock_time.side_effect = [156570150, 156570800, 156571200] + + self.vimconn.nova.servers.get.return_value.status = "ERROR" + self.vimconn.neutron.show_floatingip.side_effect = [ + Exception("Floating ip could not be shown.") + ] * 4 + with self.assertRaises(Exception) as err: + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual( + str(err.exception), + "Cannot create floating_ip: Exception Floating ip could not be shown.", + ) + + self.assertEqual(mock_get_free_floating_ip.call_count, 4) + _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list + self.assertEqual( + _call_mock_get_free_floating_ip[0][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[1][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[2][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[3][0], + ( + self.server, + floating_network, + created_items, + ), + ) + + self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4) + self.vimconn.nova.servers.get.assert_called_with(self.server.id) + mock_create_floating_ip.assert_not_called() + mock_assign_floating_ip.assert_not_called() + mock_time.assert_not_called() + mock_sleep.assert_not_called() + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_vm_status_in_active( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """Neutron show floating ip gives exception, exit_on_floating_ip_error is set to False, + VM status is in active.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": False, + } + external_network = [floating_network] + created_items = {} + vm_start_time = time_return_value + + mock_time.side_effect = [156570150, 156570800, 156571200] + + self.vimconn.nova.servers.get.return_value.status = "ACTIVE" + self.vimconn.neutron.show_floatingip.side_effect = [ + Exception("Floating ip could not be shown.") + ] * 4 + + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + # self.assertEqual(str(err.exception), "Cannot create floating_ip") + + self.assertEqual(mock_get_free_floating_ip.call_count, 4) + _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list + self.assertEqual( + _call_mock_get_free_floating_ip[0][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[1][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[2][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[3][0], + ( + self.server, + floating_network, + created_items, + ), + ) + + self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4) + self.vimconn.nova.servers.get.assert_called_with(self.server.id) + mock_create_floating_ip.assert_not_called() + mock_assign_floating_ip.assert_not_called() + mock_time.assert_not_called() + mock_sleep.assert_not_called() + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_neutron_show_fip_exception_exit_on_error( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """Neutron show floating ip gives exception, but exit_on_floating_ip_error is set to True. + VM status is not ACTIVE or ERROR, server timeout happened.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": True, + } + external_network = [floating_network] + created_items = {} + vm_start_time = time_return_value + mock_get_free_floating_ip.side_effect = None + mock_time.side_effect = [156571790, 156571795, 156571800, 156571805] + self.vimconn.nova.servers.get.return_value.status = "OTHER" + self.vimconn.neutron.show_floatingip.side_effect = [ + Exception("Floating ip could not be shown.") + ] * 5 + + with self.assertRaises(VimConnException) as err: + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual( + str(err.exception), + "Cannot create floating_ip: Exception Floating ip could not be shown.", + ) + + self.assertEqual(mock_get_free_floating_ip.call_count, 3) + _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list + self.assertEqual( + _call_mock_get_free_floating_ip[0][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[1][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[2][0], + ( + self.server, + floating_network, + created_items, + ), + ) + + self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 3) + self.vimconn.nova.servers.get.assert_called_with(self.server.id) + mock_create_floating_ip.assert_not_called() + mock_assign_floating_ip.assert_not_called() + self.assertEqual(mock_time.call_count, 3) + self.assertEqual(mock_sleep.call_count, 2) + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_assign_floating_ip_exception_exit_on_error( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """Assign floating ip method gives exception, exit_on_floating_ip_error is set to True. + VM status is in ERROR.""" + floating_network = { + "floating_ip": "y08b73-o9cc-1a6a-a270-12cc4811bd4u", + "vim_id": "608b73-r9cc-5a6a-a270-82cc4811bd4a", + "exit_on_floating_ip_error": True, + } + external_network = [floating_network] + created_items = {} + vm_start_time = time_return_value + + mock_get_free_floating_ip.side_effect = [ + "y08b73-o9cc-1a6a-a270-12cc4811bd4u" + ] * 4 + + mock_time.side_effect = [156571790, 156571795, 156571800, 156571805] + + mock_assign_floating_ip.side_effect = [ + Exception("Floating ip could not be assigned.") + ] * 4 + + self.vimconn.nova.servers.get.return_value.status = "ERROR" + self.vimconn.neutron.show_floatingip.side_effect = [ + {"floatingip": {"port_id": ""}} + ] * 4 + + with self.assertRaises(VimConnException) as err: + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + self.assertEqual( + str(err.exception), + "Cannot create floating_ip: Exception Floating ip could not be assigned.", + ) + + self.assertEqual(mock_get_free_floating_ip.call_count, 4) + _call_mock_get_free_floating_ip = mock_get_free_floating_ip.call_args_list + self.assertEqual( + _call_mock_get_free_floating_ip[0][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[1][0], + ( + self.server, + floating_network, + created_items, + ), + ) + self.assertEqual( + _call_mock_get_free_floating_ip[2][0], + ( + self.server, + floating_network, + created_items, + ), + ) + + self.assertEqual(self.vimconn.neutron.show_floatingip.call_count, 4) + self.vimconn.neutron.show_floatingip.assert_called_with( + "y08b73-o9cc-1a6a-a270-12cc4811bd4u" + ) + self.assertEqual(self.vimconn.nova.servers.get.call_count, 4) + self.vimconn.nova.servers.get.assert_called_with(self.server.id) + mock_time.assert_not_called() + mock_sleep.assert_not_called() + mock_create_floating_ip.assert_not_called() + + @patch("time.time") + @patch("time.sleep") + @patch.object(vimconnector, "_create_floating_ip") + @patch.object(vimconnector, "_get_free_floating_ip") + @patch.object(vimconnector, "_assign_floating_ip") + def test_prepare_external_network_for_vm_instance_empty_external_network_list( + self, + mock_assign_floating_ip, + mock_get_free_floating_ip, + mock_create_floating_ip, + mock_sleep, + mock_time, + ): + """External network list is empty.""" + external_network = [] + created_items = {} + vm_start_time = time_return_value + + self.vimconn._prepare_external_network_for_vminstance( + external_network, self.server, created_items, vm_start_time + ) + mock_create_floating_ip.assert_not_called() + mock_time.assert_not_called() + mock_sleep.assert_not_called() + mock_assign_floating_ip.assert_not_called() + mock_get_free_floating_ip.assert_not_called() + self.vimconn.neutron.show.show_floatingip.assert_not_called() + self.vimconn.nova.servers.get.assert_not_called() + + @patch.object(vimconnector, "_vimconnector__wait_for_vm") + def test_update_port_security_for_vm_instance(self, mock_wait_for_vm): + """no_secured_ports has port and the port has allow-address-pairs.""" + no_secured_ports = [(port2_id, "allow-address-pairs")] + + self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server) + + mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE") + + self.vimconn.neutron.update_port.assert_called_once_with( + port2_id, + {"port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]}}, + ) + + @patch.object(vimconnector, "_vimconnector__wait_for_vm") + def test_update_port_security_for_vm_instance_no_allowed_address_pairs( + self, mock_wait_for_vm + ): + """no_secured_ports has port and the port does not have allow-address-pairs.""" + no_secured_ports = [(port2_id, "something")] + + self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server) + + mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE") + + self.vimconn.neutron.update_port.assert_called_once_with( + port2_id, + {"port": {"port_security_enabled": False, "security_groups": None}}, + ) + + @patch.object(vimconnector, "_vimconnector__wait_for_vm") + def test_update_port_security_for_vm_instance_wait_for_vm_raise_exception( + self, mock_wait_for_vm + ): + """__wait_for_vm raises timeout exception.""" + no_secured_ports = [(port2_id, "something")] + + mock_wait_for_vm.side_effect = VimConnException("Timeout waiting for instance.") + + with self.assertRaises(VimConnException) as err: + self.vimconn._update_port_security_for_vminstance( + no_secured_ports, self.server + ) + self.assertEqual(str(err.exception), "Timeout waiting for instance.") + + mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE") + + self.vimconn.neutron.update_port.assert_not_called() + + @patch.object(vimconnector, "_vimconnector__wait_for_vm") + def test_update_port_security_for_vm_instance_neutron_update_port_raise_exception( + self, mock_wait_for_vm + ): + """neutron_update_port method raises exception.""" + no_secured_ports = [(port2_id, "something")] + + self.vimconn.neutron.update_port.side_effect = Exception( + "Port security could not be updated." + ) + + with self.assertRaises(VimConnException) as err: + self.vimconn._update_port_security_for_vminstance( + no_secured_ports, self.server + ) + self.assertEqual( + str(err.exception), + "It was not possible to disable port security for port 17472685-f67f-49fd-8722-eabb7692fc22", + ) + mock_wait_for_vm.assert_called_once_with(self.server.id, "ACTIVE") + + self.vimconn.neutron.update_port.assert_called_once_with( + port2_id, + {"port": {"port_security_enabled": False, "security_groups": None}}, + ) + + @patch.object(vimconnector, "_vimconnector__wait_for_vm") + def test_update_port_security_for_vm_instance_empty_port_list( + self, mock_wait_for_vm + ): + """no_secured_ports list does not have any ports.""" + no_secured_ports = [] + + self.vimconn._update_port_security_for_vminstance(no_secured_ports, self.server) + + mock_wait_for_vm.assert_not_called() + + self.vimconn.neutron.update_port.assert_not_called() + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """New VM instance creation is successful.""" + + mock_create_user_data.return_value = True, "userdata" + + mock_get_vm_availability_zone.return_value = "nova" + + self.vimconn.nova.servers.create.return_value = self.server + + mock_time.return_value = time_return_value + + expected_result = self.server.id, {} + + result = self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list2, + availability_zone_index, + availability_zone_list, + ) + self.assertEqual(result, expected_result) + + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_called_once_with( + name=name, + net_list=net_list, + created_items={}, + net_list_vim=[], + external_network=[], + no_secured_ports=[], + ) + mock_create_user_data.assert_called_once_with(cloud_config) + mock_get_vm_availability_zone.assert_called_once_with( + availability_zone_index, availability_zone_list + ) + mock_prepare_disk_for_vm_instance.assert_called_once_with( + name=name, + existing_vim_volumes=[], + created_items={}, + vm_av_zone="nova", + disk_list=disk_list2, + ) + self.vimconn.nova.servers.create.assert_called_once_with( + name=name, + image=image_id, + flavor=flavor_id, + nics=[], + security_groups="default", + availability_zone="nova", + key_name="my_keypair", + userdata="userdata", + config_drive=True, + block_device_mapping=None, + scheduler_hints={}, + ) + mock_time.assert_called_once() + mock_update_port_security.assert_called_once_with([], self.server) + mock_prepare_external_network.assert_called_once_with( + external_network=[], + server=self.server, + created_items={}, + vm_start_time=time_return_value, + ) + mock_delete_vm_instance.assert_not_called() + mock_format_exception.assert_not_called() + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance_create_user_data_fails( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """New VM instance creation failed because of user data creation failure.""" + + mock_create_user_data.side_effect = Exception( + "User data could not be retrieved." + ) + + mock_get_vm_availability_zone.return_value = "nova" + + self.vimconn.nova.servers.create.return_value = self.server + + mock_time.return_value = time_return_value + + self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list, + availability_zone_index, + availability_zone_list, + ) + + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_called_once_with( + name=name, + net_list=net_list, + created_items={}, + net_list_vim=[], + external_network=[], + no_secured_ports=[], + ) + mock_create_user_data.assert_called_once_with(cloud_config) + mock_get_vm_availability_zone.assert_not_called() + mock_prepare_disk_for_vm_instance.assert_not_called() + self.vimconn.nova.servers.create.assert_not_called() + mock_time.assert_not_called() + mock_update_port_security.assert_not_called() + mock_prepare_external_network.assert_not_called() + mock_delete_vm_instance.assert_called_once_with(None, {}) + mock_format_exception.assert_called_once() + arg = mock_format_exception.call_args[0][0] + self.assertEqual(str(arg), "User data could not be retrieved.") + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance_external_network_exception( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """New VM instance creation, external network connection has failed as floating + ip could not be created.""" + + mock_create_user_data.return_value = True, "userdata" + + mock_get_vm_availability_zone.return_value = "nova" + + self.vimconn.nova.servers.create.return_value = self.server + + mock_time.return_value = time_return_value + + mock_prepare_external_network.side_effect = VimConnException( + "Can not create floating ip." + ) + + self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list2, + availability_zone_index, + availability_zone_list, + ) + + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_called_once_with( + name=name, + net_list=net_list, + created_items={}, + net_list_vim=[], + external_network=[], + no_secured_ports=[], + ) + mock_create_user_data.assert_called_once_with(cloud_config) + mock_get_vm_availability_zone.assert_called_once_with( + availability_zone_index, availability_zone_list + ) + mock_prepare_disk_for_vm_instance.assert_called_once_with( + name=name, + existing_vim_volumes=[], + created_items={}, + vm_av_zone="nova", + disk_list=disk_list2, + ) + self.vimconn.nova.servers.create.assert_called_once_with( + name=name, + image=image_id, + flavor=flavor_id, + nics=[], + security_groups="default", + availability_zone="nova", + key_name="my_keypair", + userdata="userdata", + config_drive=True, + block_device_mapping=None, + scheduler_hints={}, + ) + mock_time.assert_called_once() + mock_update_port_security.assert_called_once_with([], self.server) + mock_prepare_external_network.assert_called_once_with( + external_network=[], + server=self.server, + created_items={}, + vm_start_time=time_return_value, + ) + mock_delete_vm_instance.assert_called_once_with(self.server.id, {}) + mock_format_exception.assert_called_once() + arg = mock_format_exception.call_args[0][0] + self.assertEqual(str(arg), "Can not create floating ip.") + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance_with_affinity_group( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """New VM creation with affinity group.""" + affinity_group_list = [ + {"affinity_group_id": "38b73-e9cc-5a6a-t270-82cc4811bd4a"} + ] + mock_create_user_data.return_value = True, "userdata" + mock_get_vm_availability_zone.return_value = "nova" + self.vimconn.nova.servers.create.return_value = self.server + mock_time.return_value = time_return_value + expected_result = self.server.id, {} + + result = self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list2, + availability_zone_index, + availability_zone_list, + ) + self.assertEqual(result, expected_result) + + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_called_once_with( + name=name, + net_list=net_list, + created_items={}, + net_list_vim=[], + external_network=[], + no_secured_ports=[], + ) + mock_create_user_data.assert_called_once_with(cloud_config) + mock_get_vm_availability_zone.assert_called_once_with( + availability_zone_index, availability_zone_list + ) + mock_prepare_disk_for_vm_instance.assert_called_once_with( + name=name, + existing_vim_volumes=[], + created_items={}, + vm_av_zone="nova", + disk_list=disk_list2, + ) + self.vimconn.nova.servers.create.assert_called_once_with( + name=name, + image=image_id, + flavor=flavor_id, + nics=[], + security_groups="default", + availability_zone="nova", + key_name="my_keypair", + userdata="userdata", + config_drive=True, + block_device_mapping=None, + scheduler_hints={"group": "38b73-e9cc-5a6a-t270-82cc4811bd4a"}, + ) + mock_time.assert_called_once() + mock_update_port_security.assert_called_once_with([], self.server) + mock_prepare_external_network.assert_called_once_with( + external_network=[], + server=self.server, + created_items={}, + vm_start_time=time_return_value, + ) + mock_delete_vm_instance.assert_not_called() + mock_format_exception.assert_not_called() + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance_nova_server_create_failed( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """New VM(server) creation failed.""" + + mock_create_user_data.return_value = True, "userdata" + + mock_get_vm_availability_zone.return_value = "nova" + + self.vimconn.nova.servers.create.side_effect = Exception( + "Server could not be created." + ) + + mock_time.return_value = time_return_value + + self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list2, + availability_zone_index, + availability_zone_list, + ) + + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_called_once_with( + name=name, + net_list=net_list, + created_items={}, + net_list_vim=[], + external_network=[], + no_secured_ports=[], + ) + mock_create_user_data.assert_called_once_with(cloud_config) + mock_get_vm_availability_zone.assert_called_once_with( + availability_zone_index, availability_zone_list + ) + mock_prepare_disk_for_vm_instance.assert_called_once_with( + name=name, + existing_vim_volumes=[], + created_items={}, + vm_av_zone="nova", + disk_list=disk_list2, + ) + + self.vimconn.nova.servers.create.assert_called_once_with( + name=name, + image=image_id, + flavor=flavor_id, + nics=[], + security_groups="default", + availability_zone="nova", + key_name="my_keypair", + userdata="userdata", + config_drive=True, + block_device_mapping=None, + scheduler_hints={}, + ) + mock_time.assert_not_called() + mock_update_port_security.assert_not_called() + mock_prepare_external_network.assert_not_called() + mock_delete_vm_instance.assert_called_once_with(None, {}) + mock_format_exception.assert_called_once() + arg = mock_format_exception.call_args[0][0] + self.assertEqual(str(arg), "Server could not be created.") + + @patch("time.time") + @patch.object(vimconnector, "_reload_connection") + @patch.object(vimconnector, "_prepare_network_for_vminstance") + @patch.object(vimconnector, "_create_user_data") + @patch.object(vimconnector, "_get_vm_availability_zone") + @patch.object(vimconnector, "_prepare_disk_for_vminstance") + @patch.object(vimconnector, "_update_port_security_for_vminstance") + @patch.object(vimconnector, "_prepare_external_network_for_vminstance") + @patch.object(vimconnector, "delete_vminstance") + @patch.object(vimconnector, "_format_exception") + def test_new_vm_instance_connection_exception( + self, + mock_format_exception, + mock_delete_vm_instance, + mock_prepare_external_network, + mock_update_port_security, + mock_prepare_disk_for_vm_instance, + mock_get_vm_availability_zone, + mock_create_user_data, + mock_prepare_network_for_vm_instance, + mock_reload_connection, + mock_time, + ): + """Connection to Cloud API has failed.""" + mock_reload_connection.side_effect = Exception("Can not connect to Cloud APIs.") + mock_create_user_data.return_value = True, "userdata" + mock_get_vm_availability_zone.return_value = "nova" + self.vimconn.nova.servers.create.return_value = self.server + mock_time.return_value = time_return_value + + self.vimconn.new_vminstance( + name, + description, + start, + image_id, + flavor_id, + affinity_group_list, + net_list, + cloud_config, + disk_list, + availability_zone_index, + availability_zone_list, + ) + mock_format_exception.assert_called_once() + arg = mock_format_exception.call_args[0][0] + self.assertEqual(str(arg), "Can not connect to Cloud APIs.") + mock_reload_connection.assert_called_once() + mock_prepare_network_for_vm_instance.assert_not_called() + mock_create_user_data.assert_not_called() + mock_get_vm_availability_zone.assert_not_called() + mock_prepare_disk_for_vm_instance.assert_not_called() + self.vimconn.nova.servers.create.assert_not_called() + mock_time.assert_not_called() + mock_update_port_security.assert_not_called() + mock_prepare_external_network.assert_not_called() + mock_delete_vm_instance.assert_called_once_with(None, {}) + + if __name__ == "__main__": unittest.main() diff --git a/RO-VIM-openstack/osm_rovim_openstack/vimconn_openstack.py b/RO-VIM-openstack/osm_rovim_openstack/vimconn_openstack.py index 3aaedf60..f3b89dcb 100644 --- a/RO-VIM-openstack/osm_rovim_openstack/vimconn_openstack.py +++ b/RO-VIM-openstack/osm_rovim_openstack/vimconn_openstack.py @@ -38,6 +38,7 @@ from pprint import pformat import random import re import time +from typing import Dict, Optional, Tuple from cinderclient import client as cClient from glanceclient import client as glClient @@ -1717,60 +1718,761 @@ class vimconnector(vimconn.VimConnector): "No enough availability zones at VIM for this deployment" ) + def _prepare_port_dict_security_groups(self, net: dict, port_dict: dict) -> None: + """Fill up the security_groups in the port_dict. + + Args: + net (dict): Network details + port_dict (dict): Port details + + """ + if ( + self.config.get("security_groups") + and net.get("port_security") is not False + and not self.config.get("no_port_security_extension") + ): + if not self.security_groups_id: + self._get_ids_from_name() + + port_dict["security_groups"] = self.security_groups_id + + def _prepare_port_dict_binding(self, net: dict, port_dict: dict) -> None: + """Fill up the network binding depending on network type in the port_dict. + + Args: + net (dict): Network details + port_dict (dict): Port details + + """ + if not net.get("type"): + raise vimconn.VimConnException("Type is missing in the network details.") + + if net["type"] == "virtual": + pass + + # For VF + elif net["type"] == "VF" or net["type"] == "SR-IOV": + + port_dict["binding:vnic_type"] = "direct" + + # VIO specific Changes + if self.vim_type == "VIO": + # Need to create port with port_security_enabled = False and no-security-groups + port_dict["port_security_enabled"] = False + port_dict["provider_security_groups"] = [] + port_dict["security_groups"] = [] + + else: + # For PT PCI-PASSTHROUGH + port_dict["binding:vnic_type"] = "direct-physical" + + @staticmethod + def _set_fixed_ip(new_port: dict, net: dict) -> None: + """Set the "ip" parameter in net dictionary. + + Args: + new_port (dict): New created port + net (dict): Network details + + """ + fixed_ips = new_port["port"].get("fixed_ips") + + if fixed_ips: + net["ip"] = fixed_ips[0].get("ip_address") + else: + net["ip"] = None + + @staticmethod + def _prepare_port_dict_mac_ip_addr(net: dict, port_dict: dict) -> None: + """Fill up the mac_address and fixed_ips in port_dict. + + Args: + net (dict): Network details + port_dict (dict): Port details + + """ + if net.get("mac_address"): + port_dict["mac_address"] = net["mac_address"] + + if net.get("ip_address"): + port_dict["fixed_ips"] = [{"ip_address": net["ip_address"]}] + # TODO add "subnet_id": + + def _create_new_port(self, port_dict: dict, created_items: dict, net: dict) -> Dict: + """Create new port using neutron. + + Args: + port_dict (dict): Port details + created_items (dict): All created items + net (dict): Network details + + Returns: + new_port (dict): New created port + + """ + new_port = self.neutron.create_port({"port": port_dict}) + created_items["port:" + str(new_port["port"]["id"])] = True + net["mac_adress"] = new_port["port"]["mac_address"] + net["vim_id"] = new_port["port"]["id"] + + return new_port + + def _create_port( + self, net: dict, name: str, created_items: dict + ) -> Tuple[dict, dict]: + """Create port using net details. + + Args: + net (dict): Network details + name (str): Name to be used as network name if net dict does not include name + created_items (dict): All created items + + Returns: + new_port, port New created port, port dictionary + + """ + + port_dict = { + "network_id": net["net_id"], + "name": net.get("name"), + "admin_state_up": True, + } + + if not port_dict["name"]: + port_dict["name"] = name + + self._prepare_port_dict_security_groups(net, port_dict) + + self._prepare_port_dict_binding(net, port_dict) + + vimconnector._prepare_port_dict_mac_ip_addr(net, port_dict) + + new_port = self._create_new_port(port_dict, created_items, net) + + vimconnector._set_fixed_ip(new_port, net) + + port = {"port-id": new_port["port"]["id"]} + + if float(self.nova.api_version.get_string()) >= 2.32: + port["tag"] = new_port["port"]["name"] + + return new_port, port + + def _prepare_network_for_vminstance( + self, + name: str, + net_list: list, + created_items: dict, + net_list_vim: list, + external_network: list, + no_secured_ports: list, + ) -> None: + """Create port and fill up net dictionary for new VM instance creation. + + Args: + name (str): Name of network + net_list (list): List of networks + created_items (dict): All created items belongs to a VM + net_list_vim (list): List of ports + external_network (list): List of external-networks + no_secured_ports (list): Port security disabled ports + """ + + self._reload_connection() + + for net in net_list: + # Skip non-connected iface + if not net.get("net_id"): + continue + + new_port, port = self._create_port(net, name, created_items) + + net_list_vim.append(port) + + if net.get("floating_ip", False): + net["exit_on_floating_ip_error"] = True + external_network.append(net) + + elif net["use"] == "mgmt" and self.config.get("use_floating_ip"): + net["exit_on_floating_ip_error"] = False + external_network.append(net) + net["floating_ip"] = self.config.get("use_floating_ip") + + # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic + # is dropped. As a workaround we wait until the VM is active and then disable the port-security + if net.get("port_security") is False and not self.config.get( + "no_port_security_extension" + ): + no_secured_ports.append( + ( + new_port["port"]["id"], + net.get("port_security_disable_strategy"), + ) + ) + + def _prepare_persistent_root_volumes( + self, + name: str, + vm_av_zone: list, + disk: dict, + base_disk_index: int, + block_device_mapping: dict, + existing_vim_volumes: list, + created_items: dict, + ) -> Optional[str]: + """Prepare persistent root volumes for new VM instance. + + Args: + name (str): Name of VM instance + vm_av_zone (list): List of availability zones + disk (dict): Disk details + base_disk_index (int): Disk index + block_device_mapping (dict): Block device details + existing_vim_volumes (list): Existing disk details + created_items (dict): All created items belongs to VM + + Returns: + boot_volume_id (str): ID of boot volume + + """ + # Disk may include only vim_volume_id or only vim_id." + # Use existing persistent root volume finding with volume_id or vim_id + key_id = "vim_volume_id" if "vim_volume_id" in disk.keys() else "vim_id" + + if disk.get(key_id): + + block_device_mapping["vd" + chr(base_disk_index)] = disk[key_id] + existing_vim_volumes.append({"id": disk[key_id]}) + + else: + # Create persistent root volume + volume = self.cinder.volumes.create( + size=disk["size"], + name=name + "vd" + chr(base_disk_index), + imageRef=disk["image_id"], + # Make sure volume is in the same AZ as the VM to be attached to + availability_zone=vm_av_zone, + ) + boot_volume_id = volume.id + created_items["volume:" + str(volume.id)] = True + block_device_mapping["vd" + chr(base_disk_index)] = volume.id + + return boot_volume_id + + def _prepare_non_root_persistent_volumes( + self, + name: str, + disk: dict, + vm_av_zone: list, + block_device_mapping: dict, + base_disk_index: int, + existing_vim_volumes: list, + created_items: dict, + ) -> None: + """Prepare persistent volumes for new VM instance. + + Args: + name (str): Name of VM instance + disk (dict): Disk details + vm_av_zone (list): List of availability zones + block_device_mapping (dict): Block device details + base_disk_index (int): Disk index + existing_vim_volumes (list): Existing disk details + created_items (dict): All created items belongs to VM + """ + # Non-root persistent volumes + # Disk may include only vim_volume_id or only vim_id." + key_id = "vim_volume_id" if "vim_volume_id" in disk.keys() else "vim_id" + + if disk.get(key_id): + + # Use existing persistent volume + block_device_mapping["vd" + chr(base_disk_index)] = disk[key_id] + existing_vim_volumes.append({"id": disk[key_id]}) + + else: + # Create persistent volume + volume = self.cinder.volumes.create( + size=disk["size"], + name=name + "vd" + chr(base_disk_index), + # Make sure volume is in the same AZ as the VM to be attached to + availability_zone=vm_av_zone, + ) + created_items["volume:" + str(volume.id)] = True + block_device_mapping["vd" + chr(base_disk_index)] = volume.id + + def _wait_for_created_volumes_availability( + self, elapsed_time: int, created_items: dict + ) -> Optional[int]: + """Wait till created volumes become available. + + Args: + elapsed_time (int): Passed time while waiting + created_items (dict): All created items belongs to VM + + Returns: + elapsed_time (int): Time spent while waiting + + """ + + while elapsed_time < volume_timeout: + for created_item in created_items: + v, _, volume_id = created_item.partition(":") + if v == "volume": + if self.cinder.volumes.get(volume_id).status != "available": + break + else: + # All ready: break from while + break + + time.sleep(5) + elapsed_time += 5 + + return elapsed_time + + def _wait_for_existing_volumes_availability( + self, elapsed_time: int, existing_vim_volumes: list + ) -> Optional[int]: + """Wait till existing volumes become available. + + Args: + elapsed_time (int): Passed time while waiting + existing_vim_volumes (list): Existing volume details + + Returns: + elapsed_time (int): Time spent while waiting + + """ + + while elapsed_time < volume_timeout: + for volume in existing_vim_volumes: + if self.cinder.volumes.get(volume["id"]).status != "available": + break + else: # all ready: break from while + break + + time.sleep(5) + elapsed_time += 5 + + return elapsed_time + + def _prepare_disk_for_vminstance( + self, + name: str, + existing_vim_volumes: list, + created_items: dict, + vm_av_zone: list, + disk_list: list = None, + ) -> None: + """Prepare all volumes for new VM instance. + + Args: + name (str): Name of Instance + existing_vim_volumes (list): List of existing volumes + created_items (dict): All created items belongs to VM + vm_av_zone (list): VM availability zone + disk_list (list): List of disks + + """ + # Create additional volumes in case these are present in disk_list + base_disk_index = ord("b") + boot_volume_id = None + elapsed_time = 0 + + block_device_mapping = {} + for disk in disk_list: + if "image_id" in disk: + # Root persistent volume + base_disk_index = ord("a") + boot_volume_id = self._prepare_persistent_root_volumes( + name=name, + vm_av_zone=vm_av_zone, + disk=disk, + base_disk_index=base_disk_index, + block_device_mapping=block_device_mapping, + existing_vim_volumes=existing_vim_volumes, + created_items=created_items, + ) + else: + # Non-root persistent volume + self._prepare_non_root_persistent_volumes( + name=name, + disk=disk, + vm_av_zone=vm_av_zone, + block_device_mapping=block_device_mapping, + base_disk_index=base_disk_index, + existing_vim_volumes=existing_vim_volumes, + created_items=created_items, + ) + base_disk_index += 1 + + # Wait until created volumes are with status available + elapsed_time = self._wait_for_created_volumes_availability( + elapsed_time, created_items + ) + # Wait until existing volumes in vim are with status available + elapsed_time = self._wait_for_existing_volumes_availability( + elapsed_time, existing_vim_volumes + ) + # If we exceeded the timeout rollback + if elapsed_time >= volume_timeout: + raise vimconn.VimConnException( + "Timeout creating volumes for instance " + name, + http_code=vimconn.HTTP_Request_Timeout, + ) + if boot_volume_id: + self.cinder.volumes.set_bootable(boot_volume_id, True) + + def _find_the_external_network_for_floating_ip(self): + """Get the external network ip in order to create floating IP. + + Returns: + pool_id (str): External network pool ID + + """ + + # Find the external network + external_nets = list() + + for net in self.neutron.list_networks()["networks"]: + if net["router:external"]: + external_nets.append(net) + + if len(external_nets) == 0: + raise vimconn.VimConnException( + "Cannot create floating_ip automatically since " + "no external network is present", + http_code=vimconn.HTTP_Conflict, + ) + + if len(external_nets) > 1: + raise vimconn.VimConnException( + "Cannot create floating_ip automatically since " + "multiple external networks are present", + http_code=vimconn.HTTP_Conflict, + ) + + # Pool ID + return external_nets[0].get("id") + + def _neutron_create_float_ip(self, param: dict, created_items: dict) -> None: + """Trigger neutron to create a new floating IP using external network ID. + + Args: + param (dict): Input parameters to create a floating IP + created_items (dict): All created items belongs to new VM instance + + Raises: + + VimConnException + """ + try: + self.logger.debug("Creating floating IP") + new_floating_ip = self.neutron.create_floatingip(param) + free_floating_ip = new_floating_ip["floatingip"]["id"] + created_items["floating_ip:" + str(free_floating_ip)] = True + + except Exception as e: + raise vimconn.VimConnException( + type(e).__name__ + ": Cannot create new floating_ip " + str(e), + http_code=vimconn.HTTP_Conflict, + ) + + def _create_floating_ip( + self, floating_network: dict, server: object, created_items: dict + ) -> None: + """Get the available Pool ID and create a new floating IP. + + Args: + floating_network (dict): Dict including external network ID + server (object): Server object + created_items (dict): All created items belongs to new VM instance + + """ + + # Pool_id is available + if ( + isinstance(floating_network["floating_ip"], str) + and floating_network["floating_ip"].lower() != "true" + ): + pool_id = floating_network["floating_ip"] + + # Find the Pool_id + else: + pool_id = self._find_the_external_network_for_floating_ip() + + param = { + "floatingip": { + "floating_network_id": pool_id, + "tenant_id": server.tenant_id, + } + } + + self._neutron_create_float_ip(param, created_items) + + def _find_floating_ip( + self, + server: object, + floating_ips: list, + floating_network: dict, + ) -> Optional[str]: + """Find the available free floating IPs if there are. + + Args: + server (object): Server object + floating_ips (list): List of floating IPs + floating_network (dict): Details of floating network such as ID + + Returns: + free_floating_ip (str): Free floating ip address + + """ + for fip in floating_ips: + if fip.get("port_id") or fip.get("tenant_id") != server.tenant_id: + continue + + if isinstance(floating_network["floating_ip"], str): + if fip.get("floating_network_id") != floating_network["floating_ip"]: + continue + + return fip["id"] + + def _assign_floating_ip( + self, free_floating_ip: str, floating_network: dict + ) -> Dict: + """Assign the free floating ip address to port. + + Args: + free_floating_ip (str): Floating IP to be assigned + floating_network (dict): ID of floating network + + Returns: + fip (dict) (dict): Floating ip details + + """ + # The vim_id key contains the neutron.port_id + self.neutron.update_floatingip( + free_floating_ip, + {"floatingip": {"port_id": floating_network["vim_id"]}}, + ) + # For race condition ensure not re-assigned to other VM after 5 seconds + time.sleep(5) + + return self.neutron.show_floatingip(free_floating_ip) + + def _get_free_floating_ip( + self, server: object, floating_network: dict, created_items: dict + ) -> Optional[str]: + """Get the free floating IP address. + + Args: + server (object): Server Object + floating_network (dict): Floating network details + created_items (dict): All created items belongs to new VM instance + + Returns: + free_floating_ip (str): Free floating ip addr + + """ + + floating_ips = self.neutron.list_floatingips().get("floatingips", ()) + + # Randomize + random.shuffle(floating_ips) + + return self._find_floating_ip( + server, floating_ips, floating_network, created_items + ) + + def _prepare_external_network_for_vminstance( + self, + external_network: list, + server: object, + created_items: dict, + vm_start_time: float, + ) -> None: + """Assign floating IP address for VM instance. + + Args: + external_network (list): ID of External network + server (object): Server Object + created_items (dict): All created items belongs to new VM instance + vm_start_time (float): Time as a floating point number expressed in seconds since the epoch, in UTC + + Raises: + VimConnException + + """ + for floating_network in external_network: + try: + assigned = False + floating_ip_retries = 3 + # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry + # several times + while not assigned: + + free_floating_ip = self._get_free_floating_ip( + server, floating_network, created_items + ) + + if not free_floating_ip: + self._create_floating_ip( + floating_network, server, created_items + ) + + try: + # For race condition ensure not already assigned + fip = self.neutron.show_floatingip(free_floating_ip) + + if fip["floatingip"].get("port_id"): + continue + + # Assign floating ip + fip = self._assign_floating_ip( + free_floating_ip, floating_network + ) + + if fip["floatingip"]["port_id"] != floating_network["vim_id"]: + self.logger.warning( + "floating_ip {} re-assigned to other port".format( + free_floating_ip + ) + ) + continue + + self.logger.debug( + "Assigned floating_ip {} to VM {}".format( + free_floating_ip, server.id + ) + ) + + assigned = True + + except Exception as e: + # Openstack need some time after VM creation to assign an IP. So retry if fails + vm_status = self.nova.servers.get(server.id).status + + if vm_status not in ("ACTIVE", "ERROR"): + if time.time() - vm_start_time < server_timeout: + time.sleep(5) + continue + elif floating_ip_retries > 0: + floating_ip_retries -= 1 + continue + + raise vimconn.VimConnException( + "Cannot create floating_ip: {} {}".format( + type(e).__name__, e + ), + http_code=vimconn.HTTP_Conflict, + ) + + except Exception as e: + if not floating_network["exit_on_floating_ip_error"]: + self.logger.error("Cannot create floating_ip. %s", str(e)) + continue + + raise + + def _update_port_security_for_vminstance( + self, + no_secured_ports: list, + server: object, + ) -> None: + """Updates the port security according to no_secured_ports list. + + Args: + no_secured_ports (list): List of ports that security will be disabled + server (object): Server Object + + Raises: + VimConnException + + """ + # Wait until the VM is active and then disable the port-security + if no_secured_ports: + self.__wait_for_vm(server.id, "ACTIVE") + + for port in no_secured_ports: + port_update = { + "port": {"port_security_enabled": False, "security_groups": None} + } + + if port[1] == "allow-address-pairs": + port_update = { + "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]} + } + + try: + self.neutron.update_port(port[0], port_update) + + except Exception: + + raise vimconn.VimConnException( + "It was not possible to disable port security for port {}".format( + port[0] + ) + ) + def new_vminstance( self, - name, - description, - start, - image_id, - flavor_id, - affinity_group_list, - net_list, + name: str, + description: str, + start: bool, + image_id: str, + flavor_id: str, + affinity_group_list: list, + net_list: list, cloud_config=None, disk_list=None, availability_zone_index=None, availability_zone_list=None, - ): - """Adds a VM instance to VIM - Params: - start: indicates if VM must start or boot in pause mode. Ignored - image_id,flavor_id: image and flavor uuid - affinity_group_list: list of affinity groups, each one is a dictionary. - Ignore if empty. - net_list: list of interfaces, each one is a dictionary with: - name: - net_id: network uuid to connect - vpci: virtual vcpi to assign, ignored because openstack lack #TODO - model: interface model, ignored #TODO - mac_address: used for SR-IOV ifaces #TODO for other types - use: 'data', 'bridge', 'mgmt' - type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared' - vim_id: filled/added by this function - floating_ip: True/False (or it can be None) - port_security: True/False - 'cloud_config': (optional) dictionary with: - 'key-pairs': (optional) list of strings with the public key to be inserted to the default user - 'users': (optional) list of users to be inserted, each item is a dict with: - 'name': (mandatory) user name, - 'key-pairs': (optional) list of strings with the public key to be inserted to the user - 'user-data': (optional) string is a text script to be passed directly to cloud-init - 'config-files': (optional). List of files to be transferred. Each item is a dict with: - 'dest': (mandatory) string with the destination absolute path - 'encoding': (optional, by default text). Can be one of: + ) -> tuple: + """Adds a VM instance to VIM. + + Args: + name (str): name of VM + description (str): description + start (bool): indicates if VM must start or boot in pause mode. Ignored + image_id (str) image uuid + flavor_id (str) flavor uuid + affinity_group_list (list): list of affinity groups, each one is a dictionary.Ignore if empty. + net_list (list): list of interfaces, each one is a dictionary with: + name: name of network + net_id: network uuid to connect + vpci: virtual vcpi to assign, ignored because openstack lack #TODO + model: interface model, ignored #TODO + mac_address: used for SR-IOV ifaces #TODO for other types + use: 'data', 'bridge', 'mgmt' + type: 'virtual', 'PCI-PASSTHROUGH'('PF'), 'SR-IOV'('VF'), 'VFnotShared' + vim_id: filled/added by this function + floating_ip: True/False (or it can be None) + port_security: True/False + cloud_config (dict): (optional) dictionary with: + key-pairs: (optional) list of strings with the public key to be inserted to the default user + users: (optional) list of users to be inserted, each item is a dict with: + name: (mandatory) user name, + key-pairs: (optional) list of strings with the public key to be inserted to the user + user-data: (optional) string is a text script to be passed directly to cloud-init + config-files: (optional). List of files to be transferred. Each item is a dict with: + dest: (mandatory) string with the destination absolute path + encoding: (optional, by default text). Can be one of: 'b64', 'base64', 'gz', 'gz+b64', 'gz+base64', 'gzip+b64', 'gzip+base64' - 'content' (mandatory): string with the content of the file - 'permissions': (optional) string with file permissions, typically octal notation '0644' - 'owner': (optional) file owner, string with the format 'owner:group' - 'boot-data-drive': boolean to indicate if user-data must be passed using a boot drive (hard disk) - 'disk_list': (optional) list with additional disks to the VM. Each item is a dict with: - 'image_id': (optional). VIM id of an existing image. If not provided an empty disk must be mounted - 'size': (mandatory) string with the size of the disk in GB - 'vim_id' (optional) should use this existing volume id - availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required - availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if + content : (mandatory) string with the content of the file + permissions: (optional) string with file permissions, typically octal notation '0644' + owner: (optional) file owner, string with the format 'owner:group' + boot-data-drive: boolean to indicate if user-data must be passed using a boot drive (hard disk) + disk_list: (optional) list with additional disks to the VM. Each item is a dict with: + image_id: (optional). VIM id of an existing image. If not provided an empty disk must be mounted + size: (mandatory) string with the size of the disk in GB + vim_id: (optional) should use this existing volume id + availability_zone_index: Index of availability_zone_list to use for this this VM. None if not AV required + availability_zone_list: list of availability zones given by user in the VNFD descriptor. Ignore if availability_zone_index is None #TODO ip, security groups - Returns a tuple with the instance identifier and created_items or raises an exception on error + + Returns: + A tuple with the instance identifier and created_items or raises an exception on error created_items can be None or a dictionary where this method can include key-values that will be passed to the method delete_vminstance and action_vminstance. Can be used to store created ports, volumes, etc. Format is vimconnector dependent, but do not use nested dictionaries and a value of None should be the same @@ -1786,235 +2488,46 @@ class vimconnector(vimconn.VimConnector): try: server = None created_items = {} - # metadata = {} net_list_vim = [] + # list of external networks to be connected to instance, later on used to create floating_ip external_network = [] - # ^list of external networks to be connected to instance, later on used to create floating_ip - no_secured_ports = [] # List of port-is with port-security disabled - self._reload_connection() - # metadata_vpci = {} # For a specific neutron plugin + # List of ports with port-security disabled + no_secured_ports = [] block_device_mapping = None + existing_vim_volumes = [] + server_group_id = None + scheduller_hints = {} - for net in net_list: - if not net.get("net_id"): # skip non connected iface - continue - - port_dict = { - "network_id": net["net_id"], - "name": net.get("name"), - "admin_state_up": True, - } - - if ( - self.config.get("security_groups") - and net.get("port_security") is not False - and not self.config.get("no_port_security_extension") - ): - if not self.security_groups_id: - self._get_ids_from_name() - - port_dict["security_groups"] = self.security_groups_id - - if net["type"] == "virtual": - pass - # if "vpci" in net: - # metadata_vpci[ net["net_id"] ] = [[ net["vpci"], "" ]] - elif net["type"] == "VF" or net["type"] == "SR-IOV": # for VF - # if "vpci" in net: - # if "VF" not in metadata_vpci: - # metadata_vpci["VF"]=[] - # metadata_vpci["VF"].append([ net["vpci"], "" ]) - port_dict["binding:vnic_type"] = "direct" - - # VIO specific Changes - if self.vim_type == "VIO": - # Need to create port with port_security_enabled = False and no-security-groups - port_dict["port_security_enabled"] = False - port_dict["provider_security_groups"] = [] - port_dict["security_groups"] = [] - else: # For PT PCI-PASSTHROUGH - # if "vpci" in net: - # if "PF" not in metadata_vpci: - # metadata_vpci["PF"]=[] - # metadata_vpci["PF"].append([ net["vpci"], "" ]) - port_dict["binding:vnic_type"] = "direct-physical" - - if not port_dict["name"]: - port_dict["name"] = name - - if net.get("mac_address"): - port_dict["mac_address"] = net["mac_address"] - - if net.get("ip_address"): - port_dict["fixed_ips"] = [{"ip_address": net["ip_address"]}] - # TODO add "subnet_id": - - new_port = self.neutron.create_port({"port": port_dict}) - created_items["port:" + str(new_port["port"]["id"])] = True - net["mac_adress"] = new_port["port"]["mac_address"] - net["vim_id"] = new_port["port"]["id"] - # if try to use a network without subnetwork, it will return a emtpy list - fixed_ips = new_port["port"].get("fixed_ips") - - if fixed_ips: - net["ip"] = fixed_ips[0].get("ip_address") - else: - net["ip"] = None - - port = {"port-id": new_port["port"]["id"]} - if float(self.nova.api_version.get_string()) >= 2.32: - port["tag"] = new_port["port"]["name"] - - net_list_vim.append(port) - - if net.get("floating_ip", False): - net["exit_on_floating_ip_error"] = True - external_network.append(net) - elif net["use"] == "mgmt" and self.config.get("use_floating_ip"): - net["exit_on_floating_ip_error"] = False - external_network.append(net) - net["floating_ip"] = self.config.get("use_floating_ip") - - # If port security is disabled when the port has not yet been attached to the VM, then all vm traffic - # is dropped. - # As a workaround we wait until the VM is active and then disable the port-security - if net.get("port_security") is False and not self.config.get( - "no_port_security_extension" - ): - no_secured_ports.append( - ( - new_port["port"]["id"], - net.get("port_security_disable_strategy"), - ) - ) - - # if metadata_vpci: - # metadata = {"pci_assignement": json.dumps(metadata_vpci)} - # if len(metadata["pci_assignement"]) >255: - # #limit the metadata size - # #metadata["pci_assignement"] = metadata["pci_assignement"][0:255] - # self.logger.warn("Metadata deleted since it exceeds the expected length (255) ") - # metadata = {} + # Check the Openstack Connection + self._reload_connection() - self.logger.debug( - "name '%s' image_id '%s'flavor_id '%s' net_list_vim '%s' description '%s'", - name, - image_id, - flavor_id, - str(net_list_vim), - description, + # Prepare network list + self._prepare_network_for_vminstance( + name=name, + net_list=net_list, + created_items=created_items, + net_list_vim=net_list_vim, + external_network=external_network, + no_secured_ports=no_secured_ports, ) - # cloud config + # Cloud config config_drive, userdata = self._create_user_data(cloud_config) - # get availability Zone + # Get availability Zone vm_av_zone = self._get_vm_availability_zone( availability_zone_index, availability_zone_list ) - # Create additional volumes in case these are present in disk_list - existing_vim_volumes = [] - base_disk_index = ord("b") - boot_volume_id = None if disk_list: - block_device_mapping = {} - for disk in disk_list: - if "image_id" in disk: - # persistent root volume - base_disk_index = ord("a") - image_id = "" - # use existing persistent root volume - if disk.get("vim_volume_id"): - block_device_mapping["vd" + chr(base_disk_index)] = disk[ - "vim_volume_id" - ] - existing_vim_volumes.append({"id": disk["vim_volume_id"]}) - # use existing persistent root volume - elif disk.get("vim_id"): - block_device_mapping["vd" + chr(base_disk_index)] = disk[ - "vim_id" - ] - existing_vim_volumes.append({"id": disk["vim_id"]}) - else: - # create persistent root volume - volume = self.cinder.volumes.create( - size=disk["size"], - name=name + "vd" + chr(base_disk_index), - imageRef=disk["image_id"], - # Make sure volume is in the same AZ as the VM to be attached to - availability_zone=vm_av_zone, - ) - boot_volume_id = volume.id - created_items["volume:" + str(volume.id)] = True - block_device_mapping[ - "vd" + chr(base_disk_index) - ] = volume.id - else: - # non-root persistent volume - key_id = ( - "vim_volume_id" - if "vim_volume_id" in disk.keys() - else "vim_id" - ) - if disk.get(key_id): - # use existing persistent volume - block_device_mapping["vd" + chr(base_disk_index)] = disk[ - key_id - ] - existing_vim_volumes.append({"id": disk[key_id]}) - else: - # create persistent volume - volume = self.cinder.volumes.create( - size=disk["size"], - name=name + "vd" + chr(base_disk_index), - # Make sure volume is in the same AZ as the VM to be attached to - availability_zone=vm_av_zone, - ) - created_items["volume:" + str(volume.id)] = True - block_device_mapping[ - "vd" + chr(base_disk_index) - ] = volume.id - - base_disk_index += 1 - - # Wait until created volumes are with status available - elapsed_time = 0 - while elapsed_time < volume_timeout: - for created_item in created_items: - v, _, volume_id = created_item.partition(":") - if v == "volume": - if self.cinder.volumes.get(volume_id).status != "available": - break - else: # all ready: break from while - break - - time.sleep(5) - elapsed_time += 5 - - # Wait until existing volumes in vim are with status available - while elapsed_time < volume_timeout: - for volume in existing_vim_volumes: - if self.cinder.volumes.get(volume["id"]).status != "available": - break - else: # all ready: break from while - break - - time.sleep(5) - elapsed_time += 5 - - # If we exceeded the timeout rollback - if elapsed_time >= volume_timeout: - raise vimconn.VimConnException( - "Timeout creating volumes for instance " + name, - http_code=vimconn.HTTP_Request_Timeout, - ) - if boot_volume_id: - self.cinder.volumes.set_bootable(boot_volume_id, True) - - # Manage affinity groups/server groups - server_group_id = None - scheduller_hints = {} + # Prepare disks + self._prepare_disk_for_vminstance( + name=name, + existing_vim_volumes=existing_vim_volumes, + created_items=created_items, + vm_av_zone=vm_av_zone, + disk_list=disk_list, + ) if affinity_group_list: # Only first id on the list will be used. Openstack restriction @@ -2038,6 +2551,8 @@ class vimconnector(vimconn.VimConnector): server_group_id, ) ) + + # Create VM server = self.nova.servers.create( name=name, image=image_id, @@ -2051,179 +2566,20 @@ class vimconnector(vimconn.VimConnector): config_drive=config_drive, block_device_mapping=block_device_mapping, scheduler_hints=scheduller_hints, - ) # , description=description) + ) vm_start_time = time.time() - # Previously mentioned workaround to wait until the VM is active and then disable the port-security - if no_secured_ports: - self.__wait_for_vm(server.id, "ACTIVE") - for port in no_secured_ports: - port_update = { - "port": {"port_security_enabled": False, "security_groups": None} - } - - if port[1] == "allow-address-pairs": - port_update = { - "port": {"allowed_address_pairs": [{"ip_address": "0.0.0.0/0"}]} - } + self._update_port_security_for_vminstance(no_secured_ports, server) - try: - self.neutron.update_port(port[0], port_update) - except Exception: - raise vimconn.VimConnException( - "It was not possible to disable port security for port {}".format( - port[0] - ) - ) - - # print "DONE :-)", server - - # pool_id = None - for floating_network in external_network: - try: - assigned = False - floating_ip_retries = 3 - # In case of RO in HA there can be conflicts, two RO trying to assign same floating IP, so retry - # several times - while not assigned: - floating_ips = self.neutron.list_floatingips().get( - "floatingips", () - ) - random.shuffle(floating_ips) # randomize - for fip in floating_ips: - if ( - fip.get("port_id") - or fip.get("tenant_id") != server.tenant_id - ): - continue - - if isinstance(floating_network["floating_ip"], str): - if ( - fip.get("floating_network_id") - != floating_network["floating_ip"] - ): - continue - - free_floating_ip = fip["id"] - break - else: - if ( - isinstance(floating_network["floating_ip"], str) - and floating_network["floating_ip"].lower() != "true" - ): - pool_id = floating_network["floating_ip"] - else: - # Find the external network - external_nets = list() - - for net in self.neutron.list_networks()["networks"]: - if net["router:external"]: - external_nets.append(net) - - if len(external_nets) == 0: - raise vimconn.VimConnException( - "Cannot create floating_ip automatically since " - "no external network is present", - http_code=vimconn.HTTP_Conflict, - ) - - if len(external_nets) > 1: - raise vimconn.VimConnException( - "Cannot create floating_ip automatically since " - "multiple external networks are present", - http_code=vimconn.HTTP_Conflict, - ) - - pool_id = external_nets[0].get("id") - - param = { - "floatingip": { - "floating_network_id": pool_id, - "tenant_id": server.tenant_id, - } - } - - try: - # self.logger.debug("Creating floating IP") - new_floating_ip = self.neutron.create_floatingip(param) - free_floating_ip = new_floating_ip["floatingip"]["id"] - created_items[ - "floating_ip:" + str(free_floating_ip) - ] = True - except Exception as e: - raise vimconn.VimConnException( - type(e).__name__ - + ": Cannot create new floating_ip " - + str(e), - http_code=vimconn.HTTP_Conflict, - ) - - try: - # for race condition ensure not already assigned - fip = self.neutron.show_floatingip(free_floating_ip) - - if fip["floatingip"]["port_id"]: - continue - - # the vim_id key contains the neutron.port_id - self.neutron.update_floatingip( - free_floating_ip, - {"floatingip": {"port_id": floating_network["vim_id"]}}, - ) - # for race condition ensure not re-assigned to other VM after 5 seconds - time.sleep(5) - fip = self.neutron.show_floatingip(free_floating_ip) - - if ( - fip["floatingip"]["port_id"] - != floating_network["vim_id"] - ): - self.logger.error( - "floating_ip {} re-assigned to other port".format( - free_floating_ip - ) - ) - continue - - self.logger.debug( - "Assigned floating_ip {} to VM {}".format( - free_floating_ip, server.id - ) - ) - assigned = True - except Exception as e: - # openstack need some time after VM creation to assign an IP. So retry if fails - vm_status = self.nova.servers.get(server.id).status - - if vm_status not in ("ACTIVE", "ERROR"): - if time.time() - vm_start_time < server_timeout: - time.sleep(5) - continue - elif floating_ip_retries > 0: - floating_ip_retries -= 1 - continue - - raise vimconn.VimConnException( - "Cannot create floating_ip: {} {}".format( - type(e).__name__, e - ), - http_code=vimconn.HTTP_Conflict, - ) - - except Exception as e: - if not floating_network["exit_on_floating_ip_error"]: - self.logger.error("Cannot create floating_ip. %s", str(e)) - continue - - raise + self._prepare_external_network_for_vminstance( + external_network=external_network, + server=server, + created_items=created_items, + vm_start_time=vm_start_time, + ) return server.id, created_items - # except nvExceptions.NotFound as e: - # error_value=-vimconn.HTTP_Not_Found - # error_text= "vm instance %s not found" % vm_id - # except TypeError as e: - # raise vimconn.VimConnException(type(e).__name__ + ": "+ str(e), http_code=vimconn.HTTP_Bad_Request) except Exception as e: server_id = None @@ -2232,6 +2588,7 @@ class vimconnector(vimconn.VimConnector): try: self.delete_vminstance(server_id, created_items) + except Exception as e2: self.logger.error("new_vminstance rollback fail {}".format(e2)) diff --git a/releasenotes/notes/Refactor_openstack_new_vminstance-18ca76a74fd351cb.yaml b/releasenotes/notes/Refactor_openstack_new_vminstance-18ca76a74fd351cb.yaml new file mode 100644 index 00000000..dbc0320b --- /dev/null +++ b/releasenotes/notes/Refactor_openstack_new_vminstance-18ca76a74fd351cb.yaml @@ -0,0 +1,20 @@ +####################################################################################### +# Copyright ETSI Contributors and Others. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. +####################################################################################### +--- +other: + - | + Refactor openstack new_vminstance method