-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconnection.py
459 lines (373 loc) · 16 KB
/
connection.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# pylint: disable=unused-argument,line-too-long,function-redefined,no-name-in-module
# type: ignore[no-redef]
from behave import step
import nmci
@step('Add "{typ}" connection with options')
@step('Add "{typ}" connection with options "{options}"')
@step('Add "{typ}" connection named "{name}"')
@step('Add "{typ}" connection named "{name}" with options')
@step('Add "{typ}" connection named "{name}" with options "{options}"')
@step('Add "{typ}" connection named "{name}" for device "{ifname}"')
@step('Add "{typ}" connection named "{name}" for device "{ifname}" with options')
@step(
'Add "{typ}" connection named "{name}" for device "{ifname}" with options "{options}"'
)
def add_new_connection(
context, typ, name=None, ifname=None, options=None, cleanup=True
):
conn_name = ""
if name is not None:
if cleanup:
nmci.cleanup.add_connection(name)
conn_name = f"con-name {name}"
iface = ""
if ifname is not None:
if cleanup:
nmci.cleanup.add_iface(ifname)
iface = f"ifname {ifname}"
if options is None:
options = context.text.replace("\n", " ") if context.text is not None else " "
options = nmci.misc.str_replace_dict(options, context.noted)
nmci.process.nmcli(f"connection add type {typ} {conn_name} {iface} {options}")
@step('Add "{count}" "{typ}" connections named "{name}" for devices "{ifname}"')
@step(
'Add "{count}" "{typ}" connections named "{name}" for devices "{ifname}" with options'
)
@step(
'Add "{count}" "{typ}" connections named "{name}" for devices "{ifname}" with options "{options}"'
)
def add_multiple_new_connections(
context, count, typ, name=None, ifname=None, options=None
):
cons = {f"{name}_{i}": f"{ifname}_{i}" for i in range(int(count))}
nmci.cleanup.add_connection(list(cons.keys()))
nmci.cleanup.add_iface(list(cons.values()), op="delete")
for _con_name, _dev_name in cons.items():
add_new_connection(context, typ, _con_name, _dev_name, options, cleanup=False)
@step(
'Add insecure "{typ}" connection named "{name}" for device "{ifname}" with options'
)
def add_insecure(context, typ, name, ifname):
nmci.cleanup.add_connection(name)
options = context.text.replace("\n", " ") if context.text is not None else " "
options = nmci.misc.str_replace_dict(options, context.noted)
command = f"con add type {typ} con-name {name} ifname {ifname} {options}"
result = nmci.process.nmcli_force(command)
message = ""
if result.stdout:
message += f"\nSTDOUT:\n{result.stdout}\n"
if result.stderr:
message += f"\nSTDERR:\n{result.stderr}\n"
assert (
result.returncode == 0
), f"Command `nmcli {command}` returned {result.returncode}\n{message}"
assert (
"Error" not in result.stdout
), f"Command `nmcli {command}` returned {result.returncode}\nprinted 'Error' in STDOUT\n{message}"
err_split = result.stderr.strip("\n").split("\n")
err_filter = [line for line in err_split if line and "Warning" not in line]
assert (
not err_filter
), f"Command `nmcli {command}` returned {result.returncode}\nprinted more than 'Warning' to STDERR\n{message}"
@step(
'Add infiniband port named "{name}" for device "{ifname}" with parent "{parent}" and p-key "{pkey}"'
)
def add_port(context, name, ifname, parent, pkey):
nmci.process.nmcli(
f"connection add type infiniband con-name {name} ifname {ifname} parent {parent} p-key {pkey}"
)
@step('Modify connection "{connection}" property "{prop}" to noted value')
@step('Modify connection "{connection}" property "{prop}" to noted value "{index}"')
def modify_connection_with_noted(context, connection, prop, index="noted-value"):
nmci.process.nmcli(f"connection modify {connection} {prop} {context.noted[index]}")
@step('Add slave connection for master "{master}" on device "{device}" named "{name}"')
def open_slave_connection(context, master, device, name):
if "team" in master:
con_type = "team-slave"
elif "bond" in master:
con_type = "bond-slave"
else:
raise ValueError("could not guess connection type")
nmci.cleanup.add_connection(name)
nmci.cleanup.add_iface(device)
nmci.process.nmcli(
f"connection add type {con_type} ifname {device} con-name {name} master {master}"
)
@step('Bring "{action}" connection "{name}"')
@step('Bring "{action}" connection "{name}" for "{device}" device')
def start_stop_connection(context, name, action, device=""):
if action == "down":
if name not in nmci.process.nmcli("connection show --active"):
print("Warning: Connection is down no need to down it again")
return
if device:
device = f"ifname {device}"
nmci.process.nmcli(f"connection {action} id {name} {device}", timeout=180)
@step('Bring "{action}" connection "{name}" ignoring error')
def bring_up_connection_ignore_error(context, name, action):
nmci.process.nmcli_force(f"connection {action} id {name}", timeout=180)
@step('Check if "{name}" is active connection')
def is_active_connection(context, name):
active_list = nmci.process.nmcli("-t -f NAME connection show --active").split("\n")
assert name in active_list, f"Connection {name} is not active"
@step('Check if "{name}" is not active connection')
def is_nonactive_connection(context, name):
active_list = nmci.process.nmcli("-t -f NAME connection show --active").split("\n")
assert name not in active_list, f"Connection {name} is not active"
@step('Delete connection "{name}"')
def delete_connection(context, name):
nmci.process.nmcli(f"connection delete {name}", timeout=95)
@step('Fail up connection "{name}"')
@step('Fail up connection "{name}" in "{timeout}" seconds')
def fail_up_connection_for_device(context, name, timeout=None):
t_arg = ""
if timeout:
t_arg = f"-w {timeout}"
try:
nmci.process.nmcli(f"{t_arg} connection up id {name}", timeout=180)
except Exception: # pylint: disable=broad-except
return
raise Exception(
f"nmcli connection up {name} was succesfull. this should not happen"
)
@step('Modify connection "{name}" changing options "{options}"')
@step('Modify connection "{name}" changing options')
def modify_connection(context, name, options=None):
if options is None:
options = context.text.replace("\n", " ") if context.text is not None else " "
options = nmci.misc.str_replace_dict(options, context.noted)
nmci.process.nmcli(f"con modify {name} {options}")
@step("Wait for testeth0")
def wait_for_eth0(context):
nmci.veth.wait_for_testeth0()
@step("Reload connections")
def reload_connections(context):
nmci.nmutil.reload_NM_connections()
def libnm_get_connection(nm_client, con_name):
con = None
for connection in nm_client.get_connections():
if connection.get_id() == con_name:
assert not con, f"multiple connections with id '{con_name}'"
con = connection
assert con, f"no connection with id '{con_name}'"
return con
def parse_nm_settings_flags_string(nm_flags, flags):
flags = [f.strip() for f in flags.split(",")]
nm_flags = nm_flags.NONE
for flag in flags:
if flag:
nm_flags |= getattr(nm_flags, flag)
return nm_flags
@step('Add connection with name "{name}" and uuid "{uuid}" using libnm')
@step(
'Add connection with name "{name}" and uuid "{uuid}" using libnm with flags "{flags}"'
)
def add_connection(context, name, uuid, flags="TO_DISK"):
nmci.cleanup.add_connection(name)
NM = nmci.util.NM # pylint: disable=invalid-name
GLib = nmci.util.GLib # pylint: disable=invalid-name
main_loop = GLib.MainLoop()
nm_client = NM.Client.new(None)
if uuid == "random":
uuid = NM.utils_uuid_generate()
elif uuid == "noted":
uuid = context.noted["noted-value"]
elif uuid.startswith("noted."):
index = uuid.replace("noted.", "")
uuid = context.noted[index]
nm_flags = parse_nm_settings_flags_string(NM.SettingsAddConnection2Flags, flags)
con2 = NM.SimpleConnection()
s_con = NM.SettingConnection(type="802-3-ethernet", id=name, uuid=uuid)
con2.add_setting(s_con)
result = {}
def _add_connection2_cb(cli, async_result, user_data):
try:
nm_client.add_connection2_finish(async_result)
except Exception as exc: # pylint: disable=broad-except
result["error"] = exc
main_loop.quit()
nm_client.add_connection2(
con2.to_dbus(NM.ConnectionSerializationFlags.ALL),
nm_flags,
None,
False,
None,
_add_connection2_cb,
None,
)
main_loop.run()
assert "error" not in result, f'Add connection {name} failed: {result["error"]}'
@step('Clone connection "{con_src}" to "{con_dst}" using libnm')
@step('Clone connection "{con_src}" to "{con_dst}" using libnm with flags "{flags}"')
def clone_connection(context, con_src, con_dst, flags="TO_DISK"):
nmci.cleanup.add_connection(con_dst)
NM = nmci.util.NM # pylint: disable=invalid-name
GLib = nmci.util.GLib # pylint: disable=invalid-name
main_loop = GLib.MainLoop()
nm_client = NM.Client.new(None)
con = libnm_get_connection(nm_client, con_src)
nm_flags = parse_nm_settings_flags_string(NM.SettingsAddConnection2Flags, flags)
con2 = NM.SimpleConnection.new_clone(con)
s_con = con2.get_setting_connection()
s_con.set_property(NM.SETTING_CONNECTION_ID, con_dst)
s_con.set_property(NM.SETTING_CONNECTION_UUID, NM.utils_uuid_generate())
result = {}
def _add_connection2_cb(cli, async_result, user_data):
try:
nm_client.add_connection2_finish(async_result)
except Exception as exc: # pylint: disable=broad-except
result["error"] = exc
main_loop.quit()
nm_client.add_connection2(
con2.to_dbus(NM.ConnectionSerializationFlags.ALL),
nm_flags,
None,
False,
None,
_add_connection2_cb,
None,
)
main_loop.run()
assert (
"error" not in result
), f'Clone connection {con_dst} failed: {result["error"]}'
@step('Update connection "{con_name}" changing options "{options}" using libnm')
@step(
'Update connection "{con_name}" changing options "{options}" using libnm with flags "{flags}"'
)
def update2_connection_autoconnect(context, con_name, options, flags=""):
NM = nmci.util.NM # pylint: disable=invalid-name
GLib = nmci.util.GLib # pylint: disable=invalid-name
main_loop = GLib.MainLoop()
nm_client = NM.Client.new(None)
con = libnm_get_connection(nm_client, con_name)
nm_flags = parse_nm_settings_flags_string(NM.SettingsUpdate2Flags, flags)
con2 = NM.SimpleConnection.new_clone(con)
s_con = con2.get_setting_connection()
for option in options.split(","):
option = [o.strip() for o in option.split(":")]
if len(option) == 3:
if option[1].lower().startswith("i"):
option[1] = int(option[2])
elif option[1].lower().startswith("f"):
option[1] = float(option[2])
elif option[1].lower().startswith("b"):
if option[2].lower().startswith("t"):
option[1] = True
else:
option[1] = False
else:
option[1] = option[2]
value = option[1]
s_con.set_property(getattr(NM, option[0]), value)
result = {}
def _update2_cb(con, async_result, user_data):
try:
con.update2_finish(async_result)
except Exception as exc: # pylint: disable=broad-except
result["error"] = exc
main_loop.quit()
con.update2(
con2.to_dbus(NM.ConnectionSerializationFlags.ALL),
nm_flags,
None,
None,
_update2_cb,
None,
)
main_loop.run()
assert (
"error" not in result
), f'Update connection {con_name} failed: {result["error"]}'
@step(
'Add bridges over VLANs in range from "{begin}" to "{end}" on interface "{ifname}" via libnm'
)
def add_bridges_vlans_range(context, begin, end, ifname):
begin = int(begin)
end = int(end)
assert begin > 0, f"invalid range: begin is not positive integer: {begin}"
assert end > 0, f"invalid range: end is not positive integer: {end}"
assert begin <= end, f"invalid range: begin is not less than end: {begin} > {end}"
vlan_range = [f"{ifname}.{id}" for id in range(begin, end + 1)]
vlan_range += [f"br{id}" for id in range(begin, end + 1)]
callbacks = []
callbacks.append(lambda: nmci.nmutil.stop_NM_service(timeout=120))
callbacks.extend(lambda dev=dev: nmci.ip.link_delete(dev) for dev in vlan_range)
callbacks.extend(
lambda dev=dev: nmci.cleanup.CleanupFile.delete_file(
f"/etc/sysconfig/network-scripts/ifcfg-{dev}"
)
for dev in vlan_range
)
callbacks.extend(
lambda dev=dev: nmci.cleanup.CleanupFile.delete_glob(
f"/etc/NetworkManager/system-connections/{dev}*"
)
for dev in vlan_range
)
callbacks.append(lambda: nmci.nmutil.restart_NM_service())
nmci.cleanup.add_callback(
name="cleanup-add_bridges_vlans_range",
callback=callbacks,
priority=nmci.Cleanup.PRIORITY_CALLBACK_DEFAULT,
)
NM = nmci.util.NM # pylint: disable=invalid-name
GLib = nmci.util.GLib # pylint: disable=invalid-name
nm_client = NM.Client.new(None)
result = {}
def _add_connection_cb(cli, async_result, user_data):
try:
cli.add_connection_finish(async_result)
except Exception as exc: # pylint: disable=broad-except
result["error"] = exc
if user_data is not None:
user_data.quit()
for i in range(begin, end + 1):
main_loop = GLib.MainLoop()
con = NM.SimpleConnection.new()
uuid = NM.utils_uuid_generate()
s_con = NM.SettingConnection(type="bridge", id=f"br{i}", uuid=uuid)
s_con.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, f"br{i}")
s_bridge = NM.SettingBridge.new()
s_ip4 = NM.SettingIP4Config.new()
s_ip4.set_property(NM.SETTING_IP_CONFIG_METHOD, "disabled")
s_ip6 = NM.SettingIP6Config.new()
s_ip6.set_property(NM.SETTING_IP_CONFIG_METHOD, "disabled")
con.add_setting(s_con)
con.add_setting(s_bridge)
con.add_setting(s_ip4)
con.add_setting(s_ip6)
nm_client.add_connection_async(con, True, None, _add_connection_cb, main_loop)
main_loop.run()
main_loop = GLib.MainLoop()
con = NM.SimpleConnection.new()
uuid = NM.utils_uuid_generate()
s_con = NM.SettingConnection(type="vlan", id=f"{ifname}.{i}", uuid=uuid)
s_con.set_property(NM.SETTING_CONNECTION_INTERFACE_NAME, f"{ifname}.{i}")
s_con.set_property(NM.SETTING_CONNECTION_SLAVE_TYPE, "bridge")
s_con.set_property(NM.SETTING_CONNECTION_MASTER, f"br{i}")
s_vlan = NM.SettingVlan(id=i, parent=ifname)
con.add_setting(s_con)
con.add_setting(s_vlan)
nm_client.add_connection_async(con, True, None, _add_connection_cb, main_loop)
main_loop.run()
assert "error" not in result, f"add connection {i} failed: {result['error']}"
@step('Cleanup connection "{connection}"')
@step('Cleanup connection "{connection}" with priority "{priority}"')
@step('Cleanup connection "{connection}" and device "{device}"')
def cleanup_connection(context, connection, device=None, priority=None):
if priority is None:
priority = nmci.cleanup.Cleanup.PRIORITY_CONNECTION
else:
priority = int(priority)
nmci.cleanup.add_connection(connection, priority=priority)
if device is not None:
context.execute_steps(f'* Cleanup device "{device}"')
@step('Note the value of property "{prop}" of connection "{con}"')
@step(
'Note the value of property "{prop}" of connection "{con}" as noted value "{index}"'
)
def note_gotten_value(context, prop, con, index="noted-value"):
if not hasattr(context, "noted"):
context.noted = {}
context.noted[index] = nmci.process.nmcli(f"-g {prop} c s {con}")