From fc2446dfeaeb4084b2e72edaef0e08a259b58ed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20Sch=C3=A4fer?= Date: Wed, 21 Jul 2021 12:41:12 +0200 Subject: [PATCH] Moving temp data handling to its own namespace Moving use of mkdtemp, NamedTemporaryFile and TemporaryDirectory into its own class called Temporary: By default all temporary data is created below /var/tmp but can be changed via the global commandline option --temp-dir. This Fixes #1870 --- doc/source/commands/kiwi.rst | 12 +- kiwi/boot/image/builtin_kiwi.py | 12 +- kiwi/builder/disk.py | 6 +- kiwi/builder/install.py | 73 ++++----- kiwi/builder/live.py | 53 +++---- kiwi/cli.py | 10 ++ kiwi/container/appx.py | 4 +- kiwi/defaults.py | 27 ++++ kiwi/filesystem/clicfs.py | 16 +- kiwi/iso_tools/cdrtools.py | 4 +- kiwi/markup/any.py | 4 +- kiwi/markup/base.py | 4 +- kiwi/mount_manager.py | 14 +- kiwi/oci_tools/umoci.py | 16 +- kiwi/partitioner/dasd.py | 4 +- kiwi/partitioner/msdos.py | 4 +- kiwi/repository/apt.py | 6 +- kiwi/repository/dnf.py | 6 +- kiwi/repository/pacman.py | 6 +- kiwi/repository/zypper.py | 10 +- kiwi/solver/repository/base.py | 50 ++---- kiwi/storage/disk.py | 4 +- kiwi/storage/luks_device.py | 4 +- kiwi/storage/subformat/gce.py | 12 +- kiwi/storage/subformat/qcow2.py | 4 +- kiwi/storage/subformat/vagrant_base.py | 12 +- kiwi/system/profile.py | 4 +- kiwi/system/root_init.py | 16 +- kiwi/system/setup.py | 6 +- kiwi/system/shell.py | 4 +- kiwi/utils/compress.py | 4 +- kiwi/utils/output.py | 4 +- kiwi/utils/temporary.py | 49 ++++++ kiwi/volume_manager/base.py | 11 +- kiwi/volume_manager/btrfs.py | 1 - kiwi/volume_manager/lvm.py | 1 - kiwi/xml_description.py | 4 +- test/unit/boot/image/builtin_kiwi_test.py | 36 ++--- test/unit/builder/disk_test.py | 2 +- test/unit/builder/install_test.py | 63 ++++---- test/unit/builder/live_test.py | 142 +++++++++--------- test/unit/cli_test.py | 1 + test/unit/container/appx_test.py | 6 +- test/unit/filesystem/clicfs_test.py | 15 +- test/unit/iso_tools/cdrtools_test.py | 2 +- test/unit/iso_tools/iso_test.py | 6 +- test/unit/mount_manager_test.py | 21 +-- test/unit/oci_tools/umoci_test.py | 37 ++--- test/unit/partitioner/dasd_test.py | 6 +- test/unit/partitioner/msdos_test.py | 6 +- test/unit/repository/apt_test.py | 5 +- test/unit/repository/dnf_test.py | 6 +- test/unit/repository/pacman_test.py | 2 +- test/unit/repository/zypper_test.py | 6 +- test/unit/solver/repository/base_test.py | 50 +++--- test/unit/storage/disk_test.py | 2 +- test/unit/storage/luks_device_test.py | 2 +- test/unit/storage/subformat/gce_test.py | 6 +- test/unit/storage/subformat/qcow2_test.py | 6 +- .../storage/subformat/vagrant_base_test.py | 12 +- .../storage/subformat/vagrant_libvirt_test.py | 6 +- .../subformat/vagrant_virtualbox_test.py | 6 +- test/unit/system/profile_test.py | 18 +-- test/unit/system/root_init_test.py | 19 +-- test/unit/system/setup_test.py | 2 +- test/unit/tasks/image_info_test.py | 4 +- test/unit/utils/compress_test.py | 4 +- test/unit/utils/output_test.py | 2 +- test/unit/utils/temporary_test.py | 22 +++ test/unit/volume_manager/base_test.py | 12 +- test/unit/volume_manager/btrfs_test.py | 23 +-- test/unit/volume_manager/lvm_test.py | 12 +- test/unit/xml_description_test.py | 10 +- 73 files changed, 524 insertions(+), 537 deletions(-) create mode 100644 kiwi/utils/temporary.py create mode 100644 test/unit/utils/temporary_test.py diff --git a/doc/source/commands/kiwi.rst b/doc/source/commands/kiwi.rst index c349c75b..1965f2b8 100644 --- a/doc/source/commands/kiwi.rst +++ b/doc/source/commands/kiwi.rst @@ -12,6 +12,7 @@ SYNOPSIS kiwi-ng -h | --help kiwi-ng [--profile=...] + [--temp-dir=] [--type=] [--logfile=] [--debug] @@ -24,6 +25,7 @@ SYNOPSIS result [...] kiwi-ng [--profile=...] [--shared-cache-dir=] + [--temp-dir=] [--target-arch=] [--type=] [--logfile=] @@ -105,7 +107,7 @@ GLOBAL OPTIONS Select profile to use. The specified profile must be part of the XML description. The option can be specified multiple times to - allow using a combination of profiles + allow using a combination of profiles. --shared-cache-dir= @@ -113,7 +115,13 @@ GLOBAL OPTIONS is shared via bind mount between the build host and image root system and contains information about package repositories and their cache and meta data. The default location is set - to /var/cache/kiwi + to `/var/cache/kiwi`. + +--temp-dir= + + Specify an alternative base temporary directory. The + provided path is used as base directory to store temporary + files and directories. By default `/var/tmp` is used. --target-arch= diff --git a/kiwi/boot/image/builtin_kiwi.py b/kiwi/boot/image/builtin_kiwi.py index 2654730a..6167ad54 100644 --- a/kiwi/boot/image/builtin_kiwi.py +++ b/kiwi/boot/image/builtin_kiwi.py @@ -17,8 +17,7 @@ # import os import logging -from tempfile import TemporaryDirectory -from tempfile import mkdtemp +from kiwi.utils.temporary import Temporary from typing import ( Optional, List ) @@ -74,9 +73,10 @@ class BootImageKiwi(BootImageBase): Prepare new root system suitable to create a kiwi initrd from it """ if self.boot_xml_state: - self.boot_root_directory = mkdtemp( + self.boot_root_directory_temporary = Temporary( prefix='kiwi_boot_root.', dir=self.target_dir - ) + ).new_dir() + self.boot_root_directory = self.boot_root_directory_temporary.name self.temp_directories.append( self.boot_root_directory ) @@ -152,9 +152,9 @@ class BootImageKiwi(BootImageBase): kiwi_initrd_basename = basename else: kiwi_initrd_basename = self.initrd_base_name - temp_boot_root = TemporaryDirectory( + temp_boot_root = Temporary( prefix='kiwi_boot_root_copy.' - ) + ).new_dir() temp_boot_root_directory = temp_boot_root.name os.chmod(temp_boot_root_directory, 0o755) data = DataSync( diff --git a/kiwi/builder/disk.py b/kiwi/builder/disk.py index 6d32fcd6..61544897 100644 --- a/kiwi/builder/disk.py +++ b/kiwi/builder/disk.py @@ -17,7 +17,6 @@ # import os import logging -from tempfile import NamedTemporaryFile from typing import ( Dict, List, Optional, Tuple, Any ) @@ -25,6 +24,7 @@ from typing import ( # project import kiwi.defaults as defaults +from kiwi.utils.temporary import Temporary from kiwi.defaults import Defaults from kiwi.filesystem.base import FileSystemBase from kiwi.bootloader.config import BootLoaderConfig @@ -742,7 +742,7 @@ class DiskBuilder: if self.root_filesystem_is_overlay: log.info('--> creating readonly root partition') - squashed_root_file = NamedTemporaryFile(dir='/var/tmp', prefix='kiwi-') + squashed_root_file = Temporary().new_file() squashed_root = FileSystemSquashFs( device_provider=DeviceProvider(), root_dir=self.root_dir, custom_args={ @@ -1052,7 +1052,7 @@ class DiskBuilder: log.info('--> Syncing root filesystem data') if self.root_filesystem_is_overlay: - squashed_root_file = NamedTemporaryFile(dir='/var/tmp', prefix='kiwi-') + squashed_root_file = Temporary().new_file() squashed_root = FileSystemSquashFs( device_provider=DeviceProvider(), root_dir=self.root_dir, custom_args={ diff --git a/kiwi/builder/install.py b/kiwi/builder/install.py index 33bfdd95..cd08d7f2 100644 --- a/kiwi/builder/install.py +++ b/kiwi/builder/install.py @@ -17,13 +17,13 @@ # import os import logging -from tempfile import mkdtemp from typing import ( Dict, Optional ) import shutil # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.storage.device_provider import DeviceProvider from kiwi.boot.image.base import BootImageBase @@ -125,9 +125,6 @@ class InstallImageBuilder: self.mbrid = SystemIdentifier() self.mbrid.calculate_id() - self.media_dir: str = '' - self.pxe_dir: str = '' - self.squashed_contents: str = '' self.custom_iso_args: Dict = {} if not boot_image_task: @@ -148,11 +145,11 @@ class InstallImageBuilder: * installiso="true|false" * installstick="true|false" """ - self.media_dir = mkdtemp( + self.media_dir = Temporary( prefix='kiwi_install_media.', dir=self.target_dir - ) + ).new_dir() # unpack cdroot user files to media dir - self.setup.import_cdroot_files(self.media_dir) + self.setup.import_cdroot_files(self.media_dir.name) # custom iso metadata self.custom_iso_args = { @@ -166,11 +163,11 @@ class InstallImageBuilder: # the system image transfer is checked against a checksum log.info('Creating disk image checksum') - self.squashed_contents = mkdtemp( + self.squashed_contents = Temporary( prefix='kiwi_install_squashfs.', dir=self.target_dir - ) + ).new_dir() checksum = Checksum(self.diskname) - checksum.md5(self.squashed_contents + '/' + self.md5name) + checksum.md5(self.squashed_contents.name + '/' + self.md5name) # the system image name is stored in a config file self._write_install_image_info_to_iso_image() @@ -182,7 +179,7 @@ class InstallImageBuilder: Command.run( [ 'cp', '-l', self.diskname, - self.squashed_contents + '/' + self.squashed_diskname + self.squashed_contents.name + '/' + self.squashed_diskname ] ) squashed_image_file = ''.join( @@ -192,7 +189,7 @@ class InstallImageBuilder: ) squashed_image = FileSystemSquashFs( device_provider=DeviceProvider(), - root_dir=self.squashed_contents, + root_dir=self.squashed_contents.name, custom_args={ 'compression': self.xml_state.build_type.get_squashfscompression() @@ -200,7 +197,7 @@ class InstallImageBuilder: ) squashed_image.create_on_file(squashed_image_file) Command.run( - ['mv', squashed_image_file, self.media_dir] + ['mv', squashed_image_file, self.media_dir.name] ) log.info( @@ -213,7 +210,7 @@ class InstallImageBuilder: # based on grub bootloader_config = BootLoaderConfig.new( 'grub2', self.xml_state, root_dir=self.root_dir, - boot_dir=self.media_dir, custom_args={ + boot_dir=self.media_dir.name, custom_args={ 'grub_directory_name': Defaults.get_grub_boot_directory_name(self.root_dir) } @@ -228,10 +225,10 @@ class InstallImageBuilder: # only. bootloader_config = BootLoaderConfig.new( 'isolinux', self.xml_state, root_dir=self.root_dir, - boot_dir=self.media_dir + boot_dir=self.media_dir.name ) IsoToolsBase.setup_media_loader_directory( - self.boot_image_task.boot_root_directory, self.media_dir, + self.boot_image_task.boot_root_directory, self.media_dir.name, bootloader_config.get_boot_theme() ) bootloader_config.write_meta_data() @@ -250,7 +247,7 @@ class InstallImageBuilder: # create iso filesystem from media_dir log.info('Creating ISO filesystem') iso_image = FileSystemIsoFs( - device_provider=DeviceProvider(), root_dir=self.media_dir, + device_provider=DeviceProvider(), root_dir=self.media_dir.name, custom_args=self.custom_iso_args ) iso_image.create_on_file(self.isoname) @@ -273,14 +270,14 @@ class InstallImageBuilder: * installpxe="true|false" """ - self.pxe_dir = mkdtemp( + self.pxe_dir = Temporary( prefix='kiwi_pxe_install_media.', dir=self.target_dir - ) + ).new_dir() # the system image is transfered as xz compressed variant log.info('xz compressing disk image') pxe_image_filename = ''.join( [ - self.pxe_dir, '/', + self.pxe_dir.name, '/', self.pxename, '.xz' ] ) @@ -297,7 +294,7 @@ class InstallImageBuilder: log.info('Creating disk image checksum') pxe_md5_filename = ''.join( [ - self.pxe_dir, '/', + self.pxe_dir.name, '/', self.pxename, '.md5' ] ) @@ -315,7 +312,7 @@ class InstallImageBuilder: [self.root_dir, 'boot', boot_names.initrd_name] ) target_initrd_name = '{0}/{1}.initrd'.format( - self.pxe_dir, self.pxename + self.pxe_dir.name, self.pxename ) shutil.copy( system_image_initrd, target_initrd_name @@ -326,7 +323,7 @@ class InstallImageBuilder: # this information helps to configure the boot server correctly append_filename = ''.join( [ - self.pxe_dir, '/', self.pxename, '.append' + self.pxe_dir.name, '/', self.pxename, '.append' ] ) if self.initrd_system == 'kiwi': @@ -354,14 +351,14 @@ class InstallImageBuilder: configname = '{0}.config.bootoptions'.format(self.pxename) shutil.copy( os.sep.join([self.root_dir, 'config.bootoptions']), - os.sep.join([self.pxe_dir, configname]) + os.sep.join([self.pxe_dir.name, configname]) ) # create pxe install tarball log.info('Creating pxe install archive') archive = ArchiveTar(self.pxetarball) - archive.create(self.pxe_dir) + archive.create(self.pxe_dir.name) self.boot_image_task.cleanup() def _create_pxe_install_kernel_and_initrd(self) -> None: @@ -369,11 +366,11 @@ class InstallImageBuilder: initrdname = 'pxeboot.{0}.initrd.xz'.format(self.pxename) kernel = Kernel(self.boot_image_task.boot_root_directory) if kernel.get_kernel(): - kernel.copy_kernel(self.pxe_dir, kernelname) + kernel.copy_kernel(self.pxe_dir.name, kernelname) os.symlink( kernelname, ''.join( [ - self.pxe_dir, '/', + self.pxe_dir.name, '/', self.pxename, '.kernel' ] ) @@ -386,7 +383,8 @@ class InstallImageBuilder: if self.xml_state.is_xen_server(): if kernel.get_xen_hypervisor(): kernel.copy_xen_hypervisor( - self.pxe_dir, '/pxeboot.{0}.xen.gz'.format(self.pxename) + self.pxe_dir.name, + '/pxeboot.{0}.xen.gz'.format(self.pxename) ) else: raise KiwiInstallBootImageError( @@ -412,13 +410,13 @@ class InstallImageBuilder: Command.run( [ 'mv', self.boot_image_task.initrd_filename, - self.pxe_dir + '/{0}'.format(initrdname) + self.pxe_dir.name + '/{0}'.format(initrdname) ] ) - os.chmod(self.pxe_dir + '/{0}'.format(initrdname), 420) + os.chmod(self.pxe_dir.name + '/{0}'.format(initrdname), 420) def _create_iso_install_kernel_and_initrd(self) -> None: - boot_path = self.media_dir + '/boot/' + self.arch + '/loader' + boot_path = self.media_dir.name + '/boot/' + self.arch + '/loader' Path.create(boot_path) kernel = Kernel(self.boot_image_task.boot_root_directory) if kernel.get_kernel(): @@ -472,11 +470,11 @@ class InstallImageBuilder: [self.root_dir, 'boot', boot_names.initrd_name] ) shutil.copy( - system_image_initrd, self.media_dir + '/initrd.system_image' + system_image_initrd, self.media_dir.name + '/initrd.system_image' ) def _write_install_image_info_to_iso_image(self) -> None: - iso_trigger = self.media_dir + '/config.isoclient' + iso_trigger = self.media_dir.name + '/config.isoclient' with open(iso_trigger, 'w') as iso_system: iso_system.write('IMAGE="%s"\n' % self.squashed_diskname) @@ -485,12 +483,3 @@ class InstallImageBuilder: self.boot_image_task.boot_root_directory + '/config.vmxsystem' with open(initrd_trigger, 'w') as vmx_system: vmx_system.write('IMAGE="%s"\n' % self.squashed_diskname) - - def __del__(self) -> None: - log.info('Cleaning up %s instance', type(self).__name__) - if self.media_dir: - Path.wipe(self.media_dir) - if self.pxe_dir: - Path.wipe(self.pxe_dir) - if self.squashed_contents: - Path.wipe(self.squashed_contents) diff --git a/kiwi/builder/live.py b/kiwi/builder/live.py index 580eefb8..6b24f265 100644 --- a/kiwi/builder/live.py +++ b/kiwi/builder/live.py @@ -17,12 +17,11 @@ # import os import logging -from tempfile import mkdtemp -from tempfile import NamedTemporaryFile from typing import Dict import shutil # project +from kiwi.utils.temporary import Temporary from kiwi.bootloader.config import BootLoaderConfig from kiwi.filesystem import FileSystem from kiwi.filesystem.isofs import FileSystemIsoFs @@ -61,8 +60,6 @@ class LiveImageBuilder: self, xml_state: XMLState, target_dir: str, root_dir: str, custom_args: Dict = None ): - self.media_dir: str = '' - self.live_container_dir: str = '' self.arch = Defaults.get_platform_name() self.root_dir = root_dir self.target_dir = target_dir @@ -115,14 +112,14 @@ class LiveImageBuilder: :rtype: instance of :class:`Result` """ # media dir to store CD contents - self.media_dir = mkdtemp( + self.media_dir = Temporary( prefix='live-media.', dir=self.target_dir - ) + ).new_dir() # unpack cdroot user files to media dir - self.system_setup.import_cdroot_files(self.media_dir) + self.system_setup.import_cdroot_files(self.media_dir.name) - rootsize = SystemSize(self.media_dir) + rootsize = SystemSize(self.media_dir.name) # custom iso metadata log.info('Using following live ISO metadata:') @@ -149,7 +146,7 @@ class LiveImageBuilder: # based on grub bootloader_config = BootLoaderConfig.new( 'grub2', self.xml_state, root_dir=self.root_dir, - boot_dir=self.media_dir, custom_args={ + boot_dir=self.media_dir.name, custom_args={ 'grub_directory_name': Defaults.get_grub_boot_directory_name(self.root_dir) } @@ -163,10 +160,10 @@ class LiveImageBuilder: # only. bootloader_config = BootLoaderConfig.new( 'isolinux', self.xml_state, root_dir=self.root_dir, - boot_dir=self.media_dir + boot_dir=self.media_dir.name ) IsoToolsBase.setup_media_loader_directory( - self.boot_image.boot_root_directory, self.media_dir, + self.boot_image.boot_root_directory, self.media_dir.name, bootloader_config.get_boot_theme() ) bootloader_config.write_meta_data() @@ -177,7 +174,7 @@ class LiveImageBuilder: # call custom editbootconfig script if present self.system_setup.call_edit_boot_config_script( - filesystem='iso:{0}'.format(self.media_dir), boot_part_id=1, + filesystem='iso:{0}'.format(self.media_dir.name), boot_part_id=1, working_directory=self.root_dir ) @@ -225,7 +222,7 @@ class LiveImageBuilder: filesystem_setup = FileSystemSetup( self.xml_state, self.root_dir ) - root_image = NamedTemporaryFile(dir='/var/tmp', prefix='kiwi-') + root_image = Temporary().new_file() loop_provider = LoopDevice( root_image.name, filesystem_setup.get_size_mbytes(root_filesystem), @@ -250,35 +247,35 @@ class LiveImageBuilder: live_filesystem.umount() log.info('--> Creating squashfs container for root image') - self.live_container_dir = mkdtemp( + self.live_container_dir = Temporary( prefix='live-container.', dir=self.target_dir - ) - Path.create(self.live_container_dir + '/LiveOS') + ).new_dir() + Path.create(self.live_container_dir.name + '/LiveOS') shutil.copy( - root_image.name, self.live_container_dir + '/LiveOS/rootfs.img' + root_image.name, self.live_container_dir.name + '/LiveOS/rootfs.img' ) live_container_image = FileSystem.new( name='squashfs', device_provider=DeviceProvider(), - root_dir=self.live_container_dir, + root_dir=self.live_container_dir.name, custom_args={ 'compression': self.xml_state.build_type.get_squashfscompression() } ) - container_image = NamedTemporaryFile(dir='/var/tmp', prefix='kiwi-') + container_image = Temporary().new_file() live_container_image.create_on_file( container_image.name ) - Path.create(self.media_dir + '/LiveOS') + Path.create(self.media_dir.name + '/LiveOS') shutil.copy( - container_image.name, self.media_dir + '/LiveOS/squashfs.img' + container_image.name, self.media_dir.name + '/LiveOS/squashfs.img' ) # create iso filesystem from media_dir log.info('Creating live ISO image') iso_image = FileSystemIsoFs( - device_provider=DeviceProvider(), root_dir=self.media_dir, + device_provider=DeviceProvider(), root_dir=self.media_dir.name, custom_args=custom_iso_args ) iso_image.create_on_file(self.isoname) @@ -332,7 +329,7 @@ class LiveImageBuilder: Copy kernel and initrd from the root tree into the iso boot structure """ boot_path = ''.join( - [self.media_dir, '/boot/', self.arch, '/loader'] + [self.media_dir.name, '/boot/', self.arch, '/loader'] ) Path.create(boot_path) @@ -368,13 +365,3 @@ class LiveImageBuilder: self.boot_image.boot_root_directory ) ) - - def __del__(self) -> None: - if self.media_dir or self.live_container_dir: - log.info( - 'Cleaning up {0} instance'.format(type(self).__name__) - ) - if self.media_dir: - Path.wipe(self.media_dir) - if self.live_container_dir: - Path.wipe(self.live_container_dir) diff --git a/kiwi/cli.py b/kiwi/cli.py index 2be9c2af..93645d94 100644 --- a/kiwi/cli.py +++ b/kiwi/cli.py @@ -18,6 +18,7 @@ """ usage: kiwi-ng -h | --help kiwi-ng [--profile=...] + [--temp-dir=] [--type=] [--logfile=] [--debug] @@ -30,6 +31,7 @@ usage: kiwi-ng -h | --help result [...] kiwi-ng [--profile=...] [--shared-cache-dir=] + [--temp-dir=] [--target-arch=] [--type=] [--logfile=] @@ -69,6 +71,10 @@ global options for services: image, system is shared via bind mount between the build host and image root system and contains information about package repositories and their cache and meta data. + --temp-dir= + specify an alternative base temporary directory. The + provided path is used as base directory to store temporary + files and directories. By default /var/tmp is used. --type= image build type. If not set the default XML specified build type will be used @@ -229,6 +235,10 @@ class Cli: value = os.sep + Defaults.get_shared_cache_location() if arg == '--shared-cache-dir' and value: Defaults.set_shared_cache_location(value) + if arg == '--temp-dir' and not value: + value = Defaults.get_temp_location() + if arg == '--temp-dir' and value: + Defaults.set_temp_location(value) if arg == '--target-arch' and value: Defaults.set_platform_name(value) if arg == '--config' and value: diff --git a/kiwi/container/appx.py b/kiwi/container/appx.py index 4438c57e..e7650c55 100644 --- a/kiwi/container/appx.py +++ b/kiwi/container/appx.py @@ -17,9 +17,9 @@ # import os import logging -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.archive.tar import ArchiveTar from kiwi.defaults import Defaults from kiwi.utils.compress import Compress @@ -98,7 +98,7 @@ class ContainerImageAppx: compressor = Compress(archive_file_name) archive_file_name = compressor.gzip() - filemap_file = NamedTemporaryFile() + filemap_file = Temporary().new_file() with open(filemap_file.name, 'w') as filemap: filemap.write('[Files]{0}'.format(os.linesep)) for topdir, dirs, files in sorted(os.walk(self.meta_data_path)): diff --git a/kiwi/defaults.py b/kiwi/defaults.py index 93915ddb..3986671e 100644 --- a/kiwi/defaults.py +++ b/kiwi/defaults.py @@ -45,6 +45,7 @@ EDIT_BOOT_INSTALL_SCRIPT = 'edit_boot_install.sh' IMAGE_METADATA_DIR = 'image' ROOT_VOLUME_NAME = 'LVRoot' SHARED_CACHE_DIR = '/var/cache/kiwi' +TEMP_DIR = '/var/tmp' CUSTOM_RUNTIME_CONFIG_FILE = None PLATFORM_MACHINE = platform.machine() @@ -252,6 +253,16 @@ class Defaults: global CUSTOM_RUNTIME_CONFIG_FILE CUSTOM_RUNTIME_CONFIG_FILE = filename + @staticmethod + def set_temp_location(location): + """ + Sets the temp directory location once + + :param str location: a location path + """ + global TEMP_DIR + TEMP_DIR = location + @staticmethod def get_shared_cache_location(): """ @@ -271,6 +282,22 @@ class Defaults: SHARED_CACHE_DIR )).lstrip(os.sep) + @staticmethod + def get_temp_location(): + """ + Provides the base temp directory location + + This is the directory used to store any temporary files + and directories created by kiwi during runtime + + :return: directory path + + :rtype: str + """ + return os.path.abspath( + os.path.normpath(TEMP_DIR) + ) + @staticmethod def get_sync_options(): """ diff --git a/kiwi/filesystem/clicfs.py b/kiwi/filesystem/clicfs.py index 347351c0..0831de13 100644 --- a/kiwi/filesystem/clicfs.py +++ b/kiwi/filesystem/clicfs.py @@ -15,18 +15,17 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import mkdtemp from typing import ( - Dict, List, Optional + Dict, List ) import logging # project +from kiwi.utils.temporary import Temporary from kiwi.filesystem.base import FileSystemBase from kiwi.filesystem.ext4 import FileSystemExt4 from kiwi.command import Command from kiwi.system.size import SystemSize -from kiwi.path import Path from kiwi.storage.loop_device import LoopDevice log = logging.getLogger('kiwi') @@ -45,7 +44,7 @@ class FileSystemClicFs(FileSystemBase): :param dict custom_args: unused """ - self.container_dir: Optional[str] = None + pass def create_on_file( self, filename: str, label: str = None, exclude: List[str] = None @@ -63,8 +62,8 @@ class FileSystemClicFs(FileSystemBase): :param string label: unused :param list exclude: unused """ - self.container_dir = mkdtemp(prefix='kiwi_clicfs.') - clicfs_container_filesystem = self.container_dir + '/fsdata.ext4' + container_dir = Temporary(prefix='kiwi_clicfs.').new_dir() + clicfs_container_filesystem = container_dir.name + '/fsdata.ext4' loop_provider = LoopDevice( clicfs_container_filesystem, self._get_container_filesystem_size_mbytes() @@ -91,8 +90,3 @@ class FileSystemClicFs(FileSystemBase): size = SystemSize(self.root_dir) root_dir_mbytes = size.accumulate_mbyte_file_sizes() return size.customize(root_dir_mbytes, 'ext4') - - def __del__(self): - if self.container_dir: - log.info('Cleaning up %s instance', type(self).__name__) - Path.wipe(self.container_dir) diff --git a/kiwi/iso_tools/cdrtools.py b/kiwi/iso_tools/cdrtools.py index 6dda79f7..a03bd8e8 100644 --- a/kiwi/iso_tools/cdrtools.py +++ b/kiwi/iso_tools/cdrtools.py @@ -17,13 +17,13 @@ # import os import re -from tempfile import NamedTemporaryFile from collections import ( namedtuple, OrderedDict ) # project +from kiwi.utils.temporary import Temporary from kiwi.defaults import Defaults from kiwi.iso_tools.base import IsoToolsBase from kiwi.utils.command_capabilities import CommandCapabilities @@ -250,7 +250,7 @@ class IsoToolsCdrTools(IsoToolsBase): :rtype: str """ - self.iso_sortfile = NamedTemporaryFile() + self.iso_sortfile = Temporary().new_file() catalog_file = \ self.source_dir + '/' + self.boot_path + '/boot.catalog' loader_file = \ diff --git a/kiwi/markup/any.py b/kiwi/markup/any.py index b6417944..d3573f6e 100644 --- a/kiwi/markup/any.py +++ b/kiwi/markup/any.py @@ -17,9 +17,9 @@ # from typing import Any import importlib -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.markup.base import MarkupBase from kiwi.exceptions import ( @@ -44,7 +44,7 @@ class MarkupAny(MarkupBase): except Exception as issue: raise KiwiAnyMarkupPluginError(issue) try: - self.description_markup_processed = NamedTemporaryFile() + self.description_markup_processed = Temporary().new_file() markup = self.anymarkup.parse_file( self.description, force_types=None ) diff --git a/kiwi/markup/base.py b/kiwi/markup/base.py index 35121dc8..9034d2bd 100644 --- a/kiwi/markup/base.py +++ b/kiwi/markup/base.py @@ -15,10 +15,10 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import NamedTemporaryFile from lxml import etree # project +from kiwi.utils.temporary import Temporary from kiwi.defaults import Defaults from kiwi.exceptions import KiwiConfigFileFormatNotSupported @@ -64,7 +64,7 @@ class MarkupBase: xslt_transform = etree.XSLT( etree.parse(Defaults.get_xsl_stylesheet_file()) ) - self.description_xslt_processed = NamedTemporaryFile(prefix='xslt-') + self.description_xslt_processed = Temporary(prefix='xslt-').new_file() with open(self.description_xslt_processed.name, "wb") as xsltout: xsltout.write( etree.tostring(xslt_transform(parsed_description)) diff --git a/kiwi/mount_manager.py b/kiwi/mount_manager.py index 23330fd2..743ac7ab 100644 --- a/kiwi/mount_manager.py +++ b/kiwi/mount_manager.py @@ -17,11 +17,10 @@ # import time import logging -from tempfile import mkdtemp # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command -from kiwi.path import Path log = logging.getLogger('kiwi') @@ -40,10 +39,11 @@ class MountManager: """ def __init__(self, device, mountpoint=None): self.device = device - self.mountpoint_created_by_mount_manager = False if not mountpoint: - self.mountpoint = mkdtemp(prefix='kiwi_mount_manager.') - self.mountpoint_created_by_mount_manager = True + self.mountpoint_tempdir = Temporary( + prefix='kiwi_mount_manager.' + ).new_dir() + self.mountpoint = self.mountpoint_tempdir.name else: self.mountpoint = mountpoint @@ -129,7 +129,3 @@ class MountManager: return True else: return False - - def __del__(self): - if self.mountpoint_created_by_mount_manager and not self.is_mounted(): - Path.wipe(self.mountpoint) diff --git a/kiwi/oci_tools/umoci.py b/kiwi/oci_tools/umoci.py index 5e75affb..41bcfaa6 100644 --- a/kiwi/oci_tools/umoci.py +++ b/kiwi/oci_tools/umoci.py @@ -15,10 +15,10 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import mkdtemp import os # project +from kiwi.utils.temporary import Temporary from kiwi.oci_tools.base import OCIBase from kiwi.command import Command from kiwi.path import Path @@ -34,7 +34,8 @@ class OCIUmoci(OCIBase): """ Initializes some umoci parameters and options """ - self.oci_dir = mkdtemp(prefix='kiwi_oci_dir.') + self.oci_dir_tempfile = Temporary(prefix='kiwi_oci_dir.').new_dir() + self.oci_dir = self.oci_dir_tempfile.name self.container_dir = os.sep.join( [self.oci_dir, 'oci_layout'] ) @@ -102,7 +103,10 @@ class OCIUmoci(OCIBase): """ Unpack current container root data """ - self.oci_root_dir = mkdtemp(prefix='kiwi_oci_root_dir.') + self.oci_root_dir_tempdir = Temporary( + prefix='kiwi_oci_root_dir.' + ).new_dir() + self.oci_root_dir = self.oci_root_dir_tempdir.name Command.run([ 'umoci', 'unpack', '--image', self.working_image, self.oci_root_dir @@ -259,9 +263,3 @@ class OCIUmoci(OCIBase): Command.run( ['umoci', 'gc', '--layout', self.container_dir] ) - - def __del__(self): - if self.oci_root_dir: - Path.wipe(self.oci_root_dir) - if self.oci_dir: - Path.wipe(self.oci_dir) diff --git a/kiwi/partitioner/dasd.py b/kiwi/partitioner/dasd.py index 6007ab30..ed04b583 100644 --- a/kiwi/partitioner/dasd.py +++ b/kiwi/partitioner/dasd.py @@ -15,10 +15,10 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import NamedTemporaryFile import logging # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.partitioner.base import PartitionerBase @@ -55,7 +55,7 @@ class PartitionerDasd(PartitionerBase): :param list flags: unused """ self.partition_id += 1 - fdasd_input = NamedTemporaryFile() + fdasd_input = Temporary().new_file() with open(fdasd_input.name, 'w') as partition: log.debug( '%s: fdasd: n p cur_position +%sM w q', diff --git a/kiwi/partitioner/msdos.py b/kiwi/partitioner/msdos.py index a205497b..8414aae9 100644 --- a/kiwi/partitioner/msdos.py +++ b/kiwi/partitioner/msdos.py @@ -15,10 +15,10 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import NamedTemporaryFile import logging # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.partitioner.base import PartitionerBase @@ -60,7 +60,7 @@ class PartitionerMsDos(PartitionerBase): :param list flags: additional flags """ self.partition_id += 1 - fdisk_input = NamedTemporaryFile() + fdisk_input = Temporary().new_file() if self.partition_id > 1: # Undefined start sector value skips this for fdisk and # use its default value diff --git a/kiwi/repository/apt.py b/kiwi/repository/apt.py index a2afec3e..b3558198 100644 --- a/kiwi/repository/apt.py +++ b/kiwi/repository/apt.py @@ -17,11 +17,11 @@ # import os import logging -from tempfile import NamedTemporaryFile from urllib.parse import urlparse from typing import List, Dict # project +from kiwi.utils.temporary import Temporary from kiwi.repository.template.apt import PackageManagerTemplateAptGet from kiwi.repository.base import RepositoryBase from kiwi.path import Path @@ -86,9 +86,9 @@ class RepositoryApt(RepositoryBase): } self.keyring = '{}/trusted.gpg'.format(self.manager_base) - self.runtime_apt_get_config_file = NamedTemporaryFile( + self.runtime_apt_get_config_file = Temporary( dir=self.root_dir - ) + ).new_file() self.apt_get_args = [ '-q', '-c', self.runtime_apt_get_config_file.name, '-y' diff --git a/kiwi/repository/dnf.py b/kiwi/repository/dnf.py index 8b0e7b10..e7118586 100644 --- a/kiwi/repository/dnf.py +++ b/kiwi/repository/dnf.py @@ -18,10 +18,10 @@ import os import glob from configparser import ConfigParser -from tempfile import NamedTemporaryFile from typing import List, Dict # project +from kiwi.utils.temporary import Temporary from kiwi.defaults import Defaults from kiwi.command import Command from kiwi.repository.base import RepositoryBase @@ -92,9 +92,9 @@ class RepositoryDnf(RepositoryBase): 'vars-dir': manager_base + '/vars' } - self.runtime_dnf_config_file = NamedTemporaryFile( + self.runtime_dnf_config_file = Temporary( dir=self.root_dir - ) + ).new_file() self.dnf_args = [ '--config', self.runtime_dnf_config_file.name, '-y' diff --git a/kiwi/repository/pacman.py b/kiwi/repository/pacman.py index c8577395..8089b80b 100644 --- a/kiwi/repository/pacman.py +++ b/kiwi/repository/pacman.py @@ -17,12 +17,12 @@ # import os from configparser import ConfigParser -from tempfile import NamedTemporaryFile from typing import ( List, Dict ) # project +from kiwi.utils.temporary import Temporary from kiwi.repository.base import RepositoryBase from kiwi.path import Path from kiwi.command import Command @@ -52,9 +52,9 @@ class RepositoryPacman(RepositoryBase): self.check_signatures = False self.repo_names: List = [] - self.runtime_pacman_config_file = NamedTemporaryFile( + self.runtime_pacman_config_file = Temporary( dir=self.root_dir - ) + ).new_file() if 'check_signatures' in self.custom_args: self.custom_args.remove('check_signatures') diff --git a/kiwi/repository/zypper.py b/kiwi/repository/zypper.py index 008129af..2b3eff7a 100644 --- a/kiwi/repository/zypper.py +++ b/kiwi/repository/zypper.py @@ -17,10 +17,10 @@ # import os from configparser import ConfigParser -from tempfile import NamedTemporaryFile from typing import List, Dict # project +from kiwi.utils.temporary import Temporary from kiwi.defaults import Defaults from kiwi.command import Command from kiwi.repository.base import RepositoryBase @@ -108,12 +108,12 @@ class RepositoryZypper(RepositoryBase): ) } - self.runtime_zypper_config_file = NamedTemporaryFile( + self.runtime_zypper_config_file = Temporary( dir=self.root_dir - ) - self.runtime_zypp_config_file = NamedTemporaryFile( + ).new_file() + self.runtime_zypp_config_file = Temporary( dir=self.root_dir - ) + ).new_file() self.zypper_args = [ '--non-interactive', diff --git a/kiwi/solver/repository/base.py b/kiwi/solver/repository/base.py index 1e49f731..c83e5d90 100644 --- a/kiwi/solver/repository/base.py +++ b/kiwi/solver/repository/base.py @@ -18,8 +18,7 @@ from base64 import b64encode from urllib.request import urlopen from urllib.request import Request -from tempfile import NamedTemporaryFile -from tempfile import mkdtemp +from kiwi.utils.temporary import Temporary from lxml import etree import random import glob @@ -46,7 +45,8 @@ class SolverRepositoryBase: self.uri = uri self.user = user self.secret = secret - self._init_temporary_dir_names() + self.repository_metadata_dirs = [] + self.repository_solvable_dir = None def create_repository_solvable( self, target_dir=Defaults.get_solvable_location() @@ -69,7 +69,6 @@ class SolverRepositoryBase: if not self.is_uptodate(target_dir): self._setup_repository_metadata() solvable = self._merge_solvables(target_dir) - self._cleanup() return solvable @@ -171,7 +170,7 @@ class SolverRepositoryBase: :rtype: str """ - dir_listing_download = NamedTemporaryFile() + dir_listing_download = Temporary().new_file() self.download_from_repository( defaults.PLATFORM_MACHINE, dir_listing_download.name ) @@ -193,7 +192,7 @@ class SolverRepositoryBase: """ repo_source = 'Packages.gz' if not download_dir: - packages_download = NamedTemporaryFile() + packages_download = Temporary().new_file() self.download_from_repository(repo_source, packages_download.name) if os.path.isfile(packages_download.name): with open(packages_download.name) as packages: @@ -216,7 +215,7 @@ class SolverRepositoryBase: :rtype: XML etree """ - xml_download = NamedTemporaryFile() + xml_download = Temporary().new_file() xml_setup_file = os.sep.join([lookup_path, 'repomd.xml']) self.download_from_repository(xml_setup_file, xml_download.name) return etree.parse(xml_download.name) @@ -282,7 +281,9 @@ class SolverRepositoryBase: :param str tool: one of the above tools """ if not self.repository_solvable_dir: - self.repository_solvable_dir = mkdtemp(prefix='solvable_dir.') + self.repository_solvable_dir = Temporary( + prefix='kiwi_solvable_dir.' + ).new_dir() if tool == 'rpms2solv': # solvable is created from a bunch of rpm files @@ -317,7 +318,8 @@ class SolverRepositoryBase: if self.repository_solvable_dir: solvable = os.sep.join([target_dir, self.uri.alias()]) bash_command = [ - 'mergesolv', '/'.join([self.repository_solvable_dir, '*']), + 'mergesolv', + '/'.join([self.repository_solvable_dir.name, '*']), '>', solvable ] Command.run(['bash', '-c', ' '.join(bash_command)]) @@ -327,18 +329,6 @@ class SolverRepositoryBase: solvable_time.write(self.timestamp()) return solvable - def _cleanup(self): - """ - Delete all temporary directories - """ - for metadata_dir in self.repository_metadata_dirs: - Path.wipe(metadata_dir) - - if self.repository_solvable_dir: - Path.wipe(self.repository_solvable_dir) - - self._init_temporary_dir_names() - def _get_mime_typed_uri(self): """ Adds `file` scheme for local URIs @@ -351,15 +341,6 @@ class SolverRepositoryBase: ['file://', self.uri.translate()] ) - def _init_temporary_dir_names(self): - """ - Initialize data structures to store temporary directory names - required to hold the repository metadata and solvable files - until the final repository solvable got created - """ - self.repository_metadata_dirs = [] - self.repository_solvable_dir = None - def _create_temporary_metadata_dir(self): """ Create and manage a temporary metadata directory @@ -368,19 +349,16 @@ class SolverRepositoryBase: :rtype: str """ - metadata_dir = mkdtemp(prefix='metadata_dir.') + metadata_dir = Temporary(prefix='kiwi_metadata_dir.').new_dir() self.repository_metadata_dirs.append(metadata_dir) - return metadata_dir + return metadata_dir.name def _get_random_solvable_name(self): if self.repository_solvable_dir: return '{0}/solvable-{1}{2}{3}{4}'.format( - self.repository_solvable_dir, + self.repository_solvable_dir.name, self._rand(), self._rand(), self._rand(), self._rand() ) def _rand(self): return '%02x' % random.randrange(1, 0xfe) - - def __del__(self): - self._cleanup() diff --git a/kiwi/storage/disk.py b/kiwi/storage/disk.py index 756f8853..92ef1833 100644 --- a/kiwi/storage/disk.py +++ b/kiwi/storage/disk.py @@ -19,9 +19,9 @@ import os import logging from collections import OrderedDict from typing import Dict -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.storage.device_provider import DeviceProvider from kiwi.storage.mapped_device import MappedDevice @@ -259,7 +259,7 @@ class Disk(DeviceProvider): """ if 'dasd' in self.table_type: log.debug('Initialize DASD disk with new VTOC table') - fdasd_input = NamedTemporaryFile() + fdasd_input = Temporary().new_file() with open(fdasd_input.name, 'w') as vtoc: vtoc.write('y\n\nw\nq\n') bash_command = ' '.join( diff --git a/kiwi/storage/luks_device.py b/kiwi/storage/luks_device.py index 1771ab00..9f7f85b3 100644 --- a/kiwi/storage/luks_device.py +++ b/kiwi/storage/luks_device.py @@ -17,10 +17,10 @@ # import os import logging -from tempfile import NamedTemporaryFile from typing import Optional # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.defaults import Defaults from kiwi.storage.device_provider import DeviceProvider @@ -119,7 +119,7 @@ class LuksDevice(DeviceProvider): log.info('--> Creating LUKS map') if passphrase: - passphrase_file_tmp = NamedTemporaryFile() + passphrase_file_tmp = Temporary().new_file() with open(passphrase_file_tmp.name, 'w') as credentials: credentials.write(passphrase) passphrase_file = passphrase_file_tmp.name diff --git a/kiwi/storage/subformat/gce.py b/kiwi/storage/subformat/gce.py index 225adff3..49fdc9af 100644 --- a/kiwi/storage/subformat/gce.py +++ b/kiwi/storage/subformat/gce.py @@ -17,9 +17,9 @@ # import os from collections import OrderedDict -from tempfile import mkdtemp # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.storage.subformat.base import DiskFormatBase from kiwi.archive.tar import ArchiveTar @@ -56,9 +56,9 @@ class DiskFormatGce(DiskFormatBase): Create GCE disk format and manifest """ gce_tar_ball_file_list = [] - self.temp_image_dir = mkdtemp( + temp_image_dir = Temporary( prefix='kiwi_gce_subformat.', dir=self.target_dir - ) + ).new_dir() diskname = ''.join( [ self.target_dir, '/', @@ -69,12 +69,12 @@ class DiskFormatGce(DiskFormatBase): ] ) if self.tag: - with open(self.temp_image_dir + '/manifest.json', 'w') as manifest: + with open(temp_image_dir.name + '/manifest.json', 'w') as manifest: manifest.write('{"licenses": ["%s"]}' % self.tag) gce_tar_ball_file_list.append('manifest.json') Command.run( - ['cp', diskname, self.temp_image_dir + '/disk.raw'] + ['cp', diskname, temp_image_dir.name + '/disk.raw'] ) gce_tar_ball_file_list.append('disk.raw') @@ -91,7 +91,7 @@ class DiskFormatGce(DiskFormatBase): file_list=gce_tar_ball_file_list ) archive.create_gnu_gzip_compressed( - self.temp_image_dir + temp_image_dir.name ) def store_to_result(self, result: Result) -> None: diff --git a/kiwi/storage/subformat/qcow2.py b/kiwi/storage/subformat/qcow2.py index 52053745..5495f7a2 100644 --- a/kiwi/storage/subformat/qcow2.py +++ b/kiwi/storage/subformat/qcow2.py @@ -14,9 +14,9 @@ # # You should have received a copy of the GNU General Public License # along with kiwi. If not, see -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.storage.subformat.base import DiskFormatBase from kiwi.command import Command from kiwi.system.result import Result @@ -41,7 +41,7 @@ class DiskFormatQcow2(DiskFormatBase): """ Create qcow2 disk format """ - intermediate = NamedTemporaryFile() + intermediate = Temporary().new_file() Command.run( [ 'qemu-img', 'convert', '-f', 'raw', self.diskname, diff --git a/kiwi/storage/subformat/vagrant_base.py b/kiwi/storage/subformat/vagrant_base.py index 6f9a3633..3f15996e 100644 --- a/kiwi/storage/subformat/vagrant_base.py +++ b/kiwi/storage/subformat/vagrant_base.py @@ -18,7 +18,7 @@ import json import os.path -from tempfile import mkdtemp +from kiwi.utils.temporary import Temporary from typing import ( Dict, Optional, List ) @@ -131,15 +131,15 @@ class DiskFormatVagrantBase(DiskFormatBase): 'vagrant_post_init: Missing provider and/or box name setup' ) - self.temp_image_dir = mkdtemp(prefix='kiwi_vagrant_box.') + temp_image_dir = Temporary(prefix='kiwi_vagrant_box.').new_dir() - box_img_files = self.create_box_img(self.temp_image_dir) + box_img_files = self.create_box_img(temp_image_dir.name) - metadata_json = os.path.join(self.temp_image_dir, 'metadata.json') + metadata_json = os.path.join(temp_image_dir.name, 'metadata.json') with open(metadata_json, 'w') as meta: meta.write(self._create_box_metadata()) - vagrantfile = os.path.join(self.temp_image_dir, 'Vagrantfile') + vagrantfile = os.path.join(temp_image_dir.name, 'Vagrantfile') with open(vagrantfile, 'w') as vagrant: # autogenerate a Vagrantfile: if not self.vagrantconfig.get_embedded_vagrantfile(): @@ -157,7 +157,7 @@ class DiskFormatVagrantBase(DiskFormatBase): Command.run( [ - 'tar', '-C', self.temp_image_dir, + 'tar', '-C', temp_image_dir.name, '-czf', self.get_target_file_path_for_format( self.image_format ), diff --git a/kiwi/system/profile.py b/kiwi/system/profile.py index c390e0a8..f05de71c 100644 --- a/kiwi/system/profile.py +++ b/kiwi/system/profile.py @@ -19,9 +19,9 @@ import os import logging import collections from typing import Dict -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.xml_state import XMLState from kiwi.system.shell import Shell from kiwi.defaults import Defaults @@ -82,7 +82,7 @@ class Profile: :param str filename: file path name """ sorted_profile = self.get_settings() - temp_profile = NamedTemporaryFile() + temp_profile = Temporary().new_file() with open(temp_profile.name, 'w') as profile: for key, value in list(sorted_profile.items()): profile.write( diff --git a/kiwi/system/root_init.py b/kiwi/system/root_init.py index fce0a4ff..42469db4 100644 --- a/kiwi/system/root_init.py +++ b/kiwi/system/root_init.py @@ -16,12 +16,11 @@ # along with kiwi. If not, see # from pwd import getpwnam -from tempfile import mkdtemp -from shutil import rmtree from shutil import copy import os # project +from kiwi.utils.temporary import Temporary from kiwi.utils.sync import DataSync from kiwi.path import Path from kiwi.defaults import Defaults @@ -73,26 +72,25 @@ class RootInit: :raises KiwiRootInitCreationError: if the init creation fails at some point """ - root = mkdtemp(prefix='kiwi_root.') + root = Temporary(prefix='kiwi_root.').new_dir() Path.create(self.root_dir) try: - self._create_base_directories(root) - self._create_base_links(root) - data = DataSync(root + '/', self.root_dir) + self._create_base_directories(root.name) + self._create_base_links(root.name) + data = DataSync(root.name + '/', self.root_dir) data.sync_data( options=['-a', '--ignore-existing'] ) if Defaults.is_buildservice_worker(): copy( os.sep + Defaults.get_buildservice_env_name(), - self.root_dir) + self.root_dir + ) except Exception as e: self.delete() raise KiwiRootInitCreationError( '%s: %s' % (type(e).__name__, format(e)) ) - finally: - rmtree(root, ignore_errors=True) def _create_base_directories(self, root): """ diff --git a/kiwi/system/setup.py b/kiwi/system/setup.py index 0722eaea..c50180e5 100644 --- a/kiwi/system/setup.py +++ b/kiwi/system/setup.py @@ -21,12 +21,12 @@ import logging import copy from collections import OrderedDict from collections import namedtuple -from tempfile import NamedTemporaryFile from typing import Any # project import kiwi.defaults as defaults +from kiwi.utils.temporary import Temporary from kiwi.utils.fstab import Fstab from kiwi.xml_state import XMLState from kiwi.runtime_config import RuntimeConfig @@ -738,9 +738,7 @@ class SystemSetup: 'partition_filesystem': self.root_dir + '/recovery.tar.filesystem' } - recovery_archive = NamedTemporaryFile( - delete=False - ) + recovery_archive = Temporary(delete=False).new_file() archive = ArchiveTar( filename=recovery_archive.name, create_from_file_list=False diff --git a/kiwi/system/shell.py b/kiwi/system/shell.py index a54872d8..5396d121 100644 --- a/kiwi/system/shell.py +++ b/kiwi/system/shell.py @@ -15,11 +15,11 @@ # You should have received a copy of the GNU General Public License # along with kiwi. If not, see # -from tempfile import NamedTemporaryFile from typing import Optional, Any, List from collections.abc import Iterable # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.defaults import Defaults @@ -63,7 +63,7 @@ class Shell: :rtype: List[str] """ - temp_copy = NamedTemporaryFile() + temp_copy = Temporary().new_file() Command.run(['cp', filename, temp_copy.name]) Shell.run_common_function('baseQuoteFile', [temp_copy.name]) with open(temp_copy.name) as quoted: diff --git a/kiwi/utils/compress.py b/kiwi/utils/compress.py index 7db4d096..7876bd6a 100644 --- a/kiwi/utils/compress.py +++ b/kiwi/utils/compress.py @@ -17,9 +17,9 @@ # import os import logging -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.defaults import Defaults @@ -107,7 +107,7 @@ class Compress: Command.run([zipper, '-d', self.source_filename]) self.uncompressed_filename = self.source_filename else: - self.temp_file = NamedTemporaryFile() + self.temp_file = Temporary().new_file() bash_command = [ zipper, '-c', '-d', self.source_filename, '>', self.temp_file.name diff --git a/kiwi/utils/output.py b/kiwi/utils/output.py index e7586ca7..8003f515 100644 --- a/kiwi/utils/output.py +++ b/kiwi/utils/output.py @@ -18,9 +18,9 @@ import json import os import logging -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.path import Path log = logging.getLogger('kiwi') @@ -87,7 +87,7 @@ class DataOutput: """ Show data in json output format with nice color highlighting """ - out_file = NamedTemporaryFile() + out_file = Temporary().new_file() out_file.write(json.dumps(self.data, sort_keys=True).encode()) out_file.flush() pjson_cmd = ''.join( diff --git a/kiwi/utils/temporary.py b/kiwi/utils/temporary.py new file mode 100644 index 00000000..0984aac8 --- /dev/null +++ b/kiwi/utils/temporary.py @@ -0,0 +1,49 @@ +# Copyright (c) 2021 SUSE Linux GmbH. All rights reserved. +# +# This file is part of kiwi. +# +# kiwi is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# kiwi is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with kiwi. If not, see +# +from typing import IO + +from tempfile import ( + NamedTemporaryFile, + TemporaryDirectory +) + +# project +import kiwi.defaults as defaults + + +class Temporary: + """ + **Provides namespace to handle temporary files and directories** + """ + def __init__( + self, dir: str = defaults.TEMP_DIR, prefix: str = '', + delete: bool = True + ): + self.prefix = f'kiwi_{prefix}' if prefix else 'kiwi_' + self.delete = delete + self.dir = dir + + def new_file(self) -> IO[bytes]: + return NamedTemporaryFile( + dir=self.dir, prefix=self.prefix, delete=self.delete + ) + + def new_dir(self) -> TemporaryDirectory: + return TemporaryDirectory( + dir=self.dir, prefix=self.prefix + ) diff --git a/kiwi/volume_manager/base.py b/kiwi/volume_manager/base.py index 973f625d..75291a61 100644 --- a/kiwi/volume_manager/base.py +++ b/kiwi/volume_manager/base.py @@ -16,11 +16,11 @@ # along with kiwi. If not, see # from collections import namedtuple -from tempfile import mkdtemp import logging import os # project +from kiwi.utils.temporary import Temporary from kiwi.command import Command from kiwi.storage.device_provider import DeviceProvider from kiwi.mount_manager import MountManager @@ -358,9 +358,6 @@ class VolumeManagerBase(DeviceProvider): Implements creation of a master directory holding the mounts of all volumes """ - self.mountpoint = mkdtemp(prefix='kiwi_volumes.') - self.temp_directories.append(self.mountpoint) - - def _cleanup_tempdirs(self): - for directory in self.temp_directories: - Path.wipe(directory) + self.mountpoint_tempdir = Temporary(prefix='kiwi_volumes.').new_dir() + self.mountpoint = self.mountpoint_tempdir.name + self.temp_directories.append(self.mountpoint_tempdir) diff --git a/kiwi/volume_manager/btrfs.py b/kiwi/volume_manager/btrfs.py index 846bebb2..af7979f9 100644 --- a/kiwi/volume_manager/btrfs.py +++ b/kiwi/volume_manager/btrfs.py @@ -454,4 +454,3 @@ class VolumeManagerBtrfs(VolumeManagerBase): log.warning('Subvolumes still busy') return Path.wipe(self.mountpoint) - self._cleanup_tempdirs() diff --git a/kiwi/volume_manager/lvm.py b/kiwi/volume_manager/lvm.py index 47b820fe..ed7c21d7 100644 --- a/kiwi/volume_manager/lvm.py +++ b/kiwi/volume_manager/lvm.py @@ -399,4 +399,3 @@ class VolumeManagerLVM(VolumeManagerBase): 'volume group %s still busy', self.volume_group ) return - self._cleanup_tempdirs() diff --git a/kiwi/xml_description.py b/kiwi/xml_description.py index 1309cc8f..f4931106 100644 --- a/kiwi/xml_description.py +++ b/kiwi/xml_description.py @@ -25,9 +25,9 @@ from lxml import ( etree, isoschematron ) -from tempfile import NamedTemporaryFile # project +from kiwi.utils.temporary import Temporary from kiwi.markup import Markup from kiwi.defaults import Defaults from kiwi import xml_parse @@ -164,7 +164,7 @@ class XMLDescription: xml_data_domtree = minidom.parseString( xml_data_unformatted ) - extension_file = NamedTemporaryFile() + extension_file = Temporary().new_file() with open(extension_file.name, 'w') as xml_data: xml_data.write(xml_data_domtree.toprettyxml()) XMLDescription._get_relaxng_validation_details( diff --git a/test/unit/boot/image/builtin_kiwi_test.py b/test/unit/boot/image/builtin_kiwi_test.py index 2cde3154..97a4a934 100644 --- a/test/unit/boot/image/builtin_kiwi_test.py +++ b/test/unit/boot/image/builtin_kiwi_test.py @@ -17,10 +17,12 @@ from kiwi.exceptions import KiwiConfigFileNotFound class TestBootImageKiwi: - @patch('kiwi.boot.image.builtin_kiwi.mkdtemp') + @patch('kiwi.boot.image.builtin_kiwi.Temporary') @patch('kiwi.boot.image.builtin_kiwi.os.path.exists') @patch('kiwi.defaults.Defaults.get_boot_image_description_path') - def setup(self, mock_boot_path, mock_exists, mock_mkdtemp): + def setup(self, mock_boot_path, mock_exists, mock_Temporary): + mock_Temporary.return_value.new_dir.return_value.name = \ + 'boot-root-directory' mock_boot_path.return_value = '../data' Defaults.set_platform_name('x86_64') mock_exists.return_value = True @@ -46,7 +48,6 @@ class TestBootImageKiwi: kiwi.boot.image.builtin_kiwi.Profile = Mock( return_value=self.profile ) - mock_mkdtemp.return_value = 'boot-root-directory' self.boot_image = BootImageKiwi( self.xml_state, 'some-target-dir' ) @@ -56,9 +57,10 @@ class TestBootImageKiwi: self.boot_image.include_file('/root/a') @patch('kiwi.defaults.Defaults.get_boot_image_description_path') - @patch('kiwi.boot.image.builtin_kiwi.mkdtemp') - def test_prepare(self, mock_mkdtemp, mock_boot_path): - mock_mkdtemp.return_value = 'boot-root-directory' + @patch('kiwi.boot.image.builtin_kiwi.Temporary') + def test_prepare(self, mock_Temporary, mock_boot_path): + mock_Temporary.return_value.new_dir.return_value.name = \ + 'boot-root-directory' mock_boot_path.return_value = '../data' self.boot_image.prepare() self.system_prepare.setup_repositories.assert_called_once_with( @@ -87,11 +89,12 @@ class TestBootImageKiwi: self.setup.call_image_script.assert_called_once_with() @patch('os.path.exists') - @patch('kiwi.boot.image.builtin_kiwi.mkdtemp') + @patch('kiwi.boot.image.builtin_kiwi.Temporary') def test_prepare_no_boot_description_found( - self, mock_mkdtemp, mock_os_path + self, mock_Temporary, mock_os_path ): - mock_mkdtemp.return_value = 'boot-root-directory' + mock_Temporary.return_value.new_dir.return_value.name = \ + 'boot-root-directory' mock_os_path.return_value = False with raises(KiwiConfigFileNotFound): self.boot_image.post_init() @@ -102,20 +105,17 @@ class TestBootImageKiwi: @patch('kiwi.boot.image.builtin_kiwi.Path.wipe') @patch('kiwi.boot.image.builtin_kiwi.DataSync') @patch('kiwi.boot.image.base.BootImageBase.is_prepared') - @patch('kiwi.boot.image.builtin_kiwi.mkdtemp') @patch('kiwi.boot.image.builtin_kiwi.os.chmod') - @patch('kiwi.boot.image.builtin_kiwi.TemporaryDirectory') + @patch('kiwi.boot.image.builtin_kiwi.Temporary') def test_create_initrd( - self, mock_TemporaryDirectory, mock_os_chmod, - mock_mkdtemp, mock_prepared, mock_sync, - mock_wipe, mock_create, mock_compress, mock_cpio + self, mock_Temporary, mock_os_chmod, + mock_prepared, mock_sync, mock_wipe, mock_create, + mock_compress, mock_cpio ): data = Mock() mock_sync.return_value = data - mock_mkdtemp.return_value = 'temp-boot-directory' - temporary_directory = Mock() - temporary_directory.name = 'temp-boot-directory' - mock_TemporaryDirectory.return_value = temporary_directory + mock_Temporary.return_value.new_dir.return_value.name = \ + 'temp-boot-directory' mock_prepared.return_value = True self.boot_image.boot_root_directory = 'boot-root-directory' mbrid = Mock() diff --git a/test/unit/builder/disk_test.py b/test/unit/builder/disk_test.py index 35d739d9..da9be021 100644 --- a/test/unit/builder/disk_test.py +++ b/test/unit/builder/disk_test.py @@ -545,7 +545,7 @@ class TestDiskBuilder: @patch('kiwi.builder.disk.Defaults.get_grub_boot_directory_name') @patch('os.path.exists') @patch('os.path.getsize') - @patch('kiwi.builder.disk.NamedTemporaryFile') + @patch('kiwi.builder.disk.Temporary.new_file') @patch('random.randrange') def test_create_disk_standard_root_is_overlay( self, mock_rand, mock_temp, mock_getsize, mock_exists, diff --git a/test/unit/builder/install_test.py b/test/unit/builder/install_test.py index 8b49a972..02696eaf 100644 --- a/test/unit/builder/install_test.py +++ b/test/unit/builder/install_test.py @@ -1,5 +1,5 @@ from mock import ( - patch, call, mock_open, ANY + patch, call, mock_open, ANY, Mock ) from pytest import raises import mock @@ -125,22 +125,28 @@ class TestInstallImageBuilder: @patch('kiwi.builder.install.BootLoaderConfig.new') @patch('kiwi.builder.install.IsoToolsBase.setup_media_loader_directory') @patch('kiwi.builder.install.shutil.copy') - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') @patch('kiwi.builder.install.Defaults.get_grub_boot_directory_name') def test_create_install_iso( - self, mock_grub_dir, mock_command, mock_dtemp, mock_copy, + self, mock_grub_dir, mock_command, mock_Temporary, mock_copy, mock_setup_media_loader_directory, mock_BootLoaderConfig, mock_DeviceProvider ): - tmpdir_name = ['temp-squashfs', 'temp_media_dir'] + temp_squashfs = Mock() + temp_squashfs.new_dir.return_value.name = 'temp-squashfs' + + temp_media_dir = Mock() + temp_media_dir.new_dir.return_value.name = 'temp_media_dir' + + tmpdir_name = [temp_squashfs, temp_media_dir] def side_effect(prefix, dir): return tmpdir_name.pop() bootloader_config = mock.Mock() mock_BootLoaderConfig.return_value = bootloader_config - mock_dtemp.side_effect = side_effect + mock_Temporary.side_effect = side_effect m_open = mock_open() with patch('builtins.open', m_open, create=True): @@ -216,7 +222,7 @@ class TestInstallImageBuilder: 'target_dir/result-image.x86_64-1.2.3.install.iso' ) - tmpdir_name = ['temp-squashfs', 'temp_media_dir'] + tmpdir_name = [temp_squashfs, temp_media_dir] self.install_image.initrd_system = 'dracut' m_open.reset_mock() @@ -243,7 +249,7 @@ class TestInstallImageBuilder: ] mock_BootLoaderConfig.reset_mock() - tmpdir_name = ['temp-squashfs', 'temp_media_dir'] + tmpdir_name = [temp_squashfs, temp_media_dir] self.firmware.efi_mode.return_value = None with patch('builtins.open', m_open, create=True): @@ -255,10 +261,10 @@ class TestInstallImageBuilder: ) @patch('kiwi.builder.install.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') def test_create_install_iso_no_kernel_found( - self, mock_command, mock_dtemp, mock_setup_media_loader_directory + self, mock_command, mock_Temporary, mock_setup_media_loader_directory ): self.kernel.get_kernel.return_value = False with patch('builtins.open'): @@ -266,44 +272,45 @@ class TestInstallImageBuilder: self.install_image.create_install_iso() @patch('kiwi.builder.install.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') def test_create_install_iso_no_hypervisor_found( - self, mock_command, mock_dtemp, mock_setup_media_loader_directory + self, mock_command, mock_Temporary, mock_setup_media_loader_directory ): self.kernel.get_xen_hypervisor.return_value = False with patch('builtins.open'): with raises(KiwiInstallBootImageError): self.install_image.create_install_iso() - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') @patch('kiwi.builder.install.Checksum') @patch('kiwi.builder.install.Compress') def test_create_install_pxe_no_kernel_found( - self, mock_compress, mock_md5, mock_command, mock_dtemp + self, mock_compress, mock_md5, mock_command, mock_Temporary ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.kernel.get_kernel.return_value = False with patch('builtins.open'): with raises(KiwiInstallBootImageError): self.install_image.create_install_pxe_archive() - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') @patch('kiwi.builder.install.Checksum') @patch('kiwi.builder.install.Compress') @patch('kiwi.builder.install.os.symlink') def test_create_install_pxe_no_hypervisor_found( - self, mock_symlink, mock_compress, mock_md5, mock_command, mock_dtemp + self, mock_symlink, mock_compress, mock_md5, mock_command, + mock_Temporary ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.kernel.get_xen_hypervisor.return_value = False with patch('builtins.open'): with raises(KiwiInstallBootImageError): self.install_image.create_install_pxe_archive() - @patch('kiwi.builder.install.mkdtemp') + @patch('kiwi.builder.install.Temporary') @patch('kiwi.builder.install.Command.run') @patch('kiwi.builder.install.ArchiveTar') @patch('kiwi.builder.install.Checksum') @@ -313,9 +320,9 @@ class TestInstallImageBuilder: @patch('kiwi.builder.install.os.chmod') def test_create_install_pxe_archive( self, mock_chmod, mock_symlink, mock_copy, mock_compress, - mock_md5, mock_archive, mock_command, mock_dtemp + mock_md5, mock_archive, mock_command, mock_Temporary ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' archive = mock.Mock() mock_archive.return_value = archive @@ -425,19 +432,3 @@ class TestInstallImageBuilder: self.boot_image_task.set_static_modules.assert_called_once_with( ['module1', 'module2'] ) - - @patch('kiwi.builder.install.Path.wipe') - @patch('os.path.exists') - def test_destructor(self, mock_exists, mock_wipe): - mock_exists.return_value = True - self.install_image.initrd_system = 'dracut' - self.install_image.pxe_dir = 'pxe-dir' - self.install_image.media_dir = 'media-dir' - self.install_image.squashed_contents = 'squashed-dir' - self.install_image.__del__() - assert mock_wipe.call_args_list == [ - call('media-dir'), call('pxe-dir'), call('squashed-dir') - ] - self.install_image.pxe_dir = None - self.install_image.media_dir = None - self.install_image.squashed_contents = None diff --git a/test/unit/builder/live_test.py b/test/unit/builder/live_test.py index 0aac1b3e..18e163e9 100644 --- a/test/unit/builder/live_test.py +++ b/test/unit/builder/live_test.py @@ -1,8 +1,7 @@ from mock import ( - patch, call + patch, call, Mock ) from pytest import raises -import mock import sys import kiwi @@ -17,89 +16,89 @@ class TestLiveImageBuilder: def setup(self): Defaults.set_platform_name('x86_64') - self.firmware = mock.Mock() - self.firmware.efi_mode = mock.Mock( + self.firmware = Mock() + self.firmware.efi_mode = Mock( return_value='uefi' ) - kiwi.builder.live.FirmWare = mock.Mock( + kiwi.builder.live.FirmWare = Mock( return_value=self.firmware ) - self.setup = mock.Mock() - kiwi.builder.live.SystemSetup = mock.Mock( + self.setup = Mock() + kiwi.builder.live.SystemSetup = Mock( return_value=self.setup ) - self.filesystem_setup = mock.Mock() - kiwi.builder.live.FileSystemSetup = mock.Mock( + self.filesystem_setup = Mock() + kiwi.builder.live.FileSystemSetup = Mock( return_value=self.filesystem_setup ) - self.loop = mock.Mock() - kiwi.builder.live.LoopDevice = mock.Mock( + self.loop = Mock() + kiwi.builder.live.LoopDevice = Mock( return_value=self.loop ) - self.bootloader = mock.Mock() - kiwi.builder.live.BootLoaderConfig.new = mock.Mock( + self.bootloader = Mock() + kiwi.builder.live.BootLoaderConfig.new = Mock( return_value=self.bootloader ) - self.boot_image_task = mock.Mock() + self.boot_image_task = Mock() self.boot_image_task.boot_root_directory = 'initrd_dir' self.boot_image_task.initrd_filename = 'initrd' - kiwi.builder.live.BootImageDracut = mock.Mock( + kiwi.builder.live.BootImageDracut = Mock( return_value=self.boot_image_task ) - self.mbrid = mock.Mock() - self.mbrid.get_id = mock.Mock( + self.mbrid = Mock() + self.mbrid.get_id = Mock( return_value='0xffffffff' ) - kiwi.builder.live.SystemIdentifier = mock.Mock( + kiwi.builder.live.SystemIdentifier = Mock( return_value=self.mbrid ) - kiwi.builder.live.Path = mock.Mock() + kiwi.builder.live.Path = Mock() - self.kernel = mock.Mock() - self.kernel.get_kernel = mock.Mock() - self.kernel.get_xen_hypervisor = mock.Mock() - self.kernel.copy_kernel = mock.Mock() - self.kernel.copy_xen_hypervisor = mock.Mock() - kiwi.builder.live.Kernel = mock.Mock( + self.kernel = Mock() + self.kernel.get_kernel = Mock() + self.kernel.get_xen_hypervisor = Mock() + self.kernel.copy_kernel = Mock() + self.kernel.copy_xen_hypervisor = Mock() + kiwi.builder.live.Kernel = Mock( return_value=self.kernel ) - self.xml_state = mock.Mock() - self.xml_state.get_fs_mount_option_list = mock.Mock( + self.xml_state = Mock() + self.xml_state.get_fs_mount_option_list = Mock( return_value=['async'] ) - self.xml_state.get_fs_create_option_list = mock.Mock( + self.xml_state.get_fs_create_option_list = Mock( return_value=['-O', 'option'] ) - self.xml_state.build_type.get_flags = mock.Mock( + self.xml_state.build_type.get_flags = Mock( return_value=None ) - self.xml_state.build_type.get_squashfscompression = mock.Mock( + self.xml_state.build_type.get_squashfscompression = Mock( return_value='lzo' ) - self.xml_state.get_image_version = mock.Mock( + self.xml_state.get_image_version = Mock( return_value='1.2.3' ) - self.xml_state.xml_data.get_name = mock.Mock( + self.xml_state.xml_data.get_name = Mock( return_value='result-image' ) - self.xml_state.build_type.get_volid = mock.Mock( + self.xml_state.build_type.get_volid = Mock( return_value='volid' ) - self.xml_state.build_type.get_kernelcmdline = mock.Mock( + self.xml_state.build_type.get_kernelcmdline = Mock( return_value='custom_cmdline' ) - self.xml_state.build_type.get_mediacheck = mock.Mock( + self.xml_state.build_type.get_mediacheck = Mock( return_value=True ) - self.xml_state.build_type.get_publisher = mock.Mock( + self.xml_state.build_type.get_publisher = Mock( return_value='Custom publisher' ) @@ -108,7 +107,7 @@ class TestLiveImageBuilder: custom_args={'signing_keys': ['key_file_a', 'key_file_b']} ) - self.result = mock.Mock() + self.result = Mock() self.live_image.result = self.result def teardown(self): @@ -116,11 +115,11 @@ class TestLiveImageBuilder: def test_init_for_ix86_platform(self): Defaults.set_platform_name('i686') - xml_state = mock.Mock() - xml_state.xml_data.get_name = mock.Mock( + xml_state = Mock() + xml_state.xml_data.get_name = Mock( return_value='some-image' ) - xml_state.get_image_version = mock.Mock( + xml_state.get_image_version = Mock( return_value='1.2.3' ) live_image = LiveImageBuilder( @@ -130,8 +129,7 @@ class TestLiveImageBuilder: @patch('kiwi.builder.live.DeviceProvider') @patch('kiwi.builder.live.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.live.mkdtemp') - @patch('kiwi.builder.live.NamedTemporaryFile') + @patch('kiwi.builder.live.Temporary') @patch('kiwi.builder.live.shutil') @patch('kiwi.builder.live.Iso.set_media_tag') @patch('kiwi.builder.live.FileSystemIsoFs') @@ -141,31 +139,37 @@ class TestLiveImageBuilder: @patch('os.path.exists') def test_create_overlay_structure( self, mock_exists, mock_grub_dir, mock_size, mock_filesystem, - mock_isofs, mock_tag, mock_shutil, mock_tmpfile, mock_dtemp, + mock_isofs, mock_tag, mock_shutil, mock_Temporary, mock_setup_media_loader_directory, mock_DeviceProvider ): - tempfile = mock.Mock() - tempfile.name = 'kiwi-tmpfile' - mock_tmpfile.return_value = tempfile mock_exists.return_value = True mock_grub_dir.return_value = 'grub2' - tmpdir_name = ['temp-squashfs', 'temp_media_dir'] - filesystem = mock.Mock() + + temp_squashfs = Mock() + temp_squashfs.name = 'temp-squashfs' + + temp_media_dir = Mock() + temp_media_dir.name = 'temp_media_dir' + + tmpdir_name = [temp_squashfs, temp_media_dir] + + filesystem = Mock() mock_filesystem.return_value = filesystem - def side_effect(prefix, dir): + def side_effect(): return tmpdir_name.pop() - mock_dtemp.side_effect = side_effect + mock_Temporary.return_value.new_dir.side_effect = side_effect + mock_Temporary.return_value.new_file.return_value.name = 'kiwi-tmpfile' self.live_image.live_type = 'overlay' - iso_image = mock.Mock() + iso_image = Mock() iso_image.create_on_file.return_value = 'offset' mock_isofs.return_value = iso_image - rootsize = mock.Mock() - rootsize.accumulate_mbyte_file_sizes = mock.Mock( + rootsize = Mock() + rootsize.accumulate_mbyte_file_sizes = Mock( return_value=8192 ) mock_size.return_value = rootsize @@ -315,7 +319,7 @@ class TestLiveImageBuilder: ) self.firmware.efi_mode.return_value = None - tmpdir_name = ['temp-squashfs', 'temp_media_dir'] + tmpdir_name = [temp_squashfs, temp_media_dir] kiwi.builder.live.BootLoaderConfig.new.reset_mock() self.live_image.create() kiwi.builder.live.BootLoaderConfig.new.assert_called_once_with( @@ -324,50 +328,38 @@ class TestLiveImageBuilder: ) @patch('kiwi.builder.live.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.live.mkdtemp') + @patch('kiwi.builder.live.Temporary') @patch('kiwi.builder.live.shutil') def test_create_no_kernel_found( - self, mock_shutil, mock_dtemp, + self, mock_shutil, mock_Temporary, mock_setup_media_loader_directory ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.kernel.get_kernel.return_value = False with raises(KiwiLiveBootImageError): self.live_image.create() @patch('kiwi.builder.live.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.live.mkdtemp') + @patch('kiwi.builder.live.Temporary') @patch('kiwi.builder.live.shutil') def test_create_no_hypervisor_found( - self, mock_shutil, mock_dtemp, + self, mock_shutil, mock_Temporary, mock_setup_media_loader_directory ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.kernel.get_xen_hypervisor.return_value = False with raises(KiwiLiveBootImageError): self.live_image.create() @patch('kiwi.builder.live.IsoToolsBase.setup_media_loader_directory') - @patch('kiwi.builder.live.mkdtemp') + @patch('kiwi.builder.live.Temporary') @patch('kiwi.builder.live.shutil') @patch('os.path.exists') def test_create_no_initrd_found( - self, mock_exists, mock_shutil, mock_dtemp, + self, mock_exists, mock_shutil, mock_Temporary, mock_setup_media_loader_directory ): - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_exists.return_value = False with raises(KiwiLiveBootImageError): self.live_image.create() - - @patch('kiwi.builder.live.Path.wipe') - def test_destructor(self, mock_wipe): - self.live_image.media_dir = 'media-dir' - self.live_image.live_container_dir = 'container-dir' - self.live_image.__del__() - assert mock_wipe.call_args_list == [ - call('media-dir'), - call('container-dir') - ] - self.live_image.media_dir = None - self.live_image.live_container_dir = None diff --git a/test/unit/cli_test.py b/test/unit/cli_test.py index 600bb905..338bafd2 100644 --- a/test/unit/cli_test.py +++ b/test/unit/cli_test.py @@ -40,6 +40,7 @@ class TestCli: 'result': False, '--profile': [], '--shared-cache-dir': '/var/cache/kiwi', + '--temp-dir': '/var/tmp', '--target-arch': None, '--help': False, '--config': 'config-file' diff --git a/test/unit/container/appx_test.py b/test/unit/container/appx_test.py index 0b1997fc..e8ab0636 100644 --- a/test/unit/container/appx_test.py +++ b/test/unit/container/appx_test.py @@ -37,11 +37,11 @@ class TestContainerImageAppx: @patch('kiwi.container.appx.Compress') @patch('kiwi.container.appx.Defaults.get_exclude_list_for_root_data_sync') @patch('kiwi.container.appx.Defaults.get_exclude_list_from_custom_exclude_files') - @patch('kiwi.container.appx.NamedTemporaryFile') + @patch('kiwi.container.appx.Temporary.new_file') @patch('kiwi.container.appx.Command.run') @patch('os.walk') def test_create( - self, mock_os_walk, mock_Command_run, mock_NamedTemporaryFile, + self, mock_os_walk, mock_Command_run, mock_Temporary_new_file, mock_get_exclude_list_from_custom_exclude_files, mock_get_exclude_list_for_root_data_sync, mock_Compress, mock_ArchiveTar @@ -53,7 +53,7 @@ class TestContainerImageAppx: ] tempfile = Mock() tempfile.name = 'tempfile' - mock_NamedTemporaryFile.return_value = tempfile + mock_Temporary_new_file.return_value = tempfile archive = Mock() mock_ArchiveTar.return_value = archive compress = Mock() diff --git a/test/unit/filesystem/clicfs_test.py b/test/unit/filesystem/clicfs_test.py index da740339..6df1f0ab 100644 --- a/test/unit/filesystem/clicfs_test.py +++ b/test/unit/filesystem/clicfs_test.py @@ -12,14 +12,13 @@ class TestFileSystemClicFs: self.clicfs = FileSystemClicFs(mock.Mock(), 'root_dir') @patch('kiwi.filesystem.clicfs.Command.run') - @patch('kiwi.filesystem.clicfs.mkdtemp') + @patch('kiwi.filesystem.clicfs.Temporary') @patch('kiwi.filesystem.clicfs.LoopDevice') @patch('kiwi.filesystem.clicfs.FileSystemExt4') @patch('kiwi.filesystem.clicfs.SystemSize') - @patch('kiwi.filesystem.clicfs.Path.wipe') def test_create_on_file( - self, mock_wipe, mock_size, mock_ext4, mock_loop, - mock_dtemp, mock_command + self, mock_size, mock_ext4, mock_loop, + mock_Temporary, mock_command ): size = mock.Mock() size.customize = mock.Mock( @@ -33,7 +32,7 @@ class TestFileSystemClicFs: mock_ext4.return_value = filesystem loop_provider = mock.Mock() mock_loop.return_value = loop_provider - mock_dtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.clicfs.create_on_file('myimage', 'label') @@ -55,9 +54,3 @@ class TestFileSystemClicFs: ['mkclicfs', 'tmpdir/fsdata.ext4', 'myimage'] ) ] - - @patch('kiwi.filesystem.clicfs.Path.wipe') - def test_destructor(self, mock_wipe): - self.clicfs.container_dir = 'tmpdir' - self.clicfs.__del__() - mock_wipe.assert_called_once_with('tmpdir') diff --git a/test/unit/iso_tools/cdrtools_test.py b/test/unit/iso_tools/cdrtools_test.py index ba2892a6..d6aa3c27 100644 --- a/test/unit/iso_tools/cdrtools_test.py +++ b/test/unit/iso_tools/cdrtools_test.py @@ -35,7 +35,7 @@ class TestIsoToolsCdrTools: self.iso_tool.get_tool_name() @patch('os.walk') - @patch('kiwi.iso_tools.cdrtools.NamedTemporaryFile') + @patch('kiwi.iso_tools.cdrtools.Temporary.new_file') def test_init_iso_creation_parameters( self, mock_tempfile, mock_walk ): diff --git a/test/unit/iso_tools/iso_test.py b/test/unit/iso_tools/iso_test.py index 6cd824fc..56d69683 100644 --- a/test/unit/iso_tools/iso_test.py +++ b/test/unit/iso_tools/iso_test.py @@ -11,7 +11,7 @@ import sys from kiwi.defaults import Defaults from kiwi.iso_tools.iso import Iso from kiwi.path import Path -from tempfile import NamedTemporaryFile +from kiwi.utils.temporary import Temporary from kiwi.exceptions import ( KiwiIsoLoaderError, @@ -76,7 +76,7 @@ class TestIso: Path.which('isoinfo') is None, reason='requires cdrtools' ) def test_create_header_end_block_on_test_iso(self): - temp_file = NamedTemporaryFile() + temp_file = Temporary().new_file() self.iso.header_end_file = temp_file.name assert self.iso.create_header_end_block( '../data/iso_with_marker.iso' @@ -101,7 +101,7 @@ class TestIso: Path.which('isoinfo') is None, reason='requires cdrtools' ) def test_create_header_end_block_raises_on_test_iso(self): - temp_file = NamedTemporaryFile() + temp_file = Temporary().new_file() self.iso.header_end_file = temp_file.name with raises(KiwiIsoLoaderError): self.iso.create_header_end_block( diff --git a/test/unit/mount_manager_test.py b/test/unit/mount_manager_test.py index de5544c1..6c461874 100644 --- a/test/unit/mount_manager_test.py +++ b/test/unit/mount_manager_test.py @@ -1,9 +1,8 @@ import logging from pytest import fixture from mock import ( - patch, call + patch, call, Mock ) -import mock from kiwi.mount_manager import MountManager @@ -18,9 +17,9 @@ class TestMountManager: '/dev/some-device', '/some/mountpoint' ) - @patch('kiwi.mount_manager.mkdtemp') - def test_setup_empty_mountpoint(self, mock_mkdtemp): - mock_mkdtemp.return_value = 'tmpdir' + @patch('kiwi.mount_manager.Temporary') + def test_setup_empty_mountpoint(self, mock_Temporary): + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mount_manager = MountManager('/dev/some-device') assert mount_manager.mountpoint == 'tmpdir' @@ -78,7 +77,7 @@ class TestMountManager: @patch('kiwi.mount_manager.Command.run') def test_is_mounted_true(self, mock_command): - command = mock.Mock() + command = Mock() command.returncode = 0 mock_command.return_value = command assert self.mount_manager.is_mounted() is True @@ -89,7 +88,7 @@ class TestMountManager: @patch('kiwi.mount_manager.Command.run') def test_is_mounted_false(self, mock_command): - command = mock.Mock() + command = Mock() command.returncode = 1 mock_command.return_value = command assert self.mount_manager.is_mounted() is False @@ -97,11 +96,3 @@ class TestMountManager: command=['mountpoint', '-q', '/some/mountpoint'], raise_on_error=False ) - - @patch('kiwi.mount_manager.Path.wipe') - @patch('kiwi.mount_manager.MountManager.is_mounted') - def test_destructor(self, mock_mounted, mock_wipe): - self.mount_manager.mountpoint_created_by_mount_manager = True - mock_mounted.return_value = False - self.mount_manager.__del__() - mock_wipe.assert_called_once_with('/some/mountpoint') diff --git a/test/unit/oci_tools/umoci_test.py b/test/unit/oci_tools/umoci_test.py index 491d2110..8c7f098a 100644 --- a/test/unit/oci_tools/umoci_test.py +++ b/test/unit/oci_tools/umoci_test.py @@ -8,9 +8,9 @@ from kiwi.oci_tools.umoci import OCIUmoci class TestOCIUmoci: @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') @patch('kiwi.oci_tools.base.datetime') - @patch('kiwi.oci_tools.umoci.mkdtemp') - def setup(self, mock_base_mkdtemp, mock_datetime, mock_cmd_caps): - mock_base_mkdtemp.return_value = 'tmpdir' + @patch('kiwi.oci_tools.umoci.Temporary') + def setup(self, mock_Temporary, mock_datetime, mock_cmd_caps): + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_cmd_caps.return_value = True strftime = Mock() strftime.strftime = Mock(return_value='current_date') @@ -27,13 +27,16 @@ class TestOCIUmoci: call(['umoci', 'new', '--image', 'tmpdir/oci_layout:base_layer']) ] - @patch('kiwi.oci_tools.umoci.mkdtemp') + @patch('kiwi.oci_tools.umoci.Temporary') @patch('kiwi.oci_tools.umoci.Command.run') - def test_unpack(self, mock_Command_run, mock_mkdtemp): - mock_mkdtemp.return_value = 'oci_root' + def test_unpack(self, mock_Command_run, mock_Temporary): + mock_Temporary.return_value.new_dir.return_value.name = 'oci_root' self.oci.unpack() mock_Command_run.assert_called_once_with( - ['umoci', 'unpack', '--image', 'tmpdir/oci_layout:base_layer', 'oci_root'] + [ + 'umoci', 'unpack', '--image', + 'tmpdir/oci_layout:base_layer', 'oci_root' + ] ) @patch('kiwi.oci_tools.base.DataSync') @@ -67,10 +70,10 @@ class TestOCIUmoci: options=['-a', '-H', '-X', '-A', '--one-file-system', '--inplace'] ) - @patch('kiwi.oci_tools.umoci.mkdtemp') + @patch('kiwi.oci_tools.umoci.Temporary') @patch('kiwi.oci_tools.umoci.Command.run') - def test_repack(self, mock_Command_run, mock_mkdtemp): - mock_mkdtemp.return_value = 'oci_root' + def test_repack(self, mock_Command_run, mock_Temporary): + mock_Temporary.return_value.new_dir.return_value.name = 'oci_root' oci_config = { 'history': { 'author': 'history author', @@ -120,12 +123,12 @@ class TestOCIUmoci: ] ) - @patch('kiwi.oci_tools.umoci.mkdtemp') + @patch('kiwi.oci_tools.umoci.Temporary') @patch('kiwi.oci_tools.base.datetime') @patch('kiwi.oci_tools.umoci.CommandCapabilities.has_option_in_help') @patch('kiwi.oci_tools.umoci.Command.run') def test_set_config_with_history( - self, mock_Command_run, mock_cmd_caps, mock_datetime, mock_mkdtemp + self, mock_Command_run, mock_cmd_caps, mock_datetime, mock_Temporary ): oci_config = { 'container_tag': 'tag', @@ -139,7 +142,7 @@ class TestOCIUmoci: 'environment': {'FOO': 'bar', 'PATH': '/bin'}, 'labels': {'a': 'value', 'b': 'value'}, } - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_cmd_caps.return_value = False strftime = Mock() strftime.strftime = Mock(return_value='current_date') @@ -219,11 +222,3 @@ class TestOCIUmoci: mock_Command_run.assert_called_once_with( ['umoci', 'gc', '--layout', 'tmpdir/oci_layout'] ) - - @patch('kiwi.oci_tools.umoci.Path') - def test_destructor(self, mock_Path): - self.oci.oci_root_dir = 'oci_root' - self.oci.__del__() - assert mock_Path.wipe.call_args_list == [ - call('oci_root'), call('tmpdir') - ] diff --git a/test/unit/partitioner/dasd_test.py b/test/unit/partitioner/dasd_test.py index 89055be1..ae0cec9d 100644 --- a/test/unit/partitioner/dasd_test.py +++ b/test/unit/partitioner/dasd_test.py @@ -15,7 +15,7 @@ class TestPartitionerDasd: self._caplog = caplog @patch('kiwi.partitioner.dasd.Command.run') - @patch('kiwi.partitioner.dasd.NamedTemporaryFile') + @patch('kiwi.partitioner.dasd.Temporary.new_file') def setup(self, mock_temp, mock_command): self.tempfile = mock.Mock() self.tempfile.name = 'tempfile' @@ -30,7 +30,7 @@ class TestPartitionerDasd: self.partitioner = PartitionerDasd(disk_provider) @patch('kiwi.partitioner.dasd.Command.run') - @patch('kiwi.partitioner.dasd.NamedTemporaryFile') + @patch('kiwi.partitioner.dasd.Temporary.new_file') def test_create(self, mock_temp, mock_command): mock_command.side_effect = Exception mock_temp.return_value = self.tempfile @@ -48,7 +48,7 @@ class TestPartitionerDasd: ) @patch('kiwi.partitioner.dasd.Command.run') - @patch('kiwi.partitioner.dasd.NamedTemporaryFile') + @patch('kiwi.partitioner.dasd.Temporary.new_file') def test_create_all_free(self, mock_temp, mock_command): mock_temp.return_value = self.tempfile diff --git a/test/unit/partitioner/msdos_test.py b/test/unit/partitioner/msdos_test.py index 115b131f..ccf238ad 100644 --- a/test/unit/partitioner/msdos_test.py +++ b/test/unit/partitioner/msdos_test.py @@ -26,7 +26,7 @@ class TestPartitionerMsDos: @patch('kiwi.partitioner.msdos.Command.run') @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag') - @patch('kiwi.partitioner.msdos.NamedTemporaryFile') + @patch('kiwi.partitioner.msdos.Temporary.new_file') def test_create(self, mock_temp, mock_flag, mock_command): mock_command.side_effect = Exception temp_type = namedtuple( @@ -55,7 +55,7 @@ class TestPartitionerMsDos: @patch('kiwi.partitioner.msdos.Command.run') @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag') - @patch('kiwi.partitioner.msdos.NamedTemporaryFile') + @patch('kiwi.partitioner.msdos.Temporary.new_file') def test_create_custom_start_sector( self, mock_temp, mock_flag, mock_command ): @@ -93,7 +93,7 @@ class TestPartitionerMsDos: @patch('kiwi.partitioner.msdos.Command.run') @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag') - @patch('kiwi.partitioner.msdos.NamedTemporaryFile') + @patch('kiwi.partitioner.msdos.Temporary.new_file') def test_create_all_free( self, mock_temp, mock_flag, mock_command ): diff --git a/test/unit/repository/apt_test.py b/test/unit/repository/apt_test.py index 46fa2da0..b743c71f 100644 --- a/test/unit/repository/apt_test.py +++ b/test/unit/repository/apt_test.py @@ -8,7 +8,7 @@ from kiwi.repository.apt import RepositoryApt class TestRepositoryApt: - @patch('kiwi.repository.apt.NamedTemporaryFile') + @patch('kiwi.repository.apt.Temporary.new_file') @patch('kiwi.repository.apt.PackageManagerTemplateAptGet') @patch('kiwi.repository.apt.Path.create') def setup(self, mock_path, mock_template, mock_temp): @@ -54,7 +54,8 @@ class TestRepositoryApt: template = mock.Mock() template.substitute.return_value = 'template-data' self.apt_conf.get_image_template.return_value = template - self.repo.use_default_location() + with patch('builtins.open', create=True): + self.repo.use_default_location() assert self.repo.shared_apt_get_dir['sources-dir'] == \ '../data/etc/apt/sources.list.d' assert self.repo.shared_apt_get_dir['preferences-dir'] == \ diff --git a/test/unit/repository/dnf_test.py b/test/unit/repository/dnf_test.py index 180c056a..db3f9949 100644 --- a/test/unit/repository/dnf_test.py +++ b/test/unit/repository/dnf_test.py @@ -12,7 +12,7 @@ class TestRepositoryDnf: def inject_fixtures(self, caplog): self._caplog = caplog - @patch('kiwi.repository.dnf.NamedTemporaryFile') + @patch('kiwi.repository.dnf.Temporary.new_file') @patch('kiwi.repository.dnf.ConfigParser') @patch('kiwi.repository.dnf.Path.create') def setup(self, mock_path, mock_config, mock_temp): @@ -50,14 +50,14 @@ class TestRepositoryDnf: call('main', 'enabled', '1') ] - @patch('kiwi.repository.dnf.NamedTemporaryFile') + @patch('kiwi.repository.dnf.Temporary.new_file') @patch('kiwi.repository.dnf.Path.create') def test_post_init_no_custom_args(self, mock_path, mock_temp): with patch('builtins.open', create=True): self.repo.post_init() assert self.repo.custom_args == [] - @patch('kiwi.repository.dnf.NamedTemporaryFile') + @patch('kiwi.repository.dnf.Temporary.new_file') @patch('kiwi.repository.dnf.Path.create') @patch('os.path.exists') def test_post_init_with_custom_args( diff --git a/test/unit/repository/pacman_test.py b/test/unit/repository/pacman_test.py index 25c38a7e..4fede859 100644 --- a/test/unit/repository/pacman_test.py +++ b/test/unit/repository/pacman_test.py @@ -9,7 +9,7 @@ from kiwi.repository.pacman import RepositoryPacman class TestRepositorPacman(object): - @patch('kiwi.repository.pacman.NamedTemporaryFile') + @patch('kiwi.repository.pacman.Temporary.new_file') @patch('kiwi.repository.pacman.ConfigParser') @patch('kiwi.repository.pacman.Path.create') def setup(self, mock_path, mock_config, mock_temp): diff --git a/test/unit/repository/zypper_test.py b/test/unit/repository/zypper_test.py index f6a4d08a..b4ca3637 100644 --- a/test/unit/repository/zypper_test.py +++ b/test/unit/repository/zypper_test.py @@ -13,7 +13,7 @@ from kiwi.exceptions import KiwiCommandError class TestRepositoryZypper: @patch('kiwi.command.Command.run') - @patch('kiwi.repository.zypper.NamedTemporaryFile') + @patch('kiwi.repository.zypper.Temporary.new_file') def setup(self, mock_temp, mock_command): self.context_manager_mock = mock.Mock() @@ -36,14 +36,14 @@ class TestRepositoryZypper: ) @patch('kiwi.command.Command.run') - @patch('kiwi.repository.zypper.NamedTemporaryFile') + @patch('kiwi.repository.zypper.Temporary.new_file') def test_custom_args_init_excludedocs(self, mock_temp, mock_command): with patch('builtins.open', create=True): repo = RepositoryZypper(self.root_bind) assert repo.custom_args == [] @patch('kiwi.command.Command.run') - @patch('kiwi.repository.zypper.NamedTemporaryFile') + @patch('kiwi.repository.zypper.Temporary.new_file') @patch('kiwi.repository.zypper.ConfigParser') def test_custom_args_init_check_signatures( self, mock_config, mock_temp, mock_command diff --git a/test/unit/solver/repository/base_test.py b/test/unit/solver/repository/base_test.py index 5da0f002..c178583c 100644 --- a/test/unit/solver/repository/base_test.py +++ b/test/unit/solver/repository/base_test.py @@ -1,6 +1,6 @@ import io from mock import ( - patch, call, mock_open, MagicMock + patch, call, mock_open, MagicMock, Mock ) from pytest import raises import os @@ -66,7 +66,7 @@ class TestSolverRepositoryBase: xml_data, 'repo:data[@type="primary"]/repo:location' )[0].get('href') == 'repodata/55f95a93-primary.xml.gz' - @patch('kiwi.solver.repository.base.NamedTemporaryFile') + @patch('kiwi.solver.repository.base.Temporary.new_file') @patch.object(SolverRepositoryBase, 'download_from_repository') @patch('os.path.isfile') def test__get_pacman_packages( @@ -86,7 +86,7 @@ class TestSolverRepositoryBase: 'x86_64', 'tmpfile' ) - @patch('kiwi.solver.repository.base.NamedTemporaryFile') + @patch('kiwi.solver.repository.base.Temporary.new_file') @patch.object(SolverRepositoryBase, 'download_from_repository') @patch('os.path.isfile') def test__get_deb_packages( @@ -111,7 +111,7 @@ class TestSolverRepositoryBase: 'Packages.gz', 'download_dir/Packages.gz' ) - @patch('kiwi.solver.repository.base.NamedTemporaryFile') + @patch('kiwi.solver.repository.base.Temporary.new_file') @patch.object(SolverRepositoryBase, 'download_from_repository') @patch('lxml.etree.parse') def test__get_repomd_xml(self, mock_parse, mock_download, mock_tmpfile): @@ -126,12 +126,13 @@ class TestSolverRepositoryBase: ) mock_parse.assert_called_once_with('tmpfile') - @patch('kiwi.solver.repository.base.mkdtemp') - def test__create_temporary_metadata_dir(self, mock_mkdtemp): - mock_mkdtemp.return_value = 'tmpdir' + @patch('kiwi.solver.repository.base.Temporary') + def test__create_temporary_metadata_dir(self, mock_Temporary): self.solver._create_temporary_metadata_dir() - assert self.solver.repository_metadata_dirs == ['tmpdir'] - mock_mkdtemp.assert_called_once_with(prefix='metadata_dir.') + assert self.solver.repository_metadata_dirs == [ + mock_Temporary.return_value.new_dir.return_value + ] + mock_Temporary.assert_called_once_with(prefix='kiwi_metadata_dir.') @patch('os.path.exists') def test_is_uptodate_static_time(self, mock_exists): @@ -263,15 +264,15 @@ class TestSolverRepositoryBase: with raises(KiwiUriOpenError): self.solver.download_from_repository('repodata/file', 'target-file') - @patch('kiwi.solver.repository.base.mkdtemp') + @patch('kiwi.solver.repository.base.Temporary') @patch('kiwi.solver.repository.base.random.randrange') @patch('kiwi.solver.repository.base.Command.run') def test__create_solvables_rpms2_solv( - self, mock_command, mock_rand, mock_mkdtemp + self, mock_command, mock_rand, mock_Temporary ): mock_rand.return_value = 0xfe self.solver.repository_metadata_dirs = ['metadata_dir.XXXX'] - mock_mkdtemp.return_value = 'solv_dir.XX' + mock_Temporary.return_value.new_dir.return_value.name = 'solv_dir.XX' self.solver._create_solvables('meta_dir.XX', 'rpms2solv') mock_command.assert_called_once_with( [ @@ -280,17 +281,17 @@ class TestSolverRepositoryBase: ] ) - @patch('kiwi.solver.repository.base.mkdtemp') + @patch('kiwi.solver.repository.base.Temporary') @patch('kiwi.solver.repository.base.random.randrange') @patch('kiwi.solver.repository.base.Command.run') @patch('kiwi.solver.repository.base.glob.iglob') def test__create_solvables_rpmmd2_solv( - self, mock_glob, mock_command, mock_rand, mock_mkdtemp + self, mock_glob, mock_command, mock_rand, mock_Temporary ): mock_glob.return_value = ['some-solv-data-file'] mock_rand.return_value = 0xfe self.solver.repository_metadata_dirs = ['metadata_dir.XXXX'] - mock_mkdtemp.return_value = 'solv_dir.XX' + mock_Temporary.return_value.new_dir.return_value.name = 'solv_dir.XX' self.solver._create_solvables('meta_dir.XX', 'rpmmd2solv') mock_glob.assert_called_once_with('meta_dir.XX/*') mock_command.assert_called_once_with( @@ -304,17 +305,17 @@ class TestSolverRepositoryBase: ] ) - @patch('kiwi.solver.repository.base.mkdtemp') + @patch('kiwi.solver.repository.base.Temporary') @patch('kiwi.solver.repository.base.random.randrange') @patch('kiwi.solver.repository.base.Command.run') @patch('kiwi.solver.repository.base.glob.iglob') def test__create_solvables_deb2_solv( - self, mock_glob, mock_command, mock_rand, mock_mkdtemp + self, mock_glob, mock_command, mock_rand, mock_Temporary ): mock_glob.return_value = ['some-solv-data-file'] mock_rand.return_value = 0xfe self.solver.repository_metadata_dirs = ['metadata_dir.XXXX'] - mock_mkdtemp.return_value = 'solv_dir.XX' + mock_Temporary.return_value.new_dir.return_value.name = 'solv_dir.XX' self.solver._create_solvables('meta_dir.XX', 'deb2solv') mock_glob.assert_called_once_with('meta_dir.XX/*') mock_command.assert_called_once_with( @@ -338,7 +339,9 @@ class TestSolverRepositoryBase: mock_path_create, mock_path_wipe, mock_command ): mock_is_uptodate.return_value = False - self.solver.repository_solvable_dir = 'solvable_dir.XX' + tempdir = Mock() + tempdir.name = 'solvable_dir.XX' + self.solver.repository_solvable_dir = tempdir self.uri.alias.return_value = 'repo-alias' self.uri.uri = 'repo-uri' @@ -363,12 +366,3 @@ class TestSolverRepositoryBase: call(''.join(['repo-uri', os.linesep])), call('static') ] - - @patch('kiwi.solver.repository.base.Path.wipe') - def test_destructor(self, mock_wipe): - self.solver.repository_metadata_dirs = ['meta_dir.XX'] - self.solver.repository_solvable_dir = 'solv_dir.XX' - self.solver.__del__() - assert mock_wipe.call_args_list == [ - call('meta_dir.XX'), call('solv_dir.XX') - ] diff --git a/test/unit/storage/disk_test.py b/test/unit/storage/disk_test.py index 9b75b831..d3927210 100644 --- a/test/unit/storage/disk_test.py +++ b/test/unit/storage/disk_test.py @@ -195,7 +195,7 @@ class TestDisk: ) @patch('kiwi.storage.disk.Command.run') - @patch('kiwi.storage.disk.NamedTemporaryFile') + @patch('kiwi.storage.disk.Temporary.new_file') def test_wipe_dasd(self, mock_temp, mock_command): mock_command.side_effect = Exception self.disk.table_type = 'dasd' diff --git a/test/unit/storage/luks_device_test.py b/test/unit/storage/luks_device_test.py index a8810ea6..c3039345 100644 --- a/test/unit/storage/luks_device_test.py +++ b/test/unit/storage/luks_device_test.py @@ -85,7 +85,7 @@ class TestLuksDevice: self.luks.luks_device = None @patch('kiwi.storage.luks_device.Command.run') - @patch('kiwi.storage.luks_device.NamedTemporaryFile') + @patch('kiwi.storage.luks_device.Temporary.new_file') @patch('os.chmod') def test_create_crypto_luks( self, mock_os_chmod, mock_tmpfile, mock_command diff --git a/test/unit/storage/subformat/gce_test.py b/test/unit/storage/subformat/gce_test.py index d193459a..a035fc46 100644 --- a/test/unit/storage/subformat/gce_test.py +++ b/test/unit/storage/subformat/gce_test.py @@ -44,11 +44,11 @@ class TestDiskFormatGce: @patch('kiwi.storage.subformat.gce.Command.run') @patch('kiwi.storage.subformat.gce.ArchiveTar') - @patch('kiwi.storage.subformat.gce.mkdtemp') + @patch('kiwi.storage.subformat.gce.Temporary') def test_create_image_format( - self, mock_mkdtemp, mock_archive, mock_command + self, mock_Temporary, mock_archive, mock_command ): - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' archive = mock.Mock() mock_archive.return_value = archive self.disk_format.tag = 'gce-license' diff --git a/test/unit/storage/subformat/qcow2_test.py b/test/unit/storage/subformat/qcow2_test.py index 97a7a9f9..623aef8c 100644 --- a/test/unit/storage/subformat/qcow2_test.py +++ b/test/unit/storage/subformat/qcow2_test.py @@ -34,11 +34,11 @@ class TestDiskFormatQcow2: assert self.disk_format.options == ['-o', 'option=value'] @patch('kiwi.storage.subformat.qcow2.Command.run') - @patch('kiwi.storage.subformat.qcow2.NamedTemporaryFile') - def test_create_image_format(self, mock_NamedTemporaryFile, mock_command): + @patch('kiwi.storage.subformat.qcow2.Temporary.new_file') + def test_create_image_format(self, mock_Temporary_new_file, mock_command): tmpfile = Mock() tmpfile.name = 'tmpfile' - mock_NamedTemporaryFile.return_value = tmpfile + mock_Temporary_new_file.return_value = tmpfile self.disk_format.create_image_format() assert mock_command.call_args_list == [ call( diff --git a/test/unit/storage/subformat/vagrant_base_test.py b/test/unit/storage/subformat/vagrant_base_test.py index f9f2d589..d5ef1061 100644 --- a/test/unit/storage/subformat/vagrant_base_test.py +++ b/test/unit/storage/subformat/vagrant_base_test.py @@ -69,10 +69,10 @@ class TestDiskFormatVagrantBase: self.disk_format.store_to_result(Mock()) @patch('kiwi.storage.subformat.vagrant_base.Command.run') - @patch('kiwi.storage.subformat.vagrant_base.mkdtemp') + @patch('kiwi.storage.subformat.vagrant_base.Temporary') @patch.object(DiskFormatVagrantBase, 'create_box_img') def test_create_image_format( - self, mock_create_box_img, mock_mkdtemp, mock_command + self, mock_create_box_img, mock_Temporary, mock_command ): # select an example provider self.disk_format.image_format = 'vagrant.libvirt.box' @@ -89,7 +89,7 @@ class TestDiskFormatVagrantBase: end ''').strip() - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' with patch('builtins.open', create=True) as mock_file: mock_file.return_value = MagicMock(spec=io.IOBase) @@ -114,17 +114,17 @@ class TestDiskFormatVagrantBase: ) @patch('kiwi.storage.subformat.vagrant_base.Command.run') - @patch('kiwi.storage.subformat.vagrant_base.mkdtemp') + @patch('kiwi.storage.subformat.vagrant_base.Temporary') @patch.object(DiskFormatVagrantBase, 'create_box_img') def test_user_provided_vagrantfile( - self, mock_create_box_img, mock_mkdtemp, mock_cmd_run + self, mock_create_box_img, mock_Temporary, mock_cmd_run ): # select an example provider self.disk_format.image_format = 'vagrant.virtualbox.box' self.disk_format.provider = 'virtualbox' # deterministic tempdir: - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.vagrantconfig.get_embedded_vagrantfile = Mock( return_value='example_Vagrantfile' diff --git a/test/unit/storage/subformat/vagrant_libvirt_test.py b/test/unit/storage/subformat/vagrant_libvirt_test.py index 2b535c5f..245aef70 100644 --- a/test/unit/storage/subformat/vagrant_libvirt_test.py +++ b/test/unit/storage/subformat/vagrant_libvirt_test.py @@ -74,12 +74,12 @@ class TestDiskFormatVagrantLibVirt: ''').strip() @patch('kiwi.storage.subformat.vagrant_base.Command.run') - @patch('kiwi.storage.subformat.vagrant_base.mkdtemp') + @patch('kiwi.storage.subformat.vagrant_base.Temporary') @patch.object(DiskFormatVagrantLibVirt, 'create_box_img') def test_create_image_format( - self, mock_create_box_img, mock_mkdtemp, mock_command + self, mock_create_box_img, mock_Temporary, mock_command ): - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_create_box_img.return_value = ['arbitrary'] m_open = mock_open() diff --git a/test/unit/storage/subformat/vagrant_virtualbox_test.py b/test/unit/storage/subformat/vagrant_virtualbox_test.py index 4cc213fb..4f960de6 100644 --- a/test/unit/storage/subformat/vagrant_virtualbox_test.py +++ b/test/unit/storage/subformat/vagrant_virtualbox_test.py @@ -94,14 +94,14 @@ class TestDiskFormatVagrantVirtualBox: expected_res @patch('kiwi.storage.subformat.vagrant_base.Command.run') - @patch('kiwi.storage.subformat.vagrant_base.mkdtemp') + @patch('kiwi.storage.subformat.vagrant_base.Temporary') @patch('kiwi.storage.subformat.vagrant_virtualbox.random.randrange') @patch.object(DiskFormatVagrantVirtualBox, 'create_box_img') def test_create_image_format_with_and_without_guest_additions( self, mock_create_box_img, mock_rand, - mock_mkdtemp, mock_command + mock_Temporary, mock_command ): - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_create_box_img.return_value = ['arbitrary'] # without guest additions diff --git a/test/unit/system/profile_test.py b/test/unit/system/profile_test.py index 48e25263..e08e2732 100644 --- a/test/unit/system/profile_test.py +++ b/test/unit/system/profile_test.py @@ -1,7 +1,6 @@ # vim: set fileencoding=utf-8 from mock import patch -import mock import os from kiwi.system.profile import Profile @@ -11,21 +10,16 @@ from kiwi.xml_description import XMLDescription class TestProfile: def setup(self): - self.tmpfile = mock.Mock() - self.tmpfile.name = 'tmpfile' self.profile_file = 'tmpfile.profile' description = XMLDescription('../data/example_dot_profile_config.xml') self.profile = Profile( XMLState(description.load()) ) - @patch('kiwi.system.profile.NamedTemporaryFile') @patch('kiwi.path.Path.which') - def test_create(self, mock_which, mock_temp): + def test_create(self, mock_which): mock_which.return_value = 'cp' - mock_temp.return_value = self.tmpfile self.profile.create(self.profile_file) - os.remove(self.tmpfile.name) os.remove(self.profile_file) assert self.profile.dot_profile == { 'kiwi_Volume_1': 'usr_lib|size:1024|usr/lib', @@ -104,32 +98,26 @@ class TestProfile: 'kiwi_rootpartuuid': None } - @patch('kiwi.system.profile.NamedTemporaryFile') @patch('kiwi.path.Path.which') - def test_create_displayname_is_image_name(self, mock_which, mock_temp): + def test_create_displayname_is_image_name(self, mock_which): mock_which.return_value = 'cp' - mock_temp.return_value = self.tmpfile description = XMLDescription('../data/example_pxe_config.xml') profile = Profile( XMLState(description.load()) ) profile.create(self.profile_file) - os.remove(self.tmpfile.name) os.remove(self.profile_file) assert profile.dot_profile['kiwi_displayname'] == \ 'LimeJeOS-openSUSE-13.2' - @patch('kiwi.system.profile.NamedTemporaryFile') @patch('kiwi.path.Path.which') - def test_create_cpio(self, mock_which, mock_temp): + def test_create_cpio(self, mock_which): mock_which.return_value = 'cp' - mock_temp.return_value = self.tmpfile description = XMLDescription('../data/example_dot_profile_config.xml') profile = Profile( XMLState(description.load(), None, 'cpio') ) profile.create(self.profile_file) - os.remove(self.tmpfile.name) os.remove(self.profile_file) assert profile.dot_profile['kiwi_cpio_name'] == \ 'LimeJeOS-openSUSE-13.2' diff --git a/test/unit/system/root_init_test.py b/test/unit/system/root_init_test.py index 72be87c5..a4b73847 100644 --- a/test/unit/system/root_init_test.py +++ b/test/unit/system/root_init_test.py @@ -25,16 +25,15 @@ class TestRootInit: @patch('os.makedirs') @patch('os.chown') @patch('os.symlink') - @patch('shutil.rmtree') @patch('kiwi.system.root_init.DataSync') - @patch('kiwi.system.root_init.mkdtemp') + @patch('kiwi.system.root_init.Temporary') @patch('kiwi.system.root_init.Path.create') def test_create_raises_error( - self, mock_Path_create, mock_temp, mock_data_sync, mock_rmtree, + self, mock_Path_create, mock_Temporary, mock_data_sync, mock_symlink, mock_chwon, mock_makedirs, mock_path ): mock_path.return_value = False - mock_temp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' mock_data_sync.side_effect = Exception root = RootInit('root_dir') with raises(KiwiRootInitCreationError): @@ -47,13 +46,12 @@ class TestRootInit: @patch('os.makedev') @patch('kiwi.path.Path.create') @patch('kiwi.system.root_init.copy') - @patch('kiwi.system.root_init.rmtree') @patch('kiwi.system.root_init.DataSync') - @patch('kiwi.system.root_init.mkdtemp') + @patch('kiwi.system.root_init.Temporary') @patch('kiwi.system.root_init.Path.create') def test_create( - self, mock_Path_create, mock_temp, mock_data_sync, - mock_rmtree, mock_copy, mock_create, mock_makedev, + self, mock_Path_create, mock_Temporary, mock_data_sync, + mock_copy, mock_create, mock_makedev, mock_symlink, mock_chwon, mock_makedirs, mock_path ): @@ -71,7 +69,7 @@ class TestRootInit: mock_path.side_effect = path_exists mock_path.return_value = False - mock_temp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' root = RootInit('root_dir', True) assert root.create() is None assert mock_makedirs.call_args_list == [ @@ -104,9 +102,6 @@ class TestRootInit: data_sync.sync_data.assert_called_once_with( options=['-a', '--ignore-existing'] ) - mock_rmtree.assert_called_once_with( - 'tmpdir', ignore_errors=True - ) mock_copy.assert_called_once_with( '/.buildenv', 'root_dir' diff --git a/test/unit/system/setup_test.py b/test/unit/system/setup_test.py index 9d442e68..692b32dc 100644 --- a/test/unit/system/setup_test.py +++ b/test/unit/system/setup_test.py @@ -901,7 +901,7 @@ class TestSystemSetup: ] @patch('kiwi.command.Command.run') - @patch('kiwi.system.setup.NamedTemporaryFile') + @patch('kiwi.system.setup.Temporary.new_file') @patch('kiwi.system.setup.ArchiveTar') @patch('kiwi.system.setup.Compress') @patch('os.path.getsize') diff --git a/test/unit/tasks/image_info_test.py b/test/unit/tasks/image_info_test.py index 262f5a04..439e344a 100644 --- a/test/unit/tasks/image_info_test.py +++ b/test/unit/tasks/image_info_test.py @@ -183,7 +183,7 @@ class TestImageInfoTask: self.task.command_args['--print-xml'] = True self.task.process() tmpfile, message = mock_out.display_file.call_args.args - assert tmpfile.startswith('/tmp/xslt-') + assert tmpfile.startswith('/var/tmp/kiwi_xslt-') assert message == 'Description(XML):' @patch('kiwi.tasks.image_info.DataOutput') @@ -192,5 +192,5 @@ class TestImageInfoTask: self.task.command_args['--print-yaml'] = True self.task.process() tmpfile, message = mock_out.display_file.call_args.args - assert tmpfile.startswith('/tmp/xslt-') + assert tmpfile.startswith('/var/tmp/kiwi_xslt-') assert message == 'Description(YAML):' diff --git a/test/unit/utils/compress_test.py b/test/unit/utils/compress_test.py index f26dd71c..f7174d4a 100644 --- a/test/unit/utils/compress_test.py +++ b/test/unit/utils/compress_test.py @@ -59,7 +59,7 @@ class TestCompress: assert self.compress.compressed_filename == 'some-file.gz' @patch('kiwi.command.Command.run') - @patch('kiwi.utils.compress.NamedTemporaryFile') + @patch('kiwi.utils.compress.Temporary.new_file') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress(self, mock_format, mock_temp, mock_command): mock_format.return_value = 'xz' @@ -70,7 +70,7 @@ class TestCompress: assert self.compress.uncompressed_filename == 'some-file' @patch('kiwi.command.Command.run') - @patch('kiwi.utils.compress.NamedTemporaryFile') + @patch('kiwi.utils.compress.Temporary.new_file') @patch('kiwi.utils.compress.Compress.get_format') def test_uncompress_temporary(self, mock_format, mock_temp, mock_command): tempfile = Mock() diff --git a/test/unit/utils/output_test.py b/test/unit/utils/output_test.py index e484aee9..1167f591 100644 --- a/test/unit/utils/output_test.py +++ b/test/unit/utils/output_test.py @@ -35,7 +35,7 @@ class TestDataOutput: @patch('sys.stdout') @patch('os.system') - @patch('kiwi.utils.output.NamedTemporaryFile') + @patch('kiwi.utils.output.Temporary.new_file') def test_display_color(self, mock_temp, mock_system, mock_stdout): out_file = mock.Mock() out_file.name = 'tmpfile' diff --git a/test/unit/utils/temporary_test.py b/test/unit/utils/temporary_test.py new file mode 100644 index 00000000..87636715 --- /dev/null +++ b/test/unit/utils/temporary_test.py @@ -0,0 +1,22 @@ +from mock import patch + +from kiwi.utils.temporary import Temporary + + +class TestTemporary: + def setup(self): + self.temporary = Temporary() + + @patch('kiwi.utils.temporary.NamedTemporaryFile') + def test_new_file(self, mock_NamedTemporaryFile): + self.temporary.new_file() + mock_NamedTemporaryFile.assert_called_once_with( + dir='/var/tmp', prefix='kiwi_', delete=True + ) + + @patch('kiwi.utils.temporary.TemporaryDirectory') + def test_new_dir(self, mock_TemporaryDirectory): + self.temporary.new_dir() + mock_TemporaryDirectory.assert_called_once_with( + dir='/var/tmp', prefix='kiwi_' + ) diff --git a/test/unit/volume_manager/base_test.py b/test/unit/volume_manager/base_test.py index 12d4bf98..36586599 100644 --- a/test/unit/volume_manager/base_test.py +++ b/test/unit/volume_manager/base_test.py @@ -236,9 +236,9 @@ class TestVolumeManagerBase: ) assert self.volume_manager.get_mountpoint() == 'mountpoint' - @patch('kiwi.volume_manager.base.mkdtemp') - def test_setup_mountpoint(self, mock_mkdtemp): - mock_mkdtemp.return_value = 'tmpdir' + @patch('kiwi.volume_manager.base.Temporary') + def test_setup_mountpoint(self, mock_Temporary): + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' self.volume_manager.setup_mountpoint() assert self.volume_manager.mountpoint == 'tmpdir' @@ -259,9 +259,3 @@ class TestVolumeManagerBase: mock_command.assert_called_once_with( ['chattr', '+C', 'toplevel/etc'] ) - - @patch('kiwi.volume_manager.base.Path.wipe') - def test_cleanup_tempdirs(self, mock_Path_wipe): - self.volume_manager.temp_directories = ['tmpdir'] - self.volume_manager._cleanup_tempdirs() - mock_Path_wipe.assert_called_once_with('tmpdir') diff --git a/test/unit/volume_manager/btrfs_test.py b/test/unit/volume_manager/btrfs_test.py index e02b1293..b320944a 100644 --- a/test/unit/volume_manager/btrfs_test.py +++ b/test/unit/volume_manager/btrfs_test.py @@ -70,12 +70,12 @@ class TestVolumeManagerBtrfs: @patch('kiwi.volume_manager.btrfs.FileSystem.new') @patch('kiwi.volume_manager.btrfs.MappedDevice') @patch('kiwi.volume_manager.btrfs.MountManager') - @patch('kiwi.volume_manager.base.mkdtemp') + @patch('kiwi.volume_manager.base.Temporary') def test_setup_no_snapshot( - self, mock_mkdtemp, mock_mount, mock_mapped_device, mock_fs, + self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs, mock_command, mock_os_exists ): - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' toplevel_mount = Mock() mock_mount.return_value = toplevel_mount command_call = Mock() @@ -101,12 +101,12 @@ class TestVolumeManagerBtrfs: @patch('kiwi.volume_manager.btrfs.FileSystem.new') @patch('kiwi.volume_manager.btrfs.MappedDevice') @patch('kiwi.volume_manager.btrfs.MountManager') - @patch('kiwi.volume_manager.base.mkdtemp') + @patch('kiwi.volume_manager.base.Temporary') def test_setup_with_snapshot( - self, mock_mkdtemp, mock_mount, mock_mapped_device, mock_fs, + self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs, mock_command, mock_os_exists ): - mock_mkdtemp.return_value = 'tmpdir' + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' toplevel_mount = Mock() mock_mount.return_value = toplevel_mount command_call = Mock() @@ -143,9 +143,9 @@ class TestVolumeManagerBtrfs: @patch('kiwi.volume_manager.btrfs.FileSystem.new') @patch('kiwi.volume_manager.btrfs.MappedDevice') @patch('kiwi.volume_manager.btrfs.MountManager') - @patch('kiwi.volume_manager.base.mkdtemp') + @patch('kiwi.volume_manager.base.Temporary') def test_setup_volume_id_not_detected( - self, mock_mkdtemp, mock_mount, mock_mapped_device, mock_fs, + self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs, mock_command, mock_os_exists ): command_call = Mock() @@ -299,12 +299,13 @@ class TestVolumeManagerBtrfs: @patch('kiwi.volume_manager.btrfs.FileSystem.new') @patch('kiwi.volume_manager.btrfs.MappedDevice') @patch('kiwi.volume_manager.btrfs.MountManager') - @patch('kiwi.volume_manager.base.mkdtemp') + @patch('kiwi.volume_manager.base.Temporary') def test_remount_volumes( - self, mock_mkdtemp, mock_mount, mock_mapped_device, mock_fs, + self, mock_Temporary, mock_mount, mock_mapped_device, mock_fs, mock_command, mock_os_exists ): - mock_mkdtemp.return_value = '/tmp/kiwi_volumes.xx' + mock_Temporary.return_value.new_dir.return_value.name = \ + '/tmp/kiwi_volumes.xx' toplevel_mount = Mock() toplevel_mount.is_mounted = Mock( return_value=False diff --git a/test/unit/volume_manager/lvm_test.py b/test/unit/volume_manager/lvm_test.py index 67ca5241..08994abb 100644 --- a/test/unit/volume_manager/lvm_test.py +++ b/test/unit/volume_manager/lvm_test.py @@ -93,9 +93,9 @@ class TestVolumeManagerLVM: '/dev/lvswap' @patch('kiwi.volume_manager.lvm.Command.run') - @patch('kiwi.volume_manager.base.mkdtemp') - def test_setup(self, mock_mkdtemp, mock_command): - mock_mkdtemp.return_value = 'tmpdir' + @patch('kiwi.volume_manager.base.Temporary') + def test_setup(self, mock_Temporary, mock_command): + mock_Temporary.return_value.new_dir.return_value.name = 'tmpdir' command = Mock() # no output for commands to mock empty information for # vgs command, indicating the volume group is not in use @@ -128,8 +128,10 @@ class TestVolumeManagerLVM: self.volume_manager.volume_group = None @patch('kiwi.volume_manager.lvm.Command.run') - @patch('kiwi.volume_manager.base.mkdtemp') - def test_setup_volume_group_host_conflict(self, mock_mkdtemp, mock_command): + @patch('kiwi.volume_manager.base.Temporary') + def test_setup_volume_group_host_conflict( + self, mock_Temporary, mock_command + ): command = Mock() command.output = 'some_data_about_volume_group' mock_command.return_value = command diff --git a/test/unit/xml_description_test.py b/test/unit/xml_description_test.py index f3650f67..31213c83 100644 --- a/test/unit/xml_description_test.py +++ b/test/unit/xml_description_test.py @@ -4,7 +4,7 @@ from builtins import bytes from lxml import etree from pytest import raises from collections import namedtuple -from tempfile import NamedTemporaryFile +from kiwi.utils.temporary import Temporary from kiwi.xml_description import XMLDescription @@ -118,26 +118,26 @@ class TestSchema: self.description_from_file = XMLDescription( description='../data/example_config.xml' ) - test_xml_file = NamedTemporaryFile() + test_xml_file = Temporary().new_file() with open(test_xml_file.name, 'wb') as description: description.write(test_xml) self.description_from_data = XMLDescription(test_xml_file.name) - test_xml_extension_file = NamedTemporaryFile() + test_xml_extension_file = Temporary().new_file() with open(test_xml_extension_file.name, 'wb') as description: description.write(test_xml_extension) self.extension_description_from_data = XMLDescription( test_xml_extension_file.name ) - test_xml_extension_not_unique_file = NamedTemporaryFile() + test_xml_extension_not_unique_file = Temporary().new_file() with open(test_xml_extension_not_unique_file.name, 'wb') as description: description.write(test_xml_extension_not_unique) self.extension_multiple_toplevel_description_from_data = XMLDescription( test_xml_extension_not_unique_file.name ) - test_xml_extension_invalid_file = NamedTemporaryFile() + test_xml_extension_invalid_file = Temporary().new_file() with open(test_xml_extension_invalid_file.name, 'wb') as description: description.write(test_xml_extension_invalid) self.extension_invalid_description_from_data = XMLDescription(