def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize', '--target-dir',
            'target_dir', '--size', '20g', '--root', '../data/root-dir'
        ]
        self.abs_root_dir = os.path.abspath('../data/root-dir')

        kiwi.tasks.image_resize.Help = mock.Mock(return_value=mock.Mock())

        self.firmware = mock.Mock()
        self.firmware.get_partition_table_type = mock.Mock(return_value='gpt')
        self.partitioner = mock.Mock()
        self.loop_provider = mock.Mock()
        self.image_format = mock.Mock()
        self.image_format.has_raw_disk = mock.Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = mock.Mock(
            return_value=self.image_format)
        kiwi.tasks.image_resize.FirmWare = mock.Mock(
            return_value=self.firmware)
        kiwi.tasks.image_resize.LoopDevice = mock.Mock(
            return_value=self.loop_provider)
        kiwi.tasks.image_resize.Partitioner = mock.Mock(
            return_value=self.partitioner)

        self.task = ImageResizeTask()
Esempio n. 2
0
    def setup(self):
        sys.argv = [
            sys.argv[0], '--profile', 'vmxSimpleFlavour', '--type', 'oem',
            'image', 'resize', '--target-dir', 'target_dir', '--size', '20g',
            '--root', '../data/root-dir'
        ]
        self.abs_root_dir = os.path.abspath('../data/root-dir')

        kiwi.tasks.image_resize.Help = Mock(
            return_value=Mock()
        )

        self.firmware = Mock()
        self.firmware.get_partition_table_type = Mock(
            return_value='gpt'
        )
        self.loop_provider = Mock()
        kiwi.tasks.image_resize.FirmWare = Mock(
            return_value=self.firmware
        )
        kiwi.tasks.image_resize.LoopDevice = Mock(
            return_value=self.loop_provider
        )

        self.task = ImageResizeTask()
Esempio n. 3
0
    def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize', '--target-dir',
            'target_dir', '--size', '20g', '--root', '../data/root-dir'
        ]
        kiwi.tasks.image_resize.Help = mock.Mock(return_value=mock.Mock())

        self.image_format = mock.Mock()
        self.image_format.has_raw_disk = mock.Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = mock.Mock(
            return_value=self.image_format)

        self.task = ImageResizeTask()
    def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize',
            '--target-dir', 'target_dir', '--size', '20g',
            '--root', '../data/root-dir'
        ]
        kiwi.tasks.image_resize.Help = mock.Mock(
            return_value=mock.Mock()
        )

        self.image_format = mock.Mock()
        self.image_format.has_raw_disk = mock.Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = mock.Mock(
            return_value=self.image_format
        )

        self.task = ImageResizeTask()
Esempio n. 5
0
class TestImageResizeTask(object):
    def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize', '--target-dir',
            'target_dir', '--size', '20g', '--root', '../data/root-dir'
        ]
        kiwi.tasks.image_resize.Help = mock.Mock(return_value=mock.Mock())

        self.image_format = mock.Mock()
        self.image_format.has_raw_disk = mock.Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = mock.Mock(
            return_value=self.image_format)

        self.task = ImageResizeTask()

    def teardown(self):
        sys.argv = argv_kiwi_tests

    def _init_command_args(self):
        self.task.command_args = {}
        self.task.command_args['help'] = False
        self.task.command_args['resize'] = False
        self.task.command_args['--target-dir'] = 'target_dir'
        self.task.command_args['--size'] = '42g'
        self.task.command_args['--root'] = '../data/root-dir'

    @raises(KiwiConfigFileNotFound)
    def test_process_no_root_directory_specified(self):
        self.task.command_args['--root'] = None
        self.task.process()

    @raises(KiwiImageResizeError)
    def test_process_no_raw_disk_found(self):
        self._init_command_args()
        self.image_format.has_raw_disk.return_value = False
        self.task.command_args['resize'] = True
        self.task.process()

    @raises(KiwiImageResizeError)
    def test_process_unsupported_size_format(self):
        self._init_command_args()
        self.task.command_args['--size'] = '20x'
        self.image_format.has_raw_disk.return_value = True
        self.task.command_args['resize'] = True
        self.task.process()

    def test_process_image_resize_gb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(42 * 1024 *
                                                                  1024 * 1024)
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_mb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42m'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(42 * 1024 *
                                                                  1024)
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_bytes(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(42)
        self.image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.logger.log.info')
    def test_process_image_resize_not_needed(self, mock_log_info):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = False
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(42)
        assert mock_log_info.call_args_list == [
            call('Loading XML description'),
            call('--> loaded %s', '../data/root-dir/image/config.xml'),
            call('--> Selected build type: %s', 'vmx'),
            call('--> Selected profiles: %s', 'vmxFlavour'),
            call('Resizing raw disk to 42 bytes'),
            call('Raw disk is already at 42 bytes')
        ]

    def test_process_image_resize_help(self):
        self._init_command_args()
        self.task.command_args['help'] = True
        self.task.command_args['resize'] = True
        self.task.process()
        self.task.manual.show.assert_called_once_with('kiwi::image::resize')
Esempio n. 6
0
class TestImageResizeTask:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        sys.argv = [
            sys.argv[0], '--profile', 'vmxSimpleFlavour', '--type', 'oem',
            'image', 'resize', '--target-dir', 'target_dir', '--size', '20g',
            '--root', '../data/root-dir'
        ]
        self.abs_root_dir = os.path.abspath('../data/root-dir')

        kiwi.tasks.image_resize.Help = Mock(
            return_value=Mock()
        )

        self.firmware = Mock()
        self.firmware.get_partition_table_type = Mock(
            return_value='gpt'
        )
        self.loop_provider = Mock()
        kiwi.tasks.image_resize.FirmWare = Mock(
            return_value=self.firmware
        )
        kiwi.tasks.image_resize.LoopDevice = Mock(
            return_value=self.loop_provider
        )

        self.task = ImageResizeTask()

    def teardown(self):
        sys.argv = argv_kiwi_tests

    def _init_command_args(self):
        self.task.command_args = {}
        self.task.command_args['help'] = False
        self.task.command_args['resize'] = False
        self.task.command_args['--target-dir'] = 'target_dir'
        self.task.command_args['--size'] = '42g'
        self.task.command_args['--root'] = '../data/root-dir'

    def test_process_no_root_directory_specified(self):
        self.task.command_args['--root'] = None
        with raises(KiwiConfigFileNotFound):
            self.task.process()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    def test_process_no_raw_disk_found(self, mock_DiskFormat):
        image_format = Mock()
        image_format.has_raw_disk.return_value = False
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['resize'] = True
        with raises(KiwiImageResizeError):
            self.task.process()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    def test_process_unsupported_size_format(self, mock_DiskFormat):
        image_format = Mock()
        image_format.has_raw_disk.return_value = True
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['--size'] = '20x'
        self.task.command_args['resize'] = True
        with raises(KiwiSizeError):
            self.task.process()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    @patch('kiwi.tasks.image_resize.Partitioner.new')
    def test_process_image_resize_gb(self, mock_Partitioner, mock_DiskFormat):
        partitioner = Mock()
        mock_Partitioner.return_value = partitioner
        image_format = Mock()
        image_format.resize_raw_disk.return_value = True
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        partitioner.resize_table.assert_called_once_with()
        image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024 * 1024
        )
        image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    @patch('kiwi.tasks.image_resize.Partitioner.new')
    def test_process_image_resize_mb(self, mock_Partitioner, mock_DiskFormat):
        partitioner = Mock()
        mock_Partitioner.return_value = partitioner
        image_format = Mock()
        image_format.resize_raw_disk.return_value = True
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42m'
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        partitioner.resize_table.assert_called_once_with()
        image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024
        )
        image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    @patch('kiwi.tasks.image_resize.Partitioner.new')
    def test_process_image_resize_bytes(
        self, mock_Partitioner, mock_DiskFormat
    ):
        partitioner = Mock()
        mock_Partitioner.return_value = partitioner
        image_format = Mock()
        image_format.resize_raw_disk.return_value = True
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        partitioner.resize_table.assert_called_once_with()
        image_format.resize_raw_disk.assert_called_once_with(
            42
        )
        image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.tasks.image_resize.DiskFormat.new')
    @patch('kiwi.tasks.image_resize.Partitioner.new')
    def test_process_image_resize_not_needed(
        self, mock_Partitioner, mock_DiskFormat
    ):
        partitioner = Mock()
        mock_Partitioner.return_value = partitioner
        image_format = Mock()
        image_format.resize_raw_disk.return_value = False
        mock_DiskFormat.return_value = image_format
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        with self._caplog.at_level(logging.INFO):
            self.task.process()
            self.loop_provider.create.assert_called_once_with(overwrite=False)
            partitioner.resize_table.assert_called_once_with()
            image_format.resize_raw_disk.assert_called_once_with(
                42
            )
            assert 'Loading XML description' in self._caplog.text
            assert '--> loaded {0}'.format(
                os.sep.join([self.abs_root_dir, 'image', 'config.xml'])
            ) in self._caplog.text
            assert '--> Selected build type: oem' in self._caplog.text
            assert '--> Selected profiles: vmxSimpleFlavour' in self._caplog.text
            assert 'Resizing raw disk to 42 bytes' in self._caplog.text
            assert 'Raw disk is already at 42 bytes' in self._caplog.text

    def test_process_image_resize_help(self):
        self._init_command_args()
        self.task.command_args['help'] = True
        self.task.command_args['resize'] = True
        self.task.process()
        self.task.manual.show.assert_called_once_with(
            'kiwi::image::resize'
        )
class TestImageResizeTask(object):
    def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize',
            '--target-dir', 'target_dir', '--size', '20g',
            '--root', '../data/root-dir'
        ]
        self.abs_root_dir = os.path.abspath('../data/root-dir')

        kiwi.tasks.image_resize.Help = mock.Mock(
            return_value=mock.Mock()
        )

        self.image_format = mock.Mock()
        self.image_format.has_raw_disk = mock.Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = mock.Mock(
            return_value=self.image_format
        )

        self.task = ImageResizeTask()

    def teardown(self):
        sys.argv = argv_kiwi_tests

    def _init_command_args(self):
        self.task.command_args = {}
        self.task.command_args['help'] = False
        self.task.command_args['resize'] = False
        self.task.command_args['--target-dir'] = 'target_dir'
        self.task.command_args['--size'] = '42g'
        self.task.command_args['--root'] = '../data/root-dir'

    @raises(KiwiConfigFileNotFound)
    def test_process_no_root_directory_specified(self):
        self.task.command_args['--root'] = None
        self.task.process()

    @raises(KiwiImageResizeError)
    def test_process_no_raw_disk_found(self):
        self._init_command_args()
        self.image_format.has_raw_disk.return_value = False
        self.task.command_args['resize'] = True
        self.task.process()

    @raises(KiwiSizeError)
    def test_process_unsupported_size_format(self):
        self._init_command_args()
        self.task.command_args['--size'] = '20x'
        self.image_format.has_raw_disk.return_value = True
        self.task.command_args['resize'] = True
        self.task.process()

    def test_process_image_resize_gb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024 * 1024
        )
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_mb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42m'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024
        )
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_bytes(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42
        )
        self.image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.logger.log.info')
    def test_process_image_resize_not_needed(self, mock_log_info):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = False
        self.task.process()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42
        )
        assert mock_log_info.call_args_list == [
            call('Loading XML description'),
            call(
                '--> loaded %s',
                os.sep.join([self.abs_root_dir, 'image', 'config.xml'])
            ),
            call('--> Selected build type: %s', 'vmx'),
            call('--> Selected profiles: %s', 'vmxFlavour'),
            call('Resizing raw disk to 42 bytes'),
            call('Raw disk is already at 42 bytes')
        ]

    def test_process_image_resize_help(self):
        self._init_command_args()
        self.task.command_args['help'] = True
        self.task.command_args['resize'] = True
        self.task.process()
        self.task.manual.show.assert_called_once_with(
            'kiwi::image::resize'
        )
Esempio n. 8
0
class TestImageResizeTask:
    def setup(self):
        sys.argv = [
            sys.argv[0], '--type', 'vmx', 'image', 'resize',
            '--target-dir', 'target_dir', '--size', '20g',
            '--root', '../data/root-dir'
        ]
        self.abs_root_dir = os.path.abspath('../data/root-dir')

        kiwi.tasks.image_resize.Help = Mock(
            return_value=Mock()
        )

        self.firmware = Mock()
        self.firmware.get_partition_table_type = Mock(
            return_value='gpt'
        )
        self.partitioner = Mock()
        self.loop_provider = Mock()
        self.image_format = Mock()
        self.image_format.has_raw_disk = Mock()
        self.image_format.diskname = 'some-disk.raw'
        kiwi.tasks.image_resize.DiskFormat = Mock(
            return_value=self.image_format
        )
        kiwi.tasks.image_resize.FirmWare = Mock(
            return_value=self.firmware
        )
        kiwi.tasks.image_resize.LoopDevice = Mock(
            return_value=self.loop_provider
        )
        kiwi.tasks.image_resize.Partitioner = Mock(
            return_value=self.partitioner
        )

        self.task = ImageResizeTask()

    def teardown(self):
        sys.argv = argv_kiwi_tests

    def _init_command_args(self):
        self.task.command_args = {}
        self.task.command_args['help'] = False
        self.task.command_args['resize'] = False
        self.task.command_args['--target-dir'] = 'target_dir'
        self.task.command_args['--size'] = '42g'
        self.task.command_args['--root'] = '../data/root-dir'

    def test_process_no_root_directory_specified(self):
        self.task.command_args['--root'] = None
        with raises(KiwiConfigFileNotFound):
            self.task.process()

    def test_process_no_raw_disk_found(self):
        self._init_command_args()
        self.image_format.has_raw_disk.return_value = False
        self.task.command_args['resize'] = True
        with raises(KiwiImageResizeError):
            self.task.process()

    def test_process_unsupported_size_format(self):
        self._init_command_args()
        self.task.command_args['--size'] = '20x'
        self.image_format.has_raw_disk.return_value = True
        self.task.command_args['resize'] = True
        with raises(KiwiSizeError):
            self.task.process()

    def test_process_image_resize_gb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        self.partitioner.resize_table.assert_called_once_with()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024 * 1024
        )
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_mb(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42m'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        self.partitioner.resize_table.assert_called_once_with()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42 * 1024 * 1024
        )
        self.image_format.create_image_format.assert_called_once_with()

    def test_process_image_resize_bytes(self):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = True
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        self.partitioner.resize_table.assert_called_once_with()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42
        )
        self.image_format.create_image_format.assert_called_once_with()

    @patch('kiwi.logger.log.info')
    def test_process_image_resize_not_needed(self, mock_log_info):
        self._init_command_args()
        self.task.command_args['resize'] = True
        self.task.command_args['--size'] = '42'
        self.image_format.resize_raw_disk.return_value = False
        self.task.process()
        self.loop_provider.create.assert_called_once_with(overwrite=False)
        self.partitioner.resize_table.assert_called_once_with()
        self.image_format.resize_raw_disk.assert_called_once_with(
            42
        )
        assert mock_log_info.call_args_list == [
            call('Loading XML description'),
            call(
                '--> loaded %s',
                os.sep.join([self.abs_root_dir, 'image', 'config.xml'])
            ),
            call('--> Selected build type: %s', 'vmx'),
            call('--> Selected profiles: %s', 'vmxFlavour'),
            call('Resizing raw disk to 42 bytes'),
            call('Raw disk is already at 42 bytes')
        ]

    def test_process_image_resize_help(self):
        self._init_command_args()
        self.task.command_args['help'] = True
        self.task.command_args['resize'] = True
        self.task.process()
        self.task.manual.show.assert_called_once_with(
            'kiwi::image::resize'
        )