def __init__(self, boxname: str, arch: str = '') -> None: self.arch = arch or platform.machine() self.box_config = BoxConfig(boxname, arch) self.box_dir = os.sep.join( [Defaults.get_local_box_cache_dir(), boxname]) self.box_stage = DirFiles(self.box_dir) self.system = '' self.kernel = '' self.initrd = '' Path.create(self.box_dir)
def __init__(self, boxname, arch=None): self.box_config = BoxConfig(boxname, arch) self.box_dir = os.sep.join( [Defaults.get_local_box_cache_dir(), boxname]) self.vm_setup_type = namedtuple( 'vm_setup_type', ['system', 'kernel', 'initrd', 'append', 'ram', 'smp']) self.box_stage = DirFiles(self.box_dir) self.system = None self.kernel = None self.initrd = None Path.create(self.box_dir)
def test_setup_raises_on_load_config(self, mock_get_plugin_config_file, mock_yaml_safe_load): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' mock_yaml_safe_load.side_effect = Exception with raises(KiwiBoxPluginConfigError): BoxConfig('suse')
def test_setup_raises_box_not_found(self, mock_get_plugin_config_file): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with raises(KiwiBoxPluginBoxNameError): self.box_config = BoxConfig('foo', 'x86_64')
def setup(self, mock_platform_machine, mock_get_plugin_config_file): mock_platform_machine.return_value = 'x86_64' mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with self._caplog.at_level(logging.INFO): self.box_config = BoxConfig('suse')
class TestBoxConfig: @fixture(autouse=True) def inject_fixtures(self, caplog): self._caplog = caplog @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') @patch('platform.machine') def setup(self, mock_platform_machine, mock_get_plugin_config_file): mock_platform_machine.return_value = 'x86_64' mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with self._caplog.at_level(logging.INFO): self.box_config = BoxConfig('suse') @patch('yaml.safe_load') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') def test_setup_raises( self, mock_get_plugin_config_file, mock_yaml_safe_load ): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' mock_yaml_safe_load.side_effect = Exception with raises(KiwiBoxPluginConfigError): BoxConfig('suse') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') def test_setup_raises_box_not_found(self, mock_get_plugin_config_file): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with raises(KiwiBoxPluginBoxNameError): self.box_config = BoxConfig('foo', 'x86_64') def test_get_box_arch(self): assert self.box_config.get_box_arch() == 'x86_64' def test_get_box_memory_mbytes(self): assert self.box_config.get_box_memory_mbytes() == 8096 def test_get_box_root(self): assert self.box_config.get_box_root() == '/dev/vda1' def test_get_box_console(self): assert self.box_config.get_box_console() == 'hvc0' def test_get_box_kernel_cmdline(self): assert self.box_config.get_box_kernel_cmdline() == 'rd.plymouth=0' def test_get_box_source(self): assert self.box_config.get_box_source() == \ 'obs://Virtualization:Appliances:SelfContained:suse/images' def test_get_box_packages_file(self): assert self.box_config.get_box_packages_file() == \ 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report' def test_get_box_packages_shasum_file(self): assert self.box_config.get_box_packages_shasum_file() == \ 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report.sha256' def test_get_box_files(self): assert self.box_config.get_box_files() == [ 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz', 'SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2' ]
class BoxDownload: """ **Implements download of box file(s)** Reads the box configuration file and provides an interface to download the box files according to the configuration :param string boxname: name of the box from kiwi_boxed_plugin.yml :param string arch: arch name for box """ def __init__(self, boxname, arch=None): self.box_config = BoxConfig(boxname, arch) self.box_dir = os.sep.join( [Defaults.get_local_box_cache_dir(), boxname]) self.vm_setup_type = namedtuple( 'vm_setup_type', ['system', 'kernel', 'initrd', 'append', 'ram', 'smp']) self.box_stage = DirFiles(self.box_dir) self.system = None self.kernel = None self.initrd = None Path.create(self.box_dir) def fetch(self, update_check=True): """ Download box from the open build service :param bool update_check: check for box updates True|False """ download = update_check repo_source = self.box_config.get_box_source() if repo_source: repo = SolverRepository.new(Uri(repo_source, 'rpm-md')) packages_file = self.box_config.get_box_packages_file() packages_shasum_file = \ self.box_config.get_box_packages_shasum_file() if update_check and packages_file and packages_shasum_file: local_packages_file = os.sep.join( [self.box_dir, packages_file]) local_packages_shasum_file = os.sep.join( [self.box_dir, packages_shasum_file]) local_packages_file_tmp = self.box_stage.register( local_packages_file) local_packages_shasum_file_tmp = self.box_stage.register( local_packages_shasum_file) repo.download_from_repository(packages_file, local_packages_file_tmp) checksum = Checksum(local_packages_file_tmp) shasum = checksum.sha256() if checksum.matches(shasum, local_packages_shasum_file): download = False else: self._create_packages_checksum( local_packages_shasum_file_tmp, shasum) for box_file in self.box_config.get_box_files(): local_box_file = os.sep.join([self.box_dir, box_file]) if not os.path.exists(local_box_file): download = True if download: log.info('Downloading {0}'.format(box_file)) local_box_file_tmp = self.box_stage.register( local_box_file) repo.download_from_repository(box_file, local_box_file_tmp) if download: self.box_stage.commit() for box_file in self.box_config.get_box_files(): local_box_file = os.sep.join([self.box_dir, box_file]) if box_file.endswith('.qcow2'): self.system = local_box_file if box_file.endswith('.tar.xz'): self.kernel = self._extract_kernel_from_tarball( local_box_file) if self.box_config.use_initrd(): self.initrd = self._extract_initrd_from_tarball( local_box_file) return self.vm_setup_type( system=self.system, kernel=self.kernel, initrd=self.initrd, append='root={0} console={1} {2}'.format( self.box_config.get_box_root(), self.box_config.get_box_console(), self.box_config.get_box_kernel_cmdline()), ram=self.box_config.get_box_memory_mbytes(), smp=self.box_config.get_box_processors()) def _create_packages_checksum(self, filename, shasum): with open(filename, 'w') as sha_file: sha_file.write(shasum) def _extract_kernel_from_tarball(self, tarfile): Command.run([ 'tar', '-C', self.box_dir, '--transform', 's/.*/kernel/', '--wildcards', '-xf', tarfile, '*.kernel' ]) return os.sep.join([self.box_dir, 'kernel']) def _extract_initrd_from_tarball(self, tarfile): Command.run([ 'tar', '-C', self.box_dir, '--transform', 's/.*/initrd/', '--wildcards', '-xf', tarfile, '*.initrd.xz' ]) return os.sep.join([self.box_dir, 'initrd'])
class TestBoxConfig: @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') @patch('platform.machine') def setup(self, mock_platform_machine, mock_get_plugin_config_file): mock_platform_machine.return_value = 'x86_64' mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' self.box_config = BoxConfig('suse') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') @patch('platform.machine') def setup_method(self, cls, mock_platform_machine, mock_get_plugin_config_file): self.setup() @patch('yaml.safe_load') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') def test_setup_raises_on_load_config(self, mock_get_plugin_config_file, mock_yaml_safe_load): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' mock_yaml_safe_load.side_effect = Exception with raises(KiwiBoxPluginConfigError): BoxConfig('suse') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') def test_setup_raises_on_unsupported_arch(self, mock_get_plugin_config_file): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with raises(KiwiBoxPluginArchNotFoundError): BoxConfig('suse', 'artificial_arch') @patch('kiwi_boxed_plugin.defaults.Defaults.get_plugin_config_file') def test_setup_raises_box_not_found(self, mock_get_plugin_config_file): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with raises(KiwiBoxPluginBoxNameError): self.box_config = BoxConfig('foo', 'x86_64') def test_get_box_arch(self): assert self.box_config.get_box_arch() == 'x86_64' def test_get_box_memory_mbytes(self): assert self.box_config.get_box_memory_mbytes() == 8096 def test_get_box_console(self): assert self.box_config.get_box_console() == 'hvc0' def test_get_box_kernel_cmdline(self): assert self.box_config.get_box_kernel_cmdline() == \ 'root=/dev/vda1 rd.plymouth=0' def test_get_box_source(self): assert self.box_config.get_box_source() == \ 'obs://Virtualization:Appliances:SelfContained:suse/images' def test_get_box_packages_file(self): assert self.box_config.get_box_packages_file() == \ 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report' def test_get_box_packages_shasum_file(self): assert self.box_config.get_box_packages_shasum_file() == \ 'SUSE-Box.x86_64-1.42.1-System-BuildBox.report.sha256' def test_get_box_files(self): assert self.box_config.get_box_files() == [ 'SUSE-Box.x86_64-1.42.1-Kernel-BuildBox.tar.xz', 'SUSE-Box.x86_64-1.42.1-System-BuildBox.qcow2' ]
def test_setup_raises_on_unsupported_arch(self, mock_get_plugin_config_file): mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' with raises(KiwiBoxPluginArchNotFoundError): BoxConfig('suse', 'artificial_arch')
def setup(self, mock_platform_machine, mock_get_plugin_config_file): mock_platform_machine.return_value = 'x86_64' mock_get_plugin_config_file.return_value = \ '../data/kiwi_boxed_plugin.yml' self.box_config = BoxConfig('suse')