From a46707a59516640ca321d48d369c496d23cbab5e Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Thu, 16 Jun 2022 02:02:25 +0200 Subject: [PATCH] auto-t: Ensure storage_dir exists, clean up In iwd.py make sure all the static methods that touch IWD storage take the storage_dir parameter instead of hardcoding IWD_STORAGE_DIR, and make sure that parameter is actually used. Create the directory if it doesn't exist before copying files into it. This fixes a problem in testNetconfig where `IWD.copy_to_storage('ssidTKIP.psk', '/tmp/storage')` would result in /tmp/storage being created as a file, rather than a directory containing a file, and resulting in IWD failing to start with: `Failed to create /tmp/storage` runner.py creates /tmp/iwd but that doesn't account for IWD sessions with a custom storage dir path. --- autotests/testNetconfig/static_test.py | 2 +- autotests/util/iwd.py | 39 ++++++++++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/autotests/testNetconfig/static_test.py b/autotests/testNetconfig/static_test.py index e82dbaeb..8d5710c1 100644 --- a/autotests/testNetconfig/static_test.py +++ b/autotests/testNetconfig/static_test.py @@ -91,7 +91,7 @@ class Test(unittest.TestCase): @classmethod def tearDownClass(cls): - IWD.clear_storage() + IWD.clear_storage(storage_dir='/tmp/storage') cls.dhcpd_pid.kill() if __name__ == '__main__': diff --git a/autotests/util/iwd.py b/autotests/util/iwd.py index 6cee1e07..f609e37e 100755 --- a/autotests/util/iwd.py +++ b/autotests/util/iwd.py @@ -1086,7 +1086,7 @@ class IWD(AsyncOpAbstract): psk_agent = None def __init__(self, start_iwd_daemon = False, iwd_config_dir = '/tmp', - iwd_storage_dir = '/tmp/iwd', namespace=ctx): + iwd_storage_dir = IWD_STORAGE_DIR, namespace=ctx): self.namespace = namespace self._bus = namespace.get_bus() @@ -1197,40 +1197,49 @@ class IWD(AsyncOpAbstract): os.system('rm -rf ' + storage_dir + '/ap/*') @staticmethod - def create_in_storage(file_name, file_content): - fo = open(IWD_STORAGE_DIR + '/' + file_name, 'w') + def create_in_storage(file_name, file_content, storage_dir=IWD_STORAGE_DIR): + fo = open(storage_dir + '/' + file_name, 'w') fo.write(file_content) fo.close() + @staticmethod + def _ensure_storage_dir_exists(storage_dir): + if not os.path.exists(storage_dir): + os.mkdir(storage_dir) + @staticmethod def copy_to_storage(source, storage_dir=IWD_STORAGE_DIR, name=None): import shutil assert not os.path.isabs(source) + target = storage_dir if name: - storage_dir += '/%s' % name + target += '/%s' % name - shutil.copy(source, storage_dir) + IWD._ensure_storage_dir_exists(storage_dir) + shutil.copy(source, target) @staticmethod - def copy_to_hotspot(source): - if not os.path.exists(IWD_STORAGE_DIR + "/hotspot"): - os.mkdir(IWD_STORAGE_DIR + "/hotspot") + def copy_to_hotspot(source, storage_dir=IWD_STORAGE_DIR): + IWD._ensure_storage_dir_exists(storage_dir) - IWD.copy_to_storage(source, IWD_STORAGE_DIR + "/hotspot") + if not os.path.exists(storage_dir + "/hotspot"): + os.mkdir(storage_dir + "/hotspot") + + IWD.copy_to_storage(source, storage_dir + "/hotspot") @staticmethod - def copy_to_ap(source): - if not os.path.exists(IWD_STORAGE_DIR + "/ap"): - os.mkdir(IWD_STORAGE_DIR + "/ap") + def copy_to_ap(source, storage_dir=IWD_STORAGE_DIR): + if not os.path.exists(storage_dir + "/ap"): + os.mkdir(storage_dir + "/ap") - IWD.copy_to_storage(source, IWD_STORAGE_DIR + '/ap/') + IWD.copy_to_storage(source, storage_dir + '/ap/') @staticmethod - def remove_from_storage(file_name): - os.system('rm -rf ' + IWD_STORAGE_DIR + '/\'' + file_name + '\'') + def remove_from_storage(file_name, storage_dir=IWD_STORAGE_DIR): + os.system('rm -rf ' + storage_dir + '/\'' + file_name + '\'') def list_devices(self, wait_to_appear = 0, max_wait = 50, p2p = False): if not wait_to_appear: