return network_req.get_response(self.api)
def _create_subnet(self, fmt, tenant_id, net_id, gateway_ip, cidr,
- allocation_pools=None, ip_version=4):
+ allocation_pools=None, ip_version=4, enable_dhcp=True):
data = {'subnet': {'tenant_id': tenant_id,
'network_id': net_id,
'cidr': cidr,
'ip_version': ip_version,
- 'tenant_id': self._tenant_id}}
+ 'tenant_id': self._tenant_id,
+ 'enable_dhcp': enable_dhcp}}
if gateway_ip:
data['subnet']['gateway_ip'] = gateway_ip
if allocation_pools:
return port_req.get_response(self.api)
def _make_subnet(self, fmt, network, gateway, cidr,
- allocation_pools=None, ip_version=4):
+ allocation_pools=None, ip_version=4, enable_dhcp=True):
res = self._create_subnet(fmt,
network['network']['tenant_id'],
network['network']['id'],
gateway,
cidr,
allocation_pools=allocation_pools,
- ip_version=ip_version)
+ ip_version=ip_version,
+ enable_dhcp=enable_dhcp)
# Things can go wrong - raise HTTP exc with res code only
# so it can be caught by unit tests
if res.status_int >= 400:
cidr='10.0.0.0/24',
fmt='json',
ip_version=4,
- allocation_pools=None):
+ allocation_pools=None,
+ enable_dhcp=True):
# TODO(anyone) DRY this
# NOTE(salvatore-orlando): we can pass the network object
# to gen function anyway, and then avoid the repetition
gateway_ip,
cidr,
allocation_pools,
- ip_version)
+ ip_version,
+ enable_dhcp)
yield subnet
self._delete('subnets', subnet['subnet']['id'])
else:
gateway_ip,
cidr,
allocation_pools,
- ip_version)
+ ip_version,
+ enable_dhcp)
yield subnet
self._delete('subnets', subnet['subnet']['id'])
keys = kwargs.copy()
keys.setdefault('cidr', '10.0.0.0/24')
keys.setdefault('ip_version', 4)
+ keys.setdefault('enable_dhcp', True)
with self.subnet(network=network, **keys) as subnet:
# verify the response has each key with the correct value
for k in keys:
cidr = '10.0.0.0/24'
allocation_pools = [{'start': '10.0.0.2',
'end': '10.0.0.254'}]
+ enable_dhcp = True
subnet = self._test_create_subnet()
# verify cidr & gw have been correctly generated
self.assertEquals(subnet['subnet']['cidr'], cidr)
self.assertEquals(subnet['subnet']['gateway_ip'], gateway)
+ self.assertEquals(subnet['subnet']['enable_dhcp'], enable_dhcp)
self.assertEquals(subnet['subnet']['allocation_pools'],
allocation_pools)
cidr=cidr,
allocation_pools=allocation_pools)
+ def test_create_subnet_with_dhcp_disabled(self):
+ enable_dhcp = False
+ self._test_create_subnet(enable_dhcp=enable_dhcp)
+
def test_create_subnet_gateway_in_allocation_pool_returns_409(self):
gateway_ip = '10.0.0.50'
cidr = '10.0.0.0/24'
self.dhcp.daemon_loop()
def test_daemon_loop_completes_single_pass(self):
+ self.dhcp._network_dhcp_enable = mock.Mock(return_value=True)
with mock.patch.object(self.dhcp, 'get_network_state_delta') as state:
with mock.patch.object(self.dhcp, 'call_driver') as call_driver:
with mock.patch('quantum.agent.dhcp_agent.time') as time:
mock.call('reload_allocations', 'updated_net'),
mock.call('disable', 'deleted_net')])
- def test_state_builder(self):
- fake_subnet = [
- FakeModel(1, network_id=1),
- FakeModel(2, network_id=2),
+ def test_state_builder_network_admin_down(self):
+ fake_network1 = FakeModel(1, admin_state_up=True)
+ fake_network2 = FakeModel(2, admin_state_up=False)
+
+ fake_subnet1 = FakeModel(1, network_id=1, enable_dhcp=True)
+ fake_subnet2 = FakeModel(2, network_id=2, enable_dhcp=True)
+ fake_subnet3 = FakeModel(3, network_id=2, enable_dhcp=True)
+
+ fake_network1.subnets = [fake_subnet1]
+ fake_network2.subnets = [fake_subnet2, fake_subnet3]
+
+ fake_subnet1.network = fake_network1
+ fake_subnet2.network = fake_network2
+ fake_subnet3.network = fake_network2
+
+ fake_allocation = [
+ FakeModel(2, subnet_id=1),
+ FakeModel(3, subnet_id=2)
]
+ fake_subnets = [fake_subnet1, fake_subnet2, fake_subnet3]
+ fake_networks = [fake_network1, fake_network2]
+
+ db = mock.Mock()
+ db.subnets.all = mock.Mock(return_value=fake_subnets)
+ db.networks.all = mock.Mock(return_value=fake_networks)
+ db.ipallocations.all = mock.Mock(return_value=fake_allocation)
+ self.dhcp.db = db
+ state = self.dhcp._state_builder()
+
+ self.assertEquals(state.networks, set([1]))
+
+ expected_subnets = set([
+ (hash(str(fake_subnets[0])), 1),
+ ])
+ self.assertEquals(state.subnet_hashes, expected_subnets)
+
+ expected_ipalloc = set([
+ (hash(str(fake_allocation[0])), 1),
+ ])
+ self.assertEquals(state.ipalloc_hashes, expected_ipalloc)
+
+ def test_state_builder_network_dhcp_partial_disable(self):
+ fake_network1 = FakeModel(1, admin_state_up=True)
+ fake_network2 = FakeModel(2, admin_state_up=True)
+
+ fake_subnet1 = FakeModel(1, network_id=1, enable_dhcp=True)
+ fake_subnet2 = FakeModel(2, network_id=2, enable_dhcp=False)
+ fake_subnet3 = FakeModel(3, network_id=2, enable_dhcp=True)
+
+ fake_network1.subnets = [fake_subnet1]
+ fake_network2.subnets = [fake_subnet2, fake_subnet3]
+
+ fake_subnet1.network = fake_network1
+ fake_subnet2.network = fake_network2
+ fake_subnet3.network = fake_network2
+
fake_allocation = [
- FakeModel(2, subnet_id=1)
+ FakeModel(2, subnet_id=1),
+ FakeModel(3, subnet_id=2),
+ FakeModel(4, subnet_id=3),
]
+ fake_subnets = [fake_subnet1, fake_subnet2, fake_subnet3]
+ fake_networks = [fake_network1, fake_network2]
+
db = mock.Mock()
- db.subnets.all = mock.Mock(return_value=fake_subnet)
+ db.subnets.all = mock.Mock(return_value=fake_subnets)
+ db.networks.all = mock.Mock(return_value=fake_networks)
db.ipallocations.all = mock.Mock(return_value=fake_allocation)
self.dhcp.db = db
state = self.dhcp._state_builder()
self.assertEquals(state.networks, set([1, 2]))
expected_subnets = set([
- (hash(str(fake_subnet[0])), 1),
- (hash(str(fake_subnet[1])), 2)
+ (hash(str(fake_subnets[0])), 1),
+ (hash(str(fake_subnets[2])), 2),
+ ])
+ self.assertEquals(state.subnet_hashes, expected_subnets)
+
+ expected_ipalloc = set([
+ (hash(str(fake_allocation[0])), 1),
+ (hash(str(fake_allocation[2])), 2),
+ ])
+ self.assertEquals(state.ipalloc_hashes, expected_ipalloc)
+
+ def test_state_builder_network_dhcp_all_disable(self):
+ fake_network1 = FakeModel(1, admin_state_up=True)
+ fake_network2 = FakeModel(2, admin_state_up=True)
+
+ fake_subnet1 = FakeModel(1, network_id=1, enable_dhcp=True)
+ fake_subnet2 = FakeModel(2, network_id=2, enable_dhcp=False)
+ fake_subnet3 = FakeModel(3, network_id=2, enable_dhcp=False)
+
+ fake_network1.subnets = [fake_subnet1]
+ fake_network2.subnets = [fake_subnet2, fake_subnet3]
+
+ fake_subnet1.network = fake_network1
+ fake_subnet2.network = fake_network2
+ fake_subnet3.network = fake_network2
+
+ fake_allocation = [
+ FakeModel(2, subnet_id=1),
+ FakeModel(3, subnet_id=2),
+ FakeModel(4, subnet_id=3),
+ ]
+
+ fake_subnets = [fake_subnet1, fake_subnet2, fake_subnet3]
+ fake_networks = [fake_network1, fake_network2]
+
+ db = mock.Mock()
+ db.subnets.all = mock.Mock(return_value=fake_subnets)
+ db.networks.all = mock.Mock(return_value=fake_networks)
+ db.ipallocations.all = mock.Mock(return_value=fake_allocation)
+ self.dhcp.db = db
+ state = self.dhcp._state_builder()
+
+ self.assertEquals(state.networks, set([1]))
+
+ expected_subnets = set([
+ (hash(str(fake_subnets[0])), 1)
+ ])
+ self.assertEquals(state.subnet_hashes, expected_subnets)
+
+ expected_ipalloc = set([
+ (hash(str(fake_allocation[0])), 1)
+ ])
+ self.assertEquals(state.ipalloc_hashes, expected_ipalloc)
+
+ def test_state_builder_mixed(self):
+ fake_network1 = FakeModel(1, admin_state_up=True)
+ fake_network2 = FakeModel(2, admin_state_up=True)
+ fake_network3 = FakeModel(3, admin_state_up=False)
+
+ fake_subnet1 = FakeModel(1, network_id=1, enable_dhcp=True)
+ fake_subnet2 = FakeModel(2, network_id=2, enable_dhcp=False)
+ fake_subnet3 = FakeModel(3, network_id=3, enable_dhcp=True)
+
+ fake_network1.subnets = [fake_subnet1]
+ fake_network2.subnets = [fake_subnet2]
+ fake_network3.subnets = [fake_subnet3]
+
+ fake_subnet1.network = fake_network1
+ fake_subnet2.network = fake_network2
+ fake_subnet3.network = fake_network3
+
+ fake_allocation = [
+ FakeModel(2, subnet_id=1)
+ ]
+
+ fake_subnets = [fake_subnet1, fake_subnet2, fake_subnet3]
+ fake_networks = [fake_network1, fake_network2, fake_network3]
+
+ db = mock.Mock()
+ db.subnets.all = mock.Mock(return_value=fake_subnets)
+ db.networks.all = mock.Mock(return_value=fake_networks)
+ db.ipallocations.all = mock.Mock(return_value=fake_allocation)
+ self.dhcp.db = db
+ state = self.dhcp._state_builder()
+
+ self.assertEquals(state.networks, set([1]))
+
+ expected_subnets = set([
+ (hash(str(fake_subnets[0])), 1),
])
self.assertEquals(state.subnet_hashes, expected_subnets)
return self.dhcp.get_network_state_delta()
def test_get_network_state_fresh(self):
- new_state = dhcp_agent.State(set([1]), set([(3, 1)]), set([(11, 1)]))
+ new_state = dhcp_agent.State(set([1]), set([(3, 1)]),
+ set([(11, 1)]))
delta = self._network_state_helper(self.dhcp.prev_state, new_state)
self.assertEqual(delta,