summaryrefslogtreecommitdiff
path: root/bin
diff options
authorJonathan Cave <jonathan.cave@canonical.com>2020-06-29 17:37:37 +0100
committerJonathan Cave <jonathan.cave@canonical.com>2020-07-20 17:54:40 +0100
commitf60e81d4db7fc3076d40d1dbb6a32ccc30d60e99 (patch)
tree1f34ff81841129197a649b8152235612da4e6b3d /bin
parent50cd2dbbac86a5e450bcb401def12f274de7ba14 (diff)
wifi_nmcli_test.py: add ping test, disable ipv6
Make sure connection status not influenced by ipv6. Make sure and IP address is assigned and the gateway address can be pinged
Diffstat (limited to 'bin')
-rwxr-xr-xbin/wifi_nmcli_test.py146
1 files changed, 126 insertions, 20 deletions
diff --git a/bin/wifi_nmcli_test.py b/bin/wifi_nmcli_test.py
index 71f01c9..18b5572 100755
--- a/bin/wifi_nmcli_test.py
+++ b/bin/wifi_nmcli_test.py
@@ -17,6 +17,7 @@ import subprocess as sp
import sys
import time
+from gateway_ping_test import ping
print = functools.partial(print, flush=True)
@@ -82,41 +83,146 @@ def list_aps(args):
return count
+def print_address_info(interface):
+ cmd = 'ip address show dev {}'.format(interface)
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ print()
+
+
+def print_route_info():
+ cmd = 'ip route'
+ print_cmd(cmd)
+ sp.call(cmd, shell=True)
+ print()
+
+
+def perform_ping_test(interface):
+ target = None
+ cmd = 'nmcli -g IP4.GATEWAY c show TEST_CON'
+ print_cmd(cmd)
+ output = sp.check_output(cmd, shell=True)
+ target = output.decode(sys.stdout.encoding).strip()
+ print('Got gateway address: {}'.format(target))
+
+ if target:
+ count = 5
+ result = ping(target, interface, count, 4, True)
+ if result['received'] == count:
+ return True
+
+ return False
+
+
+def wait_for_connected(interface, max_wait=5):
+ connected = False
+ attempts = 0
+ while not connected and attempts < max_wait:
+ 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)
+
+ if state.startswith('100'):
+ connected = True
+ break
+ time.sleep(1)
+ attempts += 1
+ if connected:
+ print("Reached connected state")
+ else:
+ print("ERROR: did not reach connected state")
+ print()
+ return connected
+
+
def open_connection(args):
+ # Configure the connection
+ # ipv4.dhcp-timeout 30 : ensure plenty of time to get address
+ # ipv6.method ignore : I believe that NM can report the device as Connected
+ # if an IPv6 address is setup. This should ensure in
+ # this test we are using IPv4
print_head("Connection attempt")
- cmd = "nmcli d wifi connect {} ifname {} name TEST_CON".format(
- args.essid, args.device)
+ cmd = ("nmcli c add con-name TEST_CON "
+ "ifname {} "
+ "type wifi "
+ "ssid {} "
+ "ipv4.dhcp-timeout 30 "
+ "ipv6.method ignore".format(args.device, args.essid))
print_cmd(cmd)
sp.call(cmd, shell=True)
- cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(
- args.device)
+
+ # Make sure the connection is brought up
+ cmd = "nmcli c up TEST_CON"
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
+ sp.call(cmd, shell=True)
print()
+
+ print_head("Ensure interface is connected")
+ reached_connected = wait_for_connected(args.device)
+
+ rc = 1
+ if reached_connected:
+ print_head("Display address")
+ print_address_info(args.device)
+
+ print_head("Display route table")
+ print_route_info()
+
+ print_head("Perform a ping test")
+ test_result = perform_ping_test(args.device)
+ if test_result:
+ rc = 0
+ print("Connection test passed\n")
+ else:
+ print("Connection test failed\n")
return rc
def secured_connection(args):
+ # Configure the connection
+ # ipv4.dhcp-timeout 30 : ensure plenty of time to get address
+ # ipv6.method ignore : I believe that NM can report the device as Connected
+ # if an IPv6 address is setup. This should ensure in
+ # this test we are using IPv4
print_head("Connection attempt")
- cmd = ("nmcli --wait 180 d wifi connect {} password {} ifname {} name "
- "TEST_CON".format(args.essid, args.psk, args.device))
+ cmd = ("nmcli c add con-name TEST_CON "
+ "ifname {} "
+ "type wifi "
+ "ssid {} "
+ "wifi-sec.key-mgmt wpa-psk "
+ "wifi-sec.psk {} "
+ "ipv4.dhcp-timeout 30 "
+ "ipv6.method ignore".format(args.device, args.essid, args.psk))
print_cmd(cmd)
sp.call(cmd, shell=True)
- cmd = "nmcli -m tabular -t -f GENERAL.STATE d show {}".format(
- args.device)
+
+ # Make sure the connection is brought up
+ cmd = "nmcli c up TEST_CON"
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
+ sp.call(cmd, shell=True)
print()
+
+ print_head("Ensure interface is connected")
+ reached_connected = wait_for_connected(args.device)
+
+ rc = 1
+ if reached_connected:
+ print_head("Display address")
+ print_address_info(args.device)
+
+ print_head("Display route table")
+ print_route_info()
+
+ print_head("Perform a ping test")
+ test_result = perform_ping_test(args.device)
+ if test_result:
+ rc = 0
+ print("Connection test passed\n")
+ else:
+ print("Connection test failed\n")
return rc