forked from rackerlabs/quark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ipam.py
184 lines (160 loc) · 7.6 KB
/
test_ipam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Copyright (c) 2013 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import contextlib
import mock
import netaddr
from neutron.common import rpc
from quark.db import api as db_api
import quark.ipam
from quark.tests.functional.base import BaseFunctionalTest
class QuarkIpamBaseFunctionalTest(BaseFunctionalTest):
def setUp(self):
super(QuarkIpamBaseFunctionalTest, self).setUp()
patcher = mock.patch("neutron.common.rpc.oslo_messaging")
patcher.start()
self.addCleanup(patcher.stop)
rpc.init(mock.MagicMock())
class QuarkIPAddressAllocate(QuarkIpamBaseFunctionalTest):
@contextlib.contextmanager
def _stubs(self, network, subnet):
self.ipam = quark.ipam.QuarkIpamANY()
with self.context.session.begin():
next_ip = subnet.pop("next_auto_assign_ip", 0)
net_mod = db_api.network_create(self.context, **network)
subnet["network"] = net_mod
sub_mod = db_api.subnet_create(self.context, **subnet)
# NOTE(asadoughi): update after cidr constructor has been invoked
db_api.subnet_update(self.context,
sub_mod,
next_auto_assign_ip=next_ip)
yield net_mod
with self.context.session.begin():
db_api.subnet_delete(self.context, sub_mod)
db_api.network_delete(self.context, net_mod)
def test_allocate_finds_no_deallocated_creates_new_ip(self):
network = dict(name="public", tenant_id="fake")
ipnet = netaddr.IPNetwork("0.0.0.0/24")
next_ip = ipnet.ipv6().first + 2
subnet = dict(id=1, cidr="0.0.0.0/24", next_auto_assign_ip=next_ip,
ip_policy=None, tenant_id="fake")
with self._stubs(network, subnet) as net:
ipaddress = []
self.ipam.allocate_ip_address(self.context, ipaddress,
net["id"], 0, 0)
self.assertIsNotNone(ipaddress[0]['id'])
self.assertEqual(ipaddress[0]['address'], 281470681743362)
self.assertEqual(ipaddress[0]['version'], 4)
self.assertEqual(ipaddress[0]['used_by_tenant_id'], "fake")
class QuarkIPAddressFindReallocatable(QuarkIpamBaseFunctionalTest):
@contextlib.contextmanager
def _stubs(self, network, subnet):
self.ipam = quark.ipam.QuarkIpamANY()
with self.context.session.begin():
next_ip = subnet.pop("next_auto_assign_ip", 0)
net_mod = db_api.network_create(self.context, **network)
subnet["network"] = net_mod
sub_mod = db_api.subnet_create(self.context, **subnet)
# NOTE(asadoughi): update after cidr constructor has been invoked
db_api.subnet_update(self.context,
sub_mod,
next_auto_assign_ip=next_ip)
yield net_mod
def test_find_reallocatable_ips_does_not_raise(self):
"""Regression testing
A patch recently introduced a bug wherein addressses
could not be returned to the ip_address_find call in
attempt_to_reallocate_ip. Adding this test to prevent
a future regression.
"""
network = dict(name="public", tenant_id="fake")
ipnet = netaddr.IPNetwork("0.0.0.0/24")
next_ip = ipnet.ipv6().first + 2
subnet = dict(id=1, cidr="0.0.0.0/24", next_auto_assign_ip=next_ip,
ip_policy=None, tenant_id="fake")
with self._stubs(network, subnet) as net:
ip_kwargs = {
"network_id": net["id"], "reuse_after": 14400,
"deallocated": True, "scope": db_api.ONE,
"lock_mode": True, "version": 4,
"order_by": "address"}
try:
db_api.ip_address_find(self.context, **ip_kwargs)
except Exception:
self.fail("This should not have raised")
class QuarkIPAddressAllocateWithFullSubnetsNotMarkedAsFull(
QuarkIpamBaseFunctionalTest):
@contextlib.contextmanager
def _fixtures(self, models):
self.ipam = quark.ipam.QuarkIpamANY()
net = dict(name="public", tenant_id='fake')
net_mod = db_api.network_create(self.context, **net)
with self.context.session.begin():
for model in models:
policy_mod = db_api.ip_policy_create(
self.context, **model['ip_policy'])
model['subnet']["network"] = net_mod
model['subnet']["ip_policy"] = policy_mod
next_ip = model['subnet'].pop("next_auto_assign_ip", 0)
sub_mod = db_api.subnet_create(self.context, **model['subnet'])
# NOTE(amir): update after cidr constructor has been invoked
db_api.subnet_update(self.context,
sub_mod,
next_auto_assign_ip=next_ip)
yield net_mod
def _create_models(self, subnet_cidr, ip_version, next_ip):
models = {}
net = netaddr.IPNetwork(subnet_cidr)
first = str(netaddr.IPAddress(net.first)) + "/32"
last = str(netaddr.IPAddress(net.last)) + "/32"
models['ip_policy'] = dict(name='testpolicy',
description='blah',
exclude=[first, last])
models["subnet"] = dict(cidr=subnet_cidr,
next_auto_assign_ip=next_ip,
tenant_id='fake',
do_not_use=False)
return models
def test_subnets_get_marked_as_full_retroactively(self):
models = []
models.append(self._create_models(
"0.0.0.0/31",
4,
netaddr.IPNetwork("0.0.0.0/31").ipv6().last))
models.append(self._create_models(
"1.1.1.0/31",
4,
netaddr.IPNetwork("1.1.1.0/31").ipv6().last))
models.append(self._create_models(
"2.2.2.0/30",
4,
netaddr.IPNetwork("2.2.2.0/30").ipv6().first))
with self._fixtures(models) as net:
ipaddress = []
self.ipam.allocate_ip_address(self.context, ipaddress,
net['id'], 0, 0)
self.assertEqual(ipaddress[0].version, 4)
self.assertEqual(ipaddress[0].address_readable, "2.2.2.1")
with self.context.session.begin():
subnets = db_api.subnet_find(self.context, None, None, None,
False).all()
self.assertEqual(len(subnets), 3)
full_subnets = [s for s in subnets
if s.next_auto_assign_ip == -1]
self.assertEqual(len(full_subnets), 2)
available_subnets = list(set(full_subnets) ^ set(subnets))
self.assertEqual(len(available_subnets), 1)
self.assertEqual(available_subnets[0].cidr, "2.2.2.0/30")
self.assertEqual(available_subnets[0].next_auto_assign_ip,
netaddr.IPAddress("2.2.2.2").ipv6().value)