about summary refs log tree commit diff
path: root/debian
diff options
context:
space:
mode:
authorRobie Basak <robie.basak@canonical.com>2018-11-27 18:10:33 +0000
committerRobie Basak <robie.basak@canonical.com>2018-11-29 12:39:13 +0000
commitef394e06cf4b7f2e1ab3132fc4a2e21ada7f52cd (patch)
tree14b3679dfb386f204bc9d7c8f1ee8e44dc5cb0b8 /debian
parent011044ef70eeacfee438a979f1f41870ff17a74c (diff)
Make dep8 nm test directly invokable
By renaming the test to nm.py, it can be called directly with:

    python3 -m unittest nm

To make this work, we also need to move required test setup and teardown
code to setUpModule() and tearDownModule() accordingly, and move other
"script" code into the __name__ == '__main__' idiom.

This then allows easier debugging of failing tests by calling unittest
to run a specific test using the usual method. For example:

    sudo python3 -m unittest nm.ColdplugWifi.test_open_b_ip6_raonly_no_pe
Diffstat (limited to 'debian')
-rw-r--r--debian/tests/control2
-rwxr-xr-xdebian/tests/nm.py (renamed from debian/tests/nm)58
2 files changed, 32 insertions, 28 deletions
diff --git a/debian/tests/control b/debian/tests/control
index 14ebb2f5..a8f4d148 100644
--- a/debian/tests/control
+++ b/debian/tests/control
@@ -2,7 +2,7 @@ Tests: wpa-dhclient
 Depends: python3, hostapd, dnsmasq-base, wpasupplicant, isc-dhcp-client, iw
 Restrictions: needs-root allow-stderr isolation-machine
 
-Tests: nm
+Tests: nm.py
 Depends: python3, dnsmasq-base, isc-dhcp-client, gir1.2-networkmanager-1.0, network-manager, hostapd, iw, python3-dbusmock
 Restrictions: needs-root isolation-machine
 
diff --git a/debian/tests/nm b/debian/tests/nm.py
index a40be9e4..64350e08 100755
--- a/debian/tests/nm
+++ b/debian/tests/nm.py
@@ -821,32 +821,36 @@ class Suspend(NetworkManagerTest, DBusTestCase):
         self.assert_iface_up(self.dev_w_client, expected_ip_a)
 
 
-# avoid unintelligible error messages, and breaking "make check" when not being
-# root
-if os.getuid() != 0:
-    sys.stderr.write('This integration test suite needs to be run as root\n')
-    sys.exit(1)
-
-# AppArmor currently does not allow us to access the system D-BUS from an
-# unshared file system. Hack the policy to allow that until that gets fixed
-# properly. See https://launchpad.net/bugs/1244157
-subprocess.check_call("sed '/nm-dhcp-client.action {/ s/{/flags=(attach_disconnected) {/'"
-                      " /etc/apparmor.d/sbin.dhclient > $ADTTMP/sbin.dhclient",
-                      shell=True)
-subprocess.check_call('apparmor_parser -Kr $ADTTMP/sbin.dhclient', shell=True)
-
-# unshare the mount namespace, so that our tmpfs mounts are guaranteed to get
-# cleaned up, and don't influence the production system
-libc6 = ctypes.cdll.LoadLibrary('libc.so.6')
-assert libc6.unshare(ctypes.c_int(0x00020000)) == 0, 'failed to unshare mount namespace'
-
-# stop system-wide NetworkManager to avoid interfering with tests
-nm_running = subprocess.call('service network-manager stop 2>&1', shell=True) == 0
-
-# write to stdout, not stderr
-runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
-try:
-    unittest.main(testRunner=runner)
-finally:
+def setUpModule():
+    # AppArmor currently does not allow us to access the system D-BUS from an
+    # unshared file system. Hack the policy to allow that until that gets fixed
+    # properly. See https://launchpad.net/bugs/1244157
+    subprocess.check_call("sed '/nm-dhcp-client.action {/ s/{/flags=(attach_disconnected) {/'"
+                          " /etc/apparmor.d/sbin.dhclient > $ADTTMP/sbin.dhclient",
+                          shell=True)
+    subprocess.check_call('apparmor_parser -Kr $ADTTMP/sbin.dhclient', shell=True)
+
+    # unshare the mount namespace, so that our tmpfs mounts are guaranteed to get
+    # cleaned up, and don't influence the production system
+    libc6 = ctypes.cdll.LoadLibrary('libc.so.6')
+    assert libc6.unshare(ctypes.c_int(0x00020000)) == 0, 'failed to unshare mount namespace'
+
+    # stop system-wide NetworkManager to avoid interfering with tests
+    nm_running = subprocess.call('service network-manager stop 2>&1', shell=True) == 0
+
+
+def tearDownModule():
     subprocess.call('dhclient eth0', shell=True)
     subprocess.call('sleep 10', shell=True)
+
+
+if __name__ == '__main__':
+    # avoid unintelligible error messages, and breaking "make check" when not being
+    # root
+    if os.getuid() != 0:
+        sys.stderr.write('This integration test suite needs to be run as root\n')
+        sys.exit(1)
+
+    # write to stdout, not stderr
+    runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
+    unittest.main(testRunner=runner)