def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock(return_value='some-disk-image') self.xml_state = mock.Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = mock.Mock(return_value='1.2.3') self.disk_format = DiskFormatBase(self.xml_state, 'root_dir', 'target_dir')
def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = Mock() xml_data.get_name = Mock(return_value='some-disk-image') self.xml_state = Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = Mock(return_value='1.2.3') self.runtime_config = Mock() self.runtime_config.get_bundle_compression.return_value = True kiwi.storage.subformat.base.RuntimeConfig = Mock( return_value=self.runtime_config) self.disk_format = DiskFormatBase(self.xml_state, 'root_dir', 'target_dir')
class TestDiskFormatBase(object): @patch('platform.machine') def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) self.xml_state = mock.Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = mock.Mock( return_value='1.2.3' ) self.disk_format = DiskFormatBase( self.xml_state, 'root_dir', 'target_dir' ) @raises(NotImplementedError) def test_create_image_format(self): self.disk_format.create_image_format() @raises(KiwiFormatSetupError) def test_get_target_name_for_format_invalid_format(self): self.disk_format.get_target_name_for_format('foo') def test_post_init(self): self.disk_format.post_init({'option': 'unhandled'}) assert self.disk_format.custom_args == {} def test_get_qemu_option_list(self): custom_args = { 'subformat=format': None, 'adapter_type=type': None } assert self.disk_format.get_qemu_option_list(custom_args) == [ '-o', 'adapter_type=type', '-o', 'subformat=format' ] def test_get_target_name_for_format(self): assert self.disk_format.get_target_name_for_format('vhd') == \ 'target_dir/some-disk-image.x86_64-1.2.3.vhd' @patch('kiwi.storage.subformat.base.Path.wipe') @patch('os.path.exists') def test_destructor(self, mock_exists, mock_wipe): mock_exists.return_value = True self.disk_format.temp_image_dir = 'tmpdir' self.disk_format.__del__() self.disk_format.temp_image_dir = None mock_wipe.assert_called_once_with('tmpdir')
def __new__(self, name, xml_state, root_dir, target_dir): # noqa: C901 custom_args = xml_state.get_build_type_format_options() if name == 'qcow2': return DiskFormatQcow2(xml_state, root_dir, target_dir, custom_args) elif name == 'vdi': return DiskFormatVdi(xml_state, root_dir, target_dir, custom_args) elif name == 'vhd': return DiskFormatVhd(xml_state, root_dir, target_dir, custom_args) elif name == 'vhdx': return DiskFormatVhdx(xml_state, root_dir, target_dir, custom_args) elif name == 'vhd-fixed': disk_tag = xml_state.build_type.get_vhdfixedtag() if disk_tag: custom_args.update({'--tag': disk_tag}) return DiskFormatVhdFixed(xml_state, root_dir, target_dir, custom_args) elif name == 'gce': gce_license_tag = xml_state.build_type.get_gcelicense() if gce_license_tag: custom_args.update({'--tag': gce_license_tag}) return DiskFormatGce(xml_state, root_dir, target_dir, custom_args) elif name == 'vmdk' or name == 'ova': vmdisk_section = xml_state.get_build_type_vmdisk_section() if vmdisk_section: disk_mode = vmdisk_section.get_diskmode() disk_controller = vmdisk_section.get_controller() if disk_mode: custom_args.update( {'subformat={0}'.format(disk_mode): None}) if disk_controller: custom_args.update( {'adapter_type={0}'.format(disk_controller): None}) if name == 'vmdk': return DiskFormatVmdk(xml_state, root_dir, target_dir, custom_args) else: return DiskFormatOva(xml_state, root_dir, target_dir, custom_args) elif name == 'vagrant': vagrant_config = xml_state.get_build_type_vagrant_config_section() if vagrant_config: custom_args.update({'vagrantconfig': vagrant_config}) provider = vagrant_config.get_provider() else: provider = 'undefined' if provider == 'libvirt': return DiskFormatVagrantLibVirt(xml_state, root_dir, target_dir, custom_args) else: raise KiwiDiskFormatSetupError( 'No support for {0} format with {1} provider'.format( name, provider)) elif name == 'raw': return DiskFormatBase(xml_state, root_dir, target_dir, custom_args) else: raise KiwiDiskFormatSetupError( 'No support for {0} disk format'.format(name))
def setup(self, mock_post_init): Defaults.set_platform_name('x86_64') xml_data = Mock() xml_data.get_name = Mock(return_value='some-disk-image') self.xml_state = Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = Mock(return_value='1.2.3') self.runtime_config = Mock() self.runtime_config.get_bundle_compression.return_value = True kiwi.storage.subformat.base.RuntimeConfig = Mock( return_value=self.runtime_config) DiskFormatBase(self.xml_state, 'root_dir', 'target_dir', {'option': 'unhandled'}) mock_post_init.assert_called_once_with({'option': 'unhandled'}) mock_post_init.reset_mock() self.disk_format = DiskFormatBase(self.xml_state, 'root_dir', 'target_dir') mock_post_init.assert_called_once_with({})
def __new__(self, name, xml_state, root_dir, target_dir): if name == 'qcow2': return DiskFormatQcow2(xml_state, root_dir, target_dir) elif name == 'vdi': return DiskFormatVdi(xml_state, root_dir, target_dir) elif name == 'vhd': return DiskFormatVhd(xml_state, root_dir, target_dir) elif name == 'vhd-fixed': custom_args = None disk_tag = xml_state.build_type.get_vhdfixedtag() if disk_tag: custom_args = {'--tag': disk_tag} return DiskFormatVhdFixed(xml_state, root_dir, target_dir, custom_args) elif name == 'gce': custom_args = None gce_license_tag = xml_state.build_type.get_gcelicense() if gce_license_tag: custom_args = {'--tag': gce_license_tag} return DiskFormatGce(xml_state, root_dir, target_dir, custom_args) elif name == 'vmdk': custom_args = None vmdisk_section = xml_state.get_build_type_vmdisk_section() if vmdisk_section: custom_args = {} disk_mode = vmdisk_section.get_diskmode() disk_controller = vmdisk_section.get_controller() if disk_mode: custom_args['subformat=%s' % disk_mode] = None if disk_controller: custom_args['adapter_type=%s' % disk_controller] = None return DiskFormatVmdk(xml_state, root_dir, target_dir, custom_args) elif name == 'vagrant': vagrant_config = xml_state.get_build_type_vagrant_config_section() if vagrant_config: provider = vagrant_config.get_provider() else: provider = 'undefined' if provider == 'libvirt': return DiskFormatVagrantLibVirt( xml_state, root_dir, target_dir, {'vagrantconfig': vagrant_config}) else: raise KiwiDiskFormatSetupError( 'No support for {0} format with {1} provider'.format( name, provider)) elif name == 'raw': return DiskFormatBase(xml_state, root_dir, target_dir) else: raise KiwiDiskFormatSetupError( 'No support for {0} disk format'.format(name))
def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) self.xml_state = mock.Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = mock.Mock( return_value='1.2.3' ) self.disk_format = DiskFormatBase( self.xml_state, 'root_dir', 'target_dir' )
class TestDiskFormatBase(object): @patch('platform.machine') def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock(return_value='some-disk-image') self.xml_state = mock.Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = mock.Mock(return_value='1.2.3') self.disk_format = DiskFormatBase(self.xml_state, 'root_dir', 'target_dir') @raises(NotImplementedError) def test_create_image_format(self): self.disk_format.create_image_format() @raises(KiwiFormatSetupError) def test_get_target_name_for_format_invalid_format(self): self.disk_format.get_target_name_for_format('foo') def test_post_init(self): self.disk_format.post_init({'option': 'unhandled'}) assert self.disk_format.custom_args == {} def test_get_qemu_option_list(self): custom_args = {'subformat=format': None, 'adapter_type=type': None} assert self.disk_format.get_qemu_option_list(custom_args) == [ '-o', 'adapter_type=type', '-o', 'subformat=format' ] def test_get_target_name_for_format(self): assert self.disk_format.get_target_name_for_format('vhd') == \ 'target_dir/some-disk-image.x86_64-1.2.3.vhd' def test_store_to_result(self): result = mock.Mock() self.disk_format.image_format = 'qcow2' self.disk_format.store_to_result(result) result.add.assert_called_once_with( compress=True, filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2', key='disk_format_image', shasum=True, use_for_bundle=True) @patch('kiwi.storage.subformat.base.Path.wipe') @patch('os.path.exists') def test_destructor(self, mock_exists, mock_wipe): mock_exists.return_value = True self.disk_format.temp_image_dir = 'tmpdir' self.disk_format.__del__() self.disk_format.temp_image_dir = None mock_wipe.assert_called_once_with('tmpdir')
class TestDiskFormatBase: @patch('platform.machine') def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = Mock() xml_data.get_name = Mock(return_value='some-disk-image') self.xml_state = Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = Mock(return_value='1.2.3') self.disk_format = DiskFormatBase(self.xml_state, 'root_dir', 'target_dir') def test_create_image_format(self): with raises(NotImplementedError): self.disk_format.create_image_format() def test_get_target_file_path_for_format_invalid_format(self): with raises(KiwiFormatSetupError): self.disk_format.get_target_file_path_for_format('foo') def test_post_init(self): self.disk_format.post_init({'option': 'unhandled'}) assert self.disk_format.custom_args == {} def test_get_qemu_option_list(self): custom_args = {'subformat=format': None, 'adapter_type=type': None} assert self.disk_format.get_qemu_option_list(custom_args) == [ '-o', 'adapter_type=type', '-o', 'subformat=format' ] def test_get_target_file_path_for_format(self): assert self.disk_format.get_target_file_path_for_format('vhd') == \ 'target_dir/some-disk-image.x86_64-1.2.3.vhd' def test_store_to_result(self): result = Mock() self.disk_format.image_format = 'qcow2' self.disk_format.store_to_result(result) result.add.assert_called_once_with( compress=True, filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2', key='disk_format_image', shasum=True, use_for_bundle=True) @patch('os.path.getsize') def test_resize_raw_disk_raises_on_shrink_disk(self, mock_getsize): mock_getsize.return_value = 42 with raises(KiwiResizeRawDiskError): self.disk_format.resize_raw_disk(10) @patch('os.path.getsize') @patch('kiwi.storage.subformat.base.Command.run') def test_resize_raw_disk(self, mock_command, mock_getsize): mock_getsize.return_value = 42 assert self.disk_format.resize_raw_disk(1024) is True mock_command.assert_called_once_with([ 'qemu-img', 'resize', 'target_dir/some-disk-image.x86_64-1.2.3.raw', '1024' ]) @patch('os.path.getsize') @patch('kiwi.storage.subformat.base.Command.run') def test_resize_raw_disk_append(self, mock_command, mock_getsize): mock_getsize.return_value = 42 assert self.disk_format.resize_raw_disk(1024, append=True) is True mock_command.assert_called_once_with([ 'qemu-img', 'resize', 'target_dir/some-disk-image.x86_64-1.2.3.raw', '+1024' ]) @patch('os.path.getsize') def test_resize_raw_disk_same_size(self, mock_getsize): mock_getsize.return_value = 42 assert self.disk_format.resize_raw_disk(42) is False @patch('os.path.exists') def test_has_raw_disk(self, mock_exists): mock_exists.return_value = True assert self.disk_format.has_raw_disk() is True mock_exists.assert_called_once_with( 'target_dir/some-disk-image.x86_64-1.2.3.raw') @patch('kiwi.storage.subformat.base.Path.wipe') @patch('os.path.exists') def test_destructor(self, mock_exists, mock_wipe): mock_exists.return_value = True self.disk_format.temp_image_dir = 'tmpdir' self.disk_format.__del__() self.disk_format.temp_image_dir = None mock_wipe.assert_called_once_with('tmpdir')
class TestDiskFormatBase(object): @patch('platform.machine') def setup(self, mock_machine): mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) self.xml_state = mock.Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = mock.Mock( return_value='1.2.3' ) self.disk_format = DiskFormatBase( self.xml_state, 'root_dir', 'target_dir' ) @raises(NotImplementedError) def test_create_image_format(self): self.disk_format.create_image_format() @raises(KiwiFormatSetupError) def test_get_target_file_path_for_format_invalid_format(self): self.disk_format.get_target_file_path_for_format('foo') def test_post_init(self): self.disk_format.post_init({'option': 'unhandled'}) assert self.disk_format.custom_args == {} def test_get_qemu_option_list(self): custom_args = { 'subformat=format': None, 'adapter_type=type': None } assert self.disk_format.get_qemu_option_list(custom_args) == [ '-o', 'adapter_type=type', '-o', 'subformat=format' ] def test_get_target_file_path_for_format(self): assert self.disk_format.get_target_file_path_for_format('vhd') == \ 'target_dir/some-disk-image.x86_64-1.2.3.vhd' def test_store_to_result(self): result = mock.Mock() self.disk_format.image_format = 'qcow2' self.disk_format.store_to_result(result) result.add.assert_called_once_with( compress=False, filename='target_dir/some-disk-image.x86_64-1.2.3.qcow2', key='disk_format_image', shasum=True, use_for_bundle=True ) @raises(KiwiResizeRawDiskError) @patch('os.path.getsize') def test_resize_raw_disk_raises_on_shrink_disk(self, mock_getsize): mock_getsize.return_value = 42 self.disk_format.resize_raw_disk(10) @patch('os.path.getsize') @patch('kiwi.storage.subformat.base.Command.run') def test_resize_raw_disk(self, mock_command, mock_getsize): mock_getsize.return_value = 42 assert self.disk_format.resize_raw_disk(1024) is True mock_command.assert_called_once_with( [ 'qemu-img', 'resize', 'target_dir/some-disk-image.x86_64-1.2.3.raw', '1024' ] ) @patch('os.path.getsize') def test_resize_raw_disk_same_size(self, mock_getsize): mock_getsize.return_value = 42 assert self.disk_format.resize_raw_disk(42) is False @patch('os.path.exists') def test_has_raw_disk(self, mock_exists): mock_exists.return_value = True assert self.disk_format.has_raw_disk() is True mock_exists.assert_called_once_with( 'target_dir/some-disk-image.x86_64-1.2.3.raw' ) @patch('kiwi.storage.subformat.base.Path.wipe') @patch('os.path.exists') def test_destructor(self, mock_exists, mock_wipe): mock_exists.return_value = True self.disk_format.temp_image_dir = 'tmpdir' self.disk_format.__del__() self.disk_format.temp_image_dir = None mock_wipe.assert_called_once_with('tmpdir')