| 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 |
import argparse |
| 17 |
from unittest import mock |
| 18 |
|
| 19 |
|
| 20 |
from osc_lib.tests import utils |
| 21 |
from oslo_utils import uuidutils |
| 22 |
|
| 23 |
from openstack.network.v2 import sfc_flow_classifier as flow_classifier |
| 24 |
from openstack.network.v2 import sfc_port_chain as port_chain |
| 25 |
from openstack.network.v2 import sfc_port_pair as port_pair |
| 26 |
from openstack.network.v2 import sfc_port_pair_group as port_pair_group |
| 27 |
from openstack.network.v2 import sfc_service_graph as service_graph |
| 28 |
|
| 29 |
|
| 30 |
class TestNeutronClientOSCV2(utils.TestCommand): |
| 31 |
|
| 32 |
def setUp(self): |
| 33 |
super(TestNeutronClientOSCV2, self).setUp() |
| 34 |
self.namespace = argparse.Namespace() |
| 35 |
self.app.client_manager.session = mock.Mock() |
| 36 |
self.app.client_manager.network = mock.Mock() |
| 37 |
self.network = self.app.client_manager.network |
| 38 |
self.network.find_sfc_flow_classifier = mock.Mock( |
| 39 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 40 |
{'id': name_or_id} |
| 41 |
) |
| 42 |
self.network.find_sfc_port_chain = mock.Mock( |
| 43 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 44 |
{'id': name_or_id} |
| 45 |
) |
| 46 |
self.network.find_sfc_port_pair = mock.Mock( |
| 47 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 48 |
{'id': name_or_id} |
| 49 |
) |
| 50 |
self.network.find_sfc_port_pair_group = mock.Mock( |
| 51 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 52 |
{'id': name_or_id} |
| 53 |
) |
| 54 |
self.network.find_sfc_service_graph = mock.Mock( |
| 55 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 56 |
{'id': name_or_id} |
| 57 |
) |
| 58 |
self.network.find_port = mock.Mock( |
| 59 |
side_effect=lambda name_or_id, ignore_missing=False: |
| 60 |
{'id': name_or_id} |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
class FakeSfcPortPair(object): |
| 65 |
"""Fake port pair attributes.""" |
| 66 |
|
| 67 |
@staticmethod |
| 68 |
def create_port_pair(attrs=None): |
| 69 |
"""Create a fake port pair. |
| 70 |
|
| 71 |
:param Dictionary attrs: |
| 72 |
A dictionary with all attributes |
| 73 |
:return: |
| 74 |
A Dictionary with id, name, description, ingress, egress, |
| 75 |
service-function-parameter, project_id |
| 76 |
""" |
| 77 |
attrs = attrs or {} |
| 78 |
|
| 79 |
# Set default attributes. |
| 80 |
port_pair_attrs = { |
| 81 |
'description': 'description', |
| 82 |
'egress': uuidutils.generate_uuid(), |
| 83 |
'id': uuidutils.generate_uuid(), |
| 84 |
'ingress': uuidutils.generate_uuid(), |
| 85 |
'name': 'port-pair-name', |
| 86 |
'service_function_parameters': [('correlation', None), |
| 87 |
('weight', 1)], |
| 88 |
'project_id': uuidutils.generate_uuid(), |
| 89 |
} |
| 90 |
|
| 91 |
# Overwrite default attributes. |
| 92 |
port_pair_attrs.update(attrs) |
| 93 |
return port_pair.SfcPortPair(**port_pair_attrs) |
| 94 |
|
| 95 |
@staticmethod |
| 96 |
def create_port_pairs(attrs=None, count=1): |
| 97 |
"""Create multiple port_pairs. |
| 98 |
|
| 99 |
:param Dictionary attrs: |
| 100 |
A dictionary with all attributes |
| 101 |
:param int count: |
| 102 |
The number of port_pairs to fake |
| 103 |
:return: |
| 104 |
A list of dictionaries faking the port_pairs |
| 105 |
""" |
| 106 |
port_pairs = [] |
| 107 |
for _ in range(count): |
| 108 |
port_pairs.append(FakeSfcPortPair.create_port_pair(attrs)) |
| 109 |
|
| 110 |
return port_pairs |
| 111 |
|
| 112 |
|
| 113 |
class FakeSfcPortPairGroup(object): |
| 114 |
"""Fake port pair group attributes.""" |
| 115 |
|
| 116 |
@staticmethod |
| 117 |
def create_port_pair_group(attrs=None): |
| 118 |
"""Create a fake port pair group. |
| 119 |
|
| 120 |
:param Dictionary attrs: |
| 121 |
A dictionary with all attributes |
| 122 |
:return: |
| 123 |
A Dictionary with id, name, description, port_pairs, group_id |
| 124 |
port_pair_group_parameters, project_id |
| 125 |
""" |
| 126 |
attrs = attrs or {} |
| 127 |
|
| 128 |
# Set default attributes. |
| 129 |
port_pair_group_attrs = { |
| 130 |
'id': uuidutils.generate_uuid(), |
| 131 |
'name': 'port-pair-group-name', |
| 132 |
'description': 'description', |
| 133 |
'port_pairs': uuidutils.generate_uuid(), |
| 134 |
'port_pair_group_parameters': {"lb_fields": []}, |
| 135 |
'project_id': uuidutils.generate_uuid(), |
| 136 |
'tap_enabled': False |
| 137 |
} |
| 138 |
|
| 139 |
port_pair_group_attrs.update(attrs) |
| 140 |
return port_pair_group.SfcPortPairGroup(**port_pair_group_attrs) |
| 141 |
|
| 142 |
@staticmethod |
| 143 |
def create_port_pair_groups(attrs=None, count=1): |
| 144 |
"""Create multiple port pair groups. |
| 145 |
|
| 146 |
:param Dictionary attrs: |
| 147 |
A dictionary with all attributes |
| 148 |
:param int count: |
| 149 |
The number of port_pair_groups to fake |
| 150 |
:return: |
| 151 |
A list of dictionaries faking the port pair groups |
| 152 |
""" |
| 153 |
port_pair_groups = [] |
| 154 |
for _ in range(count): |
| 155 |
port_pair_groups.append( |
| 156 |
FakeSfcPortPairGroup.create_port_pair_group(attrs)) |
| 157 |
|
| 158 |
return port_pair_groups |
| 159 |
|
| 160 |
|
| 161 |
class FakeSfcFlowClassifier(object): |
| 162 |
"""Fake flow classifier attributes.""" |
| 163 |
|
| 164 |
@staticmethod |
| 165 |
def create_flow_classifier(attrs=None): |
| 166 |
"""Create a fake flow classifier. |
| 167 |
|
| 168 |
:param Dictionary attrs: |
| 169 |
A dictionary with all attributes |
| 170 |
:return: |
| 171 |
A Dictionary with faking port chain attributes |
| 172 |
""" |
| 173 |
attrs = attrs or {} |
| 174 |
|
| 175 |
# Set default attributes. |
| 176 |
flow_classifier_attrs = { |
| 177 |
'id': uuidutils.generate_uuid(), |
| 178 |
'destination_ip_prefix': '2.2.2.2/32', |
| 179 |
'destination_port_range_max': '90', |
| 180 |
'destination_port_range_min': '80', |
| 181 |
'ethertype': 'IPv4', |
| 182 |
'logical_destination_port': uuidutils.generate_uuid(), |
| 183 |
'logical_source_port': uuidutils.generate_uuid(), |
| 184 |
'name': 'flow-classifier-name', |
| 185 |
'description': 'fc_description', |
| 186 |
'protocol': 'tcp', |
| 187 |
'source_ip_prefix': '1.1.1.1/32', |
| 188 |
'source_port_range_max': '20', |
| 189 |
'source_port_range_min': '10', |
| 190 |
'project_id': uuidutils.generate_uuid(), |
| 191 |
'l7_parameters': {} |
| 192 |
} |
| 193 |
flow_classifier_attrs.update(attrs) |
| 194 |
return flow_classifier.SfcFlowClassifier(**flow_classifier_attrs) |
| 195 |
|
| 196 |
@staticmethod |
| 197 |
def create_flow_classifiers(attrs=None, count=1): |
| 198 |
"""Create multiple flow classifiers. |
| 199 |
|
| 200 |
:param Dictionary attrs: |
| 201 |
A dictionary with all attributes |
| 202 |
:param int count: |
| 203 |
The number of flow classifiers to fake |
| 204 |
:return: |
| 205 |
A list of dictionaries faking the flow classifiers |
| 206 |
""" |
| 207 |
flow_classifiers = [] |
| 208 |
for _ in range(count): |
| 209 |
flow_classifiers.append( |
| 210 |
FakeSfcFlowClassifier.create_flow_classifier(attrs)) |
| 211 |
|
| 212 |
return flow_classifiers |
| 213 |
|
| 214 |
|
| 215 |
class FakeSfcPortChain(object): |
| 216 |
"""Fake port chain attributes.""" |
| 217 |
|
| 218 |
@staticmethod |
| 219 |
def create_port_chain(attrs=None): |
| 220 |
"""Create a fake port chain. |
| 221 |
|
| 222 |
:param Dictionary attrs: |
| 223 |
A dictionary with all attributes |
| 224 |
:return: |
| 225 |
A Dictionary with faking port chain attributes |
| 226 |
""" |
| 227 |
attrs = attrs or {} |
| 228 |
|
| 229 |
# Set default attributes. |
| 230 |
port_chain_attrs = { |
| 231 |
'id': uuidutils.generate_uuid(), |
| 232 |
'name': 'port-chain-name', |
| 233 |
'description': 'description', |
| 234 |
'port_pair_groups': uuidutils.generate_uuid(), |
| 235 |
'flow_classifiers': uuidutils.generate_uuid(), |
| 236 |
'chain_parameters': {"correlation": "mpls", "symmetric": False}, |
| 237 |
'project_id': uuidutils.generate_uuid(), |
| 238 |
} |
| 239 |
|
| 240 |
port_chain_attrs.update(attrs) |
| 241 |
return port_chain.SfcPortChain(**port_chain_attrs) |
| 242 |
|
| 243 |
@staticmethod |
| 244 |
def create_port_chains(attrs=None, count=1): |
| 245 |
"""Create multiple port chains. |
| 246 |
|
| 247 |
:param Dictionary attrs: |
| 248 |
A dictionary with all attributes |
| 249 |
:param int count: |
| 250 |
The number of port chains to fake |
| 251 |
:return: |
| 252 |
A list of dictionaries faking the port chains. |
| 253 |
""" |
| 254 |
port_chains = [] |
| 255 |
for _ in range(count): |
| 256 |
port_chains.append(FakeSfcPortChain.create_port_chain(attrs)) |
| 257 |
return port_chains |
| 258 |
|
| 259 |
|
| 260 |
class FakeSfcServiceGraph(object): |
| 261 |
"""Fake service graph attributes.""" |
| 262 |
|
| 263 |
@staticmethod |
| 264 |
def create_sfc_service_graph(attrs=None): |
| 265 |
"""Create a fake service graph. |
| 266 |
|
| 267 |
:param Dictionary attrs: |
| 268 |
A dictionary with all attributes |
| 269 |
:return: |
| 270 |
A Dictionary with faking service graph attributes |
| 271 |
""" |
| 272 |
attrs = attrs or {} |
| 273 |
|
| 274 |
# Set default attributes. |
| 275 |
service_graph_attrs = { |
| 276 |
'id': uuidutils.generate_uuid(), |
| 277 |
'name': 'port-pair-group-name', |
| 278 |
'description': 'description', |
| 279 |
'port_chains': {uuidutils.generate_uuid(): [ |
| 280 |
uuidutils.generate_uuid()]}, |
| 281 |
'project_id': uuidutils.generate_uuid(), |
| 282 |
} |
| 283 |
|
| 284 |
service_graph_attrs.update(attrs) |
| 285 |
return service_graph.SfcServiceGraph(**service_graph_attrs) |
| 286 |
|
| 287 |
@staticmethod |
| 288 |
def create_sfc_service_graphs(attrs=None, count=1): |
| 289 |
"""Create multiple service graphs. |
| 290 |
|
| 291 |
:param Dictionary attrs: |
| 292 |
A dictionary with all attributes |
| 293 |
:param int count: |
| 294 |
The number of service graphs to fake |
| 295 |
:return: |
| 296 |
A list of dictionaries faking the service graphs. |
| 297 |
""" |
| 298 |
service_graphs = [] |
| 299 |
for _ in range(count): |
| 300 |
service_graphs.append( |
| 301 |
FakeSfcServiceGraph.create_sfc_service_graph(attrs)) |
| 302 |
return service_graphs |