diff options
author | Jonathan Cave <jonathan.cave@canonical.com> | 2017-10-06 12:07:16 +0100 |
---|---|---|
committer | Jonathan Cave <jonathan.cave@canonical.com> | 2017-10-18 14:05:14 +0100 |
commit | 40f979c707edee5f4b8a41bf2e45142867564952 (patch) | |
tree | 7204df65cabf6e071170a90dbfcff8e2b7e96244 /bin | |
parent | c1e95b2d2c4909a577542395ae34f8630c7fedb8 (diff) |
Import wireless tests form p-p-snappy
Use Jinja templating to handle requirment testing on snappy/non-snappy systems
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/net_driver_info | 41 | ||||
-rwxr-xr-x | bin/wifi_nmcli_test | 150 |
2 files changed, 191 insertions, 0 deletions
diff --git a/bin/net_driver_info b/bin/net_driver_info new file mode 100755 index 00000000..ee5db461 --- /dev/null +++ b/bin/net_driver_info @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# Copyright 2017 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Jonathan Cave <jonathan.cave@canonical.com> +# +# Print info about drivers we can identify automatically and also those we +# identify in the special interest list! + +from pathlib import Path +import sys + +# Store pairs of (interface, driver) +driver_list = [] + +# Find drivers in sysfs +for interface in Path("/sys/class/net").iterdir(): + mod_path = Path("{}/device/driver/module".format(interface)) + if mod_path.is_symlink(): + driver_list.append((interface.name, mod_path.resolve().name)) + +# Add user requested modules to the list. Create "unknown" interfaces if none +# of the identified interfaces above are using it +for user_driver in sys.argv[1:]: + if Path("/sys/module/{}".format(user_driver)).exists(): + if not any(x[1] == user_driver for x in driver_list): + driver_list.append(("unknown", user_driver)) + else: + print("Requested driver {} not loaded\n".format(user_driver)) + +# Produce the output +for interface, driver in driver_list: + print("Interface {} using module {}".format(interface, driver)) + sysfs_path = Path("/sys/module/{}/parameters".format(driver)) + if sysfs_path.is_dir(): + print(" Parameters:") + for path in Path(sysfs_path).iterdir(): + if path.is_file(): + print(" {}: {}".format(path.name, path.read_text().strip())) + print() diff --git a/bin/wifi_nmcli_test b/bin/wifi_nmcli_test new file mode 100755 index 00000000..0f6db85d --- /dev/null +++ b/bin/wifi_nmcli_test @@ -0,0 +1,150 @@ +#!/usr/bin/env python3 +# Copyright 2017 Canonical Ltd. +# All rights reserved. +# +# Written by: +# Jonathan Cave <jonathan.cave@canonical.com> +# +# wireless connection tests using nmcli + + +import argparse +import functools +import subprocess as sp +import sys + + +print = functools.partial(print, flush=True) + + +def print_head(txt): + print("##", txt) + + +def print_cmd(cmd): + print("+", cmd) + + +def cleanup_nm_connections(): + print_head("Cleaning up NM connections") + cmd = "nmcli -t -f TYPE,UUID,NAME c" + print_cmd(cmd) + output = sp.check_output(cmd, shell=True) + for line in output.decode(sys.stdout.encoding).splitlines(): + type, uuid, name = line.strip().split(':') + if type == '802-11-wireless': + print("Deleting connection", name) + cmd = "nmcli c delete {}".format(uuid) + print_cmd(cmd) + sp.call(cmd, shell=True) + print() + + +def device_rescan(): + print_head("Calling a rescan") + cmd = "nmcli d wifi rescan" + print_cmd(cmd) + sp.call(cmd, shell=True) + print() + + +def list_aps(args): + print_head("List APs") + count = 0 + cmd = "nmcli -t -f SSID,CHAN,FREQ d wifi list ifname {}".format( + args.device) + print_cmd(cmd) + output = sp.check_output(cmd, shell=True) + for line in output.decode(sys.stdout.encoding).splitlines(): + ssid, channel, frequency = line.strip().split(':') + print("SSID: {} Chan: {} Freq: {}".format(ssid, channel, frequency)) + if hasattr(args, 'essid'): + if ssid == args.essid: + count += 1 + else: + count += 1 + print() + return count + + +def open_connection(args): + print_head("Connection attempt") + cmd = "nmcli d wifi connect {} ifname {} name TEST_CON".format( + args.essid, args.device) + print_cmd(cmd) + sp.call(cmd, shell=True) + cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device) + print_cmd(cmd) + output = sp.check_output(cmd, shell=True) + state = output.decode(sys.stdout.encoding).strip() + print(state) + rc = 1 + if state.startswith('100'): + rc = 0 + print() + return rc + + +def secured_connection(args): + print_head("Connection attempt") + cmd = "nmcli d wifi connect {} password {} ifname {} name TEST_CON".format( + args.essid, args.psk, args.device) + print_cmd(cmd) + sp.call(cmd, shell=True) + cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(args.device) + print_cmd(cmd) + output = sp.check_output(cmd, shell=True) + state = output.decode(sys.stdout.encoding).strip() + print(state) + rc = 1 + if state.startswith('100'): + rc = 0 + print() + return rc + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='WiFi connection test using mmcli') + + subparsers = parser.add_subparsers(dest='test_type') + subparsers.required = True + + parser_scan = subparsers.add_parser( + 'scan', help='Test can scan for networks only') + parser_scan.add_argument( + 'device', type=str, help='Device name e.g. wlan0') + + parser_open = subparsers.add_parser( + 'open', help='Test connection to an open access point') + parser_open.add_argument( + 'device', type=str, help='Device name e.g. wlan0') + parser_open.add_argument('essid', type=str, help='ESSID') + parser_open.set_defaults(func=open_connection) + + parser_secured = subparsers.add_parser( + 'secured', help='Test connection to a secured access point') + parser_secured.add_argument( + 'device', type=str, help='Device name e.g. wlan0') + parser_secured.add_argument('essid', type=str, help='ESSID') + parser_secured.add_argument('psk', type=str, help='Pre-Shared Key') + parser_secured.set_defaults(func=secured_connection) + args = parser.parse_args() + + cleanup_nm_connections() + device_rescan() + count = list_aps(args) + + if args.test_type == 'scan': + if count == 0: + print("Failed to find any APs") + sys.exit(1) + else: + print("Found {} access points".format(count)) + sys.exit(0) + + if args.func: + try: + sys.exit(args.func(args)) + finally: + cleanup_nm_connections() |