Example #1
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 = DiskFormatVhdFixed(self.xml_state, 'root_dir',
                                           'target_dir')
Example #2
0
 def setup(self):
     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)
     self.disk_format = DiskFormatVhdFixed(self.xml_state, 'root_dir',
                                           'target_dir',
                                           {'force_size': None})
Example #3
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))
Example #4
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))
 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 = DiskFormatVhdFixed(
         self.xml_state, 'root_dir', 'target_dir', {'force_size': None}
     )
Example #6
0
class TestDiskFormatVhdFixed(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 = DiskFormatVhdFixed(
            self.xml_state, 'root_dir', 'target_dir', {'force_size': None}
        )

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

    @raises(KiwiVhdTagError)
    @patch('kiwi.storage.subformat.vhdfixed.Command.run')
    def test_create_image_format_invalid_tag(self, mock_command):
        self.disk_format.tag = 'invalid'
        self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vhdfixed.Command.run')
    @patch_open
    def test_create_image_format(self, mock_open, mock_command):
        self.disk_format.tag = '12345678-1234-1234-1234-123456789999'
        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)
        file_mock.read.return_value = 'dev_null_data'

        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', 'vpc',
                '-o', 'force_size', '-o', 'subformat=fixed',
                'target_dir/some-disk-image.x86_64-1.2.3.vhdfixed'
            ]
        )
        assert mock_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'wb'),
            call('/dev/null', 'rb'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'wb')
        ]
        assert file_mock.write.call_args_list[0] == call(
            'dev_null_data'
        )
        if sys.byteorder == 'little':
            # on little endian machines
            assert file_mock.write.call_args_list[1] == call(
                bytes(b'xV4\x124\x124\x12\x124\x124Vx\x99\x99')
            )
        else:
            # on big endian machines
            assert file_mock.write.call_args_list[1] == call(
                bytes(b'\x124Vx\x124\x124\x124\x124Vx\x99\x99')
            )
        assert file_mock.seek.call_args_list == [
            call(65536, 0), call(0, 2),
            call(65536, 0), call(0, 2)
        ]

    def test_store_to_result(self):
        result = mock.Mock()
        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.vhdfixed',
            key='disk_format_image',
            shasum=True,
            use_for_bundle=True
        )
Example #7
0
class TestDiskFormatVhdFixed:
    def setup(self):
        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
        )
        self.disk_format = DiskFormatVhdFixed(
            self.xml_state, 'root_dir', 'target_dir', {'force_size': None}
        )

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

    @patch('kiwi.storage.subformat.vhdfixed.Command.run')
    def test_create_image_format_invalid_tag(self, mock_command):
        self.disk_format.tag = 'invalid'
        with raises(KiwiVhdTagError):
            self.disk_format.create_image_format()

    @patch('kiwi.storage.subformat.vhdfixed.Command.run')
    def test_create_image_format(self, mock_command):
        self.disk_format.tag = '12345678-1234-1234-1234-123456789999'

        m_open = mock_open(read_data='dev_zero_data')
        with patch('builtins.open', m_open, create=True):
            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', 'vpc',
                '-o', 'force_size', '-o', 'subformat=fixed',
                'target_dir/some-disk-image.x86_64-1.2.3.vhdfixed'
            ]
        )
        assert m_open.call_args_list == [
            call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'r+b'),
            call('/dev/zero', 'rb'),
            call('target_dir/some-disk-image.x86_64-1.2.3.vhdfixed', 'r+b')
        ]
        assert m_open.return_value.write.call_args_list[0] == call(
            'dev_zero_data'
        )
        if sys.byteorder == 'little':
            # on little endian machines
            assert m_open.return_value.write.call_args_list[1] == call(
                bytes(b'xV4\x124\x124\x12\x124\x124Vx\x99\x99')
            )
        else:
            # on big endian machines
            assert m_open.return_value.write.call_args_list[1] == call(
                bytes(b'\x124Vx\x124\x124\x124\x124Vx\x99\x99')
            )
        assert m_open.return_value.seek.call_args_list == [
            call(65536, 0), call(0, 2),
            call(65536, 0), call(0, 2)
        ]

    def test_store_to_result(self):
        result = Mock()
        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.vhdfixed',
            key='disk_format_image',
            shasum=True,
            use_for_bundle=True
        )