tests: Convert "hwsim-SKIP" exception to use a custom class
authorJouni Malinen <j@w1.fi>
Wed, 7 Jan 2015 11:41:31 +0000 (13:41 +0200)
committerJouni Malinen <j@w1.fi>
Wed, 7 Jan 2015 14:31:22 +0000 (16:31 +0200)
This makes the design a bit cleaner for catching the exceptions.

Signed-off-by: Jouni Malinen <j@w1.fi>
tests/hwsim/run-tests.py
tests/hwsim/test_dbus.py
tests/hwsim/test_dbus_old.py
tests/hwsim/test_scan.py
tests/hwsim/utils.py

index c7d3db2..7a6d60e 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python2
 #
 # Test case executor
-# Copyright (c) 2013-2014, Jouni Malinen <j@w1.fi>
+# Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
 #
 # This software may be distributed under the terms of the BSD license.
 # See README for more details.
@@ -27,6 +27,7 @@ from wpasupplicant import WpaSupplicant
 from hostapd import HostapdGlobal
 from check_kernel import check_kernel
 from wlantest import Wlantest
+from utils import HwsimSkip
 
 def set_term_echo(fd, enabled):
     [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] = termios.tcgetattr(fd)
@@ -441,14 +442,14 @@ def main():
                     result = "SKIP"
                 else:
                     result = "PASS"
+            except HwsimSkip, e:
+                logger.info("Skip test case: %s" % e)
+                result = "SKIP"
             except Exception, e:
-                if str(e) == "hwsim-SKIP":
-                    result = "SKIP"
-                else:
-                    logger.info(e)
-                    if args.loglevel == logging.WARNING:
-                        print "Exception: " + str(e)
-                    result = "FAIL"
+                logger.info(e)
+                if args.loglevel == logging.WARNING:
+                    print "Exception: " + str(e)
+                result = "FAIL"
             for d in dev:
                 try:
                     d.dump_monitor()
index 4d69639..f503860 100644 (file)
@@ -19,6 +19,7 @@ except ImportError:
 
 import hostapd
 from wpasupplicant import WpaSupplicant
+from utils import HwsimSkip
 from test_ap_tdls import connect_2sta_open
 
 WPAS_DBUS_SERVICE = "fi.w1.wpa_supplicant1"
@@ -35,7 +36,7 @@ WPAS_DBUS_PERSISTENT_GROUP = "fi.w1.wpa_supplicant1.PersistentGroup"
 def prepare_dbus(dev):
     if not dbus_imported:
         logger.info("No dbus module available")
-        raise Exception("hwsim-SKIP")
+        raise HwsimSkip("No dbus module available")
     try:
         from dbus.mainloop.glib import DBusGMainLoop
         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
@@ -46,8 +47,7 @@ def prepare_dbus(dev):
         if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
         return (bus,wpas_obj,path,if_obj)
     except Exception, e:
-        logger.info("No D-Bus support available: " + str(e))
-        raise Exception("hwsim-SKIP")
+        raise HwsimSkip("Could not connect to D-Bus: %s" % e)
 
 class TestDbus(object):
     def __init__(self, bus):
index 7f29c5a..dd72432 100644 (file)
@@ -15,6 +15,7 @@ except ImportError:
     dbus_imported = False
 
 import hostapd
+from utils import HwsimSkip
 from test_dbus import TestDbus, start_ap
 
 WPAS_DBUS_OLD_SERVICE = "fi.epitest.hostap.WPASupplicant"
@@ -25,8 +26,7 @@ WPAS_DBUS_OLD_NETWORK = "fi.epitest.hostap.WPASupplicant.Network"
 
 def prepare_dbus(dev):
     if not dbus_imported:
-        logger.info("No dbus module available")
-        raise Exception("hwsim-SKIP")
+        raise HwsimSkip("No dbus module available")
     try:
         from dbus.mainloop.glib import DBusGMainLoop
         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
@@ -37,8 +37,7 @@ def prepare_dbus(dev):
         if_obj = bus.get_object(WPAS_DBUS_OLD_SERVICE, path)
         return (bus,wpas_obj,path,if_obj)
     except Exception, e:
-        logger.info("No D-Bus support available: " + str(e))
-        raise Exception("hwsim-SKIP")
+        raise HwsimSkip("Could not connect to D-Bus: %s" % e)
 
 class TestDbusOldWps(TestDbus):
     def __init__(self, bus):
index a9869ae..8da3263 100644 (file)
@@ -12,6 +12,7 @@ import subprocess
 
 import hostapd
 from wpasupplicant import WpaSupplicant
+from utils import HwsimSkip
 
 def check_scan(dev, params, other_started=False, test_busy=False):
     if not other_started:
@@ -698,8 +699,7 @@ def _test_scan_random_mac(dev, apdev, params):
             raise Exception("Invalid MAC_RAND_SCAN accepted: " + args)
 
     if dev[0].get_driver_status_field('capa.mac_addr_rand_scan_supported') != '1':
-        logger.info("Driver does not support random MAC address for scanning")
-        raise Exception("hwsim-SKIP")
+        raise HwsimSkip("Driver does not support random MAC address for scanning")
 
     tests = [ "all enable=1",
               "all enable=1 addr=f2:11:22:33:44:55 mask=ff:ff:ff:ff:ff:ff",
index 66589bb..a763a25 100644 (file)
@@ -1,5 +1,5 @@
 # Testing utilities
-# Copyright (c) 2013, Jouni Malinen <j@w1.fi>
+# Copyright (c) 2013-2015, Jouni Malinen <j@w1.fi>
 #
 # This software may be distributed under the terms of the BSD license.
 # See README for more details.
@@ -13,3 +13,9 @@ def get_ifnames():
             if len(val) == 2:
                 ifnames.append(val[0].strip(' '))
     return ifnames
+
+class HwsimSkip(Exception):
+    def __init__(self, reason):
+        self.reason = reason
+    def __str__(self):
+        return self.reason