def test_retrieve_repository_info_correct(self, mock_requests):
        """
        Tests that the function returns correct data when it is all found in
        the Release file.
        """
        architectures = (
            'amd64 armel armhf i386 ia64 kfreebsd-amd64 '
            'kfreebsd-i386 mips mipsel powerpc s390 s390x sparc'.split()
        )
        components = ['main', 'contrib', 'non-free']
        mock_response_text = (
            'Suite: stable\n'
            'Codename: wheezy\n'
            'Architectures: ' + ' '.join(architectures) + '\n'
            'Components: ' + ' '.join(components) + '\n'
            'Version: 7.1\n'
            'Description: Debian 7.1 Released 15 June 2013\n'
        )
        set_mock_response(mock_requests, mock_response_text)

        repository_info = retrieve_repository_info(
            'deb http://repository.com/ stable')

        expected_info = {
            'uri': 'http://repository.com/',
            'architectures': architectures,
            'components': components,
            'binary': True,
            'source': False,
            'codename': 'wheezy',
            'suite': 'stable',
        }

        self.assertDictEqual(expected_info, repository_info)
    def test_retrieve_repository_info_missing_required(self, mock_requests):
        """
        Tests that the function raises an exception when some required keys are
        missing from the Release file.
        """
        mock_response_text = (
            'Suite: stable\n'
            'Codename: wheezy\n'
            'Architectures: amd64\n'
            'Version: 7.1\n'
            'Description: Debian 7.1 Released 15 June 2013\n'
        )
        set_mock_response(mock_requests, mock_response_text)

        from pts.core.retrieve_data import InvalidRepositoryException
        with self.assertRaises(InvalidRepositoryException):
            retrieve_repository_info('deb http://repository.com/ stable')
    def test_retrieve_repository_info_missing_non_required(self, mock_requests):
        """
        Tests the function when some non-required keys are missing from the
        Release file.
        """
        mock_response_text = (
            'Architectures: amd64\n'
            'components: main'
            'Version: 7.1\n'
            'Description: Debian 7.1 Released 15 June 2013\n'
        )
        set_mock_response(mock_requests, mock_response_text)

        repository_info = retrieve_repository_info(
            'deb http://repository.com/ stable')
        # It uses the suite name from the sources.list
        self.assertEqual(repository_info['suite'], 'stable')
        # Codename is not found
        self.assertIsNone(repository_info['codename'])
Exemplo n.º 4
0
    def clean(self, *args, **kwargs):
        """
        Overrides the :meth:`clean <django.forms.ModelForm.clean>` method of the
        parent class to allow validating the form based on the sources.list
        entry, not only the model fields.
        """
        self.cleaned_data = super(RepositoryAdminForm, self).clean(*args, **kwargs)
        if 'sources_list_entry' not in self.cleaned_data:
            # Sources list entry was given to the form but it failed
            # validation.
            return self.cleaned_data
        # Check if the entry was not even given
        if not self.cleaned_data['sources_list_entry']:
            # If not, all the fields required by the model must be found
            # instead
            for name in self.original_required_fields:
                self.fields[name].required = True
            self._clean_fields()
        else:
            # If it was given, need to make sure now that the Relase file
            # contains usable data.
            try:
                repository_info = retrieve_repository_info(
                    self.cleaned_data['sources_list_entry'])
            except InvalidRepositoryException as e:
                raise ValidationError("The Release file was invalid.")
            # Use the data to construct a Repository object.
            self.cleaned_data.update(repository_info)
            # Architectures have to be matched with their primary keys
            self.cleaned_data['architectures'] = [
                Architecture.objects.get(name=architecture_name).pk
                for architecture_name in self.cleaned_data['architectures']
                if Architecture.objects.filter(name=architecture_name).exists()
            ]

        return self.cleaned_data