Debian Codegraph @

neutron_fwaas/tests/unit/services/firewall/service_drivers/agents/l2/fake_data.py

raw symbols SCIP
1 # Copyright 2017 FUJITSU 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
17 import copy
18
19 from unittest import mock
20
21 from neutron_lib import constants as nl_consts
22 from oslo_utils import uuidutils
23
24 TENANT_UUID = uuidutils.generate_uuid()
25 TENANT_ID = TENANT_UUID
26 PROJECT_ID = TENANT_UUID
27 NETWORK_ID = uuidutils.generate_uuid()
28 SUBNET_ID = uuidutils.generate_uuid()
29 DEVICE_ID = uuidutils.generate_uuid()
30 PORT1 = uuidutils.generate_uuid()
31 PORT2 = uuidutils.generate_uuid()
32 PORT3 = uuidutils.generate_uuid()
33 PORT4 = uuidutils.generate_uuid()
34 HOST = 'fake_host'
35
36
37 class FakeFWaaSL2Agent:
38
39 def __init__(self):
40 super().__init__()
41
42 def create(self, resource, attrs=None, minimal=False):
43 """Create a fake fwaas v2 resources
44
45 :param resource: A dictionary with all attributes
46 :type resource: string
47 :param attrs: A dictionary of each attribute you need to modify
48 :type attrs: dictionary
49 :param minimal: True if minimal port_detail is necessary
50 otherwise False
51 :type minimal: boolean
52 :return:
53 A OrderedDict faking the fwaas v2 resource
54 """
55 target = getattr(self, "_" + resource)
56 return copy.deepcopy(target(attrs=attrs, minimal=minimal))
57
58 def _fwg(self, **kwargs):
59
60 fwg = {
61 'id': uuidutils.generate_uuid(),
62 'name': 'my-group-' + uuidutils.generate_uuid(),
63 'ingress_firewall_policy_id': uuidutils.generate_uuid(),
64 'egress_firewall_policy_id': uuidutils.generate_uuid(),
65 'description': 'my firewall group',
66 'status': nl_consts.PENDING_CREATE,
67 'ports': [PORT3, PORT4],
68 'admin_state_up': True,
69 'shared': False,
70 'tenant_id': TENANT_ID,
71 'project_id': PROJECT_ID
72 }
73 attrs = kwargs.get('attrs', None)
74 if attrs:
75 fwg.update(attrs)
76 return fwg
77
78 def _fwg_with_rule(self, **kwargs):
79
80 fwg_with_rule = self.create('fwg', attrs={'ports': [PORT1, PORT2]})
81 rules = {
82 'ingress_rule_list': [mock.Mock()],
83 'egress_rule_list': [mock.Mock()],
84 'add-port-ids': [PORT1],
85 'del-port-ids': [PORT2],
86 'port_details': {
87 PORT1: {
88 'device': uuidutils.generate_uuid(),
89 'device_owner': 'compute:nova',
90 'host': HOST,
91 'network_id': NETWORK_ID,
92 'fixed_ips': [
93 {'subnet_id': SUBNET_ID, 'ip_address': '172.24.4.5'}],
94 'allowed_address_pairs': [],
95 'port_security_enabled': True,
96 'id': PORT1
97 },
98 PORT2: {
99 'device': uuidutils.generate_uuid(),
100 'device_owner': 'compute:nova',
101 'host': HOST,
102 'network_id': NETWORK_ID,
103 'fixed_ips': [
104 {'subnet_id': SUBNET_ID, 'ip_address': '172.24.4.6'}],
105 'allowed_address_pairs': [],
106 'port_security_enabled': True,
107 'id': PORT2
108 }
109 },
110 }
111 fwg_with_rule.update(rules)
112
113 if kwargs.get('minimal', None):
114 fwg_with_rule.update({'ports': []})
115 fwg_with_rule.update({'add-port-ids': []})
116 fwg_with_rule.update({'del-port-ids': []})
117 fwg_with_rule.update({'port_details': {}})
118
119 attrs = kwargs.get('attrs', None)
120 if attrs:
121 fwg_with_rule.update(attrs)
122 return fwg_with_rule
123
124 def _port(self, **kwargs):
125
126 if kwargs.get('minimal', None):
127 return {'port_id': uuidutils.generate_uuid()}
128
129 port_detail = {
130 'profile': {},
131 'network_qos_policy_id': None,
132 'qos_policy_id': None,
133 'allowed_address_pairs': [],
134 'admin_state_up': True,
135 'network_id': NETWORK_ID,
136 'segmentation_id': None,
137 'fixed_ips': [
138 {'subnet_id': SUBNET_ID, 'ip_address': '172.24.4.5'}],
139 'vif_port': mock.Mock(),
140 'device_owner': 'compute:node',
141 'physical_network': 'physnet',
142 'mac_address': 'fa:16:3e:8a:80:2b',
143 'device': DEVICE_ID,
144 'port_security_enabled': True,
145 'port_id': uuidutils.generate_uuid(),
146 'network_type': 'flat',
147 'security_groups': []
148 }
149
150 attrs = kwargs.get('attrs', None)
151
152 if attrs:
153 port_detail.update(attrs)
154 return port_detail