def setup(self): Defaults.set_platform_name('x86_64') self.context_manager_mock = Mock() self.file_mock = Mock() self.enter_mock = Mock() self.exit_mock = Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) xml_data = Mock() xml_data.get_name = Mock(return_value='some-disk-image') xml_data.get_displayname = Mock(return_value=None) self.xml_state = Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = Mock(return_value='1.2.3') self.machine_setup = Mock() self.xml_state.get_build_type_machine_section = Mock( return_value=self.machine_setup) self.machine_setup.get_HWversion = Mock(return_value='42') self.machine_setup.get_guestOS = Mock(return_value='suse') self.machine_setup.get_memory = Mock(return_value='4096') self.machine_setup.get_ncpus = Mock(return_value='2') self.machine_setup.get_ovftype = Mock(return_value='vmware') self.runtime_config = Mock() self.runtime_config.get_bundle_compression.return_value = False kiwi.storage.subformat.base.RuntimeConfig = Mock( return_value=self.runtime_config) self.disk_format = DiskFormatOva(self.xml_state, 'root_dir', 'target_dir')
def setup(self, mock_machine): self.context_manager_mock = mock.Mock() self.file_mock = mock.Mock() self.enter_mock = mock.Mock() self.exit_mock = mock.Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock(return_value='some-disk-image') xml_data.get_displayname = mock.Mock(return_value=None) 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.machine_setup = mock.Mock() self.xml_state.get_build_type_machine_section = mock.Mock( return_value=self.machine_setup) self.machine_setup.get_HWversion = mock.Mock(return_value='42') self.machine_setup.get_guestOS = mock.Mock(return_value='suse') self.machine_setup.get_memory = mock.Mock(return_value='4096') self.machine_setup.get_ncpus = mock.Mock(return_value='2') self.machine_setup.get_ovftype = mock.Mock(return_value='vmware') self.disk_format = DiskFormatOva(self.xml_state, 'root_dir', 'target_dir')
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_machine): self.context_manager_mock = mock.Mock() self.file_mock = mock.Mock() self.enter_mock = mock.Mock() self.exit_mock = mock.Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) xml_data.get_displayname = mock.Mock( return_value=None ) 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.machine_setup = mock.Mock() self.xml_state.get_build_type_machine_section = mock.Mock( return_value=self.machine_setup ) self.machine_setup.get_HWversion = mock.Mock( return_value='42' ) self.machine_setup.get_guestOS = mock.Mock( return_value='suse' ) self.machine_setup.get_memory = mock.Mock( return_value='4096' ) self.machine_setup.get_ncpus = mock.Mock( return_value='2' ) self.machine_setup.get_ovftype = mock.Mock( return_value='vmware' ) self.disk_format = DiskFormatOva( self.xml_state, 'root_dir', 'target_dir' )
class TestDiskFormatOva(object): @patch('platform.machine') def setup(self, mock_machine): self.context_manager_mock = mock.Mock() self.file_mock = mock.Mock() self.enter_mock = mock.Mock() self.exit_mock = mock.Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) xml_data.get_displayname = mock.Mock( return_value=None ) 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.machine_setup = mock.Mock() self.xml_state.get_build_type_machine_section = mock.Mock( return_value=self.machine_setup ) self.machine_setup.get_HWversion = mock.Mock( return_value='42' ) self.machine_setup.get_guestOS = mock.Mock( return_value='suse' ) self.machine_setup.get_memory = mock.Mock( return_value='4096' ) self.machine_setup.get_ncpus = mock.Mock( return_value='2' ) self.machine_setup.get_ovftype = mock.Mock( return_value='vmware' ) self.disk_format = DiskFormatOva( self.xml_state, 'root_dir', 'target_dir' ) def test_post_init(self): self.disk_format.post_init({}) @raises(KiwiFormatSetupError) def test_post_init_bad_ovftype(self): self.machine_setup.get_ovftype.return_value = 'foobar' self.disk_format.post_init({}) def test_store_to_result(self): result = mock.Mock() self.disk_format.store_to_result(result) assert result.add.call_args_list == [ call( compress=False, filename='target_dir/some-disk-image.x86_64-1.2.3.ova', key='disk_format_image', shasum=True, use_for_bundle=True ) ] @patch('kiwi.storage.subformat.ova.Command.run') @patch('os.stat') @patch('os.chmod') @patch_open def test_create_image_format( self, mock_open, mock_chmod, mock_stat, mock_command ): qemu_img_result = mock.Mock() ovftool_help_result = mock.Mock() ovftool_help_result.output = """This is a new ovftool it knows options such as --shaAlgorithm and others enjoy""" ovftool_result = mock.Mock() command_results = [ ovftool_result, ovftool_help_result, qemu_img_result ] def side_effect(arg): return command_results.pop() mock_command.side_effect = side_effect mock_open.return_value = self.context_manager_mock mock_stat.return_value = mock.Mock(st_mode=0o644) self.disk_format.create_image_format() assert mock_command.call_args_list[-1] == call([ 'ovftool', '--shaAlgorithm=SHA1', 'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'target_dir/some-disk-image.x86_64-1.2.3.ova' ]) @patch('kiwi.storage.subformat.ova.Command.run') @patch_open @raises(KiwiCommandNotFound) def test_create_image_format_no_ovftool( self, mock_open, mock_command ): qemu_img_result = mock.Mock() ovftool_help_result = mock.Mock() ovftool_help_result.output = "" command_results = [ ovftool_help_result, qemu_img_result ] def side_effect(arg): if len(command_results) == 0: raise KiwiCommandNotFound('ovftool not found') return command_results.pop() mock_command.side_effect = side_effect mock_open.return_value = self.context_manager_mock self.disk_format.create_image_format()
class TestDiskFormatOva: def setup(self): Defaults.set_platform_name('x86_64') self.context_manager_mock = Mock() self.file_mock = Mock() self.enter_mock = Mock() self.exit_mock = Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) xml_data = Mock() xml_data.get_name = Mock(return_value='some-disk-image') xml_data.get_displayname = Mock(return_value=None) self.xml_state = Mock() self.xml_state.xml_data = xml_data self.xml_state.get_image_version = Mock(return_value='1.2.3') self.machine_setup = Mock() self.xml_state.get_build_type_machine_section = Mock( return_value=self.machine_setup) self.machine_setup.get_HWversion = Mock(return_value='42') self.machine_setup.get_guestOS = Mock(return_value='suse') self.machine_setup.get_memory = Mock(return_value='4096') self.machine_setup.get_ncpus = Mock(return_value='2') self.machine_setup.get_ovftype = Mock(return_value='vmware') self.runtime_config = Mock() self.runtime_config.get_bundle_compression.return_value = False kiwi.storage.subformat.base.RuntimeConfig = Mock( return_value=self.runtime_config) self.disk_format = DiskFormatOva(self.xml_state, 'root_dir', 'target_dir') def setup_method(self, cls): self.setup() def test_post_init(self): self.disk_format.post_init({}) def test_post_init_bad_ovftype(self): self.machine_setup.get_ovftype.return_value = 'foobar' with raises(KiwiFormatSetupError): self.disk_format.post_init({}) def test_store_to_result(self): result = Mock() self.disk_format.store_to_result(result) assert result.add.call_args_list == [ call(compress=False, filename='target_dir/some-disk-image.x86_64-1.2.3.ova', key='disk_format_image', shasum=True, use_for_bundle=True) ] @patch('kiwi.storage.subformat.ova.Path.which') @patch('kiwi.storage.subformat.ova.Command.run') @patch('kiwi.storage.subformat.vmdk.DiskFormatVmdk.create_image_format') @patch('kiwi.storage.subformat.ova.CommandCapabilities.has_option_in_help') @patch('os.stat') @patch('os.chmod') def test_create_image_format(self, mock_chmod, mock_stat, mock_has_option_in_help, mock_create_image_format, mock_command, mock_which): mock_has_option_in_help.return_value = True mock_which.return_value = 'ovftool' mock_stat.return_value = Mock(st_mode=0o644) self.disk_format.create_image_format() mock_command.assert_called_once_with([ 'ovftool', '--shaAlgorithm=SHA1', 'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'target_dir/some-disk-image.x86_64-1.2.3.ova' ]) @patch('kiwi.storage.subformat.ova.Path.which') def test_create_image_format_no_ovftool(self, mock_which): mock_which.return_value = None with raises(KiwiCommandNotFound): self.disk_format.create_image_format()
class TestDiskFormatOva(object): @patch('platform.machine') def setup(self, mock_machine): self.context_manager_mock = mock.Mock() self.file_mock = mock.Mock() self.enter_mock = mock.Mock() self.exit_mock = mock.Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock( return_value='some-disk-image' ) xml_data.get_displayname = mock.Mock( return_value=None ) 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.machine_setup = mock.Mock() self.xml_state.get_build_type_machine_section = mock.Mock( return_value=self.machine_setup ) self.machine_setup.get_HWversion = mock.Mock( return_value='42' ) self.machine_setup.get_guestOS = mock.Mock( return_value='suse' ) self.machine_setup.get_memory = mock.Mock( return_value='4096' ) self.machine_setup.get_ncpus = mock.Mock( return_value='2' ) self.machine_setup.get_ovftype = mock.Mock( return_value='vmware' ) self.disk_format = DiskFormatOva( self.xml_state, 'root_dir', 'target_dir' ) def test_post_init(self): self.disk_format.post_init({}) @raises(KiwiFormatSetupError) def test_post_init_bad_ovftype(self): self.machine_setup.get_ovftype.return_value = 'foobar' self.disk_format.post_init({}) def test_store_to_result(self): result = mock.Mock() self.disk_format.store_to_result(result) assert result.add.call_args_list == [ call( compress=False, filename='target_dir/some-disk-image.x86_64-1.2.3.ova', key='disk_format_image', shasum=True, use_for_bundle=True ) ] @patch('kiwi.storage.subformat.ova.Path.which') @patch('kiwi.storage.subformat.ova.Command.run') @patch('kiwi.storage.subformat.vmdk.DiskFormatVmdk.create_image_format') @patch('kiwi.storage.subformat.ova.CommandCapabilities.has_option_in_help') @patch('os.stat') @patch('os.chmod') def test_create_image_format( self, mock_chmod, mock_stat, mock_has_option_in_help, mock_create_image_format, mock_command, mock_which ): mock_has_option_in_help.return_value = True mock_which.return_value = 'ovftool' mock_stat.return_value = mock.Mock(st_mode=0o644) self.disk_format.create_image_format() mock_command.assert_called_once_with( [ 'ovftool', '--shaAlgorithm=SHA1', 'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'target_dir/some-disk-image.x86_64-1.2.3.ova' ] ) @patch('kiwi.storage.subformat.ova.Path.which') @raises(KiwiCommandNotFound) def test_create_image_format_no_ovftool(self, mock_which): mock_which.return_value = None self.disk_format.create_image_format()
class TestDiskFormatOva(object): @patch('platform.machine') def setup(self, mock_machine): self.context_manager_mock = mock.Mock() self.file_mock = mock.Mock() self.enter_mock = mock.Mock() self.exit_mock = mock.Mock() self.enter_mock.return_value = self.file_mock setattr(self.context_manager_mock, '__enter__', self.enter_mock) setattr(self.context_manager_mock, '__exit__', self.exit_mock) mock_machine.return_value = 'x86_64' xml_data = mock.Mock() xml_data.get_name = mock.Mock(return_value='some-disk-image') xml_data.get_displayname = mock.Mock(return_value=None) 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.machine_setup = mock.Mock() self.xml_state.get_build_type_machine_section = mock.Mock( return_value=self.machine_setup) self.machine_setup.get_HWversion = mock.Mock(return_value='42') self.machine_setup.get_guestOS = mock.Mock(return_value='suse') self.machine_setup.get_memory = mock.Mock(return_value='4096') self.machine_setup.get_ncpus = mock.Mock(return_value='2') self.machine_setup.get_ovftype = mock.Mock(return_value='vmware') self.disk_format = DiskFormatOva(self.xml_state, 'root_dir', 'target_dir') def test_post_init(self): self.disk_format.post_init({}) @raises(KiwiFormatSetupError) def test_post_init_bad_ovftype(self): self.machine_setup.get_ovftype.return_value = 'foobar' self.disk_format.post_init({}) def test_store_to_result(self): result = mock.Mock() self.disk_format.store_to_result(result) assert result.add.call_args_list == [ call(compress=False, filename='target_dir/some-disk-image.x86_64-1.2.3.ova', key='disk_format_image', shasum=True, use_for_bundle=True) ] @patch('kiwi.storage.subformat.ova.Command.run') @patch('os.stat') @patch('os.chmod') @patch_open def test_create_image_format(self, mock_open, mock_chmod, mock_stat, mock_command): qemu_img_result = mock.Mock() ovftool_help_result = mock.Mock() ovftool_help_result.output = """This is a new ovftool it knows options such as --shaAlgorithm and others enjoy""" ovftool_result = mock.Mock() command_results = [ ovftool_result, ovftool_help_result, qemu_img_result ] def side_effect(arg): return command_results.pop() mock_command.side_effect = side_effect mock_open.return_value = self.context_manager_mock mock_stat.return_value = mock.Mock(st_mode=0o644) self.disk_format.create_image_format() assert mock_command.call_args_list[-1] == call([ 'ovftool', '--shaAlgorithm=SHA1', 'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'target_dir/some-disk-image.x86_64-1.2.3.ova' ]) @patch('kiwi.storage.subformat.ova.Command.run') @patch_open @raises(KiwiCommandNotFound) def test_create_image_format_no_ovftool(self, mock_open, mock_command): qemu_img_result = mock.Mock() ovftool_help_result = mock.Mock() ovftool_help_result.output = "" command_results = [ovftool_help_result, qemu_img_result] def side_effect(arg): if len(command_results) == 0: raise KiwiCommandNotFound('ovftool not found') return command_results.pop() mock_command.side_effect = side_effect mock_open.return_value = self.context_manager_mock self.disk_format.create_image_format()