Debian Codegraph @

neutron_taas/tests/unit/db/test_tap_mirror_db.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from neutron.tests.unit import testlib_api
14
15 from neutron_lib import context
16 from neutron_lib.exceptions import taas as taas_exc
17
18 from oslo_utils import importutils
19 from oslo_utils import uuidutils
20
21 from neutron_taas.db import tap_mirror_db
22
23
24 DB_PLUGIN_KLAAS = 'neutron.db.db_base_plugin_v2.NeutronDbPluginV2'
25 _uuid = uuidutils.generate_uuid
26
27
28 class TapMirrorDbTestCase(testlib_api.SqlTestCase):
29
30 """Unit test for Tap Mirror DB support."""
31
32 def setUp(self):
33 super().setUp()
34 self.ctx = context.get_admin_context()
35 self.db_mixin = tap_mirror_db.Taas_mirror_db_mixin()
36 self.plugin = importutils.import_object(DB_PLUGIN_KLAAS)
37 self.project_id = 'fake-project-id'
38
39 def _get_tap_mirror_data(self, name='tm-1', port_id=None,
40 directions='{"IN": "99"}', remote_ip='10.99.8.3',
41 mirror_type='erspanv1'):
42 port_id = port_id or _uuid()
43 return {"tap_mirror": {"name": name,
44 "project_id": self.project_id,
45 "description": "test tap mirror",
46 "port_id": port_id,
47 'directions': directions,
48 'remote_ip': remote_ip,
49 'mirror_type': mirror_type
50 }
51 }
52
53 def _get_tap_mirror(self, tap_mirror_id):
54 """Helper method to retrieve tap Mirror."""
55 with self.ctx.session.begin():
56 return self.db_mixin.get_tap_mirror(self.ctx, tap_mirror_id)
57
58 def _get_tap_mirrors(self):
59 """Helper method to retrieve all tap Mirror."""
60 with self.ctx.session.begin():
61 return self.db_mixin.get_tap_mirrors(self.ctx)
62
63 def _create_tap_mirror(self, tap_mirror):
64 """Helper method to create tap Mirror."""
65 with self.ctx.session.begin():
66 return self.db_mixin.create_tap_mirror(self.ctx, tap_mirror)
67
68 def _update_tap_mirror(self, tap_mirror_id, tap_mirror):
69 """Helper method to update tap Mirror."""
70 with self.ctx.session.begin():
71 return self.db_mixin.update_tap_mirror(self.ctx,
72 tap_mirror_id,
73 tap_mirror)
74
75 def _delete_tap_mirror(self, tap_mirror_id):
76 """Helper method to delete tap Mirror."""
77 with self.ctx.session.begin():
78 return self.db_mixin.delete_tap_mirror(self.ctx, tap_mirror_id)
79
80 def test_tap_mirror_get(self):
81 name = 'test-tap-mirror'
82 data = self._get_tap_mirror_data(name=name)
83 result = self._create_tap_mirror(data)
84 get_result = self._get_tap_mirror(result['id'])
85 self.assertEqual(name, get_result['name'])
86
87 def test_tap_mirror_create(self):
88 name = 'test-tap-mirror'
89 port_id = _uuid()
90 data = self._get_tap_mirror_data(name=name, port_id=port_id)
91 result = self._create_tap_mirror(data)
92 self.assertEqual(name, result['name'])
93 self.assertEqual(port_id, result['port_id'])
94
95 def test_tap_mirror_list(self):
96 name_1 = "tm-1"
97 data_1 = self._get_tap_mirror_data(name=name_1)
98 name_2 = "tm-2"
99 data_2 = self._get_tap_mirror_data(name=name_2)
100 self._create_tap_mirror(data_1)
101 self._create_tap_mirror(data_2)
102 tap_mirrors = self._get_tap_mirrors()
103 self.assertEqual(2, len(tap_mirrors))
104
105 def test_tap_mirror_update(self):
106 original_name = "tm-1"
107 updated_name = "tm-1-got-updated"
108 data = self._get_tap_mirror_data(name=original_name)
109 tm = self._create_tap_mirror(data)
110 updated_data = self._get_tap_mirror_data(name=updated_name)
111 tm_updated = self._update_tap_mirror(tm['id'], updated_data)
112 self.assertEqual(updated_name, tm_updated['name'])
113
114 def test_tap_mirror_delete(self):
115 data = self._get_tap_mirror_data()
116 result = self._create_tap_mirror(data)
117 self._delete_tap_mirror(result['id'])
118 self.assertRaises(taas_exc.TapMirrorNotFound,
119 self._get_tap_mirror, result['id'])