diff options
author | Adrian Ratiu <adrian.ratiu@collabora.com> | 2021-07-28 12:28:06 +0300 |
---|---|---|
committer | Enric Balletbo i Serra <enric.balletbo@collabora.com> | 2021-07-29 12:51:32 +0200 |
commit | 86181a7fbd379fc42314c450740d2cea8cdf04c1 (patch) | |
tree | 2c5c5333fc6bae64c50a1c76e06f41a65da42f83 | |
parent | e4c91962f6e19466c1e43629a2c6cd04ff012e06 (diff) | |
download | cros-ec-tests-86181a7fbd379fc42314c450740d2cea8cdf04c1.tar.gz |
os.listdir() can throw exceptions like for eg FileNotFoundError
if the path passed as argument does not exist, so we need to
handle these exceptions otherwise RESULT=unknown is returned.
Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
-rwxr-xr-x[-rw-r--r--] | cros/helpers/sysfs.py | 29 | ||||
-rwxr-xr-x[-rw-r--r--] | cros/tests/cros_ec_accel.py | 41 | ||||
-rwxr-xr-x[-rw-r--r--] | cros/tests/cros_ec_extcon.py | 45 | ||||
-rwxr-xr-x[-rw-r--r--] | cros/tests/cros_ec_rtc.py | 39 |
4 files changed, 83 insertions, 71 deletions
diff --git a/cros/helpers/sysfs.py b/cros/helpers/sysfs.py index 8e52491..f3abfab 100644..100755 --- a/cros/helpers/sysfs.py +++ b/cros/helpers/sysfs.py @@ -18,18 +18,21 @@ def sysfs_check_attributes_exists(s, path, name, files, check_devtype): before checking a device path. """ match = 0 - for devname in os.listdir(path): - if check_devtype: - fd = open(path + "/" + devname + "/name", "r") - devtype = fd.read() - fd.close() - if not devtype.startswith(name): - continue - else: - if not devname.startswith(name): - continue - match += 1 - for filename in files: - s.assertEqual(os.path.exists(path + "/" + devname + "/" + filename), 1) + try: + for devname in os.listdir(path): + if check_devtype: + fd = open(path + "/" + devname + "/name", "r") + devtype = fd.read() + fd.close() + if not devtype.startswith(name): + continue + else: + if not devname.startswith(name): + continue + match += 1 + for filename in files: + s.assertEqual(os.path.exists(path + "/" + devname + "/" + filename), 1) + except IOError as e: + self.skipTest("Exception occured: {0}, skipping".format(e.strerror)) if match == 0: s.skipTest("No " + name + " found, skipping") diff --git a/cros/tests/cros_ec_accel.py b/cros/tests/cros_ec_accel.py index ec063d1..8861eb0 100644..100755 --- a/cros/tests/cros_ec_accel.py +++ b/cros/tests/cros_ec_accel.py @@ -54,24 +54,27 @@ class TestCrosECAccel(unittest.TestCase): ACCEL_1G_IN_MS2 = 9.8185 ACCEL_MAG_VALID_OFFSET = 0.25 match = 0 - for devname in os.listdir("/sys/bus/iio/devices"): - base_path = "/sys/bus/iio/devices/" + devname + "/" - fd = open(base_path + "name", "r") - devtype = fd.read() - if devtype.startswith("cros-ec-accel"): - location = read_file(base_path + "location") - accel_scale = float(read_file(base_path + "scale")) - exp = ACCEL_1G_IN_MS2 - err = exp * ACCEL_MAG_VALID_OFFSET - mag = 0 - for axis in ["x", "y", "z"]: - axis_path = base_path + "in_accel_" + axis + "_raw" - value = int(read_file(axis_path)) - value *= accel_scale - mag += value * value - mag = math.sqrt(mag) - self.assertTrue(abs(mag - exp) <= err) - match += 1 - fd.close() + try: + for devname in os.listdir("/sys/bus/iio/devices"): + base_path = "/sys/bus/iio/devices/" + devname + "/" + fd = open(base_path + "name", "r") + devtype = fd.read() + if devtype.startswith("cros-ec-accel"): + location = read_file(base_path + "location") + accel_scale = float(read_file(base_path + "scale")) + exp = ACCEL_1G_IN_MS2 + err = exp * ACCEL_MAG_VALID_OFFSET + mag = 0 + for axis in ["x", "y", "z"]: + axis_path = base_path + "in_accel_" + axis + "_raw" + value = int(read_file(axis_path)) + value *= accel_scale + mag += value * value + mag = math.sqrt(mag) + self.assertTrue(abs(mag - exp) <= err) + match += 1 + fd.close() + except IOError as e: + self.skipTest("Exception occured: {0}, skipping".format(e.strerror)) if match == 0: self.skipTest("No accelerometer found, skipping") diff --git a/cros/tests/cros_ec_extcon.py b/cros/tests/cros_ec_extcon.py index ec934b2..b2864b5 100644..100755 --- a/cros/tests/cros_ec_extcon.py +++ b/cros/tests/cros_ec_extcon.py @@ -9,26 +9,29 @@ class TestCrosECextcon(unittest.TestCase): def test_cros_ec_extcon_usbc_abi(self): """ Checks the cros-ec extcon ABI. """ match = 0 - for devname in os.listdir("/sys/class/extcon"): - devtype = read_file("/sys/class/extcon/" + devname + "/name") - if ".spi:ec@0:extcon@" in devtype: - self.assertEqual( - os.path.exists("/sys/class/extcon/" + devname + "/state"), 1 - ) - for cable in os.listdir("/sys/class/extcon/" + devname): - if cable.startswith("cable"): - self.assertEqual( - os.path.exists( - "/sys/class/extcon/" + devname + "/" + cable + "/name" - ), - 1, - ) - self.assertEqual( - os.path.exists( - "/sys/class/extcon/" + devname + "/" + cable + "/state" - ), - 1, - ) - match += 1 + try: + for devname in os.listdir("/sys/class/extcon"): + devtype = read_file("/sys/class/extcon/" + devname + "/name") + if ".spi:ec@0:extcon@" in devtype: + self.assertEqual( + os.path.exists("/sys/class/extcon/" + devname + "/state"), 1 + ) + for cable in os.listdir("/sys/class/extcon/" + devname): + if cable.startswith("cable"): + self.assertEqual( + os.path.exists( + "/sys/class/extcon/" + devname + "/" + cable + "/name" + ), + 1, + ) + self.assertEqual( + os.path.exists( + "/sys/class/extcon/" + devname + "/" + cable + "/state" + ), + 1, + ) + match += 1 + except IOError as e: + self.skipTest("Exception occured: {0}, skipping".format(e.strerror)) if match == 0: self.skipTest("No extcon device found, skipping") diff --git a/cros/tests/cros_ec_rtc.py b/cros/tests/cros_ec_rtc.py index f8ac511..9f497d2 100644..100755 --- a/cros/tests/cros_ec_rtc.py +++ b/cros/tests/cros_ec_rtc.py @@ -12,22 +12,25 @@ class TestCrosECRTC(unittest.TestCase): if not is_feature_supported(EC_FEATURE_RTC): self.skipTest("EC_FEATURE_RTC not supported, skipping") match = 0 - for devname in os.listdir("/sys/class/rtc"): - fd = open("/sys/class/rtc/" + devname + "/name", "r") - devtype = fd.read() - fd.close() - if devtype.startswith("cros-ec-rtc"): - files = [ - "date", - "hctosys", - "max_user_freq", - "since_epoch", - "time", - "wakealarm", - ] - match += 1 - for filename in files: - self.assertEqual( - os.path.exists("/sys/class/rtc/" + devname + "/" + filename), 1 - ) + try: + for devname in os.listdir("/sys/class/rtc"): + fd = open("/sys/class/rtc/" + devname + "/name", "r") + devtype = fd.read() + fd.close() + if devtype.startswith("cros-ec-rtc"): + files = [ + "date", + "hctosys", + "max_user_freq", + "since_epoch", + "time", + "wakealarm", + ] + match += 1 + for filename in files: + self.assertEqual( + os.path.exists("/sys/class/rtc/" + devname + "/" + filename), 1 + ) + except IOError as e: + self.skipTest("Exception occured: {0}, skipping".format(e.strerror)) self.assertNotEqual(match, 0) |