Esempio n. 1
0
    def test_create_custom_start_sector(self, mock_open, mock_temp, mock_flag,
                                        mock_command):
        disk_provider = mock.Mock()
        disk_provider.get_device = mock.Mock(return_value='/dev/loop0')
        partitioner = PartitionerMsDos(disk_provider, 4096)
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        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)

        partitioner.create('name', 100, 't.linux', ['f.active'])
        partitioner.create('name', 100, 't.linux', ['f.active'])

        mock_command.assert_has_calls([
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0']),
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        ])
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

        file_mock.write.assert_has_calls([
            call('n\np\n1\n4096\n+100M\nw\nq\n'),
            call('n\np\n2\n\n+100M\nw\nq\n')
        ])
Esempio n. 2
0
    def test_create_custom_start_sector(self, mock_temp, mock_flag,
                                        mock_command):
        disk_provider = Mock()
        disk_provider.get_device = Mock(return_value='/dev/loop0')
        partitioner = PartitionerMsDos(disk_provider, 4096)
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            partitioner.create('name', 100, 't.linux', ['f.active'])
            partitioner.create('name', 100, 't.linux', ['f.active'])

        mock_command.assert_has_calls([
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0']),
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        ])
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

        m_open.return_value.write.assert_has_calls([
            call('n\np\n1\n4096\n+100M\nw\nq\n'),
            call('n\np\n2\n\n+100M\nw\nq\n')
        ])
Esempio n. 3
0
    def __new__(self, table_type, storage_provider):  # noqa: C901
        host_architecture = platform.machine()
        if host_architecture == 'x86_64':
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider)

        elif host_architecture == 'i686' or host_architecture == 'i586':
            if table_type == 'msdos':
                return PartitionerMsDos(storage_provider)

        elif 'ppc64' in host_architecture:
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider)

        elif 's390' in host_architecture:
            if table_type == 'dasd':
                return PartitionerDasd(storage_provider)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider)

        elif 'arm' in host_architecture or host_architecture == 'aarch64':
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider)

        raise KiwiPartitionerSetupError(
            'Support for partitioner on %s architecture not implemented' %
            host_architecture)
Esempio n. 4
0
    def test_create_custom_start_sector(
        self, mock_open, mock_temp, mock_flag, mock_command
    ):
        disk_provider = mock.Mock()
        disk_provider.get_device = mock.Mock(
            return_value='/dev/loop0'
        )
        partitioner = PartitionerMsDos(disk_provider, 4096)
        mock_command.side_effect = Exception
        temp_type = namedtuple(
            'temp_type', ['name']
        )
        mock_temp.return_value = temp_type(
            name='tempfile'
        )
        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)

        partitioner.create('name', 100, 't.linux', ['f.active'])
        partitioner.create('name', 100, 't.linux', ['f.active'])

        mock_command.assert_has_calls([
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0']),
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        ])
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

        file_mock.write.assert_has_calls([
            call('n\np\n1\n4096\n+100M\nw\nq\n'),
            call('n\np\n2\n\n+100M\nw\nq\n')
        ])
Esempio n. 5
0
    def __new__(self,
                table_type,
                storage_provider,
                start_sector=None):  # noqa: C901
        host_architecture = platform.machine()
        if host_architecture == 'x86_64':
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider, start_sector)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider, start_sector)

        elif host_architecture == 'i686' or host_architecture == 'i586':
            if table_type == 'msdos':
                return PartitionerMsDos(storage_provider, start_sector)

        elif 'ppc64' in host_architecture:
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider, start_sector)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider, start_sector)

        elif 's390' in host_architecture:
            if table_type == 'dasd':
                if start_sector:
                    log.warning('disk_start_sector value is ignored '
                                'for dasd partitions')
                return PartitionerDasd(storage_provider)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider, start_sector)

        elif 'arm' in host_architecture or host_architecture == 'aarch64':
            if table_type == 'gpt':
                return PartitionerGpt(storage_provider, start_sector)
            elif table_type == 'msdos':
                return PartitionerMsDos(storage_provider, start_sector)

        raise KiwiPartitionerSetupError(
            'Support for partitioner on %s architecture not implemented' %
            host_architecture)
Esempio n. 6
0
 def setup(self):
     disk_provider = mock.Mock()
     disk_provider.get_device = mock.Mock(
         return_value='/dev/loop0'
     )
     self.partitioner = PartitionerMsDos(disk_provider)
Esempio n. 7
0
class TestPartitionerMsDos(object):
    def setup(self):
        disk_provider = mock.Mock()
        disk_provider.get_device = mock.Mock(
            return_value='/dev/loop0'
        )
        self.partitioner = PartitionerMsDos(disk_provider)

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.NamedTemporaryFile')
    @patch_open
    def test_create(self, mock_open, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple(
            'temp_type', ['name']
        )
        mock_temp.return_value = temp_type(
            name='tempfile'
        )
        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)

        self.partitioner.create('name', 100, 't.linux', ['f.active'])

        file_mock.write.assert_called_once_with(
            'n\np\n1\n\n+100M\nw\nq\n'
        )
        mock_command.assert_called_once_with(
            ['bash', '-c', 'cat tempfile | fdisk /dev/loop0']
        )
        call = mock_flag.call_args_list[0]
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        call = mock_flag.call_args_list[1]
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.NamedTemporaryFile')
    @patch_open
    def test_create_all_free(
        self, mock_open, mock_temp, mock_flag, mock_command
    ):
        mock_command.side_effect = Exception
        temp_type = namedtuple(
            'temp_type', ['name']
        )
        mock_temp.return_value = temp_type(
            name='tempfile'
        )
        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)

        self.partitioner.create('name', 'all_free', 't.linux')

        file_mock.write.assert_called_once_with(
            'n\np\n1\n\n\nw\nq\n'
        )

    @raises(KiwiPartitionerMsDosFlagError)
    def test_set_flag_invalid(self):
        self.partitioner.set_flag(1, 'foo')

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_flag(self, mock_command):
        self.partitioner.set_flag(1, 't.linux')
        mock_command.assert_called_once_with(
            ['sfdisk', '-c', '/dev/loop0', '1', '83']
        )

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_active(self, mock_command):
        self.partitioner.set_flag(1, 'f.active')
        mock_command.assert_called_once_with(
            ['sfdisk', '--activate=1', '/dev/loop0']
        )

    @patch('kiwi.logger.log.warning')
    def test_set_flag_ignored(self, mock_warn):
        self.partitioner.set_flag(1, 't.csm')
        assert mock_warn.called
Esempio n. 8
0
 def setup(self):
     disk_provider = Mock()
     disk_provider.get_device = Mock(return_value='/dev/loop0')
     self.partitioner = PartitionerMsDos(disk_provider)
     self.partitioner_extended = PartitionerMsDos(disk_provider,
                                                  extended_layout=True)
Esempio n. 9
0
class TestPartitionerMsDos:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        disk_provider = Mock()
        disk_provider.get_device = Mock(return_value='/dev/loop0')
        self.partitioner = PartitionerMsDos(disk_provider)
        self.partitioner_extended = PartitionerMsDos(disk_provider,
                                                     extended_layout=True)

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

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.Temporary.new_file')
    def test_create_primary(self, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        m_open = mock_open()

        with patch('builtins.open', m_open, create=True):
            self.partitioner._create_primary('name', 100, 't.linux',
                                             ['f.active'])

        m_open.return_value.write.assert_called_once_with(
            'n\np\n1\n\n+100M\nw\nq\n')
        mock_command.assert_called_once_with(
            ['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        call = mock_flag.call_args_list[0]
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        call = mock_flag.call_args_list[1]
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.Temporary.new_file')
    def test_create_extended(self, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        m_open = mock_open()

        with patch('builtins.open', m_open, create=True):
            self.partitioner._create_extended('name')

        m_open.return_value.write.assert_called_once_with(
            'n\ne\n1\n\n\nw\nq\n')
        mock_command.assert_called_once_with(
            ['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.Temporary.new_file')
    def test_create_logical(self, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        m_open = mock_open()

        with patch('builtins.open', m_open, create=True):
            self.partitioner._create_logical('name', 100, 't.linux',
                                             ['f.active'])

        m_open.return_value.write.assert_called_once_with(
            'n\n1\n\n+100M\nw\nq\n')
        mock_command.assert_called_once_with(
            ['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        call = mock_flag.call_args_list[0]
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        call = mock_flag.call_args_list[1]
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

    @patch.object(PartitionerMsDos, '_create_primary')
    @patch.object(PartitionerMsDos, '_create_extended')
    @patch.object(PartitionerMsDos, '_create_logical')
    def test_create(self, mock_create_logical, mock_create_extended,
                    mock_create_primary):
        self.partitioner.create('name', 100, 't.linux')
        mock_create_primary.assert_called_once_with('name', 100, 't.linux', [])
        mock_create_primary.reset_mock()
        self.partitioner_extended.create('name', 100, 't.linux')
        mock_create_primary.assert_called_once_with('name', 100, 't.linux', [])
        self.partitioner_extended.partition_id = 3
        self.partitioner_extended.create('name', 100, 't.linux')
        mock_create_extended.assert_called_once_with('name')
        mock_create_logical.assert_called_once_with('name', 100, 't.linux', [])
        mock_create_logical.reset_mock()
        self.partitioner_extended.partition_id = 7
        self.partitioner_extended.create('name', 100, 't.linux')
        mock_create_logical.assert_called_once_with('name', 100, 't.linux', [])

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.Temporary.new_file')
    def test_create_custom_start_sector(self, mock_temp, mock_flag,
                                        mock_command):
        disk_provider = Mock()
        disk_provider.get_device = Mock(return_value='/dev/loop0')
        partitioner = PartitionerMsDos(disk_provider, 4096)
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            partitioner.create('name', 100, 't.linux', ['f.active'])
            partitioner.create('name', 100, 't.linux', ['f.active'])

        mock_command.assert_has_calls([
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0']),
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        ])
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

        m_open.return_value.write.assert_has_calls([
            call('n\np\n1\n4096\n+100M\nw\nq\n'),
            call('n\np\n2\n\n+100M\nw\nq\n')
        ])

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.Temporary.new_file')
    def test_create_all_free(self, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            self.partitioner.create('name', 'all_free', 't.linux')

        m_open.return_value.write.assert_called_once_with(
            'n\np\n1\n\n\nw\nq\n')

    def test_set_flag_invalid(self):
        with raises(KiwiPartitionerMsDosFlagError):
            self.partitioner.set_flag(1, 'foo')

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_flag(self, mock_command):
        self.partitioner.set_flag(1, 't.linux')
        mock_command.assert_called_once_with(
            ['sfdisk', '-c', '/dev/loop0', '1', '83'])

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_active(self, mock_command):
        self.partitioner.set_flag(1, 'f.active')
        mock_command.assert_called_once_with(
            ['parted', '/dev/loop0', 'set', '1', 'boot', 'on'])

    def test_set_flag_ignored(self):
        with self._caplog.at_level(logging.WARNING):
            self.partitioner.set_flag(1, 't.csm')

    def test_resize_table(self):
        self.partitioner.resize_table()
Esempio n. 10
0
 def setup(self):
     disk_provider = mock.Mock()
     disk_provider.get_device = mock.Mock(return_value='/dev/loop0')
     self.partitioner = PartitionerMsDos(disk_provider)
Esempio n. 11
0
class TestPartitionerMsDos:
    def setup(self):
        disk_provider = mock.Mock()
        disk_provider.get_device = mock.Mock(return_value='/dev/loop0')
        self.partitioner = PartitionerMsDos(disk_provider)

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.NamedTemporaryFile')
    @patch_open
    def test_create(self, mock_open, mock_temp, mock_flag, mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        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)

        self.partitioner.create('name', 100, 't.linux', ['f.active'])

        file_mock.write.assert_called_once_with('n\np\n1\n\n+100M\nw\nq\n')
        mock_command.assert_called_once_with(
            ['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        call = mock_flag.call_args_list[0]
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        call = mock_flag.call_args_list[1]
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.NamedTemporaryFile')
    @patch_open
    def test_create_custom_start_sector(self, mock_open, mock_temp, mock_flag,
                                        mock_command):
        disk_provider = mock.Mock()
        disk_provider.get_device = mock.Mock(return_value='/dev/loop0')
        partitioner = PartitionerMsDos(disk_provider, 4096)
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        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)

        partitioner.create('name', 100, 't.linux', ['f.active'])
        partitioner.create('name', 100, 't.linux', ['f.active'])

        mock_command.assert_has_calls([
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0']),
            call(['bash', '-c', 'cat tempfile | fdisk /dev/loop0'])
        ])
        assert mock_flag.call_args_list[0] == \
            call(1, 't.linux')
        assert mock_flag.call_args_list[1] == \
            call(1, 'f.active')

        file_mock.write.assert_has_calls([
            call('n\np\n1\n4096\n+100M\nw\nq\n'),
            call('n\np\n2\n\n+100M\nw\nq\n')
        ])

    @patch('kiwi.partitioner.msdos.Command.run')
    @patch('kiwi.partitioner.msdos.PartitionerMsDos.set_flag')
    @patch('kiwi.partitioner.msdos.NamedTemporaryFile')
    @patch_open
    def test_create_all_free(self, mock_open, mock_temp, mock_flag,
                             mock_command):
        mock_command.side_effect = Exception
        temp_type = namedtuple('temp_type', ['name'])
        mock_temp.return_value = temp_type(name='tempfile')
        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)

        self.partitioner.create('name', 'all_free', 't.linux')

        file_mock.write.assert_called_once_with('n\np\n1\n\n\nw\nq\n')

    @raises(KiwiPartitionerMsDosFlagError)
    def test_set_flag_invalid(self):
        self.partitioner.set_flag(1, 'foo')

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_flag(self, mock_command):
        self.partitioner.set_flag(1, 't.linux')
        mock_command.assert_called_once_with(
            ['sfdisk', '-c', '/dev/loop0', '1', '83'])

    @patch('kiwi.partitioner.msdos.Command.run')
    def test_set_active(self, mock_command):
        self.partitioner.set_flag(1, 'f.active')
        mock_command.assert_called_once_with(
            ['parted', '/dev/loop0', 'set', '1', 'boot', 'on'])

    @patch('kiwi.logger.log.warning')
    def test_set_flag_ignored(self, mock_warn):
        self.partitioner.set_flag(1, 't.csm')
        assert mock_warn.called

    def test_resize_table(self):
        self.partitioner.resize_table()