From 2c1c70ea58bb44bb525bb60a9c12d91f34f178b4 Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Wed, 7 Jul 2010 12:06:34 -0700 Subject: [PATCH 1/6] Capture signals from dnsmasq and use them to update network state. --- bin/dhcpleasor.py | 68 ++++++++++++++++++++++++++++++++++ nova/tests/fake_flags.py | 2 +- nova/tests/network_unittest.py | 34 +++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100755 bin/dhcpleasor.py diff --git a/bin/dhcpleasor.py b/bin/dhcpleasor.py new file mode 100755 index 00000000..07e63884 --- /dev/null +++ b/bin/dhcpleasor.py @@ -0,0 +1,68 @@ +#!/opt/local/bin/python + +# Copyright [2010] [Anso Labs, LLC] +# +# 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. +""" +dhcpleasor.py + +Handle lease database updates from DHCP servers. +""" + +import sys +import os +import logging +sys.path.append(os.path.abspath(os.path.join(__file__, "../../"))) + +logging.debug(sys.path) +import getopt +from os import environ +from nova.compute import network +from nova import flags +FLAGS = flags.FLAGS + + +def add_lease(mac, ip, hostname, interface): + pass + +def old_lease(mac, ip, hostname, interface): + pass + +def del_lease(mac, ip, hostname, interface): + # TODO - get net from interface instead + net = network.get_network_by_address(ip) + net.release_ip(ip) + +def init_leases(interface): + return "" + + +def main(argv=None): + if argv is None: + argv = sys.argv + interface = environ.get('DNSMASQ_INTERFACE', 'br0') + old_redis_db = FLAGS.redis_db + FLAGS.redis_db = int(environ.get('REDIS_DB', '0')) + action = argv[1] + if action in ['add','del','old']: + mac = argv[2] + ip = argv[3] + hostname = argv[4] + logging.debug("Called %s for mac %s with ip %s and hostname %s on interface %s" % (action, mac, ip, hostname, interface)) + globals()[action+'_lease'](mac, ip, hostname, interface) + else: + print init_leases(interface) + FLAGS.redis_db = old_redis_db + +if __name__ == "__main__": + sys.exit(main()) diff --git a/nova/tests/fake_flags.py b/nova/tests/fake_flags.py index d40172cf..3f5d9ff5 100644 --- a/nova/tests/fake_flags.py +++ b/nova/tests/fake_flags.py @@ -28,5 +28,5 @@ FLAGS.fake_rabbit = True FLAGS.fake_network = True FLAGS.fake_users = True #FLAGS.keeper_backend = 'sqlite' -FLAGS.datastore_path = ':memory:' +# FLAGS.datastore_path = ':memory:' FLAGS.verbose = True diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index f215c0b3..4c9f340c 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -18,6 +18,7 @@ # License for the specific language governing permissions and limitations # under the License. +import os import logging import unittest @@ -26,6 +27,7 @@ import IPy from nova import flags from nova import test +from nova import exception from nova.compute import network from nova.auth import users from nova import utils @@ -40,6 +42,7 @@ class NetworkTestCase(test.TrialTestCase): network_size=32) logging.getLogger().setLevel(logging.DEBUG) self.manager = users.UserManager.instance() + self.dnsmasq = FakeDNSMasq() try: self.manager.create_user('netuser', 'netuser', 'netuser') except: pass @@ -63,11 +66,23 @@ class NetworkTestCase(test.TrialTestCase): self.assertTrue(IPy.IP(address) in self.network.network) def test_allocate_deallocate_ip(self): + # Address should be allocated + # Then, simulate acquisition of the address + # Deallocate it, and wait for simulated ip release + # then confirm it's gone. address = network.allocate_ip( "netuser", "project0", utils.generate_mac()) logging.debug("Was allocated %s" % (address)) + net = network.get_project_network("project0", "default") self.assertEqual(True, address in self._get_project_addresses("project0")) + mac = utils.generate_mac() + hostname = "test-host" + self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) rv = network.deallocate_ip(address) + # Doesn't go away until it's dhcp released + self.assertEqual(True, address in self._get_project_addresses("project0")) + + self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, address in self._get_project_addresses("project0")) def test_range_allocation(self): @@ -122,3 +137,22 @@ class NetworkTestCase(test.TrialTestCase): for addr in network.get_project_network(project_id).list_addresses(): project_addresses.append(addr) return project_addresses + +def binpath(script): + return os.path.abspath(os.path.join(__file__, "../../../bin", script)) + +class FakeDNSMasq(object): + def issue_ip(self, mac, ip, hostname, interface): + cmd = "%s add %s %s %s" % (binpath('dhcpleasor.py'), mac, ip, hostname) + env = {'DNSMASQ_INTERFACE': interface, 'REDIS_DB' : '8'} + (out, err) = utils.execute(cmd, addl_env=env) + logging.debug(out) + logging.debug(err) + + def release_ip(self, mac, ip, hostname, interface): + cmd = "%s del %s %s %s" % (binpath('dhcpleasor.py'), mac, ip, hostname) + env = {'DNSMASQ_INTERFACE': interface, 'REDIS_DB' : '8'} + (out, err) = utils.execute(cmd, addl_env=env) + logging.debug(out) + logging.debug(err) + \ No newline at end of file From 2599f7dbd12f6def98547764d8b54617221d4a5b Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Wed, 7 Jul 2010 12:15:11 -0700 Subject: [PATCH 2/6] Got dhcpleasor working, with test ENV for testing, and rpc.cast for real world. --- bin/dhcpleasor.py | 36 ++++++++++++++++++++++-------- docs/conf.py | 2 +- nova/endpoint/cloud.py | 8 +++++++ nova/tests/network_unittest.py | 40 ++++++++++++++++++++++++++-------- 4 files changed, 67 insertions(+), 19 deletions(-) diff --git a/bin/dhcpleasor.py b/bin/dhcpleasor.py index 07e63884..63ed5242 100755 --- a/bin/dhcpleasor.py +++ b/bin/dhcpleasor.py @@ -27,32 +27,50 @@ sys.path.append(os.path.abspath(os.path.join(__file__, "../../"))) logging.debug(sys.path) import getopt from os import environ +from nova.compute import linux_net from nova.compute import network +from nova import rpc + from nova import flags FLAGS = flags.FLAGS def add_lease(mac, ip, hostname, interface): - pass + if FLAGS.fake_rabbit: + network.lease_ip(ip) + else: + rpc.cast(FLAGS.cloud_topic, {"method": "lease_ip", + "args" : {"address": ip}}) def old_lease(mac, ip, hostname, interface): - pass + logging.debug("Adopted old lease or got a change of mac/hostname") def del_lease(mac, ip, hostname, interface): - # TODO - get net from interface instead - net = network.get_network_by_address(ip) - net.release_ip(ip) + if FLAGS.fake_rabbit: + network.release_ip(ip) + else: + rpc.cast(FLAGS.cloud_topic, {"method": "release_ip", + "args" : {"address": ip}}) def init_leases(interface): - return "" + net = network.get_network_by_interface(interface) + res = "" + for host_name in net.hosts: + res += "%s\n" % linux_net.hostDHCP(net, host_name, net.hosts[host_name]) + return res def main(argv=None): if argv is None: argv = sys.argv interface = environ.get('DNSMASQ_INTERFACE', 'br0') - old_redis_db = FLAGS.redis_db - FLAGS.redis_db = int(environ.get('REDIS_DB', '0')) + if int(environ.get('TESTING', '0')): + FLAGS.fake_rabbit = True + FLAGS.redis_db = 8 + FLAGS.network_size = 32 + FLAGS.fake_libvirt=True + FLAGS.fake_network=True + FLAGS.fake_users = True action = argv[1] if action in ['add','del','old']: mac = argv[2] @@ -62,7 +80,7 @@ def main(argv=None): globals()[action+'_lease'](mac, ip, hostname, interface) else: print init_leases(interface) - FLAGS.redis_db = old_redis_db + exit(0) if __name__ == "__main__": sys.exit(main()) diff --git a/docs/conf.py b/docs/conf.py index 9dfdfc8b..bc61f438 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,7 +16,7 @@ import sys, os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -#sys.path.append(os.path.abspath('.')) +sys.path.append(os.path.abspath('/Users/jmckenty/Projects/cc')) sys.path.append([os.path.abspath('../nova'),os.path.abspath('../'),os.path.abspath('../vendor')]) from nova import vendor diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 931c6c6e..269fb395 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -498,6 +498,14 @@ class CloudController(object): # TODO - Strip the IP from the instance return defer.succeed({'disassociateResponse': ["Address disassociated."]}) + def release_ip(self, context, private_ip, **kwargs): + self.network.release_ip(private_ip) + return defer.succeed({'releaseResponse': ["Address released."]}) + + def lease_ip(self, context, private_ip, **kwargs): + self.network.lease_ip(private_ip) + return defer.succeed({'leaseResponse': ["Address lease."]}) + @rbac.allow('projectmanager', 'sysadmin') def run_instances(self, context, **kwargs): # make sure user can access the image diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 4c9f340c..8cbc2b7c 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -21,6 +21,7 @@ import os import logging import unittest +import time from nova import vendor import IPy @@ -86,18 +87,36 @@ class NetworkTestCase(test.TrialTestCase): self.assertEqual(False, address in self._get_project_addresses("project0")) def test_range_allocation(self): + mac = utils.generate_mac() + secondmac = utils.generate_mac() + hostname = "test-host" address = network.allocate_ip( - "netuser", "project0", utils.generate_mac()) + "netuser", "project0", mac) secondaddress = network.allocate_ip( - "netuser", "project1", utils.generate_mac()) + "netuser", "project1", secondmac) + net = network.get_project_network("project0", "default") + secondnet = network.get_project_network("project1", "default") + self.assertEqual(True, address in self._get_project_addresses("project0")) self.assertEqual(True, secondaddress in self._get_project_addresses("project1")) self.assertEqual(False, address in self._get_project_addresses("project1")) + # Addresses are allocated before they're issued + self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) + self.dnsmasq.issue_ip(secondmac, secondaddress, + hostname, secondnet.bridge_name) + rv = network.deallocate_ip(address) + self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, address in self._get_project_addresses("project0")) + # First address release shouldn't affect the second + self.assertEqual(True, + secondaddress in self._get_project_addresses("project1")) + rv = network.deallocate_ip(secondaddress) + self.dnsmasq.release_ip(secondmac, secondaddress, + hostname, secondnet.bridge_name) self.assertEqual(False, secondaddress in self._get_project_addresses("project1")) @@ -127,9 +146,14 @@ class NetworkTestCase(test.TrialTestCase): for i in range(0, 30): name = 'toomany-project%s' % i self.manager.create_project(name, 'netuser', name) + net = network.get_project_network(name, "default") + mac = utils.generate_mac() + hostname = "toomany-hosts" address = network.allocate_ip( - "netuser", name, utils.generate_mac()) + "netuser", name, mac) + self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) rv = network.deallocate_ip(address) + self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.manager.delete_project(name) def _get_project_addresses(self, project_id): @@ -144,15 +168,13 @@ def binpath(script): class FakeDNSMasq(object): def issue_ip(self, mac, ip, hostname, interface): cmd = "%s add %s %s %s" % (binpath('dhcpleasor.py'), mac, ip, hostname) - env = {'DNSMASQ_INTERFACE': interface, 'REDIS_DB' : '8'} + env = {'DNSMASQ_INTERFACE': interface, 'TESTING' : '1'} (out, err) = utils.execute(cmd, addl_env=env) - logging.debug(out) - logging.debug(err) + logging.debug("ISSUE_IP: %s, %s " % (out, err)) def release_ip(self, mac, ip, hostname, interface): cmd = "%s del %s %s %s" % (binpath('dhcpleasor.py'), mac, ip, hostname) - env = {'DNSMASQ_INTERFACE': interface, 'REDIS_DB' : '8'} + env = {'DNSMASQ_INTERFACE': interface, 'TESTING' : '1'} (out, err) = utils.execute(cmd, addl_env=env) - logging.debug(out) - logging.debug(err) + logging.debug("RELEASE_IP: %s, %s " % (out, err)) \ No newline at end of file From 36b2a69a27bf6aea2cde7f18e4aac5251872ec08 Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Wed, 7 Jul 2010 12:15:11 -0700 Subject: [PATCH 3/6] Adding more tests, refactoring for dhcp logic. --- nova/tests/network_unittest.py | 108 +++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 8cbc2b7c..67ae7ad2 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -29,6 +29,7 @@ import IPy from nova import flags from nova import test from nova import exception +from nova.compute.exception import NoMoreAddresses from nova.compute import network from nova.auth import users from nova import utils @@ -67,24 +68,21 @@ class NetworkTestCase(test.TrialTestCase): self.assertTrue(IPy.IP(address) in self.network.network) def test_allocate_deallocate_ip(self): - # Address should be allocated - # Then, simulate acquisition of the address - # Deallocate it, and wait for simulated ip release - # then confirm it's gone. address = network.allocate_ip( "netuser", "project0", utils.generate_mac()) logging.debug("Was allocated %s" % (address)) net = network.get_project_network("project0", "default") - self.assertEqual(True, address in self._get_project_addresses("project0")) + self.assertEqual(True, is_in_project(address, "project0")) mac = utils.generate_mac() hostname = "test-host" self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) rv = network.deallocate_ip(address) + # Doesn't go away until it's dhcp released - self.assertEqual(True, address in self._get_project_addresses("project0")) + self.assertEqual(True, is_in_project(address, "project0")) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) - self.assertEqual(False, address in self._get_project_addresses("project0")) + self.assertEqual(False, is_in_project(address, "project0")) def test_range_allocation(self): mac = utils.generate_mac() @@ -97,11 +95,10 @@ class NetworkTestCase(test.TrialTestCase): net = network.get_project_network("project0", "default") secondnet = network.get_project_network("project1", "default") - self.assertEqual(True, - address in self._get_project_addresses("project0")) - self.assertEqual(True, - secondaddress in self._get_project_addresses("project1")) - self.assertEqual(False, address in self._get_project_addresses("project1")) + self.assertEqual(True, is_in_project(address, "project0")) + self.assertEqual(True, is_in_project(secondaddress, "project1")) + self.assertEqual(False, is_in_project(address, "project1")) + # Addresses are allocated before they're issued self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) self.dnsmasq.issue_ip(secondmac, secondaddress, @@ -109,58 +106,79 @@ class NetworkTestCase(test.TrialTestCase): rv = network.deallocate_ip(address) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) - self.assertEqual(False, address in self._get_project_addresses("project0")) + self.assertEqual(False, is_in_project(address, "project0")) + # First address release shouldn't affect the second - self.assertEqual(True, - secondaddress in self._get_project_addresses("project1")) + self.assertEqual(True, is_in_project(secondaddress, "project1")) rv = network.deallocate_ip(secondaddress) self.dnsmasq.release_ip(secondmac, secondaddress, hostname, secondnet.bridge_name) - self.assertEqual(False, - secondaddress in self._get_project_addresses("project1")) + self.assertEqual(False, is_in_project(secondaddress, "project1")) def test_subnet_edge(self): secondaddress = network.allocate_ip("netuser", "project0", utils.generate_mac()) + hostname = "toomany-hosts" for project in range(1,5): project_id = "project%s" % (project) + mac = utils.generate_mac() + mac2 = utils.generate_mac() + mac3 = utils.generate_mac() address = network.allocate_ip( - "netuser", project_id, utils.generate_mac()) + "netuser", project_id, mac) address2 = network.allocate_ip( - "netuser", project_id, utils.generate_mac()) + "netuser", project_id, mac2) address3 = network.allocate_ip( - "netuser", project_id, utils.generate_mac()) - self.assertEqual(False, - address in self._get_project_addresses("project0")) - self.assertEqual(False, - address2 in self._get_project_addresses("project0")) - self.assertEqual(False, - address3 in self._get_project_addresses("project0")) + "netuser", project_id, mac3) + self.assertEqual(False, is_in_project(address, "project0")) + self.assertEqual(False, is_in_project(address2, "project0")) + self.assertEqual(False, is_in_project(address3, "project0")) rv = network.deallocate_ip(address) rv = network.deallocate_ip(address2) rv = network.deallocate_ip(address3) - rv = network.deallocate_ip(secondaddress) - - def test_too_many_projects(self): - for i in range(0, 30): - name = 'toomany-project%s' % i - self.manager.create_project(name, 'netuser', name) - net = network.get_project_network(name, "default") - mac = utils.generate_mac() - hostname = "toomany-hosts" - address = network.allocate_ip( - "netuser", name, mac) - self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) - rv = network.deallocate_ip(address) + net = network.get_project_network(project_id, "default") self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) - self.manager.delete_project(name) + self.dnsmasq.release_ip(mac2, address2, hostname, net.bridge_name) + self.dnsmasq.release_ip(mac3, address3, hostname, net.bridge_name) + net = network.get_project_network("project0", "default") + rv = network.deallocate_ip(secondaddress) + self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) - def _get_project_addresses(self, project_id): - project_addresses = [] - for addr in network.get_project_network(project_id).list_addresses(): - project_addresses.append(addr) - return project_addresses + def test_release_before_deallocate(self): + pass + + def test_deallocate_before_issued(self): + pass + + def test_too_many_addresses(self): + """ + Network size is 32, there are 5 addresses reserved for VPN. + So we should get 24 usable addresses + """ + net = network.get_project_network("project0", "default") + hostname = "toomany-hosts" + macs = {} + addresses = {} + for i in range(0, 23): + macs[i] = utils.generate_mac() + addresses[i] = network.allocate_ip("netuser", "project0", macs[i]) + self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name) + + self.assertRaises(NoMoreAddresses, network.allocate_ip, "netuser", "project0", utils.generate_mac()) + + for i in range(0, 23): + rv = network.deallocate_ip(addresses[i]) + self.dnsmasq.release_ip(macs[i], addresses[i], hostname, net.bridge_name) + +def is_in_project(address, project_id): + return address in network.get_project_network(project_id).list_addresses() + +def _get_project_addresses(project_id): + project_addresses = [] + for addr in network.get_project_network(project_id).list_addresses(): + project_addresses.append(addr) + return project_addresses def binpath(script): return os.path.abspath(os.path.join(__file__, "../../../bin", script)) From 2cca4302f4f68339ef6bd0781b650caf102283c4 Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Wed, 7 Jul 2010 12:24:17 -0700 Subject: [PATCH 4/6] Off by one error in the allocation test (can someone check my subnet math?) --- nova/tests/network_unittest.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 67ae7ad2..4d79758f 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -21,7 +21,6 @@ import os import logging import unittest -import time from nova import vendor import IPy @@ -154,20 +153,20 @@ class NetworkTestCase(test.TrialTestCase): def test_too_many_addresses(self): """ Network size is 32, there are 5 addresses reserved for VPN. - So we should get 24 usable addresses + So we should get 23 usable addresses """ net = network.get_project_network("project0", "default") hostname = "toomany-hosts" macs = {} addresses = {} - for i in range(0, 23): + for i in range(0, 22): macs[i] = utils.generate_mac() addresses[i] = network.allocate_ip("netuser", "project0", macs[i]) self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name) self.assertRaises(NoMoreAddresses, network.allocate_ip, "netuser", "project0", utils.generate_mac()) - for i in range(0, 23): + for i in range(0, 22): rv = network.deallocate_ip(addresses[i]) self.dnsmasq.release_ip(macs[i], addresses[i], hostname, net.bridge_name) From fd2c2543b91f2296ab6403438d0684f9bfe87988 Mon Sep 17 00:00:00 2001 From: Joshua McKenty Date: Wed, 7 Jul 2010 12:24:22 -0700 Subject: [PATCH 5/6] Fixes as per Vish review (whitespace, import statements) --- nova/endpoint/cloud.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/endpoint/cloud.py b/nova/endpoint/cloud.py index 269fb395..a2f60b47 100644 --- a/nova/endpoint/cloud.py +++ b/nova/endpoint/cloud.py @@ -504,7 +504,7 @@ class CloudController(object): def lease_ip(self, context, private_ip, **kwargs): self.network.lease_ip(private_ip) - return defer.succeed({'leaseResponse': ["Address lease."]}) + return defer.succeed({'leaseResponse': ["Address leased."]}) @rbac.allow('projectmanager', 'sysadmin') def run_instances(self, context, **kwargs): From edf25418f7c3640735762c4492af21b075f47379 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Wed, 7 Jul 2010 12:25:22 -0700 Subject: [PATCH 6/6] whitespace fixes and header changes --- bin/dhcpleasor.py | 42 +++++++++++++++++++--------------- nova/tests/network_unittest.py | 36 ++++++++++++++--------------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/bin/dhcpleasor.py b/bin/dhcpleasor.py index 63ed5242..30f8fbdc 100755 --- a/bin/dhcpleasor.py +++ b/bin/dhcpleasor.py @@ -1,18 +1,24 @@ -#!/opt/local/bin/python +#!/usr/bin/env python +# vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright [2010] [Anso Labs, LLC] -# -# 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 -# +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# Copyright 2010 Anso Labs, LLC +# +# 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. +# 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. + """ dhcpleasor.py @@ -37,21 +43,21 @@ FLAGS = flags.FLAGS def add_lease(mac, ip, hostname, interface): if FLAGS.fake_rabbit: - network.lease_ip(ip) + network.lease_ip(ip) else: rpc.cast(FLAGS.cloud_topic, {"method": "lease_ip", "args" : {"address": ip}}) def old_lease(mac, ip, hostname, interface): logging.debug("Adopted old lease or got a change of mac/hostname") - + def del_lease(mac, ip, hostname, interface): if FLAGS.fake_rabbit: - network.release_ip(ip) + network.release_ip(ip) else: rpc.cast(FLAGS.cloud_topic, {"method": "release_ip", - "args" : {"address": ip}}) - + "args" : {"address": ip}}) + def init_leases(interface): net = network.get_network_by_interface(interface) res = "" diff --git a/nova/tests/network_unittest.py b/nova/tests/network_unittest.py index 4d79758f..bccaacfa 100644 --- a/nova/tests/network_unittest.py +++ b/nova/tests/network_unittest.py @@ -76,10 +76,10 @@ class NetworkTestCase(test.TrialTestCase): hostname = "test-host" self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) rv = network.deallocate_ip(address) - + # Doesn't go away until it's dhcp released self.assertEqual(True, is_in_project(address, "project0")) - + self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, is_in_project(address, "project0")) @@ -93,25 +93,25 @@ class NetworkTestCase(test.TrialTestCase): "netuser", "project1", secondmac) net = network.get_project_network("project0", "default") secondnet = network.get_project_network("project1", "default") - + self.assertEqual(True, is_in_project(address, "project0")) self.assertEqual(True, is_in_project(secondaddress, "project1")) self.assertEqual(False, is_in_project(address, "project1")) - + # Addresses are allocated before they're issued self.dnsmasq.issue_ip(mac, address, hostname, net.bridge_name) - self.dnsmasq.issue_ip(secondmac, secondaddress, + self.dnsmasq.issue_ip(secondmac, secondaddress, hostname, secondnet.bridge_name) - + rv = network.deallocate_ip(address) self.dnsmasq.release_ip(mac, address, hostname, net.bridge_name) self.assertEqual(False, is_in_project(address, "project0")) - + # First address release shouldn't affect the second self.assertEqual(True, is_in_project(secondaddress, "project1")) - + rv = network.deallocate_ip(secondaddress) - self.dnsmasq.release_ip(secondmac, secondaddress, + self.dnsmasq.release_ip(secondmac, secondaddress, hostname, secondnet.bridge_name) self.assertEqual(False, is_in_project(secondaddress, "project1")) @@ -146,15 +146,15 @@ class NetworkTestCase(test.TrialTestCase): def test_release_before_deallocate(self): pass - + def test_deallocate_before_issued(self): pass - - def test_too_many_addresses(self): + + def test_too_many_addresses(self): """ Network size is 32, there are 5 addresses reserved for VPN. So we should get 23 usable addresses - """ + """ net = network.get_project_network("project0", "default") hostname = "toomany-hosts" macs = {} @@ -163,10 +163,10 @@ class NetworkTestCase(test.TrialTestCase): macs[i] = utils.generate_mac() addresses[i] = network.allocate_ip("netuser", "project0", macs[i]) self.dnsmasq.issue_ip(macs[i], addresses[i], hostname, net.bridge_name) - + self.assertRaises(NoMoreAddresses, network.allocate_ip, "netuser", "project0", utils.generate_mac()) - - for i in range(0, 22): + + for i in range(0, 22): rv = network.deallocate_ip(addresses[i]) self.dnsmasq.release_ip(macs[i], addresses[i], hostname, net.bridge_name) @@ -188,10 +188,10 @@ class FakeDNSMasq(object): env = {'DNSMASQ_INTERFACE': interface, 'TESTING' : '1'} (out, err) = utils.execute(cmd, addl_env=env) logging.debug("ISSUE_IP: %s, %s " % (out, err)) - + def release_ip(self, mac, ip, hostname, interface): cmd = "%s del %s %s %s" % (binpath('dhcpleasor.py'), mac, ip, hostname) env = {'DNSMASQ_INTERFACE': interface, 'TESTING' : '1'} (out, err) = utils.execute(cmd, addl_env=env) logging.debug("RELEASE_IP: %s, %s " % (out, err)) - \ No newline at end of file +