1
# Copyright (c) 2013 OpenStack Foundation
2
# Copyright 2013, Nachi Ueno, NTT MCL, Inc.
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
19
from quantum.api.v2 import attributes
20
from quantum.extensions import securitygroup as ext_sg
21
from quantum import manager
22
from quantum.tests.unit import test_extension_security_group as test_sg
23
from quantum.tests.unit import test_security_groups_rpc as test_sg_rpc
25
PLUGIN_NAME = 'quantum.plugins.ml2.plugin.Ml2Plugin'
26
NOTIFIER = 'quantum.plugins.ml2.rpc.AgentNotifierApi'
29
class Ml2SecurityGroupsTestCase(test_sg.SecurityGroupDBTestCase):
30
_plugin_name = PLUGIN_NAME
32
def setUp(self, plugin=None):
33
test_sg_rpc.set_firewall_driver(test_sg_rpc.FIREWALL_HYBRID_DRIVER)
34
self.addCleanup(mock.patch.stopall)
35
notifier_p = mock.patch(NOTIFIER)
36
notifier_cls = notifier_p.start()
37
self.notifier = mock.Mock()
38
notifier_cls.return_value = self.notifier
39
self._attribute_map_bk_ = {}
40
for item in attributes.RESOURCE_ATTRIBUTE_MAP:
41
self._attribute_map_bk_[item] = (attributes.
42
RESOURCE_ATTRIBUTE_MAP[item].
44
super(Ml2SecurityGroupsTestCase, self).setUp(PLUGIN_NAME)
47
super(Ml2SecurityGroupsTestCase, self).tearDown()
48
attributes.RESOURCE_ATTRIBUTE_MAP = self._attribute_map_bk_
51
class TestMl2SecurityGroups(Ml2SecurityGroupsTestCase,
52
test_sg.TestSecurityGroups,
53
test_sg_rpc.SGNotificationTestMixin):
54
def test_security_group_get_port_from_device(self):
55
with self.network() as n:
57
with self.security_group() as sg:
58
security_group_id = sg['security_group']['id']
59
res = self._create_port(self.fmt, n['network']['id'])
60
port = self.deserialize(self.fmt, res)
61
fixed_ips = port['port']['fixed_ips']
62
data = {'port': {'fixed_ips': fixed_ips,
63
'name': port['port']['name'],
64
ext_sg.SECURITYGROUPS:
67
req = self.new_update_request('ports', data,
69
res = self.deserialize(self.fmt,
70
req.get_response(self.api))
71
port_id = res['port']['id']
72
plugin = manager.QuantumManager.get_plugin()
73
port_dict = plugin.callbacks.get_port_from_device(port_id)
74
self.assertEqual(port_id, port_dict['id'])
75
self.assertEqual([security_group_id],
76
port_dict[ext_sg.SECURITYGROUPS])
77
self.assertEqual([], port_dict['security_group_rules'])
78
self.assertEqual([fixed_ips[0]['ip_address']],
79
port_dict['fixed_ips'])
80
self._delete('ports', port_id)
82
def test_security_group_get_port_from_device_with_no_port(self):
83
plugin = manager.QuantumManager.get_plugin()
84
port_dict = plugin.callbacks.get_port_from_device('bad_device_id')
85
self.assertEqual(None, port_dict)
88
class TestMl2SecurityGroupsXML(TestMl2SecurityGroups):