Merge ~jocave/plainbox-provider-checkbox:wireless-nm-conn-backup into plainbox-provider-checkbox:master

Proposed by Jonathan Cave
Status: Merged
Approved by: Jonathan Cave
Approved revision: 1b78eb86ff91861c9f527762afcbdcb4bff6d98c
Merged at revision: 928732fb124a96929fbab342dd40a8c5d2ca703e
Proposed branch: ~jocave/plainbox-provider-checkbox:wireless-nm-conn-backup
Merge into: plainbox-provider-checkbox:master
Diff against target: 208 lines (+127/-0)
3 files modified
bin/wifi_nmcli_backup.py (+79/-0)
units/wireless/jobs.pxu (+28/-0)
units/wireless/test-plan.pxu (+20/-0)
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+362872@code.launchpad.net

Description of the change

Bug #1765350 requested that the wireless connection jobs restore any saved NetworkManager connection information upon completion of the test process.

The jobs removed any stored configurations to prevent any unintended re-connections to an AP that is not the one specified in the test procedure.

This MR creates new templated jobs to bracket the cert test plans that copy the NetworkManager configuration files to a predictable location prior and then restore and delete them afterwards. The files are root readable only and reload the configuration also requires root privileges.

Tested on my development machine. My stored APs were still present after running wireless-cert-automated test plan and the active network was rejoined.

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

LGTM, +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/wifi_nmcli_backup.py b/bin/wifi_nmcli_backup.py
2new file mode 100755
3index 0000000..7ec9c89
4--- /dev/null
5+++ b/bin/wifi_nmcli_backup.py
6@@ -0,0 +1,79 @@
7+#!/usr/bin/env python3
8+# Copyright 2019 Canonical Ltd.
9+# All rights reserved.
10+#
11+# Written by:
12+# Jonathan Cave <jonathan.cave@canonical.com>
13+#
14+# Save/Restore NetworkManager wifi connections
15+
16+import os
17+import shutil
18+import subprocess as sp
19+import sys
20+
21+NM_CON_DIR = '/etc/NetworkManager/system-connections'
22+SAVE_DIR = os.path.join(os.path.expandvars(
23+ '$PLAINBOX_SESSION_SHARE'), 'stored-system-connections')
24+
25+
26+def get_nm_connections():
27+ c = []
28+ cmd = 'nmcli -t -f TYPE,NAME c'
29+ output = sp.check_output(cmd, shell=True)
30+ for line in output.decode(sys.stdout.encoding).splitlines():
31+ con_type, name = line.strip().split(':')
32+ if con_type == '802-11-wireless':
33+ c.append(name)
34+ return c
35+
36+
37+def reload_nm_connections():
38+ cmd = 'nmcli c reload'
39+ sp.check_call(cmd, shell=True)
40+
41+
42+def save_connections(con_list):
43+ if len(con_list) == 0:
44+ print('No stored 802.11 connections to save')
45+ return
46+ if not os.path.exists(SAVE_DIR):
47+ os.makedirs(SAVE_DIR)
48+ for c in con_list:
49+ print('Save connection {}'.format(c))
50+ c_loc = os.path.join(NM_CON_DIR, c)
51+ if not os.path.exists(c_loc):
52+ print(' No stored connection fount at {}'.format(c_loc))
53+ continue
54+ print(' Found file {}'.format(c_loc))
55+ save_f = shutil.copy(c_loc, SAVE_DIR)
56+ print(' Saved copy at {}'.format(save_f))
57+
58+
59+def restore_connections():
60+ saved_list = [f for f in os.listdir(
61+ SAVE_DIR) if os.path.isfile(os.path.join(SAVE_DIR, f))]
62+ if len(saved_list) == 0:
63+ print('No stored 802.11 connections found')
64+ return
65+ for f in saved_list:
66+ save_f = os.path.join(SAVE_DIR, f)
67+ print('Restore connection {}'.format(save_f))
68+ restore_f = shutil.copy(save_f, NM_CON_DIR)
69+ print(' Restored file at {}'.format(restore_f))
70+ os.remove(save_f)
71+ print(' Removed copy from {}'.format(save_f))
72+
73+
74+if __name__ == '__main__':
75+ if len(sys.argv) != 2:
76+ raise SystemExit('ERROR: please specify save or restore')
77+ action = sys.argv[1]
78+
79+ if action == 'save':
80+ save_connections(get_nm_connections())
81+ elif action == 'restore':
82+ restore_connections()
83+ reload_nm_connections()
84+ else:
85+ raise SystemExit('ERROR: unrecognised action')
86diff --git a/units/wireless/jobs.pxu b/units/wireless/jobs.pxu
87index 52b2b01..9c25c71 100644
88--- a/units/wireless/jobs.pxu
89+++ b/units/wireless/jobs.pxu
90@@ -442,3 +442,31 @@ command:
91 estimated_duration: 330.0
92 _description:
93 Tests the performance of a system's wireless connection through the iperf tool, using UDP packets.
94+
95+
96+unit: template
97+template-resource: device
98+template-filter: device.category == 'WIRELESS' and device.interface != 'UNKNOWN'
99+id: wireless/nm_connection_save_{category}
100+category_id: com.canonical.plainbox::wireless
101+_summary: Save any NetworkManager 802.11 configurations prior to testing
102+plugin: shell
103+user: root
104+command:
105+ wifi_nmcli_backup.py save
106+estimated_duration: 2.0
107+flags: preserve-locale also-after-suspend also-after-suspend-manual
108+
109+unit: template
110+template-resource: device
111+template-filter: device.category == 'WIRELESS' and device.interface != 'UNKNOWN'
112+id: wireless/nm_connection_restore_{category}
113+category_id: com.canonical.plainbox::wireless
114+_summary: Restore any NetworkManager 802.11 configurations after testing
115+plugin: shell
116+user: root
117+command:
118+ wifi_nmcli_backup.py restore
119+estimated_duration: 2.0
120+depends: wireless/nm_connection_save_{category}
121+flags: preserve-locale also-after-suspend also-after-suspend-manual
122\ No newline at end of file
123diff --git a/units/wireless/test-plan.pxu b/units/wireless/test-plan.pxu
124index 74bf271..8b4aafe 100644
125--- a/units/wireless/test-plan.pxu
126+++ b/units/wireless/test-plan.pxu
127@@ -30,7 +30,10 @@ id: wireless-cert-automated
128 unit: test plan
129 _name: Wireless tests
130 _description: Wireless connection tests
131+bootstrap_include:
132+ device
133 include:
134+ wireless/nm_connection_save_.*
135 wireless/wireless_scanning_.* certification-status=blocker
136 wireless/wireless_connection_wpa_bg_nm_.* certification-status=blocker
137 wireless/wireless_connection_open_bg_nm_.* certification-status=blocker
138@@ -38,36 +41,48 @@ include:
139 wireless/wireless_connection_open_n_nm_.* certification-status=blocker
140 wireless/wireless_connection_wpa_ac_nm_.* certification-status=blocker
141 wireless/wireless_connection_open_ac_nm_.* certification-status=blocker
142+ wireless/nm_connection_restore_.*
143
144 id: after-suspend-wireless-cert-automated
145 unit: test plan
146 _name: Wireless tests (after suspend, automated)
147 _description: Wireless connection tests (after suspend, automated)
148+bootstrap_include:
149+ device
150 include:
151+ after-suspend-wireless/nm_connection_save_.*
152 after-suspend-wireless/wireless_connection_wpa_bg_nm_.* certification-status=blocker
153 after-suspend-wireless/wireless_connection_open_bg_nm_.* certification-status=blocker
154 after-suspend-wireless/wireless_connection_wpa_n_nm_.* certification-status=blocker
155 after-suspend-wireless/wireless_connection_open_n_nm_.* certification-status=blocker
156 after-suspend-wireless/wireless_connection_wpa_ac_nm_.* certification-status=blocker
157 after-suspend-wireless/wireless_connection_open_ac_nm_.* certification-status=blocker
158+ after-suspend-wireless/nm_connection_restore_.*
159
160 id: after-suspend-manual-wireless-cert-automated
161 unit: test plan
162 _name: Wireless tests (after manual suspend, automated)
163 _description: Wireless connection tests (after manual suspend, automated)
164+bootstrap_include:
165+ device
166 include:
167+ after-suspend-manual-wireless/nm_connection_save_.*
168 after-suspend-manual-wireless/wireless_connection_wpa_bg_nm_.* certification-status=blocker
169 after-suspend-manual-wireless/wireless_connection_open_bg_nm_.* certification-status=blocker
170 after-suspend-manual-wireless/wireless_connection_wpa_n_nm_.* certification-status=blocker
171 after-suspend-manual-wireless/wireless_connection_open_n_nm_.* certification-status=blocker
172 after-suspend-manual-wireless/wireless_connection_wpa_ac_nm_.* certification-status=blocker
173 after-suspend-manual-wireless/wireless_connection_open_ac_nm_.* certification-status=blocker
174+ after-suspend-manual-wireless/nm_connection_restore_.*
175
176 id: wireless-cert-blockers
177 unit: test plan
178 _name: Wireless tests (certification blockers only)
179 _description: Wireless connection tests (certification blockers only)
180+bootstrap_include:
181+ device
182 include:
183+ wireless/nm_connection_save_.*
184 wireless/wireless_scanning_.* certification-status=blocker
185 wireless/wireless_connection_wpa_bg_nm_.* certification-status=blocker
186 wireless/wireless_connection_open_bg_nm_.* certification-status=blocker
187@@ -75,16 +90,21 @@ include:
188 wireless/wireless_connection_open_n_nm_.* certification-status=blocker
189 wireless/wireless_connection_wpa_ac_nm_.* certification-status=blocker
190 wireless/wireless_connection_open_ac_nm_.* certification-status=blocker
191+ wireless/nm_connection_restore_.*
192
193 id: after-suspend-wireless-cert-blockers
194 unit: test plan
195 _name: Wireless tests (after manual suspend, certification blockers only)
196 _description:
197 Wireless connection tests (after manual suspend, certification blockers only)
198+bootstrap_include:
199+ device
200 include:
201+ after-suspend-manual-wireless/nm_connection_save_.*
202 after-suspend-manual-wireless/wireless_connection_wpa_bg_nm_.* certification-status=blocker
203 after-suspend-manual-wireless/wireless_connection_open_bg_nm_.* certification-status=blocker
204 after-suspend-manual-wireless/wireless_connection_wpa_n_nm_.* certification-status=blocker
205 after-suspend-manual-wireless/wireless_connection_open_n_nm_.* certification-status=blocker
206 after-suspend-manual-wireless/wireless_connection_wpa_ac_nm_.* certification-status=blocker
207 after-suspend-manual-wireless/wireless_connection_open_ac_nm_.* certification-status=blocker
208+ after-suspend-manual-wireless/nm_connection_restore_.*

Subscribers

People subscribed via source and target branches