55
59
plugin = manager.NeutronManager.get_plugin()
56
60
plugin.start_rpc_listeners()
58
def test_security_group_get_port_from_device(self):
62
def _make_port_with_new_sec_group(self, net_id):
63
sg = self._make_security_group(self.fmt, 'name', 'desc')
64
port = self._make_port(
65
self.fmt, net_id, security_groups=[sg['security_group']['id']])
68
def test_security_group_get_ports_from_devices(self):
59
69
with self.network() as n:
60
70
with self.subnet(n):
61
with self.security_group() as sg:
62
security_group_id = sg['security_group']['id']
63
res = self._create_port(self.fmt, n['network']['id'])
64
port = self.deserialize(self.fmt, res)
65
fixed_ips = port['port']['fixed_ips']
66
data = {'port': {'fixed_ips': fixed_ips,
67
'name': port['port']['name'],
68
ext_sg.SECURITYGROUPS:
71
req = self.new_update_request('ports', data,
73
res = self.deserialize(self.fmt,
74
req.get_response(self.api))
75
port_id = res['port']['id']
76
plugin = manager.NeutronManager.get_plugin()
77
port_dict = plugin.get_port_from_device(port_id)
78
self.assertEqual(port_id, port_dict['id'])
79
self.assertEqual([security_group_id],
71
port1 = self._make_port_with_new_sec_group(n['network']['id'])
72
port2 = self._make_port_with_new_sec_group(n['network']['id'])
73
plugin = manager.NeutronManager.get_plugin()
74
# should match full ID and starting chars
75
ports = plugin.get_ports_from_devices(
76
[port1['id'], port2['id'][0:8]])
77
self.assertEqual(2, len(ports))
78
for port_dict in ports:
79
p = port1 if port1['id'] == port_dict['id'] else port2
80
self.assertEqual(p['id'], port_dict['id'])
81
self.assertEqual(p['security_groups'],
80
82
port_dict[ext_sg.SECURITYGROUPS])
81
83
self.assertEqual([], port_dict['security_group_rules'])
82
self.assertEqual([fixed_ips[0]['ip_address']],
84
self.assertEqual([p['fixed_ips'][0]['ip_address']],
83
85
port_dict['fixed_ips'])
84
self._delete('ports', port_id)
86
def test_security_group_get_port_from_device_with_no_port(self):
87
plugin = manager.NeutronManager.get_plugin()
88
port_dict = plugin.get_port_from_device('bad_device_id')
89
self.assertIsNone(port_dict)
86
self._delete('ports', p['id'])
88
def test_security_group_get_ports_from_devices_with_bad_id(self):
89
plugin = manager.NeutronManager.get_plugin()
90
ports = plugin.get_ports_from_devices(['bad_device_id'])
91
self.assertFalse(ports)
93
def test_security_group_no_db_calls_with_no_ports(self):
94
plugin = manager.NeutronManager.get_plugin()
96
'neutron.plugins.ml2.db.get_sg_ids_grouped_by_port'
98
self.assertFalse(plugin.get_ports_from_devices([]))
99
self.assertFalse(get_mock.called)
101
def test_large_port_count_broken_into_parts(self):
102
plugin = manager.NeutronManager.get_plugin()
103
max_ports_per_query = 5
105
for max_ports_per_query in (1, 2, 5, 7, 9, 31):
106
with contextlib.nested(
107
mock.patch('neutron.plugins.ml2.db.MAX_PORTS_PER_QUERY',
108
new=max_ports_per_query),
109
mock.patch('neutron.plugins.ml2.db.get_sg_ids_grouped_by_port',
111
) as (max_mock, get_mock):
112
plugin.get_ports_from_devices(
113
['%s%s' % (const.TAP_DEVICE_PREFIX, i)
114
for i in range(ports_to_query)])
115
all_call_args = map(lambda x: x[1][0], get_mock.mock_calls)
116
last_call_args = all_call_args.pop()
117
# all but last should be getting MAX_PORTS_PER_QUERY ports
119
all(map(lambda x: len(x) == max_ports_per_query,
122
remaining = ports_to_query % max_ports_per_query
124
self.assertEqual(remaining, len(last_call_args))
125
# should be broken into ceil(total/MAX_PORTS_PER_QUERY) calls
127
math.ceil(ports_to_query / float(max_ports_per_query)),
131
def test_full_uuids_skip_port_id_lookup(self):
132
plugin = manager.NeutronManager.get_plugin()
133
# when full UUIDs are provided, the _or statement should only
134
# have one matching 'IN' critiera for all of the IDs
135
with contextlib.nested(
136
mock.patch('neutron.plugins.ml2.db.or_'),
137
mock.patch('neutron.plugins.ml2.db.db_api.get_session')
138
) as (or_mock, sess_mock):
139
fmock = sess_mock.query.return_value.outerjoin.return_value.filter
140
# return no ports to exit the method early since we are mocking
142
fmock.return_value.all.return_value = []
143
plugin.get_ports_from_devices([test_api_v2._uuid(),
144
test_api_v2._uuid()])
145
# the or_ function should only have one argument
146
or_mock.assert_called_once_with(mock.ANY)
92
149
class TestMl2SecurityGroupsXML(TestMl2SecurityGroups):