Debian Codegraph @

networking_l2gw/tests/unit/l2gatewayclient/osc/test_osc_l2gw_connection.py

raw
1 # All Rights Reserved 2018
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import operator
16 from unittest import mock
17
18 from openstack.network.v2 import network as sdk_network
19 from osc_lib import utils as osc_utils
20 from osc_lib.utils import columns as column_util
21
22 from neutronclient.tests.unit.osc.v2 import fakes as test_fakes
23
24 from networking_l2gw.l2gatewayclient.osc import l2gw_connection as \
25 osc_l2gw_conn
26 from networking_l2gw.l2gatewayclient.osc import sdk_objects
27 from networking_l2gw.tests.unit.l2gatewayclient.osc import fakes
28
29
30 columns_long = tuple(col for col, _, listing_mode in osc_l2gw_conn._attr_map
31 if listing_mode in (column_util.LIST_BOTH,
32 column_util.LIST_LONG_ONLY))
33 headers_long = tuple(head for _, head, listing_mode in osc_l2gw_conn._attr_map
34 if listing_mode in (column_util.LIST_BOTH,
35 column_util.LIST_LONG_ONLY))
36 sorted_attr_map = sorted(osc_l2gw_conn._attr_map, key=operator.itemgetter(1))
37 sorted_columns = tuple(col for col, _, _ in sorted_attr_map)
38 sorted_headers = tuple(head for _, head, _ in sorted_attr_map)
39 columns = (
40 'id',
41 'l2_gateway_id',
42 'network_id',
43 'segmentation_id',
44 )
45
46
47 def _get_data(attrs, columns=sorted_columns):
48 return osc_utils.get_dict_properties(attrs, columns)
49
50
51 def _get_columns(item):
52 column_map = {}
53 hidden_columns = ['location', 'tenant_id']
54 return osc_utils.get_osc_show_columns_for_sdk_resource(
55 item,
56 column_map,
57 hidden_columns
58 )
59
60
61 class TestCreateL2gwConnection(test_fakes.TestNeutronClientOSCV2):
62 def setUp(self):
63 super().setUp()
64 self.cmd = osc_l2gw_conn.CreateL2gwConnection(
65 self.app, self.namespace)
66
67 def test_create_l2gateway_connection(self):
68 """Test Create l2gateway-connection."""
69
70 fake_connection = fakes.FakeL2GWConnection.create_l2gw_connection()
71
72 self.app.client_manager.network = mock.Mock()
73 self.app.client_manager.network._create = mock.Mock(
74 return_value=fake_connection)
75 self.app.client_manager.network._find = mock.Mock(
76 return_value=sdk_objects.L2gw(
77 **{'id': fake_connection['l2_gateway_id']}))
78 self.app.client_manager.network.find_network = mock.Mock(
79 return_value=sdk_network.Network(
80 **{'id': fake_connection['network_id']}))
81
82 arg_list = [
83 fake_connection['l2_gateway_id'],
84 fake_connection['network_id'],
85 '--default-segmentation-id', fake_connection['segmentation_id']
86 ]
87 verify_list = [
88 ('gateway_name', fake_connection['l2_gateway_id']),
89 ('network', fake_connection['network_id']),
90 ('seg_id', fake_connection['segmentation_id'])
91 ]
92
93 parsed_args = self.check_parser(self.cmd, arg_list, verify_list)
94 columns, data = self.cmd.take_action(parsed_args)
95 self.app.client_manager.network._create.assert_called_once_with(
96 sdk_objects.L2gwConnection,
97 **{'segmentation_id': fake_connection['segmentation_id'],
98 'network_id': fake_connection['network_id'],
99 'l2_gateway_id': fake_connection['l2_gateway_id']}
100 )
101 self.assertEqual(columns, columns)
102 fake_data = _get_data(
103 fake_connection,
104 _get_columns(fake_connection)[1])
105 self.assertEqual(fake_data, data)
106 self.assertItemEqual(fake_data, data)
107
108
109 class TestListL2gwConnection(test_fakes.TestNeutronClientOSCV2):
110 def setUp(self):
111 super().setUp()
112 self.cmd = osc_l2gw_conn.ListL2gwConnection(self.app, self.namespace)
113
114 def test_list_l2gateway_connection(self):
115 """Test List l2gateway-connections."""
116
117 fake_connections = fakes.FakeL2GWConnection.create_l2gw_connections(
118 count=3)
119
120 self.app.client_manager.network = mock.Mock()
121 self.app.client_manager.network._list = mock.Mock(
122 return_value=fake_connections)
123 arg_list = []
124 verify_list = []
125
126 parsed_args = self.check_parser(self.cmd, arg_list, verify_list)
127
128 headers, data = self.cmd.take_action(parsed_args)
129
130 self.app.client_manager.network._list.assert_called_once()
131 self.assertEqual(headers, list(headers_long))
132 self.assertListItemEqual(
133 list(data),
134 [_get_data(fake_connection, columns_long) for fake_connection
135 in fake_connections]
136 )
137
138
139 class TestShowL2gwConnection(test_fakes.TestNeutronClientOSCV2):
140 def setUp(self):
141 super().setUp()
142 self.cmd = osc_l2gw_conn.ShowL2gwConnection(self.app, self.namespace)
143 self.app.client_manager.network._find = mock.Mock(
144 side_effect=lambda _, name_or_id: {'id': name_or_id})
145
146 def test_show_l2gateway_connection(self):
147 """Test Show l2gateway-connection."""
148
149 fake_connection = fakes.FakeL2GWConnection.create_l2gw_connection()
150
151 self.app.client_manager.network._get = mock.Mock(
152 return_value=fake_connection)
153 arg_list = [fake_connection['id']]
154 verify_list = [
155 (osc_l2gw_conn.L2_GATEWAY_CONNECTION, fake_connection['id'])
156 ]
157
158 parsed_args = self.check_parser(self.cmd, arg_list, verify_list)
159
160 headers, data = self.cmd.take_action(parsed_args)
161
162 self.app.client_manager.network._get.assert_called_once_with(
163 sdk_objects.L2gwConnection, {'id': fake_connection['id']})
164 self.assertEqual(columns, headers)
165 fake_data = _get_data(
166 fake_connection,
167 _get_columns(fake_connection)[1])
168 self.assertEqual(fake_data, data)
169
170
171 class TestDeleteL2gwConnection(test_fakes.TestNeutronClientOSCV2):
172 def setUp(self):
173 super().setUp()
174 self.app.client_manager.network = mock.Mock()
175 self.app.client_manager.network._find = mock.Mock(
176 side_effect=lambda _,
177 name_or_id: sdk_objects.L2gw(**{'id': name_or_id}))
178 self.cmd = osc_l2gw_conn.DeleteL2gwConnection(self.app, self.namespace)
179
180 def test_delete_l2gateway_connection(self):
181 """Test Delete l2gateway-connection."""
182
183 fake_connection = fakes.FakeL2GWConnection.create_l2gw_connection()
184 self.app.client_manager.network._delete = mock.Mock()
185 arg_list = [fake_connection['id']]
186 verify_list = [
187 (osc_l2gw_conn.L2_GATEWAY_CONNECTIONS, [fake_connection['id']])
188 ]
189
190 parsed_args = self.check_parser(self.cmd, arg_list, verify_list)
191
192 result = self.cmd.take_action(parsed_args)
193
194 self.app.client_manager.network._delete.assert_called_once_with(
195 sdk_objects.L2gwConnection, fake_connection['id'])
196 self.assertIsNone(result)