From 94469fc37911e4d7d04837d5a4f8b1e34c1de0fb Mon Sep 17 00:00:00 2001 From: rajarammallya Date: Mon, 23 Jan 2012 11:37:32 +0530 Subject: [PATCH] fixes bug 919158 , default ip blocks are now optional Change-Id: Ic15f18a7f56992afa214c667a84c834fe58566ea --- etc/melange/melange.conf.sample | 7 +++--- melange/ipam/models.py | 6 ++++- melange/tests/functional/test_cli.py | 3 +++ melange/tests/unit/test_ipam_models.py | 11 +++++++++ melange/tests/unit/test_ipam_service.py | 31 ++++++++++++++----------- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/etc/melange/melange.conf.sample b/etc/melange/melange.conf.sample index 132c2451..bc8ed1a3 100644 --- a/etc/melange/melange.conf.sample +++ b/etc/melange/melange.conf.sample @@ -33,10 +33,11 @@ db_api_implementation = "melange.db.sqlalchemy.api" # Path to the extensions api_extensions_path = melange/extensions -# Cidr used for auto creating private ip block in a network -default_cidr = 10.0.0.0/24 +# Cidr for auto creating first ip block in a network +# If unspecified, auto creating is turned off +# default_cidr = 10.0.0.0/24 -#IPV6 Generator Factory +#IPV6 Generator Factory, defaults to rfc2462 #ipv6_generator=melange.ipv6.tenant_based_generator.TenantBasedIpV6Generator #DNS info for a data_center diff --git a/melange/ipam/models.py b/melange/ipam/models.py index 4ccfb44d..3cd2a615 100644 --- a/melange/ipam/models.py +++ b/melange/ipam/models.py @@ -985,7 +985,11 @@ class Network(ModelBase): try: return cls.find_by(id=id, tenant_id=tenant_id) except ModelNotFoundError: - ip_block = IpBlock.create(cidr=config.Config.get('default_cidr'), + default_cidr = config.Config.get('default_cidr', None) + if not default_cidr: + raise ModelNotFoundError(_("Network %s not found") % id) + + ip_block = IpBlock.create(cidr=default_cidr, network_id=id, tenant_id=tenant_id, type=IpBlock.PRIVATE_TYPE) diff --git a/melange/tests/functional/test_cli.py b/melange/tests/functional/test_cli.py index 36988c80..b96758e1 100644 --- a/melange/tests/functional/test_cli.py +++ b/melange/tests/functional/test_cli.py @@ -470,6 +470,9 @@ class TestInterfaceCLI(tests.BaseTest): def test_create(self): factory_models.MacAddressRangeFactory() + factory_models.IpBlockFactory(network_id="network_id", + tenant_id="tenant_id") + exitcode, out, err = run("interface create vif_id=vif_id " "tenant_id=tenant_id " "device_id=device_id " diff --git a/melange/tests/unit/test_ipam_models.py b/melange/tests/unit/test_ipam_models.py index 9074ab09..4873d99b 100644 --- a/melange/tests/unit/test_ipam_models.py +++ b/melange/tests/unit/test_ipam_models.py @@ -1999,6 +1999,17 @@ class TestNetwork(tests.BaseTest): self.assertEqual(network.ip_blocks[0].network_id, '1') self.assertEqual(network.ip_blocks[0].type, 'private') + def test_find_or_create_raises_err_if_no_default_cidr_and_net_blocks(self): + noise_ip_block = factory_models.PublicIpBlockFactory(network_id="9999", + tenant_id="tnt") + + with unit.StubConfig(default_cidr=None): + self.assertRaisesExcMessage(models.ModelNotFoundError, + "Network 100 not found", + models.Network.find_or_create_by, + id="100", + tenant_id="tnt") + def test_allocate_ip_to_allocate_both_ipv4_and_ipv6_addresses(self): ipv4_block = factory_models.PublicIpBlockFactory(network_id="1", cidr="10.0.0.0/24", diff --git a/melange/tests/unit/test_ipam_service.py b/melange/tests/unit/test_ipam_service.py index 185fe344..35ced398 100644 --- a/melange/tests/unit/test_ipam_service.py +++ b/melange/tests/unit/test_ipam_service.py @@ -1966,6 +1966,9 @@ class TestInterfaceIpAllocationsController(ControllerTestBase): self.assertEqual(interface.virtual_interface_id, "123") def test_create_makes_network_owner_the_interface_owner_by_default(self): + network_1_block = factory_models.IpBlockFactory(tenant_id="tnt_id", + network_id="1") + path = "/ipam/tenants/tnt_id/networks/1/interfaces/123/ip_allocations" response = self.app.post_json(path) @@ -2049,14 +2052,15 @@ class TestInterfaceIpAllocationsController(ControllerTestBase): response.json['ip_addresses']) def test_create_when_network_not_found_creates_default_cidr_block(self): - response = self.app.post("/ipam/tenants/tnt_id/networks/1" - "/interfaces/123/ip_allocations") + with unit.StubConfig(default_cidr="10.0.0.0/24"): + response = self.app.post("/ipam/tenants/tnt_id/networks/1" + "/interfaces/123/ip_allocations") self.assertEqual(response.status_int, 201) ip_address_json = response.json['ip_addresses'][0] created_block = models.IpAddress.find(ip_address_json['id']).ip_block self.assertEqual(created_block.network_id, "1") - self.assertEqual(created_block.cidr, config.Config.get('default_cidr')) + self.assertEqual(created_block.cidr, "10.0.0.0/24") self.assertEqual(created_block.type, "private") self.assertEqual(created_block.tenant_id, "tnt_id") @@ -2217,20 +2221,21 @@ class TestInterfacesController(ControllerTestBase): self.assertEquals(ipv6_address.interface_id, created_interface.id) def test_create_when_network_not_found_creates_default_cidr_block(self): - self.app.post_json("/ipam/interfaces", - {'interface': { - 'id': "virt_iface", - 'device_id': "instance", - 'tenant_id': "tnt_id", - 'network': {'id': "net1"}, - } - }) - interface = models.Interface.find_by(virtual_interface_id='virt_iface') + with unit.StubConfig(default_cidr="10.0.0.0/24"): + self.app.post_json("/ipam/interfaces", + {'interface': { + 'id': "virt_iface", + 'device_id': "instance", + 'tenant_id': "tnt_id", + 'network': {'id': "net1"}, + } + }) + interface = models.Interface.find_by(virtual_interface_id='virt_iface') created_block = models.IpAddress.find_by( interface_id=interface.id).ip_block self.assertEqual(created_block.network_id, "net1") - self.assertEqual(created_block.cidr, config.Config.get('default_cidr')) + self.assertEqual(created_block.cidr, "10.0.0.0/24") self.assertEqual(created_block.type, "private") self.assertEqual(created_block.tenant_id, "tnt_id")