Ejemplo n.º 1
0
    def create_box_img(self, temp_image_dir):
        """
        Create the virtual machine image for the Virtualbox vagrant provider.

        This function creates the vmdk disk image and the ovf file. The latter
        is created via the class
        :class:`kiwi.storage.subformat.template.virtualbox_ovf.VirtualboxOvfTemplate`.
        """
        vmdk = DiskFormatVmdk(self.xml_state, self.root_dir, self.target_dir)
        vmdk.create_image_format()
        box_img = os.sep.join([temp_image_dir, 'box.vmdk'])
        Command.run(
            [
                'mv', self.get_target_file_path_for_format(vmdk.image_format),
                box_img
            ]
        )
        box_ovf = os.sep.join([temp_image_dir, 'box.ovf'])
        ovf_template = VirtualboxOvfTemplate()
        disk_image_capacity = self.vagrantconfig.get_virtualsize() or 42
        xml_description_specification = self.xml_state \
            .get_description_section().specification
        with open(box_ovf, "w") as ovf_file:
            ovf_file.write(
                ovf_template.get_template().substitute({
                    'vm_name': self.xml_state.xml_data.name,
                    'disk_image_capacity': disk_image_capacity,
                    'vm_description': xml_description_specification
                })
            )
        return [box_img, box_ovf]
Ejemplo n.º 2
0
    def setup(self, mock_machine):
        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)
        mock_machine.return_value = 'x86_64'
        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.xml_state.get_build_type_vmconfig_entries = Mock(
            return_value=['custom entry 1', 'custom entry 2'])

        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.iso_setup = Mock()
        self.xml_state.get_build_type_vmdvd_section = Mock(
            return_value=self.iso_setup)
        self.iso_setup.get_controller = Mock(return_value='ide')
        self.iso_setup.get_id = Mock(return_value='0')

        self.network_setup = [Mock()]
        self.xml_state.get_build_type_vmnic_entries = Mock(
            return_value=self.network_setup)
        self.network_setup[0].get_interface = Mock(return_value='0')
        self.network_setup[0].get_mac = Mock(return_value='98:90:96:a0:3c:58')
        self.network_setup[0].get_mode = Mock(return_value='bridged')
        self.network_setup[0].get_driver = Mock(return_value='e1000')

        self.disk_setup = Mock()
        self.xml_state.get_build_type_vmdisk_section = Mock(
            return_value=self.disk_setup)
        self.disk_setup.get_controller = Mock(return_value='lsilogic')
        self.disk_setup.get_id = Mock(return_value='0')
        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 = DiskFormatVmdk(self.xml_state, 'root_dir',
                                          'target_dir')
Ejemplo n.º 3
0
    def post_init(self, custom_args):
        """
        vmdk disk format post initialization method

        Store qemu options as list from custom args dict

        :param dict custom_args: custom qemu arguments dictionary
        """
        ovftype = self.xml_state.get_build_type_machine_section().get_ovftype()
        if ovftype != 'vmware':
            raise KiwiFormatSetupError('Unsupported ovftype %s' % ovftype)
        self.image_format = 'ova'
        self.options = self.get_qemu_option_list(custom_args)
        self.vmdk = DiskFormatVmdk(self.xml_state, self.root_dir,
                                   self.target_dir, custom_args)
Ejemplo n.º 4
0
 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))
Ejemplo n.º 5
0
    def create_box_img(self, temp_image_dir: str) -> List[str]:
        """
        Create the vmdk image for the Virtualbox vagrant provider.

        This function creates the vmdk disk image and the ovf file.
        The latter is created via the class :class:`VirtualboxOvfTemplate`.

        :param str temp_image_dir:
            Path to the temporary directory used to build the box image

        :return:
            A list of files relevant for the virtualbox box to be
            included in the vagrant box

        :rtype: list
        """
        vmdk = DiskFormatVmdk(self.xml_state, self.root_dir, self.target_dir)
        vmdk.create_image_format()
        box_img = os.sep.join([temp_image_dir, 'box.vmdk'])
        Command.run([
            'mv',
            self.get_target_file_path_for_format(vmdk.image_format), box_img
        ])
        box_ovf = os.sep.join([temp_image_dir, 'box.ovf'])
        ovf_template = VirtualboxOvfTemplate()
        disk_image_capacity = self.vagrantconfig.get_virtualsize() or 42
        xml_description_specification = self.xml_state \
            .get_description_section().specification
        with open(box_ovf, "w") as ovf_file:
            ovf_file.write(ovf_template.get_template().substitute({
                'root_uuid':
                self.xml_state.get_root_filesystem_uuid(),
                'vm_name':
                self.xml_state.xml_data.name,
                'disk_image_capacity':
                disk_image_capacity,
                'vm_description':
                xml_description_specification
            }))
        return [box_img, box_ovf]
Ejemplo n.º 6
0
 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))
Ejemplo n.º 7
0
 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 = DiskFormatVmdk(
         self.xml_state, 'root_dir', 'target_dir'
     )
Ejemplo n.º 8
0
class TestDiskFormatVmdk:
    @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.xml_state.get_build_type_vmconfig_entries = mock.Mock(
            return_value=['custom entry 1', 'custom entry 2'])

        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup)
        self.iso_setup.get_controller = mock.Mock(return_value='ide')
        self.iso_setup.get_id = mock.Mock(return_value='0')

        self.network_setup = [mock.Mock()]
        self.xml_state.get_build_type_vmnic_entries = mock.Mock(
            return_value=self.network_setup)
        self.network_setup[0].get_interface = mock.Mock(return_value='0')
        self.network_setup[0].get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58')
        self.network_setup[0].get_mode = mock.Mock(return_value='bridged')
        self.network_setup[0].get_driver = mock.Mock(return_value='e1000')

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup)
        self.disk_setup.get_controller = mock.Mock(return_value='lsilogic')
        self.disk_setup.get_id = mock.Mock(return_value='0')

        self.disk_format = DiskFormatVmdk(self.xml_state, 'root_dir',
                                          'target_dir')

    def test_post_init(self):
        self.disk_format.post_init({
            'option': 'value',
            'adapter_type=pvscsi': None
        })
        assert self.disk_format.options == [
            '-o', 'adapter_type=lsilogic', '-o', 'option=value'
        ]
        assert self.disk_format.patch_header_for_pvscsi is True

    def test_store_to_result(self):
        result = mock.Mock()
        self.disk_format.store_to_result(result)
        assert result.add.call_args_list == [
            call(compress=True,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmdk',
                 key='disk_format_image',
                 shasum=True,
                 use_for_bundle=True),
            call(compress=False,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmx',
                 key='disk_format_machine_settings',
                 shasum=False,
                 use_for_bundle=True)
        ]

    @raises(KiwiTemplateError)
    @patch('kiwi.storage.subformat.vmdk.VmwareSettingsTemplate.get_template')
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_template_error(self, mock_open, mock_exists,
                                                mock_command,
                                                mock_get_template):
        template = mock.Mock()
        mock_get_template.return_value = template
        template.substitute.side_effect = Exception
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format(self, mock_open, mock_exists, mock_command):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_encoding_exists(self, mock_open, mock_exists,
                                                 mock_command):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]
        assert self.file_mock.write.call_args_list[2] == call(
            'custom entry 2' + os.linesep)

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_pvscsi_adapter(self, mock_open, mock_exists,
                                                mock_command):
        self.disk_format.patch_header_for_pvscsi = True
        mock_open.return_value = self.context_manager_mock
        self.file_mock.read.return_value = b'ddb.adapterType = "lsilogic"'

        self.disk_format.create_image_format()

        assert mock_open.call_args_list[0:2] == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'rb'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'r+b')
        ]
        assert self.file_mock.seek.called_once_with(512, 0)
        assert self.file_mock.read.called_once_with(1024)
        assert self.file_mock.write.call_args_list[0] == call(
            b'ddb.adapterType = "pvscsi"')
Ejemplo n.º 9
0
class TestDiskFormatVmdk:
    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.xml_state.get_build_type_vmconfig_entries = Mock(
            return_value=['custom entry 1', 'custom entry 2'])

        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.iso_setup = Mock()
        self.xml_state.get_build_type_vmdvd_section = Mock(
            return_value=self.iso_setup)
        self.iso_setup.get_controller = Mock(return_value='ide')
        self.iso_setup.get_id = Mock(return_value='0')

        self.network_setup = [Mock()]
        self.xml_state.get_build_type_vmnic_entries = Mock(
            return_value=self.network_setup)
        self.network_setup[0].get_interface = Mock(return_value='0')
        self.network_setup[0].get_mac = Mock(return_value='98:90:96:a0:3c:58')
        self.network_setup[0].get_mode = Mock(return_value='bridged')
        self.network_setup[0].get_driver = Mock(return_value='e1000')

        self.disk_setup = Mock()
        self.xml_state.get_build_type_vmdisk_section = Mock(
            return_value=self.disk_setup)
        self.disk_setup.get_controller = Mock(return_value='lsilogic')
        self.disk_setup.get_id = Mock(return_value='0')
        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 = DiskFormatVmdk(self.xml_state, 'root_dir',
                                          'target_dir')

    def setup_method(self, cls):
        self.setup()

    def test_post_init(self):
        self.disk_format.post_init({
            'option': 'value',
            'adapter_type=pvscsi': None
        })
        assert self.disk_format.options == [
            '-o', 'adapter_type=lsilogic', '-o', 'option=value'
        ]

    def test_store_to_result(self):
        result = Mock()
        self.disk_format.store_to_result(result)
        assert result.add.call_args_list == [
            call(compress=True,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmdk',
                 key='disk_format_image',
                 shasum=True,
                 use_for_bundle=True),
            call(compress=False,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmx',
                 key='disk_format_machine_settings',
                 shasum=False,
                 use_for_bundle=True)
        ]

    @patch('kiwi.storage.subformat.vmdk.VmwareSettingsTemplate.get_template')
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format_template_error(self, mock_exists,
                                                mock_command,
                                                mock_get_template):
        template = Mock()
        mock_get_template.return_value = template
        template.substitute.side_effect = Exception
        with patch('builtins.open'):
            with raises(KiwiTemplateError):
                self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format(self, mock_exists, mock_command):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            self.disk_format.create_image_format()

        m_open.assert_called_once_with(
            'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format_encoding_exists(self, mock_exists,
                                                 mock_command):
        mock_exists.return_value = True

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            self.disk_format.create_image_format()

        m_open.assert_called_once_with(
            'target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        assert m_open.return_value.write.call_args_list[2] == call(
            'custom entry 2' + os.linesep)
Ejemplo n.º 10
0
class TestDiskFormatVmdk(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 = DiskFormatVmdk(
            self.xml_state, 'root_dir', 'target_dir'
        )

    def test_post_init(self):
        self.disk_format.post_init({'option': 'value'})
        assert self.disk_format.options == ['-o', 'option', 'value']

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.logger.log.warning')
    def test_create_image_format_skip_descriptor_update(
        self, mock_log_warn, mock_exists, mock_command
    ):
        mock_exists.return_value = False
        self.disk_format.create_image_format()
        mock_command.assert_called_once_with(
            [
                'qemu-img', 'convert', '-c', '-f', 'raw',
                'target_dir/some-disk-image.x86_64-1.2.3.raw', '-O', 'vmdk',
                'target_dir/some-disk-image.x86_64-1.2.3.vmdk'
            ]
        )
        assert mock_log_warn.called

    @raises(KiwiVmdkToolsError)
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format_invalid_tools_version(
        self, mock_exists, mock_command
    ):
        command = mock.Mock()
        command.output = 'bogus-version'
        mock_command.return_value = command
        mock_exists.return_value = True
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch('builtins.open')
    def test_create_image_format(
        self, mock_open, mock_exists, mock_command
    ):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'dd-out'

        command_results = [
            dd_result, vmtoolsd_result, qemu_img_result
        ]

        def side_effect(arg):
            return command_results.pop()

        mock_command.side_effect = side_effect
        context_manager_mock = mock.Mock()
        mock_open.return_value = context_manager_mock
        file_mock = mock.Mock()
        enter_mock = mock.Mock()
        exit_mock = mock.Mock()
        enter_mock.return_value = file_mock
        setattr(context_manager_mock, '__enter__', enter_mock)
        setattr(context_manager_mock, '__exit__', exit_mock)
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'wb')
        ]
        assert file_mock.write.call_args_list == [
            call('encoding="UTF-8"\ndd-out\nddb.toolsInstallType = "4"\nddb.toolsVersion = "9350"')
        ]
        assert file_mock.seek.call_args_list == [
            call(512, 0), call(0, 2)
        ]
Ejemplo n.º 11
0
    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.xml_state.get_build_type_vmconfig_entries = mock.Mock(
            return_value=['custom entry 1', 'custom entry 2']
        )

        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup
        )
        self.iso_setup.get_controller = mock.Mock(
            return_value='ide'
        )
        self.iso_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.network_setup = [mock.Mock()]
        self.xml_state.get_build_type_vmnic_entries = mock.Mock(
            return_value=self.network_setup
        )
        self.network_setup[0].get_interface = mock.Mock(
            return_value='0'
        )
        self.network_setup[0].get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58'
        )
        self.network_setup[0].get_mode = mock.Mock(
            return_value='bridged'
        )
        self.network_setup[0].get_driver = mock.Mock(
            return_value='e1000'
        )

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup
        )
        self.disk_setup.get_controller = mock.Mock(
            return_value='lsilogic'
        )
        self.disk_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.disk_format = DiskFormatVmdk(
            self.xml_state, 'root_dir', 'target_dir'
        )
Ejemplo n.º 12
0
    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')
        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup)
        self.iso_setup.get_controller = mock.Mock(return_value='ide')
        self.iso_setup.get_id = mock.Mock(return_value='0')

        self.network_setup = mock.Mock()
        self.xml_state.get_build_type_vmnic_section = mock.Mock(
            return_value=self.network_setup)
        self.network_setup.get_interface = mock.Mock(return_value='0')
        self.network_setup.get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58')
        self.network_setup.get_mode = mock.Mock(return_value='bridged')
        self.network_setup.get_driver = mock.Mock(return_value='e1000')

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup)
        self.disk_setup.get_controller = mock.Mock(return_value='lsilogic')
        self.disk_setup.get_id = mock.Mock(return_value='0')

        self.disk_format = DiskFormatVmdk(self.xml_state, 'root_dir',
                                          'target_dir')
        self.vmdk_header_update = dedent('''
            encoding="UTF-8"
            dd-out
            ddb.toolsInstallType = "4"
            ddb.toolsVersion = "9350"
        ''').strip()

        self.vmdk_settings_formatted = dedent('''
            #!/usr/bin/env vmware
            # kiwi generated VMware settings file
            config.version = "8"
            tools.syncTime = "true"
            uuid.action = "create"
            virtualHW.version = "42"
            displayName = "some-disk-image"
            guestOS = "suse"
            priority.grabbed = "normal"
            priority.ungrabbed = "normal"
            powerType.powerOff = "soft"
            powerType.powerOn  = "soft"
            powerType.suspend  = "soft"
            powerType.reset    = "soft"
            memsize = "4096"
            numvcpus = "2"
            scsi0.present = "true"
            scsi0.sharedBus = "none"
            scsi0.virtualDev = "lsilogic"
            scsi0:0.present = "true"
            scsi0:0.fileName = "target_dir/some-disk-image.x86_64-1.2.3.vmdk"
            scsi0:0.deviceType = "scsi-hardDisk"
            ethernet0.present = "true"
            ethernet0.allow64bitVmxnet = "true"
            ethernet0.addressType = "static"
            ethernet0.address = "98:90:96:a0:3c:58"
            ethernet0.virtualDev = "e1000"
            ethernet0.connectionType = "bridged"
            ide0:0.present = "true"
            ide0:0.deviceType = "cdrom-raw"
            ide0:0.autodetect = "true"
            ide0:0.startConnected = "true"
            usb.present = "true"
        ''').strip() + '\n'

        self.vmdk_settings = ''
        for entry in self.vmdk_settings_formatted.split('\n'):
            if entry:
                self.vmdk_settings += entry.strip() + '\n'
Ejemplo n.º 13
0
class TestDiskFormatVmdk(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')
        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup)
        self.iso_setup.get_controller = mock.Mock(return_value='ide')
        self.iso_setup.get_id = mock.Mock(return_value='0')

        self.network_setup = mock.Mock()
        self.xml_state.get_build_type_vmnic_section = mock.Mock(
            return_value=self.network_setup)
        self.network_setup.get_interface = mock.Mock(return_value='0')
        self.network_setup.get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58')
        self.network_setup.get_mode = mock.Mock(return_value='bridged')
        self.network_setup.get_driver = mock.Mock(return_value='e1000')

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup)
        self.disk_setup.get_controller = mock.Mock(return_value='lsilogic')
        self.disk_setup.get_id = mock.Mock(return_value='0')

        self.disk_format = DiskFormatVmdk(self.xml_state, 'root_dir',
                                          'target_dir')
        self.vmdk_header_update = dedent('''
            encoding="UTF-8"
            dd-out
            ddb.toolsInstallType = "4"
            ddb.toolsVersion = "9350"
        ''').strip()

        self.vmdk_settings_formatted = dedent('''
            #!/usr/bin/env vmware
            # kiwi generated VMware settings file
            config.version = "8"
            tools.syncTime = "true"
            uuid.action = "create"
            virtualHW.version = "42"
            displayName = "some-disk-image"
            guestOS = "suse"
            priority.grabbed = "normal"
            priority.ungrabbed = "normal"
            powerType.powerOff = "soft"
            powerType.powerOn  = "soft"
            powerType.suspend  = "soft"
            powerType.reset    = "soft"
            memsize = "4096"
            numvcpus = "2"
            scsi0.present = "true"
            scsi0.sharedBus = "none"
            scsi0.virtualDev = "lsilogic"
            scsi0:0.present = "true"
            scsi0:0.fileName = "target_dir/some-disk-image.x86_64-1.2.3.vmdk"
            scsi0:0.deviceType = "scsi-hardDisk"
            ethernet0.present = "true"
            ethernet0.allow64bitVmxnet = "true"
            ethernet0.addressType = "static"
            ethernet0.address = "98:90:96:a0:3c:58"
            ethernet0.virtualDev = "e1000"
            ethernet0.connectionType = "bridged"
            ide0:0.present = "true"
            ide0:0.deviceType = "cdrom-raw"
            ide0:0.autodetect = "true"
            ide0:0.startConnected = "true"
            usb.present = "true"
        ''').strip() + '\n'

        self.vmdk_settings = ''
        for entry in self.vmdk_settings_formatted.split('\n'):
            if entry:
                self.vmdk_settings += entry.strip() + '\n'

    def test_post_init(self):
        self.disk_format.post_init({'option': 'value'})
        assert self.disk_format.options == ['-o', 'option', 'value']

    def test_store_to_result(self):
        result = mock.Mock()
        self.disk_format.store_to_result(result)
        assert result.add.call_args_list == [
            call(compress=True,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmdk',
                 key='disk_format_image',
                 shasum=True,
                 use_for_bundle=True),
            call(compress=False,
                 filename='target_dir/some-disk-image.x86_64-1.2.3.vmx',
                 key='disk_format_machine_settings',
                 shasum=False,
                 use_for_bundle=True)
        ]

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.logger.log.warning')
    @patch_open
    def test_create_image_format_skip_descriptor_update(
            self, mock_open, mock_log_warn, mock_exists, mock_command):
        mock_exists.return_value = False
        self.disk_format.create_image_format()
        mock_command.assert_called_once_with([
            'qemu-img', 'convert', '-f', 'raw',
            'target_dir/some-disk-image.x86_64-1.2.3.raw', '-O', 'vmdk',
            'target_dir/some-disk-image.x86_64-1.2.3.vmdk'
        ])
        assert mock_log_warn.called

    @raises(KiwiVmdkToolsError)
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format_invalid_tools_version(self, mock_exists,
                                                       mock_command):
        command = mock.Mock()
        command.output = 'bogus-version'
        mock_command.return_value = command
        mock_exists.return_value = True
        self.disk_format.create_image_format()

    @raises(KiwiTemplateError)
    @patch('kiwi.storage.subformat.vmdk.VmwareSettingsTemplate.get_template')
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_template_error(self, mock_open, mock_exists,
                                                mock_command,
                                                mock_get_template):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'dd-out'

        command_results = [dd_result, vmtoolsd_result, qemu_img_result]

        def side_effect(arg):
            return command_results.pop()

        mock_command.side_effect = side_effect
        template = mock.Mock()
        mock_get_template.return_value = template
        template.substitute.side_effect = Exception
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format(self, mock_open, mock_exists, mock_command):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'dd-out'

        command_results = [dd_result, vmtoolsd_result, qemu_img_result]

        def side_effect(arg):
            return command_results.pop()

        mock_command.side_effect = side_effect
        context_manager_mock = mock.Mock()
        mock_open.return_value = context_manager_mock
        file_mock = mock.Mock()
        enter_mock = mock.Mock()
        exit_mock = mock.Mock()
        enter_mock.return_value = file_mock
        setattr(context_manager_mock, '__enter__', enter_mock)
        setattr(context_manager_mock, '__exit__', exit_mock)
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'wb'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]
        assert file_mock.write.call_args_list[0] == call(
            self.vmdk_header_update)
        assert file_mock.write.call_args_list[1] == call(self.vmdk_settings)
        assert file_mock.seek.call_args_list == [call(512, 0), call(0, 2)]
Ejemplo n.º 14
0
class DiskFormatOva(DiskFormatBase):
    """
    **Create ova disk format, based on vmdk**
    """
    def post_init(self, custom_args):
        """
        vmdk disk format post initialization method

        Store qemu options as list from custom args dict

        :param dict custom_args: custom qemu arguments dictionary
        """
        ovftype = self.xml_state.get_build_type_machine_section().get_ovftype()
        if ovftype != 'vmware':
            raise KiwiFormatSetupError('Unsupported ovftype %s' % ovftype)
        self.image_format = 'ova'
        self.options = self.get_qemu_option_list(custom_args)
        self.vmdk = DiskFormatVmdk(self.xml_state, self.root_dir,
                                   self.target_dir, custom_args)

    def create_image_format(self):
        """
        Create ova disk format using ovftool from
        https://www.vmware.com/support/developer/ovf
        """
        # Check for required ovftool
        ovftool = Path.which(filename='ovftool', access_mode=os.X_OK)
        if not ovftool:
            tool_not_found_message = dedent('''\n
                Required tool {0} not found in PATH on the build host

                Building OVA images requires VMware's {0} tool which
                can be installed from the following location

                https://www.vmware.com/support/developer/ovf
            ''')
            raise KiwiCommandNotFound(tool_not_found_message.format(ovftool))

        # Create the vmdk disk image and vmx config
        self.vmdk.create_image_format()

        # Convert to ova using ovftool
        vmx = self.get_target_file_path_for_format('vmx')
        ova = self.get_target_file_path_for_format('ova')
        try:
            os.unlink(ova)
        except OSError:
            pass
        ovftool_options = []
        if CommandCapabilities.has_option_in_help(ovftool,
                                                  '--shaAlgorithm',
                                                  raise_on_error=False):
            ovftool_options.append('--shaAlgorithm=SHA1')
        Command.run([ovftool] + ovftool_options + [vmx, ova])
        # ovftool ignores the umask and creates files with 0600
        # apply file permission bits set in the vmx file to the
        # ova file
        st = os.stat(vmx)
        os.chmod(ova, stat.S_IMODE(st.st_mode))

    def store_to_result(self, result):
        """
        Store the resulting ova file into the provided result instance.

        :param object result: Instance of Result
        """
        result.add(
            key='disk_format_image',
            filename=self.get_target_file_path_for_format('ova'),
            use_for_bundle=True,
            compress=self.runtime_config.get_bundle_compression(default=False),
            shasum=True)
class TestDiskFormatVmdk(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.xml_state.get_build_type_vmconfig_entries = mock.Mock(
            return_value=['custom entry 1', 'custom entry 2']
        )

        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup
        )
        self.iso_setup.get_controller = mock.Mock(
            return_value='ide'
        )
        self.iso_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.network_setup = mock.Mock()
        self.xml_state.get_build_type_vmnic_section = mock.Mock(
            return_value=self.network_setup
        )
        self.network_setup.get_interface = mock.Mock(
            return_value='0'
        )
        self.network_setup.get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58'
        )
        self.network_setup.get_mode = mock.Mock(
            return_value='bridged'
        )
        self.network_setup.get_driver = mock.Mock(
            return_value='e1000'
        )

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup
        )
        self.disk_setup.get_controller = mock.Mock(
            return_value='lsilogic'
        )
        self.disk_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.disk_format = DiskFormatVmdk(
            self.xml_state, 'root_dir', 'target_dir'
        )
        self.vmdk_header_update = dedent('''
            encoding="UTF-8"
            dd-out
            ddb.toolsInstallType = "4"
            ddb.toolsVersion = "9350"
        ''').strip()
        self.vmdk_header_update = bytes(self.vmdk_header_update, 'utf-8')

        self.vmdk_settings_formatted = dedent('''
            #!/usr/bin/env vmware
            # kiwi generated VMware settings file
            config.version = "8"
            tools.syncTime = "true"
            uuid.action = "create"
            virtualHW.version = "42"
            displayName = "some-disk-image"
            guestOS = "suse"
            priority.grabbed = "normal"
            priority.ungrabbed = "normal"
            powerType.powerOff = "soft"
            powerType.powerOn  = "soft"
            powerType.suspend  = "soft"
            powerType.reset    = "soft"
            memsize = "4096"
            numvcpus = "2"
            scsi0.present = "true"
            scsi0.sharedBus = "none"
            scsi0.virtualDev = "lsilogic"
            scsi0:0.present = "true"
            scsi0:0.fileName = "target_dir/some-disk-image.x86_64-1.2.3.vmdk"
            scsi0:0.deviceType = "scsi-hardDisk"
            ethernet0.present = "true"
            ethernet0.allow64bitVmxnet = "true"
            ethernet0.addressType = "static"
            ethernet0.address = "98:90:96:a0:3c:58"
            ethernet0.virtualDev = "e1000"
            ethernet0.connectionType = "bridged"
            ide0:0.present = "true"
            ide0:0.deviceType = "cdrom-raw"
            ide0:0.autodetect = "true"
            ide0:0.startConnected = "true"
            usb.present = "true"
        ''').strip() + '\n'

        self.vmdk_settings = ''
        for entry in self.vmdk_settings_formatted.split('\n'):
            if entry:
                self.vmdk_settings += entry.strip() + '\n'

    def test_post_init(self):
        self.disk_format.post_init({'option': 'value'})
        assert self.disk_format.options == ['-o', 'option', 'value']

    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.vmdk',
                key='disk_format_image',
                shasum=True,
                use_for_bundle=True
            ),
            call(
                compress=False,
                filename='target_dir/some-disk-image.x86_64-1.2.3.vmx',
                key='disk_format_machine_settings',
                shasum=False,
                use_for_bundle=True
            )
        ]

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.logger.log.warning')
    @patch_open
    def test_create_image_format_skip_descriptor_update(
        self, mock_open, mock_log_warn, mock_exists, mock_command
    ):
        mock_exists.return_value = False
        self.disk_format.create_image_format()
        mock_command.assert_called_once_with(
            [
                'qemu-img', 'convert', '-f', 'raw',
                'target_dir/some-disk-image.x86_64-1.2.3.raw', '-O', 'vmdk',
                'target_dir/some-disk-image.x86_64-1.2.3.vmdk'
            ]
        )
        assert mock_log_warn.called

    @raises(KiwiVmdkToolsError)
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    def test_create_image_format_invalid_tools_version(
        self, mock_exists, mock_command
    ):
        command = mock.Mock()
        command.output = 'bogus-version'
        mock_command.return_value = command
        mock_exists.return_value = True
        self.disk_format.create_image_format()

    @raises(KiwiTemplateError)
    @patch('kiwi.storage.subformat.vmdk.VmwareSettingsTemplate.get_template')
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_template_error(
        self, mock_open, mock_exists, mock_command, mock_get_template
    ):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'dd-out\0\0\0\0\0'

        command_results = [
            dd_result, vmtoolsd_result, qemu_img_result
        ]

        def side_effect(arg):
            return command_results.pop()

        mock_command.side_effect = side_effect
        template = mock.Mock()
        mock_get_template.return_value = template
        template.substitute.side_effect = Exception
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format(
        self, mock_open, mock_exists, mock_command
    ):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'dd-out\0\0\0\0\0'

        command_results = [
            dd_result, vmtoolsd_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_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'ab'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]
        assert self.file_mock.write.call_args_list[0] == call(
            self.vmdk_header_update
        )
        assert self.file_mock.write.call_args_list[1] == call(
            self.vmdk_settings
        )
        assert self.file_mock.seek.call_args_list == [
            call(512, 0), call(0, 2)
        ]

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_encoding_exists(
        self, mock_open, mock_exists, mock_command
    ):
        qemu_img_result = mock.Mock()
        vmtoolsd_result = mock.Mock()
        vmtoolsd_result.output = \
            'VMware Tools daemon, version 9.4.6.33107 (build-0815)'
        dd_result = mock.Mock()
        dd_result.output = 'encoding="UTF-8"\ndd-out\0\0\0\0\0'

        command_results = [
            dd_result, vmtoolsd_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_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmdk', 'ab'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]
        assert self.file_mock.write.call_args_list[0] == call(
            self.vmdk_header_update
        )
        assert self.file_mock.write.call_args_list[1] == call(
            self.vmdk_settings
        )
        assert self.file_mock.write.call_args_list[2] == call(
            'custom entry 1' + os.linesep
        )
        assert self.file_mock.write.call_args_list[3] == call(
            'custom entry 2' + os.linesep
        )
        assert self.file_mock.seek.call_args_list == [
            call(512, 0), call(0, 2)
        ]
    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.xml_state.get_build_type_vmconfig_entries = mock.Mock(
            return_value=['custom entry 1', 'custom entry 2']
        )

        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup
        )
        self.iso_setup.get_controller = mock.Mock(
            return_value='ide'
        )
        self.iso_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.network_setup = mock.Mock()
        self.xml_state.get_build_type_vmnic_section = mock.Mock(
            return_value=self.network_setup
        )
        self.network_setup.get_interface = mock.Mock(
            return_value='0'
        )
        self.network_setup.get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58'
        )
        self.network_setup.get_mode = mock.Mock(
            return_value='bridged'
        )
        self.network_setup.get_driver = mock.Mock(
            return_value='e1000'
        )

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup
        )
        self.disk_setup.get_controller = mock.Mock(
            return_value='lsilogic'
        )
        self.disk_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.disk_format = DiskFormatVmdk(
            self.xml_state, 'root_dir', 'target_dir'
        )
        self.vmdk_header_update = dedent('''
            encoding="UTF-8"
            dd-out
            ddb.toolsInstallType = "4"
            ddb.toolsVersion = "9350"
        ''').strip()
        self.vmdk_header_update = bytes(self.vmdk_header_update, 'utf-8')

        self.vmdk_settings_formatted = dedent('''
            #!/usr/bin/env vmware
            # kiwi generated VMware settings file
            config.version = "8"
            tools.syncTime = "true"
            uuid.action = "create"
            virtualHW.version = "42"
            displayName = "some-disk-image"
            guestOS = "suse"
            priority.grabbed = "normal"
            priority.ungrabbed = "normal"
            powerType.powerOff = "soft"
            powerType.powerOn  = "soft"
            powerType.suspend  = "soft"
            powerType.reset    = "soft"
            memsize = "4096"
            numvcpus = "2"
            scsi0.present = "true"
            scsi0.sharedBus = "none"
            scsi0.virtualDev = "lsilogic"
            scsi0:0.present = "true"
            scsi0:0.fileName = "target_dir/some-disk-image.x86_64-1.2.3.vmdk"
            scsi0:0.deviceType = "scsi-hardDisk"
            ethernet0.present = "true"
            ethernet0.allow64bitVmxnet = "true"
            ethernet0.addressType = "static"
            ethernet0.address = "98:90:96:a0:3c:58"
            ethernet0.virtualDev = "e1000"
            ethernet0.connectionType = "bridged"
            ide0:0.present = "true"
            ide0:0.deviceType = "cdrom-raw"
            ide0:0.autodetect = "true"
            ide0:0.startConnected = "true"
            usb.present = "true"
        ''').strip() + '\n'

        self.vmdk_settings = ''
        for entry in self.vmdk_settings_formatted.split('\n'):
            if entry:
                self.vmdk_settings += entry.strip() + '\n'
Ejemplo n.º 17
0
class TestDiskFormatVmdk(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.xml_state.get_build_type_vmconfig_entries = mock.Mock(
            return_value=['custom entry 1', 'custom entry 2']
        )

        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.iso_setup = mock.Mock()
        self.xml_state.get_build_type_vmdvd_section = mock.Mock(
            return_value=self.iso_setup
        )
        self.iso_setup.get_controller = mock.Mock(
            return_value='ide'
        )
        self.iso_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.network_setup = [mock.Mock()]
        self.xml_state.get_build_type_vmnic_entries = mock.Mock(
            return_value=self.network_setup
        )
        self.network_setup[0].get_interface = mock.Mock(
            return_value='0'
        )
        self.network_setup[0].get_mac = mock.Mock(
            return_value='98:90:96:a0:3c:58'
        )
        self.network_setup[0].get_mode = mock.Mock(
            return_value='bridged'
        )
        self.network_setup[0].get_driver = mock.Mock(
            return_value='e1000'
        )

        self.disk_setup = mock.Mock()
        self.xml_state.get_build_type_vmdisk_section = mock.Mock(
            return_value=self.disk_setup
        )
        self.disk_setup.get_controller = mock.Mock(
            return_value='lsilogic'
        )
        self.disk_setup.get_id = mock.Mock(
            return_value='0'
        )

        self.disk_format = DiskFormatVmdk(
            self.xml_state, 'root_dir', 'target_dir'
        )

    def test_post_init(self):
        self.disk_format.post_init({'option': 'value'})
        assert self.disk_format.options == ['-o', 'option=value']

    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.vmdk',
                key='disk_format_image',
                shasum=True,
                use_for_bundle=True
            ),
            call(
                compress=False,
                filename='target_dir/some-disk-image.x86_64-1.2.3.vmx',
                key='disk_format_machine_settings',
                shasum=False,
                use_for_bundle=True
            )
        ]

    @raises(KiwiTemplateError)
    @patch('kiwi.storage.subformat.vmdk.VmwareSettingsTemplate.get_template')
    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_template_error(
        self, mock_open, mock_exists, mock_command, mock_get_template
    ):
        template = mock.Mock()
        mock_get_template.return_value = template
        template.substitute.side_effect = Exception
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format(
        self, mock_open, mock_exists, mock_command
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]

    @patch('kiwi.storage.subformat.vmdk.Command.run')
    @patch('os.path.exists')
    @patch_open
    def test_create_image_format_encoding_exists(
        self, mock_open, mock_exists, mock_command
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True

        self.disk_format.create_image_format()

        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vmx', 'w')
        ]
        assert self.file_mock.write.call_args_list[2] == call(
            'custom entry 2' + os.linesep
        )