Coverage for manila/tests/fake_network.py: 49%
122 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2013 OpenStack Foundation
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.
16from oslo_config import cfg
17from oslo_utils import uuidutils
19CONF = cfg.CONF
22class FakeNetwork(object):
23 def __init__(self, **kwargs):
24 self.id = kwargs.pop('id', 'fake_net_id')
25 self.name = kwargs.pop('name', 'net_name')
26 self.subnets = kwargs.pop('subnets', [])
27 for key, value in kwargs.items(): 27 ↛ 28line 27 didn't jump to line 28 because the loop on line 27 never started
28 setattr(self, key, value)
30 def __getitem__(self, attr):
31 return getattr(self, attr)
34class FakeSubnet(object):
35 def __init__(self, **kwargs):
36 self.id = kwargs.pop('id', 'fake_subnet_id')
37 self.network_id = kwargs.pop('network_id', 'fake_net_id')
38 self.cidr = kwargs.pop('cidr', 'fake_cidr')
39 for key, value in kwargs.items():
40 setattr(self, key, value)
42 def __getitem__(self, attr):
43 return getattr(self, attr)
46class FakePort(object):
47 def __init__(self, **kwargs):
48 self.id = kwargs.pop('id', 'fake_subnet_id')
49 self.network_id = kwargs.pop('network_id', 'fake_net_id')
50 self.fixed_ips = kwargs.pop('fixed_ips', [])
51 for key, value in kwargs.items():
52 setattr(self, key, value)
54 def __getitem__(self, attr):
55 return getattr(self, attr)
58class FakeRouter(object):
59 def __init__(self, **kwargs):
60 self.id = kwargs.pop('id', 'fake_router_id')
61 self.name = kwargs.pop('name', 'fake_router_name')
62 for key, value in kwargs.items(): 62 ↛ 63line 62 didn't jump to line 63 because the loop on line 62 never started
63 setattr(self, key, value)
65 def __getitem__(self, attr):
66 return getattr(self, attr)
68 def __setitem__(self, attr, value):
69 setattr(self, attr, value)
72class FakeDeviceAddr(object):
73 def __init__(self, list_of_addresses=None):
74 self.addresses = list_of_addresses or [
75 dict(ip_version=4, cidr='1.0.0.0/27'),
76 dict(ip_version=4, cidr='2.0.0.0/27'),
77 dict(ip_version=6, cidr='3.0.0.0/27'),
78 ]
80 def list(self):
81 return self.addresses
84class FakeDevice(object):
85 def __init__(self, name=None, list_of_addresses=None):
86 self.addr = FakeDeviceAddr(list_of_addresses)
87 self.name = name or 'fake_device_name'
90class API(object):
91 """Fake Network API."""
92 admin_project_id = 'fake_admin_project_id'
94 network = {
95 "status": "ACTIVE",
96 "subnets": ["fake_subnet_id"],
97 "name": "fake_network",
98 "tenant_id": "fake_tenant_id",
99 "shared": False,
100 "id": "fake_id",
101 "router:external": False,
102 }
104 port = {
105 "status": "ACTIVE",
106 "allowed_address_pairs": [],
107 "admin_state_up": True,
108 "network_id": "fake_network_id",
109 "tenant_id": "fake_tenant_id",
110 "extra_dhcp_opts": [],
111 "device_owner": "fake",
112 "binding:capabilities": {"port_filter": True},
113 "mac_address": "00:00:00:00:00:00",
114 "fixed_ips": [
115 {"subnet_id": "56537094-98d7-430a-b513-81c4dc6d9903",
116 "ip_address": "10.12.12.10"}
117 ],
118 "id": "fake_port_id",
119 "security_groups": ["fake_sec_group_id"],
120 "device_id": "fake_device_id"
121 }
123 def get_all_admin_project_networks(self):
124 net1 = self.network.copy()
125 net1['tenant_id'] = self.admin_project_id
126 net1['id'] = uuidutils.generate_uuid()
128 net2 = self.network.copy()
129 net2['tenant_id'] = self.admin_project_id
130 net2['id'] = uuidutils.generate_uuid()
131 return [net1, net2]
133 def create_port(self, tenant_id, network_id, subnet_id=None,
134 fixed_ip=None, device_owner=None, device_id=None):
135 port = self.port.copy()
136 port['network_id'] = network_id
137 port['admin_state_up'] = True
138 port['tenant_id'] = tenant_id
139 if fixed_ip:
140 fixed_ip_dict = {'ip_address': fixed_ip}
141 if subnet_id:
142 fixed_ip_dict.update({'subnet_id': subnet_id})
143 port['fixed_ips'] = [fixed_ip_dict]
144 if device_owner:
145 port['device_owner'] = device_owner
146 if device_id:
147 port['device_id'] = device_id
148 return port
150 def list_ports(self, **search_opts):
151 """List ports for the client based on search options."""
152 ports = []
153 for i in range(2):
154 ports.append(self.port.copy())
155 for port in ports:
156 port['id'] = uuidutils.generate_uuid()
157 for key, val in search_opts.items():
158 port[key] = val
159 if 'id' in search_opts:
160 return ports
161 return ports
163 def show_port(self, port_id):
164 """Return the port for the client given the port id."""
165 port = self.port.copy()
166 port['id'] = port_id
167 return port
169 def delete_port(self, port_id):
170 pass
172 def get_subnet(self, subnet_id):
173 pass
175 def subnet_create(self, *args, **kwargs):
176 pass
178 def router_add_interface(self, *args, **kwargs):
179 pass
181 def show_router(self, *args, **kwargs):
182 pass
184 def update_port_fixed_ips(self, *args, **kwargs):
185 pass
187 def router_remove_interface(self, *args, **kwargs):
188 pass
190 def update_subnet(self, *args, **kwargs):
191 pass
193 def get_all_networks(self):
194 """Get all networks for client."""
195 net1 = self.network.copy()
196 net2 = self.network.copy()
197 net1['id'] = uuidutils.generate_uuid()
198 net2['id'] = uuidutils.generate_uuid()
199 return [net1, net2]
201 def get_network(self, network_uuid):
202 """Get specific network for client."""
203 network = self.network.copy()
204 network['id'] = network_uuid
205 return network
207 def network_create(self, tenant_id, name):
208 network = self.network.copy()
209 network['tenant_id'] = tenant_id
210 network['name'] = name
211 return network