Debian Codegraph @

neutron_taas/tests/unit/db/test_taas_db.py

raw
1 # Copyright 2016 VMware, Inc.
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 from neutron.tests.unit import testlib_api
17
18 from neutron_lib import context
19 from neutron_lib.exceptions import taas as taas_exc
20
21 from oslo_utils import importutils
22 from oslo_utils import uuidutils
23
24 from neutron_taas.db import taas_db
25
26
27 DB_PLUGIN_KLAAS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
28 _uuid = uuidutils.generate_uuid
29
30
31 class TaaSDbTestCase(testlib_api.SqlTestCase):
32
33 """Unit test for TaaS DB support."""
34
35 def setUp(self):
36 super().setUp()
37 self.ctx = context.get_admin_context()
38 self.mixin = taas_db.Taas_db_Mixin()
39 self.plugin = importutils.import_object(DB_PLUGIN_KLAAS)
40 self.tenant_id = 'fake-tenant-id'
41
42 def _get_tap_service_data(self, name='ts-1', port_id=None):
43 port_id = port_id or _uuid()
44 return {"tap_service": {"name": name,
45 "tenant_id": self.tenant_id,
46 "description": "test tap service",
47 "port_id": port_id}}
48
49 def _get_tap_flow_data(self, tap_service_id, name='tf-1',
50 direction='BOTH', source_port=None,
51 vlan_filter=None):
52 source_port = source_port or _uuid()
53 return {"tap_flow": {"name": name,
54 "tenant_id": self.tenant_id,
55 "description": "test tap flow",
56 "tap_service_id": tap_service_id,
57 "source_port": source_port,
58 "direction": direction,
59 "vlan_filter": vlan_filter}}
60
61 def _get_tap_service(self, tap_service_id):
62 """Helper method to retrieve tap service."""
63 with self.ctx.session.begin():
64 return self.mixin.get_tap_service(self.ctx, tap_service_id)
65
66 def _get_tap_services(self):
67 """Helper method to retrieve all tap services."""
68 with self.ctx.session.begin():
69 return self.mixin.get_tap_services(self.ctx)
70
71 def _create_tap_service(self, tap_service):
72 """Helper method to create tap service."""
73 with self.ctx.session.begin():
74 return self.mixin.create_tap_service(self.ctx, tap_service)
75
76 def _update_tap_service(self, tap_service_id, tap_service):
77 """Helper method to update tap service."""
78 with self.ctx.session.begin():
79 return self.mixin.update_tap_service(self.ctx,
80 tap_service_id,
81 tap_service)
82
83 def _delete_tap_service(self, tap_service_id):
84 """Helper method to delete tap service."""
85 with self.ctx.session.begin():
86 return self.mixin.delete_tap_service(self.ctx, tap_service_id)
87
88 def _get_tap_flow(self, tap_flow_id):
89 """Helper method to retrieve tap flow."""
90 with self.ctx.session.begin():
91 return self.mixin.get_tap_flow(self.ctx, tap_flow_id)
92
93 def _get_tap_flows(self):
94 """Helper method to retrieve all tap flows."""
95 with self.ctx.session.begin():
96 return self.mixin.get_tap_flows(self.ctx)
97
98 def _create_tap_flow(self, tap_flow):
99 """Helper method to create tap flow."""
100 with self.ctx.session.begin():
101 return self.mixin.create_tap_flow(self.ctx, tap_flow)
102
103 def _update_tap_flow(self, tap_flow_id, tap_flow):
104 """Helper method to update tap flow."""
105 with self.ctx.session.begin():
106 return self.mixin.update_tap_flow(self.ctx, tap_flow_id, tap_flow)
107
108 def _delete_tap_flow(self, tap_flow_id):
109 """Helper method to delete tap flow."""
110 with self.ctx.session.begin():
111 return self.mixin.delete_tap_flow(self.ctx, tap_flow_id)
112
113 def test_tap_service_get(self):
114 """Test to retrieve a tap service from the database."""
115 name = 'test-tap-service'
116 data = self._get_tap_service_data(name=name)
117 result = self._create_tap_service(data)
118 get_result = self._get_tap_service(result['id'])
119 self.assertEqual(name, get_result['name'])
120
121 def test_tap_service_create(self):
122 """Test to create a tap service in the database."""
123 name = 'test-tap-service'
124 port_id = _uuid()
125 data = self._get_tap_service_data(name=name, port_id=port_id)
126 result = self._create_tap_service(data)
127 self.assertEqual(name, result['name'])
128 self.assertEqual(port_id, result['port_id'])
129
130 def test_tap_service_list(self):
131 """Test to retrieve all tap services from the database."""
132 name_1 = "ts-1"
133 data_1 = self._get_tap_service_data(name=name_1)
134 name_2 = "ts-2"
135 data_2 = self._get_tap_service_data(name=name_2)
136 self._create_tap_service(data_1)
137 self._create_tap_service(data_2)
138 tap_services = self._get_tap_services()
139 self.assertEqual(2, len(tap_services))
140
141 def test_tap_service_update(self):
142 """Test to update a tap service in the database."""
143 original_name = "ts-1"
144 updated_name = "ts-1-got-updated"
145 data = self._get_tap_service_data(name=original_name)
146 ts = self._create_tap_service(data)
147 updated_data = self._get_tap_service_data(name=updated_name)
148 ts_updated = self._update_tap_service(ts['id'], updated_data)
149 self.assertEqual(updated_name, ts_updated['name'])
150
151 def test_tap_service_delete(self):
152 """Test to delete a tap service from the database."""
153 data = self._get_tap_service_data()
154 result = self._create_tap_service(data)
155 self._delete_tap_service(result['id'])
156 self.assertRaises(taas_exc.TapServiceNotFound,
157 self._get_tap_service, result['id'])
158
159 def test_tap_flow_get(self):
160 """Test to retrieve a tap flow from the database."""
161 ts_data = self._get_tap_service_data()
162 ts = self._create_tap_service(ts_data)
163 tf_name = 'test-tap-flow'
164 tf_data = self._get_tap_flow_data(tap_service_id=ts['id'],
165 name=tf_name)
166 tf = self._create_tap_flow(tf_data)
167 get_tf = self._get_tap_flow(tf['id'])
168 self.assertEqual(tf_name, get_tf['name'])
169
170 def test_tap_flow_create(self):
171 """Test to create a tap flow in the database."""
172 ts_data = self._get_tap_service_data()
173 ts = self._create_tap_service(ts_data)
174 tf_name = 'test-tap-flow'
175 tf_direction = 'IN'
176 tf_source_port = _uuid()
177 tf_data = self._get_tap_flow_data(tap_service_id=ts['id'],
178 name=tf_name,
179 source_port=tf_source_port,
180 direction=tf_direction)
181 tf = self._create_tap_flow(tf_data)
182 self.assertEqual(tf_name, tf['name'])
183 self.assertEqual(tf_direction, tf['direction'])
184 self.assertEqual(tf_source_port, tf['source_port'])
185
186 def test_tap_flow_create_with_vlan_filter(self):
187 """Test to create a tap flow (with vlan_filter) in the database."""
188 ts_data = self._get_tap_service_data()
189 ts = self._create_tap_service(ts_data)
190 tf_name = 'test-tap-flow'
191 tf_direction = 'IN'
192 tf_source_port = _uuid()
193 tf_vlan_filter = '9-18,27,36-45'
194 tf_data = self._get_tap_flow_data(tap_service_id=ts['id'],
195 name=tf_name,
196 source_port=tf_source_port,
197 direction=tf_direction,
198 vlan_filter=tf_vlan_filter)
199 tf = self._create_tap_flow(tf_data)
200 self.assertEqual(tf_name, tf['name'])
201 self.assertEqual(tf_direction, tf['direction'])
202 self.assertEqual(tf_source_port, tf['source_port'])
203 self.assertEqual(tf_vlan_filter, tf['vlan_filter'])
204
205 def test_tap_flow_list(self):
206 """Test to retrieve all tap flows from the database."""
207 ts_data = self._get_tap_service_data()
208 ts = self._create_tap_service(ts_data)
209 tf_1_name = "tf-1"
210 tf_1_data = self._get_tap_flow_data(tap_service_id=ts['id'],
211 name=tf_1_name)
212 tf_2_name = "tf-2"
213 tf_2_data = self._get_tap_flow_data(tap_service_id=ts['id'],
214 name=tf_2_name)
215 self._create_tap_flow(tf_1_data)
216 self._create_tap_flow(tf_2_data)
217 tap_flows = self._get_tap_flows()
218 self.assertEqual(2, len(tap_flows))
219
220 def test_tap_flow_delete(self):
221 """Test to delete a tap flow from the database."""
222 ts_data = self._get_tap_service_data()
223 ts = self._create_tap_service(ts_data)
224 tf_name = "test-tap-flow"
225 tf_data = self._get_tap_flow_data(tap_service_id=ts['id'],
226 name=tf_name)
227 tf = self._create_tap_flow(tf_data)
228 self._delete_tap_flow(tf['id'])
229 self.assertRaises(taas_exc.TapFlowNotFound,
230 self._get_tap_flow, tf['id'])