Statistics
| Branch: | Tag: | Revision:

root / qa / qa_network.py @ ddfd3c3d

History | View | Annotate | Download (13.7 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
from ganeti import constants
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

    
54
def GetNicParams():
55
  default_mode = "bridged"
56
  default_link = "br0"
57
  nicparams = qa_config.get("default-nicparams")
58
  if nicparams:
59
    mode = nicparams.get("mode", default_mode)
60
    link = nicparams.get("link", default_link)
61
  else:
62
    mode = default_mode
63
    link = default_link
64

    
65
  return mode, link
66

    
67

    
68
def GetNetOption(idx=-1, action=None, mac=None, ip=None, network=None,
69
                 mode=None, link=None):
70
  net = "%d:" % idx
71
  if action:
72
    net += action
73
  if mac:
74
    net += ",mac=" + mac
75
  if ip:
76
    net += ",ip=" + ip
77
  if network:
78
    net += ",network=" + network
79
  if mode:
80
    net += ",mode=" + mode
81
  if link:
82
    net += ",link=" + link
83

    
84
  return net.replace(":,", ":")
85

    
86

    
87
def RemoveInstance(instance):
88
  name = instance["name"]
89
  AssertCommand(["gnt-instance", "remove", "-f", name])
90
  qa_config.ReleaseInstance(instance)
91

    
92

    
93
def GetInstance():
94
  inst = qa_config.AcquireInstance()
95
  qa_config.SetInstanceTemplate(inst, constants.DT_PLAIN)
96
  return inst
97

    
98

    
99
def LaunchInstance(instance, mac=None, ip=None, network=None,
100
                   mode=None, link=None, fail=False):
101

    
102
  name = instance["name"]
103
  templ = qa_config.GetInstanceTemplate(instance)
104
  net = GetNetOption(0, None, mac, ip, network, mode, link)
105
  AssertCommand(["gnt-instance", "add", "-o", qa_config.get("os"),
106
                 "-t", templ, "--disk", "0:size=1G", "--net", net,
107
                 "--no-name-check", "--no-ip-check", "--no-install", name],
108
                 fail=fail)
109

    
110

    
111
def ModifyInstance(instance, idx=-1, action="add", mac=None,
112
                   ip=None, network=None, mode=None, link=None, fail=False):
113

    
114
  name = instance["name"]
115
  net = GetNetOption(idx, action, mac, ip, network, mode, link)
116
  AssertCommand(["gnt-instance", "modify", "--net", net, name], fail=fail)
117

    
118

    
119
def TestNetworkAddRemove():
120
  """gnt-network add/remove"""
121
  (network1, network2, network3, ) = qa_utils.GetNonexistentNetworks(3)
122

    
123
  # Note: Using RFC5737 addresses.
124
  # Add a network without subnet
125
  AssertCommand(["gnt-network", "add", network1])
126
  # remove non-existing network
127
  AssertCommand(["gnt-network", "remove", network2], fail=True)
128
  AssertCommand(["gnt-network", "remove", network1])
129

    
130
  # Check wrong opcode parameters
131
  # wrone cidr notation
132
  AssertCommand(["gnt-network", "add", "--network", "xxxxx", network1],
133
                fail=True)
134
  # gateway outside the network
135
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
136
                 "--gateway", GW_IN_NET_2, network1],
137
                fail=True)
138
  # v6 gateway but not network
139
  #AssertCommand(["gnt-network", "add", "--gateway6", IP_IN_NET6_1, network1],
140
  #              fail=False)
141
  # gateway but not network
142
  #AssertCommand(["gnt-network", "add", "--gateway", IP_IN_NET_1, network1],
143
  #              fail=False)
144
  # wrong mac prefix
145
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
146
                 "--mac-prefix", "xxxxxx", network1],
147
                fail=True)
148

    
149
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
150
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
151
                 "--add-reserved-ips", IP_IN_NET_1,
152
                 "--network6", TEST_NET6_1,
153
                 "--gateway6", GW_IN_NET6_1, network1])
154

    
155
  # TODO: add a network that contains the nodes' IPs
156
  # This should reserve them
157
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2,
158
                 network2])
159

    
160
  # This does not reserve master/node IPs
161
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_3,
162
                 "--no-conflicts-check", network3])
163

    
164
  # Try to add a network with an existing name.
165
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network2],
166
                fail=True)
167

    
168
  AssertCommand(["gnt-network", "remove", network1])
169
  AssertCommand(["gnt-network", "remove", network2])
170
  AssertCommand(["gnt-network", "remove", network3])
171

    
172

    
173
def TestNetworkSetParams():
174
  """gnt-network modify"""
175
  (network1, ) = qa_utils.GetNonexistentNetworks(1)
176

    
177
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
178
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
179
                 "--add-reserved-ips", IP_IN_NET_1,
180
                 "--network6", TEST_NET6_1,
181
                 "--gateway6", GW_IN_NET6_1, network1])
182

    
183
  # Cannot modify subnet
184
  AssertCommand(["gnt-network", "modify", "--network", TEST_NET_2,
185
                 network1], fail=True)
186

    
187
  # Gateway outside network
188
  AssertCommand(["gnt-network", "modify", "--gateway", IP_IN_NET_2,
189
                 network1], fail=True)
190

    
191
  # Gateway with reserved ips
192
  AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1,
193
                 "--add-reserved-ips", IP2_IN_NET_1,
194
                 network1], fail=True)
195

    
196
  # Edit all
197
  # TODO: test reserved ips
198
  AssertCommand(["gnt-network", "modify", "--gateway", GW2_IN_NET_1,
199
                 "--network6", TEST_NET6_2,
200
                 "--gateway6", GW_IN_NET6_2,
201
                 network1])
202

    
203
  # reset everything
204
  AssertCommand(["gnt-network", "modify", "--gateway", "none",
205
                 "--network6", "none", "--gateway6", "none",
206
                 "--mac-prefix", "none",
207
                 network1])
208

    
209
  AssertCommand(["gnt-network", "remove", network1])
210

    
211

    
212
def TestNetworkConnect():
213
  """gnt-network connect/disconnect"""
214
  (group1, group2, ) = qa_utils.GetNonexistentGroups(2)
215
  (network1, network2, ) = qa_utils.GetNonexistentNetworks(2)
216
  defmode, deflink = GetNicParams()
217

    
218
  AssertCommand(["gnt-group", "add", group1])
219
  AssertCommand(["gnt-group", "add", group2])
220
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1, network1])
221
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
222

    
223
  AssertCommand(["gnt-network", "connect", network1,
224
                 defmode, deflink, group1])
225
  # This should produce a warning for group1
226
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
227
  # Network still connected
228
  AssertCommand(["gnt-network", "remove", network1], fail=True)
229

    
230
  instance1 = GetInstance()
231
  # Add instance inside the network
232
  LaunchInstance(instance1, ip="pool", network=network1)
233
  # Conflicting IP, at least one instance belongs to the network
234
  AssertCommand(["gnt-network", "disconnect", network1], fail=True)
235
  RemoveInstance(instance1)
236

    
237
  AssertCommand(["gnt-network", "disconnect", network1])
238
  # This should only produce a warning.
239
  AssertCommand(["gnt-network", "disconnect", network1])
240

    
241
  instance1 = GetInstance()
242
  # TODO: add conflicting image.
243
  LaunchInstance(instance1, ip=IP_IN_NET_2)
244
  # Conflicting IPs
245
  AssertCommand(["gnt-network", "connect", network2, defmode, deflink],
246
                fail=True)
247
  AssertCommand(["gnt-network", "connect", "--no-conflicts-check",
248
                 network2, defmode, deflink])
249
  AssertCommand(["gnt-network", "disconnect", network2])
250

    
251
  RemoveInstance(instance1)
252
  AssertCommand(["gnt-group", "remove", group1])
253
  AssertCommand(["gnt-group", "remove", group2])
254
  AssertCommand(["gnt-network", "remove", network1])
255
  AssertCommand(["gnt-network", "remove", network2])
256

    
257

    
258
def TestInstanceAddAndNetAdd():
259
  """ gnt-istance add / gnt-instance modify --net -1:add """
260
  (network1, network2, network3) = qa_utils.GetNonexistentNetworks(3)
261
  defmode, deflink = GetNicParams()
262

    
263
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
264
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
265
                 "--add-reserved-ips", IP_IN_NET_1,
266
                 "--network6", TEST_NET6_1,
267
                 "--gateway6", GW_IN_NET6_1, network1])
268
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
269

    
270
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
271
  AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"])
272

    
273
  AssertCommand(["gnt-network", "add", network3])
274
  AssertCommand(["gnt-network", "connect", network3, "routed", "rt5000"])
275

    
276
  # (mac, ip, network, mode, link)
277
  success_cases = [
278
    (None, None, None, None, None),  # random mac and default nicparams
279
    ("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params
280
    (None, "pool", network1, None, None), # first IP in network given
281
    # TODO: include this use case with --no-conflicts-check
282
    #       just add an extra field in Launch|ModifyInstance
283
    #(None, "192.168.1.6", None, None, None), # IP but no net
284
    (None, None, network1, None, None), # nicparams/mac  inherited by network
285
    ]
286

    
287
  for (mac, ip, network, mode, link) in success_cases:
288
    instance1 = GetInstance()
289
    LaunchInstance(instance1, mac, ip, network, mode, link)
290
    ModifyInstance(instance1, idx=-1, action="add", mac=mac,
291
                   ip=ip, network=network, mode=mode, link=link)
292
    ModifyInstance(instance1, idx=1, action="remove")
293
    RemoveInstance(instance1)
294

    
295
  # test _AllIPs()
296
  instance1 = GetInstance()
297
  LaunchInstance(instance1, ip=IP_IN_NET_3)
298
  # this results to "Configuration data not consistent
299
  ModifyInstance(instance1, idx=-1, action="add", ip=IP_IN_NET_3)
300
  ModifyInstance(instance1, idx=-1, action="add",
301
                 ip=IP_IN_NET_3, network=network3)
302
  # this raises Corrupt configuration data
303
  ModifyInstance(instance1, idx=-1, action="add",
304
                 ip=IP_IN_NET_3, network=network3, fail=True)
305
  RemoveInstance(instance1)
306

    
307

    
308
  fail_cases = [
309
    (None, None, None, "lala", None),
310
    (None, "lala", None, None, None),
311
    (None, None, "lala", None, None),
312
    (None, IP_IN_NET_2, None, None, None), # conflicting IP
313
    (None, None, None, "routed", None), # routed with no IP
314
    (None, "pool", network1, "routed", None), # nicparams along with network
315
    (None, "pool", network1, None, deflink),
316
   ]
317

    
318
  instance1 = GetInstance()
319
  instance2 = GetInstance()
320
  LaunchInstance(instance2)
321
  for (mac, ip, network, mode, link) in fail_cases:
322
    LaunchInstance(instance1, mac=mac, ip=ip, network=network,
323
                   mode=mode, link=link, fail=True)
324
    ModifyInstance(instance2, idx=-1, action="add", mac=mac,
325
                  ip=ip, network=network, mode=mode, link=link, fail=True)
326
    ModifyInstance(instance2, idx=0, action="modify", mac=mac,
327
                  ip=ip, network=network, mode=mode, link=link, fail=True)
328

    
329
  RemoveInstance(instance2)
330
  AssertCommand(["gnt-network", "disconnect", network1])
331
  AssertCommand(["gnt-network", "remove", network1])
332
  AssertCommand(["gnt-network", "disconnect", network2])
333
  AssertCommand(["gnt-network", "remove", network2])
334
  AssertCommand(["gnt-network", "disconnect", network3])
335
  AssertCommand(["gnt-network", "remove", network3])
336

    
337

    
338
def TestInstanceNetMod():
339
  """ gnt-istance modify --net 0:modify """
340
  (network1, network2) = qa_utils.GetNonexistentNetworks(2)
341
  defmode, deflink = GetNicParams()
342

    
343
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_1,
344
                 "--gateway", GW_IN_NET_1, "--mac-prefix", "aa:bb:cc",
345
                 "--add-reserved-ips", IP_IN_NET_1,
346
                 "--network6", TEST_NET6_1,
347
                 "--gateway6", GW_IN_NET6_1, network1])
348
  AssertCommand(["gnt-network", "connect", network1, defmode, deflink])
349

    
350
  success_cases = [
351
    ("generate", IP_IN_NET_3, None, "routed", "rt5000"), # given params
352
    (None, "pool", network1, None, None), # first IP in network given
353
    (None, "none", "none", None, None),  # random mac and default nicparams
354
    (None, IP2_IN_NET_1, network1, None, None), # IP inside network
355
    #TODO: include this use case with --no-conflickts-check
356
    #(None, IP2_IN_NET_1, None, None, None), # IP but no net
357
    (None, None, network1, None, None), # nicparams/mac  inherited by network
358
    ]
359

    
360
  instance1 = GetInstance()
361
  LaunchInstance(instance1)
362
  for (mac, ip, network, mode, link) in success_cases:
363
    ModifyInstance(instance1, idx=0, action="modify", mac=mac,
364
                  ip=ip, network=network, mode=mode, link=link)
365
    # reset to defaults
366
    ModifyInstance(instance1, idx=0, action="modify", mac="generate",
367
                   ip="none", network="none", mode=defmode, link=deflink)
368

    
369
  AssertCommand(["gnt-network", "add", "--network", TEST_NET_2, network2])
370
  AssertCommand(["gnt-network", "connect", network2, "routed", "rt5000"])
371
  # put instance inside network1
372
  ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network1)
373
  # move instance to network2
374
  ModifyInstance(instance1, idx=0, action="modify", ip="pool", network=network2)
375

    
376
  RemoveInstance(instance1)
377
  AssertCommand(["gnt-network", "disconnect", network1])
378
  AssertCommand(["gnt-network", "remove", network1])
379
  AssertCommand(["gnt-network", "disconnect", network2])
380
  AssertCommand(["gnt-network", "remove", network2])