| 1 |
# Copyright (c) 2017 Huawei Technologies India Pvt.Limited. |
| 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 unittest import mock |
| 17 |
|
| 18 |
from osc_lib import exceptions |
| 19 |
import testtools |
| 20 |
|
| 21 |
from neutronclient.osc.v2.sfc import sfc_port_chain |
| 22 |
from neutronclient.tests.unit.osc.v2.sfc import fakes |
| 23 |
|
| 24 |
|
| 25 |
def _get_id(client, id_or_name, resource): |
| 26 |
return id_or_name |
| 27 |
|
| 28 |
|
| 29 |
class TestCreateSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 30 |
# The new port_chain created |
| 31 |
_port_chain = fakes.FakeSfcPortChain.create_port_chain() |
| 32 |
|
| 33 |
columns = ('Chain Parameters', |
| 34 |
'Description', |
| 35 |
'Flow Classifiers', |
| 36 |
'ID', |
| 37 |
'Name', |
| 38 |
'Port Pair Groups', |
| 39 |
'Project') |
| 40 |
|
| 41 |
def get_data(self): |
| 42 |
return ( |
| 43 |
self._port_chain['chain_parameters'], |
| 44 |
self._port_chain['description'], |
| 45 |
self._port_chain['flow_classifiers'], |
| 46 |
self._port_chain['id'], |
| 47 |
self._port_chain['name'], |
| 48 |
self._port_chain['port_pair_groups'], |
| 49 |
self._port_chain['project_id'], |
| 50 |
) |
| 51 |
|
| 52 |
def setUp(self): |
| 53 |
super(TestCreateSfcPortChain, self).setUp() |
| 54 |
self.network.create_sfc_port_chain = mock.Mock( |
| 55 |
return_value=self._port_chain) |
| 56 |
self.data = self.get_data() |
| 57 |
|
| 58 |
# Get the command object to test |
| 59 |
self.cmd = sfc_port_chain.CreateSfcPortChain(self.app, self.namespace) |
| 60 |
|
| 61 |
def test_create_port_chain_default_options(self): |
| 62 |
arglist = [ |
| 63 |
self._port_chain['name'], |
| 64 |
"--port-pair-group", self._port_chain['port_pair_groups'] |
| 65 |
] |
| 66 |
verifylist = [ |
| 67 |
('name', self._port_chain['name']), |
| 68 |
('port_pair_groups', [self._port_chain['port_pair_groups']]), |
| 69 |
('flow_classifiers', []), |
| 70 |
('chain_parameters', None), |
| 71 |
] |
| 72 |
|
| 73 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 74 |
columns, data = (self.cmd.take_action(parsed_args)) |
| 75 |
|
| 76 |
self.network.create_sfc_port_chain.assert_called_once_with( |
| 77 |
**{ |
| 78 |
'name': self._port_chain['name'], |
| 79 |
'port_pair_groups': [self._port_chain['port_pair_groups']] |
| 80 |
} |
| 81 |
) |
| 82 |
self.assertEqual(self.columns, columns) |
| 83 |
self.assertEqual(self.data, data) |
| 84 |
|
| 85 |
def test_create_port_chain_all_options(self): |
| 86 |
arglist = [ |
| 87 |
"--description", self._port_chain['description'], |
| 88 |
"--port-pair-group", self._port_chain['port_pair_groups'], |
| 89 |
self._port_chain['name'], |
| 90 |
"--flow-classifier", self._port_chain['flow_classifiers'], |
| 91 |
"--chain-parameters", 'correlation=mpls,symmetric=true', |
| 92 |
] |
| 93 |
|
| 94 |
cp = {'correlation': 'mpls', 'symmetric': 'true'} |
| 95 |
|
| 96 |
verifylist = [ |
| 97 |
('port_pair_groups', [self._port_chain['port_pair_groups']]), |
| 98 |
('name', self._port_chain['name']), |
| 99 |
('description', self._port_chain['description']), |
| 100 |
('flow_classifiers', [self._port_chain['flow_classifiers']]), |
| 101 |
('chain_parameters', [cp]) |
| 102 |
] |
| 103 |
|
| 104 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 105 |
columns, data = (self.cmd.take_action(parsed_args)) |
| 106 |
|
| 107 |
self.network.create_sfc_port_chain.assert_called_once_with( |
| 108 |
**{ |
| 109 |
'name': self._port_chain['name'], |
| 110 |
'port_pair_groups': [self._port_chain['port_pair_groups']], |
| 111 |
'description': self._port_chain['description'], |
| 112 |
'flow_classifiers': [self._port_chain['flow_classifiers']], |
| 113 |
'chain_parameters': cp |
| 114 |
} |
| 115 |
) |
| 116 |
self.assertEqual(self.columns, columns) |
| 117 |
self.assertEqual(self.data, data) |
| 118 |
|
| 119 |
|
| 120 |
class TestDeleteSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 121 |
|
| 122 |
_port_chain = fakes.FakeSfcPortChain.create_port_chains(count=1) |
| 123 |
|
| 124 |
def setUp(self): |
| 125 |
super(TestDeleteSfcPortChain, self).setUp() |
| 126 |
self.network.delete_sfc_port_chain = mock.Mock(return_value=None) |
| 127 |
self.cmd = sfc_port_chain.DeleteSfcPortChain(self.app, self.namespace) |
| 128 |
|
| 129 |
def test_delete_port_chain(self): |
| 130 |
client = self.app.client_manager.network |
| 131 |
mock_port_chain_delete = client.delete_sfc_port_chain |
| 132 |
arglist = [ |
| 133 |
self._port_chain[0]['id'], |
| 134 |
] |
| 135 |
verifylist = [ |
| 136 |
('port_chain', [self._port_chain[0]['id']]), |
| 137 |
] |
| 138 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 139 |
result = self.cmd.take_action(parsed_args) |
| 140 |
mock_port_chain_delete.assert_called_once_with( |
| 141 |
self._port_chain[0]['id']) |
| 142 |
self.assertIsNone(result) |
| 143 |
|
| 144 |
def test_delete_multiple_port_chains_with_exception(self): |
| 145 |
client = self.app.client_manager.network |
| 146 |
target1 = self._port_chain[0]['id'] |
| 147 |
arglist = [target1] |
| 148 |
verifylist = [('port_chain', [target1])] |
| 149 |
|
| 150 |
client.find_sfc_port_chain.side_effect = [ |
| 151 |
target1, exceptions.CommandError |
| 152 |
] |
| 153 |
|
| 154 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 155 |
msg = "1 of 2 port chain(s) failed to delete." |
| 156 |
with testtools.ExpectedException(exceptions.CommandError) as e: |
| 157 |
self.cmd.take_action(parsed_args) |
| 158 |
self.assertEqual(msg, str(e)) |
| 159 |
|
| 160 |
|
| 161 |
class TestListSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 162 |
_port_chains = fakes.FakeSfcPortChain.create_port_chains(count=1) |
| 163 |
columns = ('ID', 'Name', 'Port Pair Groups', 'Flow Classifiers', |
| 164 |
'Chain Parameters') |
| 165 |
columns_long = ('ID', 'Name', 'Port Pair Groups', 'Flow Classifiers', |
| 166 |
'Chain Parameters', 'Description', 'Project') |
| 167 |
_port_chain = _port_chains[0] |
| 168 |
data = [ |
| 169 |
_port_chain['id'], |
| 170 |
_port_chain['name'], |
| 171 |
_port_chain['port_pair_groups'], |
| 172 |
_port_chain['flow_classifiers'], |
| 173 |
_port_chain['chain_parameters'], |
| 174 |
] |
| 175 |
data_long = [ |
| 176 |
_port_chain['id'], |
| 177 |
_port_chain['name'], |
| 178 |
_port_chain['project_id'], |
| 179 |
_port_chain['port_pair_groups'], |
| 180 |
_port_chain['flow_classifiers'], |
| 181 |
_port_chain['chain_parameters'], |
| 182 |
_port_chain['description'] |
| 183 |
] |
| 184 |
_port_chain1 = {'port_chains': _port_chain} |
| 185 |
_port_chain_id = _port_chain['id'] |
| 186 |
|
| 187 |
def setUp(self): |
| 188 |
super(TestListSfcPortChain, self).setUp() |
| 189 |
self.network.sfc_port_chains = mock.Mock( |
| 190 |
return_value=self._port_chains |
| 191 |
) |
| 192 |
# Get the command object to test |
| 193 |
self.cmd = sfc_port_chain.ListSfcPortChain(self.app, self.namespace) |
| 194 |
|
| 195 |
def test_list_port_chains(self): |
| 196 |
arglist = [] |
| 197 |
verifylist = [] |
| 198 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 199 |
columns = self.cmd.take_action(parsed_args)[0] |
| 200 |
pcs = self.network.sfc_port_chains() |
| 201 |
pc = pcs[0] |
| 202 |
data = [ |
| 203 |
pc['id'], |
| 204 |
pc['name'], |
| 205 |
pc['port_pair_groups'], |
| 206 |
pc['flow_classifiers'], |
| 207 |
pc['chain_parameters'], |
| 208 |
] |
| 209 |
self.assertEqual(list(self.columns), columns) |
| 210 |
self.assertEqual(self.data, data) |
| 211 |
|
| 212 |
def test_list_port_chain_with_long_opion(self): |
| 213 |
arglist = ['--long'] |
| 214 |
verifylist = [('long', True)] |
| 215 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 216 |
columns = self.cmd.take_action(parsed_args)[0] |
| 217 |
pcs = self.network.sfc_port_chains() |
| 218 |
pc = pcs[0] |
| 219 |
data = [ |
| 220 |
pc['id'], |
| 221 |
pc['name'], |
| 222 |
pc['project_id'], |
| 223 |
pc['port_pair_groups'], |
| 224 |
pc['flow_classifiers'], |
| 225 |
pc['chain_parameters'], |
| 226 |
pc['description'] |
| 227 |
] |
| 228 |
self.assertEqual(list(self.columns_long), columns) |
| 229 |
self.assertEqual(self.data_long, data) |
| 230 |
|
| 231 |
|
| 232 |
class TestSetSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 233 |
_port_chain = fakes.FakeSfcPortChain.create_port_chain() |
| 234 |
resource = _port_chain |
| 235 |
res = 'port_chain' |
| 236 |
_port_chain_name = _port_chain['name'] |
| 237 |
_port_chain_id = _port_chain['id'] |
| 238 |
pc_ppg = _port_chain['port_pair_groups'] |
| 239 |
pc_fc = _port_chain['flow_classifiers'] |
| 240 |
|
| 241 |
def setUp(self): |
| 242 |
super(TestSetSfcPortChain, self).setUp() |
| 243 |
self.mocked = self.network.update_sfc_port_chain |
| 244 |
self.cmd = sfc_port_chain.SetSfcPortChain(self.app, self.namespace) |
| 245 |
|
| 246 |
def test_set_port_chain(self): |
| 247 |
client = self.app.client_manager.network |
| 248 |
mock_port_chain_update = client.update_sfc_port_chain |
| 249 |
arglist = [ |
| 250 |
self._port_chain_name, |
| 251 |
'--name', 'name_updated', |
| 252 |
'--description', 'desc_updated', |
| 253 |
] |
| 254 |
verifylist = [ |
| 255 |
('port_chain', self._port_chain_name), |
| 256 |
('name', 'name_updated'), |
| 257 |
('description', 'desc_updated'), |
| 258 |
] |
| 259 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 260 |
result = self.cmd.take_action(parsed_args) |
| 261 |
attrs = {'name': 'name_updated', 'description': 'desc_updated'} |
| 262 |
mock_port_chain_update.assert_called_once_with(self._port_chain_name, |
| 263 |
**attrs) |
| 264 |
self.assertIsNone(result) |
| 265 |
|
| 266 |
def test_set_flow_classifiers(self): |
| 267 |
target = self.resource['id'] |
| 268 |
fc1 = 'flow_classifier1' |
| 269 |
fc2 = 'flow_classifier2' |
| 270 |
|
| 271 |
self.network.find_sfc_port_chain = mock.Mock( |
| 272 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 273 |
{'id': name_or_id, 'flow_classifiers': [self.pc_fc]} |
| 274 |
) |
| 275 |
self.network.find_sfc_flow_classifier.side_effect = \ |
| 276 |
lambda name_or_id, ignore_missing=False: {'id': name_or_id} |
| 277 |
arglist = [ |
| 278 |
target, |
| 279 |
'--flow-classifier', fc1, |
| 280 |
'--flow-classifier', fc2, |
| 281 |
] |
| 282 |
verifylist = [ |
| 283 |
(self.res, target), |
| 284 |
('flow_classifiers', [fc1, fc2]) |
| 285 |
] |
| 286 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 287 |
result = self.cmd.take_action(parsed_args) |
| 288 |
expect = {'flow_classifiers': [self.pc_fc, fc1, fc2]} |
| 289 |
self.mocked.assert_called_once_with(target, **expect) |
| 290 |
self.assertIsNone(result) |
| 291 |
|
| 292 |
def test_set_no_flow_classifier(self): |
| 293 |
client = self.app.client_manager.network |
| 294 |
mock_port_chain_update = client.update_sfc_port_chain |
| 295 |
arglist = [ |
| 296 |
self._port_chain_name, |
| 297 |
'--no-flow-classifier', |
| 298 |
] |
| 299 |
verifylist = [ |
| 300 |
('port_chain', self._port_chain_name), |
| 301 |
('no_flow_classifier', True), |
| 302 |
] |
| 303 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 304 |
result = self.cmd.take_action(parsed_args) |
| 305 |
attrs = {'flow_classifiers': []} |
| 306 |
mock_port_chain_update.assert_called_once_with(self._port_chain_name, |
| 307 |
**attrs) |
| 308 |
self.assertIsNone(result) |
| 309 |
|
| 310 |
def test_set_port_pair_groups(self): |
| 311 |
target = self.resource['id'] |
| 312 |
existing_ppg = self.pc_ppg |
| 313 |
ppg1 = 'port_pair_group1' |
| 314 |
ppg2 = 'port_pair_group2' |
| 315 |
|
| 316 |
self.network.find_sfc_port_chain = mock.Mock( |
| 317 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 318 |
{'id': name_or_id, 'port_pair_groups': [self.pc_ppg]} |
| 319 |
) |
| 320 |
arglist = [ |
| 321 |
target, |
| 322 |
'--port-pair-group', ppg1, |
| 323 |
'--port-pair-group', ppg2, |
| 324 |
] |
| 325 |
verifylist = [ |
| 326 |
(self.res, target), |
| 327 |
('port_pair_groups', [ppg1, ppg2]) |
| 328 |
] |
| 329 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 330 |
result = self.cmd.take_action(parsed_args) |
| 331 |
expect = {'port_pair_groups': [existing_ppg, ppg1, ppg2]} |
| 332 |
self.mocked.assert_called_once_with(target, **expect) |
| 333 |
self.assertIsNone(result) |
| 334 |
|
| 335 |
def test_set_no_port_pair_group(self): |
| 336 |
target = self.resource['id'] |
| 337 |
ppg1 = 'port_pair_group1' |
| 338 |
|
| 339 |
arglist = [ |
| 340 |
target, |
| 341 |
'--no-port-pair-group', |
| 342 |
'--port-pair-group', ppg1, |
| 343 |
] |
| 344 |
verifylist = [ |
| 345 |
(self.res, target), |
| 346 |
('no_port_pair_group', True), |
| 347 |
('port_pair_groups', [ppg1]) |
| 348 |
] |
| 349 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 350 |
result = self.cmd.take_action(parsed_args) |
| 351 |
expect = {'port_pair_groups': [ppg1]} |
| 352 |
self.mocked.assert_called_once_with(target, **expect) |
| 353 |
self.assertIsNone(result) |
| 354 |
|
| 355 |
def test_set_only_no_port_pair_group(self): |
| 356 |
target = self.resource['id'] |
| 357 |
arglist = [ |
| 358 |
target, |
| 359 |
'--no-port-pair-group', |
| 360 |
] |
| 361 |
verifylist = [ |
| 362 |
(self.res, target), |
| 363 |
('no_port_pair_group', True), |
| 364 |
] |
| 365 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 366 |
self.assertRaises( |
| 367 |
exceptions.CommandError, self.cmd.take_action, parsed_args) |
| 368 |
|
| 369 |
|
| 370 |
class TestShowSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 371 |
|
| 372 |
_pc = fakes.FakeSfcPortChain.create_port_chain() |
| 373 |
data = ( |
| 374 |
_pc['chain_parameters'], |
| 375 |
_pc['description'], |
| 376 |
_pc['flow_classifiers'], |
| 377 |
_pc['id'], |
| 378 |
_pc['name'], |
| 379 |
_pc['port_pair_groups'], |
| 380 |
_pc['project_id'] |
| 381 |
) |
| 382 |
_port_chain = _pc |
| 383 |
_port_chain_id = _pc['id'] |
| 384 |
columns = ('Chain Parameters', |
| 385 |
'Description', |
| 386 |
'Flow Classifiers', |
| 387 |
'ID', |
| 388 |
'Name', |
| 389 |
'Port Pair Groups', |
| 390 |
'Project') |
| 391 |
|
| 392 |
def setUp(self): |
| 393 |
super(TestShowSfcPortChain, self).setUp() |
| 394 |
self.network.get_sfc_port_chain = mock.Mock( |
| 395 |
return_value=self._port_chain |
| 396 |
) |
| 397 |
# Get the command object to test |
| 398 |
self.cmd = sfc_port_chain.ShowSfcPortChain(self.app, self.namespace) |
| 399 |
|
| 400 |
def test_show_port_chain(self): |
| 401 |
client = self.app.client_manager.network |
| 402 |
mock_port_chain_show = client.get_sfc_port_chain |
| 403 |
arglist = [ |
| 404 |
self._port_chain_id, |
| 405 |
] |
| 406 |
verifylist = [ |
| 407 |
('port_chain', self._port_chain_id), |
| 408 |
] |
| 409 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 410 |
|
| 411 |
columns, data = self.cmd.take_action(parsed_args) |
| 412 |
mock_port_chain_show.assert_called_once_with(self._port_chain_id) |
| 413 |
self.assertEqual(self.columns, columns) |
| 414 |
self.assertEqual(self.data, data) |
| 415 |
|
| 416 |
|
| 417 |
class TestUnsetSfcPortChain(fakes.TestNeutronClientOSCV2): |
| 418 |
_port_chain = fakes.FakeSfcPortChain.create_port_chain() |
| 419 |
resource = _port_chain |
| 420 |
res = 'port_chain' |
| 421 |
_port_chain_name = _port_chain['name'] |
| 422 |
_port_chain_id = _port_chain['id'] |
| 423 |
pc_ppg = _port_chain['port_pair_groups'] |
| 424 |
pc_fc = _port_chain['flow_classifiers'] |
| 425 |
|
| 426 |
def setUp(self): |
| 427 |
super(TestUnsetSfcPortChain, self).setUp() |
| 428 |
self.network.update_sfc_port_chain = mock.Mock( |
| 429 |
return_value=None) |
| 430 |
self.mocked = self.network.update_sfc_port_chain |
| 431 |
self.cmd = sfc_port_chain.UnsetSfcPortChain(self.app, self.namespace) |
| 432 |
|
| 433 |
def test_unset_port_pair_group(self): |
| 434 |
target = self.resource['id'] |
| 435 |
ppg1 = 'port_pair_group1' |
| 436 |
|
| 437 |
self.network.find_sfc_port_chain = mock.Mock( |
| 438 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 439 |
{'id': name_or_id, 'port_pair_groups': [self.pc_ppg]} |
| 440 |
) |
| 441 |
self.network.find_sfc_port_pair_group.side_effect = \ |
| 442 |
lambda name_or_id, ignore_missing=False: {'id': name_or_id} |
| 443 |
|
| 444 |
arglist = [ |
| 445 |
target, |
| 446 |
'--port-pair-group', ppg1, |
| 447 |
] |
| 448 |
verifylist = [ |
| 449 |
(self.res, target), |
| 450 |
('port_pair_groups', [ppg1]) |
| 451 |
] |
| 452 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 453 |
result = self.cmd.take_action(parsed_args) |
| 454 |
expect = {'port_pair_groups': [self.pc_ppg]} |
| 455 |
self.mocked.assert_called_once_with(target, **expect) |
| 456 |
self.assertIsNone(result) |
| 457 |
|
| 458 |
def test_unset_flow_classifier(self): |
| 459 |
target = self.resource['id'] |
| 460 |
fc1 = 'flow_classifier1' |
| 461 |
self.network.find_sfc_port_chain = mock.Mock( |
| 462 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 463 |
{'id': name_or_id, 'flow_classifiers': [self.pc_fc]} |
| 464 |
) |
| 465 |
self.network.find_sfc_flow_classifier.side_effect = \ |
| 466 |
lambda name_or_id, ignore_missing=False: {'id': name_or_id} |
| 467 |
|
| 468 |
arglist = [ |
| 469 |
target, |
| 470 |
'--flow-classifier', fc1, |
| 471 |
] |
| 472 |
verifylist = [ |
| 473 |
(self.res, target), |
| 474 |
('flow_classifiers', [fc1]) |
| 475 |
] |
| 476 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 477 |
result = self.cmd.take_action(parsed_args) |
| 478 |
expect = {'flow_classifiers': [self.pc_fc]} |
| 479 |
self.mocked.assert_called_once_with(target, **expect) |
| 480 |
self.assertIsNone(result) |
| 481 |
|
| 482 |
def test_unset_all_flow_classifier(self): |
| 483 |
client = self.app.client_manager.network |
| 484 |
target = self.resource['id'] |
| 485 |
mock_port_chain_update = client.update_sfc_port_chain |
| 486 |
arglist = [ |
| 487 |
target, |
| 488 |
'--all-flow-classifier', |
| 489 |
] |
| 490 |
verifylist = [ |
| 491 |
(self.res, target), |
| 492 |
('all_flow_classifier', True) |
| 493 |
] |
| 494 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 495 |
result = self.cmd.take_action(parsed_args) |
| 496 |
expect = {'flow_classifiers': []} |
| 497 |
mock_port_chain_update.assert_called_once_with(target, |
| 498 |
**expect) |
| 499 |
self.assertIsNone(result) |