root / qa / qa_network.py @ 6023923b
History | View | Annotate | Download (13.6 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2013 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""QA tests for networks.
|
23 |
|
24 |
"""
|
25 |
|
26 |
import qa_config |
27 |
import qa_utils |
28 |
|
29 |
from qa_utils import AssertCommand |
30 |
|
31 |
TEST_NET_1 = "192.0.2.0/24"
|
32 |
TEST_NET_2 = "198.51.100.0/24"
|
33 |
TEST_NET_3 = "203.0.113.0/24"
|
34 |
|
35 |
TEST_NET6_1 = "2001:648:2ffc:1201::/64"
|
36 |
GW_IN_NET6_1 = "2001:648:2ffc:1201::1"
|
37 |
IP_IN_NET6_1 = "2001:648:2ffc:1201::81"
|
38 |
|
39 |
TEST_NET6_2 = "2002:648:2c:101::/64"
|
40 |
GW_IN_NET6_2 = "2002:648:2c:101::1"
|
41 |
IP_IN_NET6_2 = "2002:648:2c:101::54"
|
42 |
|
43 |
GW_IN_NET_1 = "192.0.2.1"
|
44 |
GW2_IN_NET_1 = "192.0.2.100"
|
45 |
GW_IN_NET_2 = "198.51.100.1"
|
46 |
GW_IN_NET_3 = "203.0.113.1"
|
47 |
IP_IN_NET_1 = "192.0.2.50"
|
48 |
IP2_IN_NET_1 = "192.0.2.70"
|
49 |
IP_IN_NET_2 = "198.51.100.82"
|
50 |
IP_IN_NET_3 = "203.0.113.118"
|
51 |
|
52 |
|
53 |
def GetNicParams(): |
54 |
default_mode = "bridged"
|
55 |
default_link = "br0"
|
56 |
nicparams = qa_config.get("default-nicparams")
|
57 |
if nicparams:
|
58 |
mode = nicparams.get("mode", default_mode)
|
59 |
link = nicparams.get("link", default_link)
|
60 |
else:
|
61 |
mode = default_mode |
62 |
link = default_link |
63 |
|
64 |
return mode, link
|
65 |
|
66 |
|
67 |
def GetNetOption(idx=-1, action=None, mac=None, ip=None, network=None, |
68 |
mode=None, link=None): |
69 |
net = "%d:" % idx
|
70 |
if action:
|
71 |
net += action |
72 |
if mac:
|
73 |
net += ",mac=" + mac
|
74 |
if ip:
|
75 |
net += ",ip=" + ip
|
76 |
if network:
|
77 |
net += ",network=" + network
|
78 |
if mode:
|
79 |
net += ",mode=" + mode
|
80 |
if link:
|
81 |
net += ",link=" + link
|
82 |
|
83 |
return net.replace(":,", ":") |
84 |
|
85 |
|
86 |
def RemoveInstance(instance): |
87 |
name = instance.name |
88 |
AssertCommand(["gnt-instance", "remove", "-f", name]) |
89 |
instance.Release() |
90 |
|
91 |
|
92 |
def GetInstance(): |
93 |
return qa_config.AcquireInstance()
|
94 |
|
95 |
|
96 |
def LaunchInstance(instance, mac=None, ip=None, network=None, |
97 |
mode=None, link=None, fail=False): |
98 |
|
99 |
name = instance.name |
100 |
templ = qa_config.GetDefaultDiskTemplate() |
101 |
net = GetNetOption(0, None, mac, ip, network, mode, link) |
102 |
AssertCommand(["gnt-instance", "add", "-o", qa_config.get("os"), |
103 |
"-t", templ, "--disk", "0:size=1G", "--net", net, |
104 |
"--no-name-check", "--no-ip-check", "--no-install", name], |
105 |
fail=fail) |
106 |
|
107 |
|
108 |
def ModifyInstance(instance, idx=-1, action="add", mac=None, |
109 |
ip=None, network=None, mode=None, link=None, fail=False): |
110 |
|
111 |
name = instance.name |
112 |
net = GetNetOption(idx, action, mac, ip, network, mode, link) |
113 |
AssertCommand(["gnt-instance", "modify", "--net", net, name], fail=fail) |
114 |
|
115 |
|
116 |
def TestNetworkAddRemove(): |
117 |
"""gnt-network add/remove"""
|
118 |
(network1, network2, network3) = qa_utils.GetNonexistentNetworks(3)
|
119 |
|
120 |
# Note: Using RFC5737 addresses.
|
121 |
# Add a network without subnet
|
122 |
AssertCommand(["gnt-network", "add", network1]) |
123 |
AssertCommand(["gnt-network", "remove", network1]) |
124 |
# remove non-existing network
|
125 |
AssertCommand(["gnt-network", "remove", network2], fail=True) |
126 |
|
127 |
# Check wrong opcode parameters
|
128 |
# wrone cidr notation
|
129 |
AssertCommand(["gnt-network", "add", "--network", "xxxxx", network1], |
130 |
fail=True)
|
131 |
# gateway outside the network
|
132 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
133 |
"--gateway", GW_IN_NET_2, network1],
|
134 |
fail=True)
|
135 |
# v6 gateway but not network
|
136 |
#AssertCommand(["gnt-network", "add", "--gateway6", IP_IN_NET6_1, network1],
|
137 |
# fail=False)
|
138 |
# gateway but not network
|
139 |
#AssertCommand(["gnt-network", "add", "--gateway", IP_IN_NET_1, network1],
|
140 |
# fail=False)
|
141 |
# wrong mac prefix
|
142 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
143 |
"--mac-prefix", "xxxxxx", network1], |
144 |
fail=True)
|
145 |
|
146 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
147 |
"--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc", |
148 |
"--add-reserved-ips", IP_IN_NET_1,
|
149 |
"--network6", TEST_NET6_1,
|
150 |
"--gateway6", GW_IN_NET6_1, network1])
|
151 |
|
152 |
# TODO: add a network that contains the nodes' IPs
|
153 |
# This should reserve them
|
154 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, |
155 |
network2]) |
156 |
|
157 |
# This does not reserve master/node IPs
|
158 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_3, |
159 |
"--no-conflicts-check", network3])
|
160 |
|
161 |
# Try to add a network with an existing name.
|
162 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network2], |
163 |
fail=True)
|
164 |
|
165 |
AssertCommand(["gnt-network", "remove", network1]) |
166 |
AssertCommand(["gnt-network", "remove", network2]) |
167 |
AssertCommand(["gnt-network", "remove", network3]) |
168 |
|
169 |
|
170 |
def TestNetworkSetParams(): |
171 |
"""gnt-network modify"""
|
172 |
(network1, ) = qa_utils.GetNonexistentNetworks(1)
|
173 |
|
174 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
175 |
"--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc", |
176 |
"--add-reserved-ips", IP_IN_NET_1,
|
177 |
"--network6", TEST_NET6_1,
|
178 |
"--gateway6", GW_IN_NET6_1, network1])
|
179 |
|
180 |
# Cannot modify subnet
|
181 |
AssertCommand(["gnt-network", "modify", "--network", TEST_NET_2, |
182 |
network1], fail=True)
|
183 |
|
184 |
# Gateway outside network
|
185 |
AssertCommand(["gnt-network", "modify", "--gateway", IP_IN_NET_2, |
186 |
network1], fail=True)
|
187 |
|
188 |
# Gateway with reserved ips
|
189 |
AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1, |
190 |
"--add-reserved-ips", IP2_IN_NET_1,
|
191 |
network1], fail=True)
|
192 |
|
193 |
# Edit all
|
194 |
# TODO: test reserved ips
|
195 |
AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1, |
196 |
"--network6", TEST_NET6_2,
|
197 |
"--gateway6", GW_IN_NET6_2,
|
198 |
network1]) |
199 |
|
200 |
# reset everything
|
201 |
AssertCommand(["gnt-network", "modify", "--gateway", "none", |
202 |
"--network6", "none", "--gateway6", "none", |
203 |
"--mac-prefix", "none", |
204 |
network1]) |
205 |
|
206 |
AssertCommand(["gnt-network", "remove", network1]) |
207 |
|
208 |
|
209 |
def TestNetworkConnect(): |
210 |
"""gnt-network connect/disconnect"""
|
211 |
(group1, group2, ) = qa_utils.GetNonexistentGroups(2)
|
212 |
(network1, network2, ) = qa_utils.GetNonexistentNetworks(2)
|
213 |
defmode, deflink = GetNicParams() |
214 |
|
215 |
AssertCommand(["gnt-group", "add", group1]) |
216 |
AssertCommand(["gnt-group", "add", group2]) |
217 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network1]) |
218 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2]) |
219 |
|
220 |
AssertCommand(["gnt-network", "connect", network1, |
221 |
defmode, deflink, group1]) |
222 |
# This should produce a warning for group1
|
223 |
AssertCommand(["gnt-network", "connect", network1, defmode, deflink]) |
224 |
# Network still connected
|
225 |
AssertCommand(["gnt-network", "remove", network1], fail=True) |
226 |
|
227 |
instance1 = GetInstance() |
228 |
# Add instance inside the network
|
229 |
LaunchInstance(instance1, ip="pool", network=network1)
|
230 |
# Conflicting IP, at least one instance belongs to the network
|
231 |
AssertCommand(["gnt-network", "disconnect", network1], fail=True) |
232 |
RemoveInstance(instance1) |
233 |
|
234 |
AssertCommand(["gnt-network", "disconnect", network1]) |
235 |
# This should only produce a warning.
|
236 |
AssertCommand(["gnt-network", "disconnect", network1]) |
237 |
|
238 |
instance1 = GetInstance() |
239 |
# TODO: add conflicting image.
|
240 |
LaunchInstance(instance1, ip=IP_IN_NET_2) |
241 |
# Conflicting IPs
|
242 |
AssertCommand(["gnt-network", "connect", network2, defmode, deflink], |
243 |
fail=True)
|
244 |
AssertCommand(["gnt-network", "connect", "--no-conflicts-check", |
245 |
network2, defmode, deflink]) |
246 |
AssertCommand(["gnt-network", "disconnect", network2]) |
247 |
|
248 |
RemoveInstance(instance1) |
249 |
AssertCommand(["gnt-group", "remove", group1]) |
250 |
AssertCommand(["gnt-group", "remove", group2]) |
251 |
AssertCommand(["gnt-network", "remove", network1]) |
252 |
AssertCommand(["gnt-network", "remove", network2]) |
253 |
|
254 |
|
255 |
def TestInstanceAddAndNetAdd(): |
256 |
""" gnt-istance add / gnt-instance modify --net -1:add """
|
257 |
(network1, network2, network3) = qa_utils.GetNonexistentNetworks(3)
|
258 |
defmode, deflink = GetNicParams() |
259 |
|
260 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
261 |
"--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc", |
262 |
"--add-reserved-ips", IP_IN_NET_1,
|
263 |
"--network6", TEST_NET6_1,
|
264 |
"--gateway6", GW_IN_NET6_1, network1])
|
265 |
AssertCommand(["gnt-network", "connect", network1, defmode, deflink]) |
266 |
|
267 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2]) |
268 |
AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"]) |
269 |
|
270 |
AssertCommand(["gnt-network", "add", network3]) |
271 |
AssertCommand(["gnt-network", "connect", network3, "routed", "rt100"]) |
272 |
|
273 |
# (mac, ip, network, mode, link)
|
274 |
success_cases = [ |
275 |
(None, None, None, None, None), # random mac and default nicparams |
276 |
("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params |
277 |
(None, "pool", network1, None, None), # first IP in network given |
278 |
# TODO: include this use case with --no-conflicts-check
|
279 |
# just add an extra field in Launch|ModifyInstance
|
280 |
#(None, "192.168.1.6", None, None, None), # IP but no net
|
281 |
(None, None, network1, None, None), # nicparams/mac inherited by network |
282 |
] |
283 |
|
284 |
for (mac, ip, network, mode, link) in success_cases: |
285 |
instance1 = GetInstance() |
286 |
LaunchInstance(instance1, mac, ip, network, mode, link) |
287 |
ModifyInstance(instance1, idx=-1, action="add", mac=mac, |
288 |
ip=ip, network=network, mode=mode, link=link) |
289 |
ModifyInstance(instance1, idx=1, action="remove") |
290 |
RemoveInstance(instance1) |
291 |
|
292 |
# test _AllIPs()
|
293 |
instance1 = qa_config.AcquireInstance() |
294 |
LaunchInstance(instance1, ip="10.10.10.10")
|
295 |
# this results to "Configuration data not consistent
|
296 |
ModifyInstance(instance1, idx=-1, action="add", ip="10.10.10.10") |
297 |
ModifyInstance(instance1, idx=-1, action="add", |
298 |
ip="10.10.10.10", network=network3)
|
299 |
# this raises Corrupt configuration data
|
300 |
ModifyInstance(instance1, idx=-1, action="add", |
301 |
ip="10.10.10.10", network=network3, fail=True) |
302 |
RemoveInstance(instance1) |
303 |
|
304 |
fail_cases = [ |
305 |
(None, None, None, "lala", None), |
306 |
(None, "lala", None, None, None), |
307 |
(None, None, "lala", None, None), |
308 |
(None, IP_IN_NET_2, None, None, None), # conflicting IP |
309 |
(None, None, None, "routed", None), # routed with no IP |
310 |
(None, "pool", network1, "routed", None), # nicparams along with network |
311 |
(None, "pool", network1, None, deflink), |
312 |
] |
313 |
|
314 |
instance1 = GetInstance() |
315 |
instance2 = GetInstance() |
316 |
LaunchInstance(instance2) |
317 |
for (mac, ip, network, mode, link) in fail_cases: |
318 |
LaunchInstance(instance1, mac=mac, ip=ip, network=network, |
319 |
mode=mode, link=link, fail=True)
|
320 |
ModifyInstance(instance2, idx=-1, action="add", mac=mac, |
321 |
ip=ip, network=network, mode=mode, link=link, fail=True)
|
322 |
ModifyInstance(instance2, idx=0, action="modify", mac=mac, |
323 |
ip=ip, network=network, mode=mode, link=link, fail=True)
|
324 |
|
325 |
RemoveInstance(instance2) |
326 |
AssertCommand(["gnt-network", "disconnect", network1]) |
327 |
AssertCommand(["gnt-network", "remove", network1]) |
328 |
AssertCommand(["gnt-network", "disconnect", network2]) |
329 |
AssertCommand(["gnt-network", "remove", network2]) |
330 |
AssertCommand(["gnt-network", "disconnect", network3]) |
331 |
AssertCommand(["gnt-network", "remove", network3]) |
332 |
|
333 |
|
334 |
def TestInstanceNetMod(): |
335 |
""" gnt-istance modify --net 0:modify """
|
336 |
(network1, network2) = qa_utils.GetNonexistentNetworks(2)
|
337 |
defmode, deflink = GetNicParams() |
338 |
|
339 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, |
340 |
"--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc", |
341 |
"--add-reserved-ips", IP_IN_NET_1,
|
342 |
"--network6", TEST_NET6_1,
|
343 |
"--gateway6", GW_IN_NET6_1, network1])
|
344 |
AssertCommand(["gnt-network", "connect", network1, defmode, deflink]) |
345 |
|
346 |
success_cases = [ |
347 |
("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params |
348 |
(None, "pool", network1, None, None), # first IP in network given |
349 |
(None, "none", "none", None, None), # random mac and default nicparams |
350 |
(None, IP2_IN_NET_1, network1, None, None), # IP inside network |
351 |
#TODO: include this use case with --no-conflickts-check
|
352 |
#(None, IP2_IN_NET_1, None, None, None), # IP but no net
|
353 |
(None, None, network1, None, None), # nicparams/mac inherited by network |
354 |
] |
355 |
|
356 |
instance1 = GetInstance() |
357 |
LaunchInstance(instance1) |
358 |
for (mac, ip, network, mode, link) in success_cases: |
359 |
ModifyInstance(instance1, idx=0, action="modify", mac=mac, |
360 |
ip=ip, network=network, mode=mode, link=link) |
361 |
# reset to defaults
|
362 |
ModifyInstance(instance1, idx=0, action="modify", mac="generate", |
363 |
ip="none", network="none", mode=defmode, link=deflink) |
364 |
|
365 |
AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2]) |
366 |
AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"]) |
367 |
# put instance inside network1
|
368 |
ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network1) |
369 |
# move instance to network2
|
370 |
ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network2) |
371 |
|
372 |
RemoveInstance(instance1) |
373 |
AssertCommand(["gnt-network", "disconnect", network1]) |
374 |
AssertCommand(["gnt-network", "remove", network1]) |
375 |
AssertCommand(["gnt-network", "disconnect", network2]) |
376 |
AssertCommand(["gnt-network", "remove", network2]) |