Statistics
| Branch: | Tag: | Revision:

root / qa / qa_network.py @ 16eb7455

History | View | Annotate | Download (12.9 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

    
32
TEST_NET_1 = "192.0.2.0/24"
33
TEST_NET_2 = "198.51.100.0/24"
34
TEST_NET_3 = "203.0.113.0/24"
35

    
36
TEST_NET6_1 = "2001:648:2ffc:1201::/64"
37
GW_IN_NET6_1 = "2001:648:2ffc:1201::1"
38
IP_IN_NET6_1 = "2001:648:2ffc:1201::81"
39

    
40
TEST_NET6_2 = "2002:648:2c:101::/64"
41
GW_IN_NET6_2 = "2002:648:2c:101::1"
42
IP_IN_NET6_2 = "2002:648:2c:101::54"
43

    
44
GW_IN_NET_1 = "192.0.2.1"
45
GW2_IN_NET_1 = "192.0.2.100"
46
GW_IN_NET_2 = "198.51.100.1"
47
GW_IN_NET_3 = "203.0.113.1"
48
IP_IN_NET_1 = "192.0.2.50"
49
IP2_IN_NET_1 = "192.0.2.70"
50
IP_IN_NET_2 = "198.51.100.82"
51
IP_IN_NET_3 = "203.0.113.118"
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
  qa_config.ReleaseInstance(instance)
90

    
91

    
92
def LaunchInstance(instance, mac=None, ip=None, network=None,
93
                   mode=None, link=None, fail=False):
94

    
95
  name = instance["name"]
96
  net = GetNetOption(0, None, mac, ip, network, mode, link)
97
  AssertCommand(["gnt-instance", "add", "-o", "debootstrap+default",
98
                 "-t", "file", "--disk", "0:size=1G", "--net", net,
99
                 "--no-name-check", "--no-ip-check", "--no-install", name],
100
                 fail=fail)
101

    
102

    
103
def ModifyInstance(instance, idx=-1, action="add", mac=None,
104
                   ip=None, network=None, mode=None, link=None, fail=False):
105

    
106
  name = instance["name"]
107
  net = GetNetOption(idx, action, mac, ip, network, mode, link)
108
  AssertCommand(["gnt-instance", "modify", "--net", net, name], fail=fail)
109

    
110

    
111
def TestNetworkAddRemove():
112
  """gnt-network add/remove"""
113
  (network1, network2, network3, ) = qa_utils.GetNonexistentNetworks(3)
114

    
115
  # Note: Using RFC5737 addresses.
116
  # Add a network without subnet
117
  # TODO: make this fail=False once abstract networks are implemented
118
  AssertCommand(["gnt-network", "add", network1], fail=True)
119
  # remove non-existing network
120
  AssertCommand(["gnt-network", "remove", network1], fail=True)
121

    
122
  # Check wrong opcode parameters
123
  # wrone cidr notation
124
  AssertCommand(["gnt-network", "add", "--network", "xxxxx", network1],
125
                fail=True)
126
  # gateway outside the network
127
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
128
                 "--gateway", GW_IN_NET_2, network1],
129
                fail=True)
130
  # v6 gateway but not network
131
  #AssertCommand(["gnt-network", "add", "--gateway6", IP_IN_NET6_1, network1],
132
  #              fail=False)
133
  # gateway but not network
134
  #AssertCommand(["gnt-network", "add", "--gateway", IP_IN_NET_1, network1],
135
  #              fail=False)
136
  # wrong mac prefix
137
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
138
                 "--mac-prefix", "xxxxxx", network1],
139
                fail=True)
140

    
141
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
142
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
143
                 "--add-reserved-ips", IP_IN_NET_1,
144
                 "--network6", TEST_NET6_1,
145
                 "--gateway6", GW_IN_NET6_1, network1])
146

    
147
  # TODO: add a network that contains the nodes' IPs
148
  # This should reserve them
149
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2,
150
                 network2])
151

    
152
  # This does not reserve master/node IPs
153
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_3,
154
                 "--no-conflicts-check", network3])
155

    
156
  # Try to add a network with an existing name.
157
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network2],
158
                fail=True)
159

    
160
  AssertCommand(["gnt-network", "remove", network1])
161
  AssertCommand(["gnt-network", "remove", network2])
162
  AssertCommand(["gnt-network", "remove", network3])
163

    
164

    
165
def TestNetworkSetParams():
166
  """gnt-network modify"""
167
  (network1, ) = qa_utils.GetNonexistentNetworks(1)
168

    
169
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
170
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
171
                 "--add-reserved-ips", IP_IN_NET_1,
172
                 "--network6", TEST_NET6_1,
173
                 "--gateway6", GW_IN_NET6_1, network1])
174

    
175
  # Cannot modify subnet
176
  AssertCommand(["gnt-network", "modify", "--network", TEST_NET_2,
177
                 network1], fail=True)
178

    
179
  # Gateway outside network
180
  AssertCommand(["gnt-network", "modify", "--gateway", IP_IN_NET_2,
181
                 network1], fail=True)
182

    
183
  # Gateway with reserved ips
184
  AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1,
185
                 "--add-reserved-ips", IP2_IN_NET_1,
186
                 network1], fail=True)
187

    
188
  # Edit all
189
  # TODO: test reserved ips
190
  AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1,
191
                 "--network6", TEST_NET6_2,
192
                 "--gateway6", GW_IN_NET6_2,
193
                 network1])
194

    
195
  # reset everything
196
  AssertCommand(["gnt-network", "modify", "--gateway", "none",
197
                 "--network6", "none", "--gateway6", "none",
198
                 "--mac-prefix", "none",
199
                 network1])
200

    
201
  AssertCommand(["gnt-network", "remove", network1])
202

    
203

    
204
def TestNetworkConnect():
205
  """gnt-network connect/disconnect"""
206
  (group1, group2, ) = qa_utils.GetNonexistentGroups(2)
207
  (network1, network2, ) = qa_utils.GetNonexistentNetworks(2)
208
  defmode, deflink = GetNicParams()
209

    
210
  AssertCommand(["gnt-group", "add", group1])
211
  AssertCommand(["gnt-group", "add", group2])
212
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network1])
213
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
214

    
215
  AssertCommand(["gnt-network", "connect", network1,
216
                 defmode, deflink, group1])
217
  # This should produce a warning for group1
218
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
219
  # Network still connected
220
  AssertCommand(["gnt-network", "remove", network1], fail=True)
221

    
222
  instance1 = qa_config.AcquireInstance()
223
  # Add instance inside the network
224
  LaunchInstance(instance1, ip="pool", network=network1)
225
  # Conflicting IP, at least one instance belongs to the network
226
  AssertCommand(["gnt-network", "disconnect", network1], fail=True)
227
  RemoveInstance(instance1)
228

    
229
  AssertCommand(["gnt-network", "disconnect", network1])
230
  # This should only produce a warning.
231
  AssertCommand(["gnt-network", "disconnect", network1])
232

    
233
  instance1 = qa_config.AcquireInstance()
234
  # TODO: add conflicting image.
235
  LaunchInstance(instance1, ip=IP_IN_NET_2)
236
  # Conflicting IPs
237
  AssertCommand(["gnt-network", "connect", network2, defmode, deflink],
238
                fail=True)
239
  AssertCommand(["gnt-network", "connect", "--no-conflicts-check",
240
                 network2, defmode, deflink])
241
  AssertCommand(["gnt-network", "disconnect", network2])
242

    
243
  RemoveInstance(instance1)
244
  AssertCommand(["gnt-group", "remove", group1])
245
  AssertCommand(["gnt-group", "remove", group2])
246
  AssertCommand(["gnt-network", "remove", network1])
247
  AssertCommand(["gnt-network", "remove", network2])
248

    
249

    
250
def TestInstanceAddAndNetAdd():
251
  """ gnt-istance add / gnt-instance modify --net -1:add """
252
  (network1, network2) = qa_utils.GetNonexistentNetworks(2)
253
  defmode, deflink = GetNicParams()
254

    
255
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
256
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
257
                 "--add-reserved-ips", IP_IN_NET_1,
258
                 "--network6", TEST_NET6_1,
259
                 "--gateway6", GW_IN_NET6_1, network1])
260
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
261

    
262
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
263
  AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"])
264

    
265

    
266
  # (mac, ip, network, mode, link)
267
  success_cases = [
268
    (None, None, None, None, None),  # random mac and default nicparams
269
    ("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params
270
    (None, "pool", network1, None, None), # first IP in network given
271
    # TODO: include this use case with --no-conflicts-check
272
    #       just add an extra field in Launch|ModifyInstance
273
    #(None, "192.168.1.6", None, None, None), # IP but no net
274
    (None, None, network1, None, None) # nicparams/mac  inherited by network
275
    ]
276

    
277
  for (mac, ip, network, mode, link) in success_cases:
278
    instance1 = qa_config.AcquireInstance()
279
    LaunchInstance(instance1, mac, ip, network, mode, link)
280
    ModifyInstance(instance1, idx=-1, action="add", mac=mac,
281
                   ip=ip, network=network, mode=mode, link=link)
282
    ModifyInstance(instance1, idx=1, action="remove")
283
    RemoveInstance(instance1)
284

    
285
  fail_cases = [
286
    (None, None, None, "lala", None),
287
    (None, "lala", None, None, None),
288
    (None, None, "lala", None, None),
289
    (None, IP_IN_NET_2, None, None, None), # conflicting IP
290
    (None, None, None, "routed", None), # routed with no IP
291
    (None, "pool", network1, "routed", None), # nicparams along with network
292
    (None, "pool", network1, None, deflink)
293
   ]
294

    
295
  instance1 = qa_config.AcquireInstance()
296
  instance2 = qa_config.AcquireInstance()
297
  LaunchInstance(instance2)
298
  for (mac, ip, network, mode, link) in fail_cases:
299
    LaunchInstance(instance1, mac=mac, ip=ip, network=network,
300
                   mode=mode, link=link, fail=True)
301
    ModifyInstance(instance2, idx=-1, action="add", mac=mac,
302
                  ip=ip, network=network, mode=mode, link=link, fail=True)
303
    ModifyInstance(instance2, idx=0, action="modify", mac=mac,
304
                  ip=ip, network=network, mode=mode, link=link, fail=True)
305

    
306
  RemoveInstance(instance2)
307
  AssertCommand(["gnt-network", "disconnect", network1])
308
  AssertCommand(["gnt-network", "remove", network1])
309
  AssertCommand(["gnt-network", "disconnect", network2])
310
  AssertCommand(["gnt-network", "remove", network2])
311

    
312

    
313
def TestInstanceNetMod():
314
  """ gnt-istance modify --net 0:modify """
315
  (network1, network2) = qa_utils.GetNonexistentNetworks(2)
316
  defmode, deflink = GetNicParams()
317

    
318
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
319
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
320
                 "--add-reserved-ips", IP_IN_NET_1,
321
                 "--network6", TEST_NET6_1,
322
                 "--gateway6", GW_IN_NET6_1, network1])
323
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
324

    
325
  success_cases = [
326
    ("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params
327
    (None, "pool", network1, None, None), # first IP in network given
328
    (None, "none", "none", None, None),  # random mac and default nicparams
329
    (None, IP2_IN_NET_1, network1, None, None), # IP inside network
330
    #TODO: include this use case with --no-conflickts-check
331
    #(None, IP2_IN_NET_1, None, None, None), # IP but no net
332
    (None, None, network1, None, None) # nicparams/mac  inherited by network
333
    ]
334

    
335
  instance1 = qa_config.AcquireInstance()
336
  LaunchInstance(instance1)
337
  for (mac, ip, network, mode, link) in success_cases:
338
    ModifyInstance(instance1, idx=0, action="modify", mac=mac,
339
                  ip=ip, network=network, mode=mode, link=link)
340
    # reset to defaults
341
    ModifyInstance(instance1, idx=0, action="modify", mac="generate",
342
                   ip="none", network="none", mode=defmode, link=deflink)
343

    
344
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
345
  AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"])
346
  # put instance inside network1
347
  ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network1)
348
  # move instance to network2
349
  ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network2)
350

    
351
  RemoveInstance(instance1)
352
  AssertCommand(["gnt-network", "disconnect", network1])
353
  AssertCommand(["gnt-network", "remove", network1])
354
  AssertCommand(["gnt-network", "disconnect", network2])
355
  AssertCommand(["gnt-network", "remove", network2])