| 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_flow_classifier |
| 22 |
from neutronclient.tests.unit.osc.v2.sfc import fakes |
| 23 |
|
| 24 |
|
| 25 |
class TestCreateSfcFlowClassifier(fakes.TestNeutronClientOSCV2): |
| 26 |
|
| 27 |
_fc = fakes.FakeSfcFlowClassifier.create_flow_classifier() |
| 28 |
|
| 29 |
columns = ('Description', |
| 30 |
'Destination IP', |
| 31 |
'Destination Port Range Max', |
| 32 |
'Destination Port Range Min', |
| 33 |
'Ethertype', |
| 34 |
'ID', |
| 35 |
'L7 Parameters', |
| 36 |
'Logical Destination Port', |
| 37 |
'Logical Source Port', |
| 38 |
'Name', |
| 39 |
'Project', |
| 40 |
'Protocol', |
| 41 |
'Source IP', |
| 42 |
'Source Port Range Max', |
| 43 |
'Source Port Range Min', |
| 44 |
'Summary',) |
| 45 |
|
| 46 |
def get_data(self): |
| 47 |
return ( |
| 48 |
self._fc['description'], |
| 49 |
self._fc['destination_ip_prefix'], |
| 50 |
self._fc['destination_port_range_max'], |
| 51 |
self._fc['destination_port_range_min'], |
| 52 |
self._fc['ethertype'], |
| 53 |
self._fc['id'], |
| 54 |
self._fc['l7_parameters'], |
| 55 |
self._fc['logical_destination_port'], |
| 56 |
self._fc['logical_source_port'], |
| 57 |
self._fc['name'], |
| 58 |
self._fc['project_id'], |
| 59 |
self._fc['protocol'], |
| 60 |
self._fc['source_ip_prefix'], |
| 61 |
self._fc['source_port_range_max'], |
| 62 |
self._fc['source_port_range_min'] |
| 63 |
) |
| 64 |
|
| 65 |
def setUp(self): |
| 66 |
super(TestCreateSfcFlowClassifier, self).setUp() |
| 67 |
self.network.create_sfc_flow_classifier = mock.Mock( |
| 68 |
return_value=self._fc) |
| 69 |
self.data = self.get_data() |
| 70 |
|
| 71 |
# Get the command object to test |
| 72 |
self.cmd = sfc_flow_classifier.CreateSfcFlowClassifier(self.app, |
| 73 |
self.namespace) |
| 74 |
|
| 75 |
def test_create_flow_classifier_default_options(self): |
| 76 |
arglist = [ |
| 77 |
"--logical-source-port", self._fc['logical_source_port'], |
| 78 |
"--ethertype", self._fc['ethertype'], |
| 79 |
self._fc['name'], |
| 80 |
] |
| 81 |
verifylist = [ |
| 82 |
('logical_source_port', self._fc['logical_source_port']), |
| 83 |
('ethertype', self._fc['ethertype']), |
| 84 |
('name', self._fc['name']), |
| 85 |
] |
| 86 |
|
| 87 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 88 |
columns, data = (self.cmd.take_action(parsed_args)) |
| 89 |
|
| 90 |
self.network.create_sfc_flow_classifier.assert_called_once_with( |
| 91 |
**{'name': self._fc['name'], |
| 92 |
'logical_source_port': self._fc['logical_source_port'], |
| 93 |
'ethertype': self._fc['ethertype'] |
| 94 |
} |
| 95 |
) |
| 96 |
self.assertEqual(self.columns, columns) |
| 97 |
|
| 98 |
def test_create_flow_classifier(self): |
| 99 |
arglist = [ |
| 100 |
"--description", self._fc['description'], |
| 101 |
"--ethertype", self._fc['ethertype'], |
| 102 |
"--protocol", self._fc['protocol'], |
| 103 |
"--source-ip-prefix", self._fc['source_ip_prefix'], |
| 104 |
"--destination-ip-prefix", self._fc['destination_ip_prefix'], |
| 105 |
"--logical-source-port", self._fc['logical_source_port'], |
| 106 |
"--logical-destination-port", self._fc['logical_destination_port'], |
| 107 |
self._fc['name'], |
| 108 |
"--l7-parameters", 'url=path', |
| 109 |
] |
| 110 |
|
| 111 |
param = 'url=path' |
| 112 |
|
| 113 |
verifylist = [ |
| 114 |
('description', self._fc['description']), |
| 115 |
('name', self._fc['name']), |
| 116 |
('ethertype', self._fc['ethertype']), |
| 117 |
('protocol', self._fc['protocol']), |
| 118 |
('source_ip_prefix', self._fc['source_ip_prefix']), |
| 119 |
('destination_ip_prefix', self._fc['destination_ip_prefix']), |
| 120 |
('logical_source_port', self._fc['logical_source_port']), |
| 121 |
('logical_destination_port', |
| 122 |
self._fc['logical_destination_port']), |
| 123 |
('l7_parameters', param) |
| 124 |
] |
| 125 |
|
| 126 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 127 |
columns, data = (self.cmd.take_action(parsed_args)) |
| 128 |
self.network.create_sfc_flow_classifier.assert_called_once_with( |
| 129 |
**{ |
| 130 |
'name': self._fc['name'], |
| 131 |
'description': self._fc['description'], |
| 132 |
'ethertype': self._fc['ethertype'], |
| 133 |
'protocol': self._fc['protocol'], |
| 134 |
'source_ip_prefix': self._fc['source_ip_prefix'], |
| 135 |
'destination_ip_prefix': self._fc['destination_ip_prefix'], |
| 136 |
'logical_source_port': self._fc['logical_source_port'], |
| 137 |
'logical_destination_port': |
| 138 |
self._fc['logical_destination_port'], |
| 139 |
'l7_parameters': param |
| 140 |
} |
| 141 |
) |
| 142 |
self.assertEqual(self.columns, columns) |
| 143 |
|
| 144 |
|
| 145 |
class TestDeleteSfcFlowClassifier(fakes.TestNeutronClientOSCV2): |
| 146 |
|
| 147 |
_flow_classifier = \ |
| 148 |
fakes.FakeSfcFlowClassifier.create_flow_classifiers(count=1) |
| 149 |
|
| 150 |
def setUp(self): |
| 151 |
super(TestDeleteSfcFlowClassifier, self).setUp() |
| 152 |
self.network.delete_sfc_flow_classifier = mock.Mock( |
| 153 |
return_value=None) |
| 154 |
self.cmd = sfc_flow_classifier.DeleteSfcFlowClassifier(self.app, |
| 155 |
self.namespace) |
| 156 |
|
| 157 |
def test_delete_flow_classifier(self): |
| 158 |
client = self.app.client_manager.network |
| 159 |
mock_flow_classifier_delete = client.delete_sfc_flow_classifier |
| 160 |
arglist = [ |
| 161 |
self._flow_classifier[0]['id'], |
| 162 |
] |
| 163 |
verifylist = [ |
| 164 |
('flow_classifier', [self._flow_classifier[0]['id']]), |
| 165 |
] |
| 166 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 167 |
result = self.cmd.take_action(parsed_args) |
| 168 |
mock_flow_classifier_delete.assert_called_once_with( |
| 169 |
self._flow_classifier[0]['id']) |
| 170 |
self.assertIsNone(result) |
| 171 |
|
| 172 |
def test_delete_multiple_flow_classifiers_with_exception(self): |
| 173 |
client = self.app.client_manager.network |
| 174 |
target1 = self._flow_classifier[0]['id'] |
| 175 |
arglist = [target1] |
| 176 |
verifylist = [('flow_classifier', [target1])] |
| 177 |
|
| 178 |
client.find_sfc_flow_classifier.side_effect = [ |
| 179 |
target1, exceptions.CommandError |
| 180 |
] |
| 181 |
|
| 182 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 183 |
msg = "1 of 2 flow classifier(s) failed to delete." |
| 184 |
with testtools.ExpectedException(exceptions.CommandError) as e: |
| 185 |
self.cmd.take_action(parsed_args) |
| 186 |
self.assertEqual(msg, str(e)) |
| 187 |
|
| 188 |
|
| 189 |
class TestSetSfcFlowClassifier(fakes.TestNeutronClientOSCV2): |
| 190 |
_flow_classifier = fakes.FakeSfcFlowClassifier.create_flow_classifier() |
| 191 |
_flow_classifier_name = _flow_classifier['name'] |
| 192 |
_flow_classifier_id = _flow_classifier['id'] |
| 193 |
|
| 194 |
def setUp(self): |
| 195 |
super(TestSetSfcFlowClassifier, self).setUp() |
| 196 |
self.network.update_sfc_flow_classifier = mock.Mock( |
| 197 |
return_value=None) |
| 198 |
self.cmd = sfc_flow_classifier.SetSfcFlowClassifier(self.app, |
| 199 |
self.namespace) |
| 200 |
|
| 201 |
def test_set_flow_classifier(self): |
| 202 |
client = self.app.client_manager.network |
| 203 |
mock_flow_classifier_update = client.update_sfc_flow_classifier |
| 204 |
arglist = [ |
| 205 |
self._flow_classifier_name, |
| 206 |
'--name', 'name_updated', |
| 207 |
'--description', 'desc_updated' |
| 208 |
] |
| 209 |
verifylist = [ |
| 210 |
('flow_classifier', self._flow_classifier_name), |
| 211 |
('name', 'name_updated'), |
| 212 |
('description', 'desc_updated'), |
| 213 |
] |
| 214 |
|
| 215 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 216 |
result = self.cmd.take_action(parsed_args) |
| 217 |
|
| 218 |
attrs = { |
| 219 |
'name': 'name_updated', |
| 220 |
'description': 'desc_updated'} |
| 221 |
mock_flow_classifier_update.assert_called_once_with( |
| 222 |
self._flow_classifier_name, **attrs) |
| 223 |
self.assertIsNone(result) |
| 224 |
|
| 225 |
|
| 226 |
class TestShowSfcFlowClassifier(fakes.TestNeutronClientOSCV2): |
| 227 |
|
| 228 |
_fc = fakes.FakeSfcFlowClassifier.create_flow_classifier() |
| 229 |
data = ( |
| 230 |
_fc['description'], |
| 231 |
_fc['destination_ip_prefix'], |
| 232 |
_fc['destination_port_range_max'], |
| 233 |
_fc['destination_port_range_min'], |
| 234 |
_fc['ethertype'], |
| 235 |
_fc['id'], |
| 236 |
_fc['l7_parameters'], |
| 237 |
_fc['logical_destination_port'], |
| 238 |
_fc['logical_source_port'], |
| 239 |
_fc['name'], |
| 240 |
_fc['project_id'], |
| 241 |
_fc['protocol'], |
| 242 |
_fc['source_ip_prefix'], |
| 243 |
_fc['source_port_range_max'], |
| 244 |
_fc['source_port_range_min'] |
| 245 |
) |
| 246 |
_flow_classifier = _fc |
| 247 |
_flow_classifier_id = _fc['id'] |
| 248 |
columns = ('Description', |
| 249 |
'Destination IP', |
| 250 |
'Destination Port Range Max', |
| 251 |
'Destination Port Range Min', |
| 252 |
'Ethertype', |
| 253 |
'ID', |
| 254 |
'L7 Parameters', |
| 255 |
'Logical Destination Port', |
| 256 |
'Logical Source Port', |
| 257 |
'Name', |
| 258 |
'Project', |
| 259 |
'Protocol', |
| 260 |
'Source IP', |
| 261 |
'Source Port Range Max', |
| 262 |
'Source Port Range Min', |
| 263 |
'Summary',) |
| 264 |
|
| 265 |
def setUp(self): |
| 266 |
super(TestShowSfcFlowClassifier, self).setUp() |
| 267 |
self.network.get_sfc_flow_classifier = mock.Mock( |
| 268 |
return_value=self._flow_classifier |
| 269 |
) |
| 270 |
# Get the command object to test |
| 271 |
self.cmd = sfc_flow_classifier.ShowSfcFlowClassifier(self.app, |
| 272 |
self.namespace) |
| 273 |
|
| 274 |
def test_show_flow_classifier(self): |
| 275 |
client = self.app.client_manager.network |
| 276 |
mock_flow_classifier_show = client.get_sfc_flow_classifier |
| 277 |
arglist = [ |
| 278 |
self._flow_classifier_id, |
| 279 |
] |
| 280 |
verifylist = [ |
| 281 |
('flow_classifier', self._flow_classifier_id), |
| 282 |
] |
| 283 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 284 |
|
| 285 |
columns, data = self.cmd.take_action(parsed_args) |
| 286 |
mock_flow_classifier_show.assert_called_once_with( |
| 287 |
self._flow_classifier_id) |
| 288 |
self.assertEqual(self.columns, columns) |
| 289 |
|
| 290 |
|
| 291 |
class TestListSfcFlowClassifier(fakes.TestNeutronClientOSCV2): |
| 292 |
|
| 293 |
_fc = fakes.FakeSfcFlowClassifier.create_flow_classifiers(count=1) |
| 294 |
|
| 295 |
columns = ('ID', 'Name', 'Summary') |
| 296 |
|
| 297 |
columns_long = ('ID', 'Name', 'Protocol', 'Ethertype', 'Source IP', |
| 298 |
'Destination IP', 'Logical Source Port', |
| 299 |
'Logical Destination Port', 'Source Port Range Min', |
| 300 |
'Source Port Range Max', 'Destination Port Range Min', |
| 301 |
'Destination Port Range Max', 'L7 Parameters', |
| 302 |
'Description', 'Project') |
| 303 |
_flow_classifier = _fc[0] |
| 304 |
data = [ |
| 305 |
_flow_classifier['id'], |
| 306 |
_flow_classifier['name'], |
| 307 |
_flow_classifier['protocol'], |
| 308 |
_flow_classifier['source_ip_prefix'], |
| 309 |
_flow_classifier['destination_ip_prefix'], |
| 310 |
_flow_classifier['logical_source_port'], |
| 311 |
_flow_classifier['logical_destination_port'] |
| 312 |
] |
| 313 |
data_long = [ |
| 314 |
_flow_classifier['id'], |
| 315 |
_flow_classifier['name'], |
| 316 |
_flow_classifier['protocol'], |
| 317 |
_flow_classifier['ethertype'], |
| 318 |
_flow_classifier['source_ip_prefix'], |
| 319 |
_flow_classifier['destination_ip_prefix'], |
| 320 |
_flow_classifier['logical_source_port'], |
| 321 |
_flow_classifier['logical_destination_port'], |
| 322 |
_flow_classifier['source_port_range_min'], |
| 323 |
_flow_classifier['source_port_range_max'], |
| 324 |
_flow_classifier['destination_port_range_min'], |
| 325 |
_flow_classifier['destination_port_range_max'], |
| 326 |
_flow_classifier['l7_parameters'], |
| 327 |
_flow_classifier['description'] |
| 328 |
] |
| 329 |
|
| 330 |
_flow_classifier1 = {'flow_classifiers': _flow_classifier} |
| 331 |
_flow_classifier_id = _flow_classifier['id'] |
| 332 |
|
| 333 |
def setUp(self): |
| 334 |
super(TestListSfcFlowClassifier, self).setUp() |
| 335 |
self.network.sfc_flow_classifiers = mock.Mock( |
| 336 |
return_value=self._fc |
| 337 |
) |
| 338 |
# Get the command object to test |
| 339 |
self.cmd = sfc_flow_classifier.ListSfcFlowClassifier(self.app, |
| 340 |
self.namespace) |
| 341 |
|
| 342 |
def test_list_flow_classifiers(self): |
| 343 |
arglist = [] |
| 344 |
verifylist = [] |
| 345 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 346 |
columns = self.cmd.take_action(parsed_args) |
| 347 |
fcs = self.network.sfc_flow_classifiers() |
| 348 |
fc = fcs[0] |
| 349 |
data = [ |
| 350 |
fc['id'], |
| 351 |
fc['name'], |
| 352 |
fc['protocol'], |
| 353 |
fc['source_ip_prefix'], |
| 354 |
fc['destination_ip_prefix'], |
| 355 |
fc['logical_source_port'], |
| 356 |
fc['logical_destination_port'] |
| 357 |
] |
| 358 |
self.assertEqual(list(self.columns), columns[0]) |
| 359 |
self.assertEqual(self.data, data) |
| 360 |
|
| 361 |
def test_list_with_long_option(self): |
| 362 |
arglist = ['--long'] |
| 363 |
verifylist = [('long', True)] |
| 364 |
fcs = self.network.sfc_flow_classifiers() |
| 365 |
fc = fcs[0] |
| 366 |
data = [ |
| 367 |
fc['id'], |
| 368 |
fc['name'], |
| 369 |
fc['protocol'], |
| 370 |
fc['ethertype'], |
| 371 |
fc['source_ip_prefix'], |
| 372 |
fc['destination_ip_prefix'], |
| 373 |
fc['logical_source_port'], |
| 374 |
fc['logical_destination_port'], |
| 375 |
fc['source_port_range_min'], |
| 376 |
fc['source_port_range_max'], |
| 377 |
fc['destination_port_range_min'], |
| 378 |
fc['destination_port_range_max'], |
| 379 |
fc['l7_parameters'], |
| 380 |
fc['description'] |
| 381 |
] |
| 382 |
parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 383 |
columns_long = self.cmd.take_action(parsed_args)[0] |
| 384 |
self.assertEqual(list(self.columns_long), columns_long) |
| 385 |
self.assertEqual(self.data_long, data) |