Bugfix for lp904932

Allow stubbing of "open" via a helper function

Change-Id: I1bf32c00ba3e565b2c895d8b7e11cbda87cd3597
This commit is contained in:
Jason Kölker
2011-12-15 15:41:32 -06:00
parent 2348edd892
commit 384d438a59

View File

@@ -1,462 +1,495 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 NTT # Copyright 2011 NTT
# All Rights Reserved. # All Rights Reserved.
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # 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 # not use this file except in compliance with the License. You may obtain
# a copy of the License at # a copy of the License at
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import mox import os
from nova import db import mox
from nova import flags
from nova import log as logging from nova import db
from nova import test from nova import flags
from nova import utils from nova import log as logging
from nova.network import linux_net from nova import test
from nova import utils
from nova.network import linux_net
FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.network') FLAGS = flags.FLAGS
LOG = logging.getLogger('nova.tests.network')
HOST = "testhost"
instances = [{'id': 0, HOST = "testhost"
'host': 'fake_instance00',
'hostname': 'fake_instance00'}, instances = [{'id': 0,
{'id': 1, 'host': 'fake_instance00',
'host': 'fake_instance01', 'hostname': 'fake_instance00'},
'hostname': 'fake_instance01'}] {'id': 1,
'host': 'fake_instance01',
'hostname': 'fake_instance01'}]
addresses = [{"address": "10.0.0.1"},
{"address": "10.0.0.2"},
{"address": "10.0.0.3"}, addresses = [{"address": "10.0.0.1"},
{"address": "10.0.0.4"}, {"address": "10.0.0.2"},
{"address": "10.0.0.5"}, {"address": "10.0.0.3"},
{"address": "10.0.0.6"}] {"address": "10.0.0.4"},
{"address": "10.0.0.5"},
{"address": "10.0.0.6"}]
networks = [{'id': 0,
'uuid': "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
'label': 'test0', networks = [{'id': 0,
'injected': False, 'uuid': "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
'multi_host': False, 'label': 'test0',
'cidr': '192.168.0.0/24', 'injected': False,
'cidr_v6': '2001:db8::/64', 'multi_host': False,
'gateway_v6': '2001:db8::1', 'cidr': '192.168.0.0/24',
'netmask_v6': '64', 'cidr_v6': '2001:db8::/64',
'netmask': '255.255.255.0', 'gateway_v6': '2001:db8::1',
'bridge': 'fa0', 'netmask_v6': '64',
'bridge_interface': 'fake_fa0', 'netmask': '255.255.255.0',
'gateway': '192.168.0.1', 'bridge': 'fa0',
'broadcast': '192.168.0.255', 'bridge_interface': 'fake_fa0',
'dns1': '192.168.0.1', 'gateway': '192.168.0.1',
'dns2': '192.168.0.2', 'broadcast': '192.168.0.255',
'dhcp_server': '0.0.0.0', 'dns1': '192.168.0.1',
'dhcp_start': '192.168.100.1', 'dns2': '192.168.0.2',
'vlan': None, 'dhcp_server': '0.0.0.0',
'host': None, 'dhcp_start': '192.168.100.1',
'project_id': 'fake_project', 'vlan': None,
'vpn_public_address': '192.168.0.2'}, 'host': None,
{'id': 1, 'project_id': 'fake_project',
'uuid': "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", 'vpn_public_address': '192.168.0.2'},
'label': 'test1', {'id': 1,
'injected': False, 'uuid': "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
'multi_host': False, 'label': 'test1',
'cidr': '192.168.1.0/24', 'injected': False,
'cidr_v6': '2001:db9::/64', 'multi_host': False,
'gateway_v6': '2001:db9::1', 'cidr': '192.168.1.0/24',
'netmask_v6': '64', 'cidr_v6': '2001:db9::/64',
'netmask': '255.255.255.0', 'gateway_v6': '2001:db9::1',
'bridge': 'fa1', 'netmask_v6': '64',
'bridge_interface': 'fake_fa1', 'netmask': '255.255.255.0',
'gateway': '192.168.1.1', 'bridge': 'fa1',
'broadcast': '192.168.1.255', 'bridge_interface': 'fake_fa1',
'dns1': '192.168.0.1', 'gateway': '192.168.1.1',
'dns2': '192.168.0.2', 'broadcast': '192.168.1.255',
'dhcp_server': '0.0.0.0', 'dns1': '192.168.0.1',
'dhcp_start': '192.168.100.1', 'dns2': '192.168.0.2',
'vlan': None, 'dhcp_server': '0.0.0.0',
'host': None, 'dhcp_start': '192.168.100.1',
'project_id': 'fake_project', 'vlan': None,
'vpn_public_address': '192.168.1.2'}] 'host': None,
'project_id': 'fake_project',
'vpn_public_address': '192.168.1.2'}]
fixed_ips = [{'id': 0,
'network_id': 0,
'address': '192.168.0.100', fixed_ips = [{'id': 0,
'instance_id': 0, 'network_id': 0,
'allocated': True, 'address': '192.168.0.100',
'virtual_interface_id': 0, 'instance_id': 0,
'virtual_interface': addresses[0], 'allocated': True,
'instance': instances[0], 'virtual_interface_id': 0,
'floating_ips': []}, 'virtual_interface': addresses[0],
{'id': 1, 'instance': instances[0],
'network_id': 1, 'floating_ips': []},
'address': '192.168.1.100', {'id': 1,
'instance_id': 0, 'network_id': 1,
'allocated': True, 'address': '192.168.1.100',
'virtual_interface_id': 1, 'instance_id': 0,
'virtual_interface': addresses[1], 'allocated': True,
'instance': instances[0], 'virtual_interface_id': 1,
'floating_ips': []}, 'virtual_interface': addresses[1],
{'id': 2, 'instance': instances[0],
'network_id': 1, 'floating_ips': []},
'address': '192.168.0.101', {'id': 2,
'instance_id': 1, 'network_id': 1,
'allocated': True, 'address': '192.168.0.101',
'virtual_interface_id': 2, 'instance_id': 1,
'virtual_interface': addresses[2], 'allocated': True,
'instance': instances[1], 'virtual_interface_id': 2,
'floating_ips': []}, 'virtual_interface': addresses[2],
{'id': 3, 'instance': instances[1],
'network_id': 0, 'floating_ips': []},
'address': '192.168.1.101', {'id': 3,
'instance_id': 1, 'network_id': 0,
'allocated': True, 'address': '192.168.1.101',
'virtual_interface_id': 3, 'instance_id': 1,
'virtual_interface': addresses[3], 'allocated': True,
'instance': instances[1], 'virtual_interface_id': 3,
'floating_ips': []}, 'virtual_interface': addresses[3],
{'id': 4, 'instance': instances[1],
'network_id': 0, 'floating_ips': []},
'address': '192.168.0.102', {'id': 4,
'instance_id': 0, 'network_id': 0,
'allocated': True, 'address': '192.168.0.102',
'virtual_interface_id': 4, 'instance_id': 0,
'virtual_interface': addresses[4], 'allocated': True,
'instance': instances[0], 'virtual_interface_id': 4,
'floating_ips': []}, 'virtual_interface': addresses[4],
{'id': 5, 'instance': instances[0],
'network_id': 1, 'floating_ips': []},
'address': '192.168.1.102', {'id': 5,
'instance_id': 1, 'network_id': 1,
'allocated': True, 'address': '192.168.1.102',
'virtual_interface_id': 5, 'instance_id': 1,
'virtual_interface': addresses[5], 'allocated': True,
'instance': instances[1], 'virtual_interface_id': 5,
'floating_ips': []}] 'virtual_interface': addresses[5],
'instance': instances[1],
'floating_ips': []}]
vifs = [{'id': 0,
'address': 'DE:AD:BE:EF:00:00',
'uuid': '00000000-0000-0000-0000-0000000000000000', vifs = [{'id': 0,
'network_id': 0, 'address': 'DE:AD:BE:EF:00:00',
'network': networks[0], 'uuid': '00000000-0000-0000-0000-0000000000000000',
'instance_id': 0}, 'network_id': 0,
{'id': 1, 'network': networks[0],
'address': 'DE:AD:BE:EF:00:01', 'instance_id': 0},
'uuid': '00000000-0000-0000-0000-0000000000000001', {'id': 1,
'network_id': 1, 'address': 'DE:AD:BE:EF:00:01',
'network': networks[1], 'uuid': '00000000-0000-0000-0000-0000000000000001',
'instance_id': 0}, 'network_id': 1,
{'id': 2, 'network': networks[1],
'address': 'DE:AD:BE:EF:00:02', 'instance_id': 0},
'uuid': '00000000-0000-0000-0000-0000000000000002', {'id': 2,
'network_id': 1, 'address': 'DE:AD:BE:EF:00:02',
'network': networks[1], 'uuid': '00000000-0000-0000-0000-0000000000000002',
'instance_id': 1}, 'network_id': 1,
{'id': 3, 'network': networks[1],
'address': 'DE:AD:BE:EF:00:03', 'instance_id': 1},
'uuid': '00000000-0000-0000-0000-0000000000000003', {'id': 3,
'network_id': 0, 'address': 'DE:AD:BE:EF:00:03',
'network': networks[0], 'uuid': '00000000-0000-0000-0000-0000000000000003',
'instance_id': 1}, 'network_id': 0,
{'id': 4, 'network': networks[0],
'address': 'DE:AD:BE:EF:00:04', 'instance_id': 1},
'uuid': '00000000-0000-0000-0000-0000000000000004', {'id': 4,
'network_id': 0, 'address': 'DE:AD:BE:EF:00:04',
'network': networks[0], 'uuid': '00000000-0000-0000-0000-0000000000000004',
'instance_id': 0}, 'network_id': 0,
{'id': 5, 'network': networks[0],
'address': 'DE:AD:BE:EF:00:05', 'instance_id': 0},
'uuid': '00000000-0000-0000-0000-0000000000000005', {'id': 5,
'network_id': 1, 'address': 'DE:AD:BE:EF:00:05',
'network': networks[1], 'uuid': '00000000-0000-0000-0000-0000000000000005',
'instance_id': 1}] 'network_id': 1,
'network': networks[1],
'instance_id': 1}]
class LinuxNetworkTestCase(test.TestCase):
def setUp(self): class LinuxNetworkTestCase(test.TestCase):
super(LinuxNetworkTestCase, self).setUp()
network_driver = FLAGS.network_driver def setUp(self):
self.driver = utils.import_object(network_driver) super(LinuxNetworkTestCase, self).setUp()
self.driver.db = db network_driver = FLAGS.network_driver
self.driver = utils.import_object(network_driver)
def test_update_dhcp_for_nw00(self): self.driver.db = db
self.flags(use_single_default_gateway=True)
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') def test_update_dhcp_for_nw00(self):
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') self.flags(use_single_default_gateway=True)
db.network_get_associated_fixed_ips(mox.IgnoreArg(), self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
mox.IgnoreArg())\ self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
.AndReturn([fixed_ips[0], self.mox.StubOutWithMock(self.driver, 'write_to_file')
fixed_ips[3]]) self.mox.StubOutWithMock(self.driver, 'ensure_path')
self.mox.StubOutWithMock(os, 'chmod')
db.network_get_associated_fixed_ips(mox.IgnoreArg(),
mox.IgnoreArg())\ db.network_get_associated_fixed_ips(mox.IgnoreArg(),
.AndReturn([fixed_ips[0], mox.IgnoreArg())\
fixed_ips[3]]) .AndReturn([fixed_ips[0],
db.virtual_interface_get_by_instance(mox.IgnoreArg(), fixed_ips[3]])
mox.IgnoreArg())\
.AndReturn([vifs[0], vifs[1]]) db.network_get_associated_fixed_ips(mox.IgnoreArg(),
db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\
mox.IgnoreArg())\ .AndReturn([fixed_ips[0],
.AndReturn([vifs[2], vifs[3]]) fixed_ips[3]])
self.mox.ReplayAll() db.virtual_interface_get_by_instance(mox.IgnoreArg(),
mox.IgnoreArg())\
self.driver.update_dhcp(None, "eth0", networks[0]) .AndReturn([vifs[0], vifs[1]])
db.virtual_interface_get_by_instance(mox.IgnoreArg(),
def test_update_dhcp_for_nw01(self): mox.IgnoreArg())\
self.flags(use_single_default_gateway=True) .AndReturn([vifs[2], vifs[3]])
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg())
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg())
self.driver.ensure_path(mox.IgnoreArg())
db.network_get_associated_fixed_ips(mox.IgnoreArg(), self.driver.ensure_path(mox.IgnoreArg())
mox.IgnoreArg())\ self.driver.ensure_path(mox.IgnoreArg())
.AndReturn([fixed_ips[1], self.driver.ensure_path(mox.IgnoreArg())
fixed_ips[2]]) self.driver.ensure_path(mox.IgnoreArg())
self.driver.ensure_path(mox.IgnoreArg())
db.network_get_associated_fixed_ips(mox.IgnoreArg(), self.driver.ensure_path(mox.IgnoreArg())
mox.IgnoreArg())\ os.chmod(mox.IgnoreArg(), mox.IgnoreArg())
.AndReturn([fixed_ips[1], os.chmod(mox.IgnoreArg(), mox.IgnoreArg())
fixed_ips[2]])
db.virtual_interface_get_by_instance(mox.IgnoreArg(), self.mox.ReplayAll()
mox.IgnoreArg())\
.AndReturn([vifs[0], vifs[1]]) self.driver.update_dhcp(None, "eth0", networks[0])
db.virtual_interface_get_by_instance(mox.IgnoreArg(),
mox.IgnoreArg())\ def test_update_dhcp_for_nw01(self):
.AndReturn([vifs[2], vifs[3]]) self.flags(use_single_default_gateway=True)
self.mox.ReplayAll() self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
self.driver.update_dhcp(None, "eth0", networks[0]) self.mox.StubOutWithMock(self.driver, 'write_to_file')
self.mox.StubOutWithMock(self.driver, 'ensure_path')
def test_get_dhcp_hosts_for_nw00(self): self.mox.StubOutWithMock(os, 'chmod')
self.flags(use_single_default_gateway=True)
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(),
mox.IgnoreArg())\
db.network_get_associated_fixed_ips(mox.IgnoreArg(), .AndReturn([fixed_ips[1],
mox.IgnoreArg())\ fixed_ips[2]])
.AndReturn([fixed_ips[0],
fixed_ips[3]]) db.network_get_associated_fixed_ips(mox.IgnoreArg(),
self.mox.ReplayAll() mox.IgnoreArg())\
.AndReturn([fixed_ips[1],
expected = \ fixed_ips[2]])
"10.0.0.1,fake_instance00.novalocal,"\ db.virtual_interface_get_by_instance(mox.IgnoreArg(),
"192.168.0.100,net:NW-i00000000-0\n"\ mox.IgnoreArg())\
"10.0.0.4,fake_instance01.novalocal,"\ .AndReturn([vifs[0], vifs[1]])
"192.168.1.101,net:NW-i00000001-0" db.virtual_interface_get_by_instance(mox.IgnoreArg(),
actual_hosts = self.driver.get_dhcp_hosts(None, networks[1]) mox.IgnoreArg())\
.AndReturn([vifs[2], vifs[3]])
self.assertEquals(actual_hosts, expected) self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg())
self.driver.write_to_file(mox.IgnoreArg(), mox.IgnoreArg())
def test_get_dhcp_hosts_for_nw01(self): self.driver.ensure_path(mox.IgnoreArg())
self.flags(use_single_default_gateway=True) self.driver.ensure_path(mox.IgnoreArg())
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.driver.ensure_path(mox.IgnoreArg())
self.driver.ensure_path(mox.IgnoreArg())
db.network_get_associated_fixed_ips(mox.IgnoreArg(), self.driver.ensure_path(mox.IgnoreArg())
mox.IgnoreArg())\ self.driver.ensure_path(mox.IgnoreArg())
.AndReturn([fixed_ips[1], self.driver.ensure_path(mox.IgnoreArg())
fixed_ips[2]]) os.chmod(mox.IgnoreArg(), mox.IgnoreArg())
self.mox.ReplayAll() os.chmod(mox.IgnoreArg(), mox.IgnoreArg())
expected = \ self.mox.ReplayAll()
"10.0.0.2,fake_instance00.novalocal,"\
"192.168.1.100,net:NW-i00000000-1\n"\ self.driver.update_dhcp(None, "eth0", networks[0])
"10.0.0.3,fake_instance01.novalocal,"\
"192.168.0.101,net:NW-i00000001-1" def test_get_dhcp_hosts_for_nw00(self):
actual_hosts = self.driver.get_dhcp_hosts(None, networks[0]) self.flags(use_single_default_gateway=True)
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
self.assertEquals(actual_hosts, expected)
db.network_get_associated_fixed_ips(mox.IgnoreArg(),
def test_get_dhcp_opts_for_nw00(self): mox.IgnoreArg())\
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') .AndReturn([fixed_ips[0],
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') fixed_ips[3]])
self.mox.ReplayAll()
db.network_get_associated_fixed_ips(mox.IgnoreArg(),
mox.IgnoreArg())\ expected = \
.AndReturn([fixed_ips[0], "10.0.0.1,fake_instance00.novalocal,"\
fixed_ips[3], "192.168.0.100,net:NW-i00000000-0\n"\
fixed_ips[4]]) "10.0.0.4,fake_instance01.novalocal,"\
db.virtual_interface_get_by_instance(mox.IgnoreArg(), "192.168.1.101,net:NW-i00000001-0"
mox.IgnoreArg())\ actual_hosts = self.driver.get_dhcp_hosts(None, networks[1])
.AndReturn([vifs[0],
vifs[1], self.assertEquals(actual_hosts, expected)
vifs[4]])
db.virtual_interface_get_by_instance(mox.IgnoreArg(), def test_get_dhcp_hosts_for_nw01(self):
mox.IgnoreArg())\ self.flags(use_single_default_gateway=True)
.AndReturn([vifs[2], self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
vifs[3],
vifs[5]]) db.network_get_associated_fixed_ips(mox.IgnoreArg(),
self.mox.ReplayAll() mox.IgnoreArg())\
.AndReturn([fixed_ips[1],
expected_opts = 'NW-i00000001-0,3' fixed_ips[2]])
actual_opts = self.driver.get_dhcp_opts(None, networks[0]) self.mox.ReplayAll()
self.assertEquals(actual_opts, expected_opts) expected = \
"10.0.0.2,fake_instance00.novalocal,"\
def test_get_dhcp_opts_for_nw01(self): "192.168.1.100,net:NW-i00000000-1\n"\
self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') "10.0.0.3,fake_instance01.novalocal,"\
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') "192.168.0.101,net:NW-i00000001-1"
actual_hosts = self.driver.get_dhcp_hosts(None, networks[0])
db.network_get_associated_fixed_ips(mox.IgnoreArg(),
mox.IgnoreArg())\ self.assertEquals(actual_hosts, expected)
.AndReturn([fixed_ips[1],
fixed_ips[2], def test_get_dhcp_opts_for_nw00(self):
fixed_ips[5]]) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
db.virtual_interface_get_by_instance(mox.IgnoreArg(), self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
mox.IgnoreArg())\
.AndReturn([vifs[0], db.network_get_associated_fixed_ips(mox.IgnoreArg(),
vifs[1], mox.IgnoreArg())\
vifs[4]]) .AndReturn([fixed_ips[0],
db.virtual_interface_get_by_instance(mox.IgnoreArg(), fixed_ips[3],
mox.IgnoreArg())\ fixed_ips[4]])
.AndReturn([vifs[2], db.virtual_interface_get_by_instance(mox.IgnoreArg(),
vifs[3], mox.IgnoreArg())\
vifs[5]]) .AndReturn([vifs[0],
self.mox.ReplayAll() vifs[1],
vifs[4]])
expected_opts = "NW-i00000000-1,3" db.virtual_interface_get_by_instance(mox.IgnoreArg(),
actual_opts = self.driver.get_dhcp_opts(None, networks[1]) mox.IgnoreArg())\
.AndReturn([vifs[2],
self.assertEquals(actual_opts, expected_opts) vifs[3],
vifs[5]])
def test_dhcp_opts_not_default_gateway_network(self): self.mox.ReplayAll()
expected = "NW-i00000000-0,3"
actual = self.driver._host_dhcp_opts(fixed_ips[0]) expected_opts = 'NW-i00000001-0,3'
self.assertEquals(actual, expected) actual_opts = self.driver.get_dhcp_opts(None, networks[0])
def test_host_dhcp_without_default_gateway_network(self): self.assertEquals(actual_opts, expected_opts)
expected = ("10.0.0.1,fake_instance00.novalocal,192.168.0.100")
actual = self.driver._host_dhcp(fixed_ips[0]) def test_get_dhcp_opts_for_nw01(self):
self.assertEquals(actual, expected) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips')
self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance')
def test_linux_bridge_driver_plug(self):
"""Makes sure plug doesn't drop FORWARD by default. db.network_get_associated_fixed_ips(mox.IgnoreArg(),
mox.IgnoreArg())\
Ensures bug 890195 doesn't reappear.""" .AndReturn([fixed_ips[1],
fixed_ips[2],
def fake_execute(*args, **kwargs): fixed_ips[5]])
return "", "" db.virtual_interface_get_by_instance(mox.IgnoreArg(),
self.stubs.Set(utils, 'execute', fake_execute) mox.IgnoreArg())\
.AndReturn([vifs[0],
def verify_add_rule(chain, rule): vifs[1],
self.assertEqual(chain, 'FORWARD') vifs[4]])
self.assertIn('ACCEPT', rule) db.virtual_interface_get_by_instance(mox.IgnoreArg(),
self.stubs.Set(linux_net.iptables_manager.ipv4['filter'], mox.IgnoreArg())\
'add_rule', verify_add_rule) .AndReturn([vifs[2],
driver = linux_net.LinuxBridgeInterfaceDriver() vifs[3],
driver.plug({"bridge": "br100", "bridge_interface": "eth0"}, vifs[5]])
"fakemac") self.mox.ReplayAll()
def _test_initialize_gateway(self, existing, expected, routes=''): expected_opts = "NW-i00000000-1,3"
self.flags(fake_network=False) actual_opts = self.driver.get_dhcp_opts(None, networks[1])
executes = []
self.assertEquals(actual_opts, expected_opts)
def fake_execute(*args, **kwargs):
executes.append(args) def test_dhcp_opts_not_default_gateway_network(self):
if args[0] == 'ip' and args[1] == 'addr' and args[2] == 'show': expected = "NW-i00000000-0,3"
return existing, "" actual = self.driver._host_dhcp_opts(fixed_ips[0])
if args[0] == 'route' and args[1] == '-n': self.assertEquals(actual, expected)
return routes, ""
self.stubs.Set(utils, 'execute', fake_execute) def test_host_dhcp_without_default_gateway_network(self):
network = {'dhcp_server': '192.168.1.1', expected = ("10.0.0.1,fake_instance00.novalocal,192.168.0.100")
'cidr': '192.168.1.0/24', actual = self.driver._host_dhcp(fixed_ips[0])
'broadcast': '192.168.1.255', self.assertEquals(actual, expected)
'cidr_v6': '2001:db8::/64'}
self.driver.initialize_gateway_device('eth0', network) def test_linux_bridge_driver_plug(self):
self.assertEqual(executes, expected) """Makes sure plug doesn't drop FORWARD by default.
def test_initialize_gateway_moves_wrong_ip(self): Ensures bug 890195 doesn't reappear."""
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> "
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n" def fake_execute(*args, **kwargs):
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n" return "", ""
" inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n" self.stubs.Set(utils, 'execute', fake_execute)
" inet6 dead::beef:dead:beef:dead/64 scope link\n"
" valid_lft forever preferred_lft forever\n") def verify_add_rule(chain, rule):
expected = [ self.assertEqual(chain, 'FORWARD')
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'), self.assertIn('ACCEPT', rule)
('route', '-n'), self.stubs.Set(linux_net.iptables_manager.ipv4['filter'],
('ip', 'addr', 'del', '192.168.0.1/24', 'add_rule', verify_add_rule)
'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'), driver = linux_net.LinuxBridgeInterfaceDriver()
('ip', 'addr', 'add', '192.168.1.1/24', driver.plug({"bridge": "br100", "bridge_interface": "eth0"},
'brd', '192.168.1.255', 'dev', 'eth0'), "fakemac")
('ip', 'addr', 'add', '192.168.0.1/24',
'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'), def _test_initialize_gateway(self, existing, expected, routes=''):
('ip', '-f', 'inet6', 'addr', 'change', self.flags(fake_network=False)
'2001:db8::/64', 'dev', 'eth0'), executes = []
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'),
] def fake_execute(*args, **kwargs):
self._test_initialize_gateway(existing, expected) executes.append(args)
if args[0] == 'ip' and args[1] == 'addr' and args[2] == 'show':
def test_initialize_gateway_resets_route(self): return existing, ""
routes = "0.0.0.0 192.68.0.1 0.0.0.0 " \ if args[0] == 'route' and args[1] == '-n':
"UG 100 0 0 eth0" return routes, ""
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> " self.stubs.Set(utils, 'execute', fake_execute)
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n" network = {'dhcp_server': '192.168.1.1',
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n" 'cidr': '192.168.1.0/24',
" inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n" 'broadcast': '192.168.1.255',
" inet6 dead::beef:dead:beef:dead/64 scope link\n" 'cidr_v6': '2001:db8::/64'}
" valid_lft forever preferred_lft forever\n") self.driver.initialize_gateway_device('eth0', network)
expected = [ self.assertEqual(executes, expected)
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'),
('route', '-n'), def test_initialize_gateway_moves_wrong_ip(self):
('route', 'del', 'default', 'gw', '192.68.0.1', 'dev', 'eth0'), existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> "
('ip', 'addr', 'del', '192.168.0.1/24', " mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n"
'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'), " link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n"
('ip', 'addr', 'add', '192.168.1.1/24', " inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n"
'brd', '192.168.1.255', 'dev', 'eth0'), " inet6 dead::beef:dead:beef:dead/64 scope link\n"
('ip', 'addr', 'add', '192.168.0.1/24', " valid_lft forever preferred_lft forever\n")
'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'), expected = [
('route', 'add', 'default', 'gw', '192.68.0.1'), ('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'),
('ip', '-f', 'inet6', 'addr', 'change', ('route', '-n'),
'2001:db8::/64', 'dev', 'eth0'), ('ip', 'addr', 'del', '192.168.0.1/24',
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'), 'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'),
] ('ip', 'addr', 'add', '192.168.1.1/24',
self._test_initialize_gateway(existing, expected, routes) 'brd', '192.168.1.255', 'dev', 'eth0'),
('ip', 'addr', 'add', '192.168.0.1/24',
def test_initialize_gateway_no_move_right_ip(self): 'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'),
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> " ('ip', '-f', 'inet6', 'addr', 'change',
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n" '2001:db8::/64', 'dev', 'eth0'),
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n" ('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'),
" inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0\n" ]
" inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n" self._test_initialize_gateway(existing, expected)
" inet6 dead::beef:dead:beef:dead/64 scope link\n"
" valid_lft forever preferred_lft forever\n") def test_initialize_gateway_resets_route(self):
expected = [ routes = "0.0.0.0 192.68.0.1 0.0.0.0 " \
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'), "UG 100 0 0 eth0"
('ip', '-f', 'inet6', 'addr', 'change', existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> "
'2001:db8::/64', 'dev', 'eth0'), " mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n"
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'), " link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n"
] " inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n"
self._test_initialize_gateway(existing, expected) " inet6 dead::beef:dead:beef:dead/64 scope link\n"
" valid_lft forever preferred_lft forever\n")
def test_initialize_gateway_add_if_blank(self): expected = [
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> " ('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'),
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n" ('route', '-n'),
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n" ('route', 'del', 'default', 'gw', '192.68.0.1', 'dev', 'eth0'),
" inet6 dead::beef:dead:beef:dead/64 scope link\n" ('ip', 'addr', 'del', '192.168.0.1/24',
" valid_lft forever preferred_lft forever\n") 'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'),
expected = [ ('ip', 'addr', 'add', '192.168.1.1/24',
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'), 'brd', '192.168.1.255', 'dev', 'eth0'),
('route', '-n'), ('ip', 'addr', 'add', '192.168.0.1/24',
('ip', 'addr', 'add', '192.168.1.1/24', 'brd', '192.168.0.255', 'scope', 'global', 'dev', 'eth0'),
'brd', '192.168.1.255', 'dev', 'eth0'), ('route', 'add', 'default', 'gw', '192.68.0.1'),
('ip', '-f', 'inet6', 'addr', 'change', ('ip', '-f', 'inet6', 'addr', 'change',
'2001:db8::/64', 'dev', 'eth0'), '2001:db8::/64', 'dev', 'eth0'),
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'), ('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'),
] ]
self._test_initialize_gateway(existing, expected) self._test_initialize_gateway(existing, expected, routes)
def test_initialize_gateway_no_move_right_ip(self):
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> "
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n"
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n"
" inet 192.168.1.1/24 brd 192.168.1.255 scope global eth0\n"
" inet 192.168.0.1/24 brd 192.168.0.255 scope global eth0\n"
" inet6 dead::beef:dead:beef:dead/64 scope link\n"
" valid_lft forever preferred_lft forever\n")
expected = [
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'),
('ip', '-f', 'inet6', 'addr', 'change',
'2001:db8::/64', 'dev', 'eth0'),
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'),
]
self._test_initialize_gateway(existing, expected)
def test_initialize_gateway_add_if_blank(self):
existing = ("2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> "
" mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000\n"
" link/ether de:ad:be:ef:be:ef brd ff:ff:ff:ff:ff:ff\n"
" inet6 dead::beef:dead:beef:dead/64 scope link\n"
" valid_lft forever preferred_lft forever\n")
expected = [
('ip', 'addr', 'show', 'dev', 'eth0', 'scope', 'global'),
('route', '-n'),
('ip', 'addr', 'add', '192.168.1.1/24',
'brd', '192.168.1.255', 'dev', 'eth0'),
('ip', '-f', 'inet6', 'addr', 'change',
'2001:db8::/64', 'dev', 'eth0'),
('ip', 'link', 'set', 'dev', 'eth0', 'promisc', 'on'),
]
self._test_initialize_gateway(existing, expected)