From d37ec7878e5fdc8f00ecf321d57083f0506a3120 Mon Sep 17 00:00:00 2001 From: micafer Date: Thu, 24 Mar 2022 10:27:53 +0100 Subject: [PATCH] Use PRIVATE_NET_MASKS in Azure conn --- IM/connectors/Azure.py | 11 +++++--- test/unit/connectors/Azure.py | 49 +++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/IM/connectors/Azure.py b/IM/connectors/Azure.py index 0babbd9a6..3d4307a9a 100644 --- a/IM/connectors/Azure.py +++ b/IM/connectors/Azure.py @@ -422,10 +422,9 @@ def create_nics(self, radl, credentials, subscription_id, group_name, subnets, n hasPublicIP = False hasPrivateIP = False pub_network_name = None + publicAdded = False while system.getValue("net_interface." + str(i) + ".connection"): network_name = system.getValue("net_interface." + str(i) + ".connection") - # TODO: check how to do that - # fixed_ip = system.getValue("net_interface." + str(i) + ".ip") network = radl.get_network_by_id(network_name) if network.isPublic(): @@ -434,11 +433,17 @@ def create_nics(self, radl, credentials, subscription_id, group_name, subnets, n else: hasPrivateIP = True + if not publicAdded and network_name in subnets: + subnet_network_mask = IPNetwork(subnets[network_name].address_prefix) + is_private = any([IPAddress(subnet_network_mask.ip) in IPNetwork(mask) + for mask in Config.PRIVATE_NET_MASKS]) + if not is_private: + publicAdded = True + i += 1 i = 0 res = [] - publicAdded = False while system.getValue("net_interface." + str(i) + ".connection"): network_name = system.getValue("net_interface." + str(i) + ".connection") fixed_ip = system.getValue("net_interface." + str(i) + ".ip") diff --git a/test/unit/connectors/Azure.py b/test/unit/connectors/Azure.py index 279478b5c..6bf5ad87c 100755 --- a/test/unit/connectors/Azure.py +++ b/test/unit/connectors/Azure.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from distutils.command.config import config import sys import unittest @@ -30,6 +31,7 @@ from IM.connectors.Azure import AzureCloudConnector from azure.core.exceptions import ResourceNotFoundError from mock import patch, MagicMock, call +from IM.config import Config class TestAzureConnector(TestCloudConnectorBase): @@ -165,6 +167,7 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, subnet_create = MagicMock() subnet_create_res = MagicMock() subnet_create_res.id = "subnet-0" + subnet_create_res.address_prefix = "10.0.1.0/24" subnet_create.result.return_value = subnet_create_res nclient.subnets.begin_create_or_update.return_value = subnet_create nclient.subnets.get.side_effect = ResourceNotFoundError() @@ -217,19 +220,19 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, self.assertTrue(res[0][0]) self.assertTrue(res[1][0]) self.assertTrue(res[2][0]) - self.assertEquals(nclient.network_interfaces.begin_delete.call_count, 1) + self.assertEqual(nclient.network_interfaces.begin_delete.call_count, 1) self.assertIn("nic_name", nclient.network_interfaces.begin_delete.call_args_list[0][0][1]) json_vm_req = cclient.virtual_machines.begin_create_or_update.call_args_list[0][0][2] - self.assertEquals(json_vm_req['storage_profile']['data_disks'][0]['disk_size_gb'], 1) - self.assertEquals(json_vm_req['storage_profile']['data_disks'][1]['managed_disk']['id'], "did") + self.assertEqual(json_vm_req['storage_profile']['data_disks'][0]['disk_size_gb'], 1) + self.assertEqual(json_vm_req['storage_profile']['data_disks'][1]['managed_disk']['id'], "did") image_res = {'sku': '16.04.0-LTS', 'publisher': 'Canonical', 'version': 'latest', 'offer': 'UbuntuServer'} - self.assertEquals(json_vm_req['storage_profile']['image_reference'], image_res) - self.assertEquals(json_vm_req['hardware_profile']['vm_size'], 'Standard_A1') - self.assertEquals(json_vm_req['os_profile']['admin_username'], 'user') - self.assertEquals(json_vm_req['os_profile']['admin_password'], 'pass') - self.assertEquals(json_vm_req['os_profile']['admin_password'], 'pass') - self.assertEquals(nclient.subnets.begin_create_or_update.call_args_list[0][0][3]['address_prefix'], + self.assertEqual(json_vm_req['storage_profile']['image_reference'], image_res) + self.assertEqual(json_vm_req['hardware_profile']['vm_size'], 'Standard_A1') + self.assertEqual(json_vm_req['os_profile']['admin_username'], 'user') + self.assertEqual(json_vm_req['os_profile']['admin_password'], 'pass') + self.assertEqual(json_vm_req['os_profile']['admin_password'], 'pass') + self.assertEqual(nclient.subnets.begin_create_or_update.call_args_list[0][0][3]['address_prefix'], '10.0.1.0/24') radl_data = """ @@ -252,7 +255,7 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, radl.check() with self.assertRaises(Exception) as ex: azure_cloud.launch(inf, radl, radl, 1, auth) - self.assertEquals(str(ex.exception), "Incorrect image url: it must be snapshot or disk.") + self.assertEqual(str(ex.exception), "Incorrect image url: it must be snapshot or disk.") radl_data = """ network net1 (outbound = 'yes') @@ -274,8 +277,8 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, radl.check() res = azure_cloud.launch(inf, radl, radl, 1, auth) json_vm_req = cclient.virtual_machines.begin_create_or_update.call_args_list[5][0][2] - self.assertEquals(json_vm_req['storage_profile']['os_disk']['os_type'], 'linux') - self.assertEquals(nclient.subnets.begin_create_or_update.call_args_list[2][0][3]['address_prefix'], + self.assertEqual(json_vm_req['storage_profile']['os_disk']['os_type'], 'linux') + self.assertEqual(nclient.subnets.begin_create_or_update.call_args_list[2][0][3]['address_prefix'], '192.168.1.0/24') radl_data = """ @@ -304,10 +307,18 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, subnet_create.address_prefix = "10.0.1.0/24" nclient.subnets.get.return_value = subnet_create res = azure_cloud.launch(inf, radl, radl, 1, auth) - self.assertEquals(nclient.subnets.get.call_args_list[3][0][1], 'vnet') - self.assertEquals(nclient.subnets.get.call_args_list[3][0][2], 'subnet1') - self.assertEquals(nclient.subnets.begin_create_or_update.call_count, 3) - self.assertEquals(nclient.virtual_networks.begin_create_or_update.call_count, 3) + self.assertEqual(nclient.subnets.get.call_args_list[3][0][1], 'vnet') + self.assertEqual(nclient.subnets.get.call_args_list[3][0][2], 'subnet1') + self.assertEqual(nclient.subnets.begin_create_or_update.call_count, 3) + self.assertEqual(nclient.virtual_networks.begin_create_or_update.call_count, 3) + self.assertEqual(nclient.public_ip_addresses.begin_create_or_update.call_count, 7) + + old_priv = Config.PRIVATE_NET_MASKS + Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"] + res = azure_cloud.launch(inf, radl, radl, 1, auth) + Config.PRIVATE_NET_MASKS = old_priv + # Check that public ip is not created + self.assertEqual(nclient.public_ip_addresses.begin_create_or_update.call_count, 7) @patch('IM.connectors.Azure.NetworkManagementClient') @patch('IM.connectors.Azure.ComputeManagementClient') @@ -393,9 +404,9 @@ def test_30_updateVMInfo(self, credentials, dns_client, compute_client, network_ success, vm = azure_cloud.updateVMInfo(vm, auth) self.assertTrue(success, msg="ERROR: updating VM info.") - self.assertEquals(dclient.zones.create_or_update.call_args_list, + self.assertEqual(dclient.zones.create_or_update.call_args_list, [call('rg0', 'domain.com', {'location': 'global'})]) - self.assertEquals(dclient.record_sets.create_or_update.call_args_list, + self.assertEqual(dclient.record_sets.create_or_update.call_args_list, [call('rg0', 'domain.com', 'test', 'A', {'arecords': [{'ipv4_address': '13.0.0.1'}], 'ttl': 300})]) self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) @@ -564,7 +575,7 @@ def test_60_finalize(self, credentials, resource_client, compute_client): self.assertTrue(success, msg="ERROR: finalizing VM info.") self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue()) - self.assertEquals(cclient.virtual_machines.begin_delete.call_count, 2) + self.assertEqual(cclient.virtual_machines.begin_delete.call_count, 2) self.assertEqual(cclient.virtual_machines.begin_delete.call_args_list[0][0], ('rg0', 'vm0')) self.assertEqual(rclient.resource_groups.begin_delete.call_count, 1) self.assertEqual(rclient.resource_groups.begin_delete.call_args_list[0][0], ('rg0',))