From deaef79f3cbb7acb879e121b7f3d081e712bf665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Doktor?= Date: Tue, 20 Sep 2016 13:46:06 +0200 Subject: [PATCH] avocado.utils.iso9660: Define "mnt_dir" API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Iso9660Mount defines "mnt_dir" variable, which is frequently used in tests to make the content of the image available. This is not supported by other providers, which adds complexity to the code around mnt_dir. This patch makes "mnt_dir" property part of the API and implements it in all current providers. Note that the "mnt_dir" feature is currently uses the "Iso9660Mount" features, therefor the user must be root and the mount has to be available, otherwise one gets exception when he tries to use "mnt_dir". Signed-off-by: Lukáš Doktor --- avocado/utils/iso9660.py | 65 +++++++++++-- selftests/.data/sample.iso | Bin 0 -> 378880 bytes selftests/unit/test_utils_iso9660.py | 133 +++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 selftests/.data/sample.iso create mode 100644 selftests/unit/test_utils_iso9660.py diff --git a/avocado/utils/iso9660.py b/avocado/utils/iso9660.py index 6c183684..55cca289 100644 --- a/avocado/utils/iso9660.py +++ b/avocado/utils/iso9660.py @@ -151,6 +151,12 @@ class BaseIso9660(object): output.write(content) output.close() + def mnt_dir(self): + """ + Returns a path to the browsable content of the iso + """ + raise NotImplementedError + def close(self): """ Cleanup and free any resources being used @@ -160,7 +166,38 @@ class BaseIso9660(object): pass -class Iso9660IsoInfo(BaseIso9660): +class MixInMntDirMount(object): + """ + Mix in class which defines `mnt_dir` property and instantiates the + Iso9660Mount class to provide one. It requires `self.path` to store + path to the target iso file. + """ + _mount_instance = None + path = None + + @property + def mnt_dir(self): + """ + Returns a path to the browsable content of the iso + """ + if self._mount_instance is None: + if not self.path: + raise RuntimeError("Path to iso image not available: %s" + % self.path) + self._mount_instance = Iso9660Mount(self.path) + return self._mount_instance.mnt_dir + + def close(self): + """ + Cleanups and frees any resources being used + """ + super(MixInMntDirMount, self).close() + if self._mount_instance: + self._mount_instance.close() + self._mount_instance = None + + +class Iso9660IsoInfo(MixInMntDirMount, BaseIso9660): """ Represents a ISO9660 filesystem @@ -212,7 +249,8 @@ class Iso9660IsoInfo(BaseIso9660): else: fname = self._get_filename_in_iso(path) if not fname: - logging.warn("Could not find '%s' in iso '%s'", path, self.path) + logging.warn( + "Could not find '%s' in iso '%s'", path, self.path) return "" cmd.append("-x %s" % fname) @@ -220,7 +258,7 @@ class Iso9660IsoInfo(BaseIso9660): return result.stdout -class Iso9660IsoRead(BaseIso9660): +class Iso9660IsoRead(MixInMntDirMount, BaseIso9660): """ Represents a ISO9660 filesystem @@ -243,6 +281,7 @@ class Iso9660IsoRead(BaseIso9660): process.run(cmd) def close(self): + super(Iso9660IsoRead, self).close() shutil.rmtree(self.temp_dir, True) @@ -260,7 +299,7 @@ class Iso9660Mount(BaseIso9660): :type path: str """ super(Iso9660Mount, self).__init__(path) - self.mnt_dir = tempfile.mkdtemp(prefix='avocado_' + __name__) + self._mnt_dir = tempfile.mkdtemp(prefix='avocado_' + __name__) process.run('mount -t iso9660 -v -o loop,ro %s %s' % (path, self.mnt_dir)) @@ -293,11 +332,19 @@ class Iso9660Mount(BaseIso9660): :rtype: None """ - if os.path.ismount(self.mnt_dir): - process.run('fuser -k %s' % self.mnt_dir, ignore_status=True) - process.run('umount %s' % self.mnt_dir) - - shutil.rmtree(self.mnt_dir) + if self._mnt_dir: + if os.path.ismount(self._mnt_dir): + process.run('fuser -k %s' % self.mnt_dir, ignore_status=True) + process.run('umount %s' % self.mnt_dir) + shutil.rmtree(self._mnt_dir) + self._mnt_dir = None + + @property + def mnt_dir(self): + if not self._mnt_dir: + raise RuntimeError("Trying to get mnt_dir of already closed iso %s" + % self.path) + return self._mnt_dir def iso9660(path): diff --git a/selftests/.data/sample.iso b/selftests/.data/sample.iso new file mode 100644 index 0000000000000000000000000000000000000000..61f32da0b26ccd951b9004e6d09dee7ccaba4aef GIT binary patch literal 378880 zcmeI*U5^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