コード例 #1
0
    def test_get_packages_for_repository(self):
        """
        Tests that the cache correctly returns a list of cached Packages files
        for a given repository.
        """
        with make_temp_directory('-pts-cache') as cache_directory:
            with self.settings(PTS_CACHE_DIRECTORY=cache_directory):
                self.create_cache()
                repository = Repository.objects.create(
                    name='stable',
                    shorthand='stable',
                    uri='http://cdn.debian.net/debian/dists',
                    suite='stable')
                expected_packages_files = [
                    'main_binary-amd64_Packages',
                    'main_binary-i386_Packages',
                ]
                files = expected_packages_files + [
                    'Release',
                    'main_source_Sources',
                ]
                self.set_stub_cached_files_for_repository(repository, files)

                packages = self.cache.get_packages_files_for_repository(repository)

                self.assertEqual(len(expected_packages_files), len(packages))
                for expected, returned in zip(
                        expected_packages_files, packages):
                    self.assertTrue(returned.endswith(expected))
コード例 #2
0
    def test_cache_multiple_insert_no_remove(self):
        """
        Tests that the cache does not remove packages unless the size limit is
        exceeded.
        """
        with make_temp_directory('-pts-cache') as cache_directory:
            with self.settings(
                    PTS_CACHE_DIRECTORY=cache_directory,
                    PTS_APT_CACHE_MAX_SIZE=10):
                self.create_cache()
                # Sanity check: old size is 0 as nothing was ever cached in the
                # brand new directory
                self.assert_cache_size_equal(0)
                content = b'a' * 5  # 5 bytes
                self.set_stub_acquire_content(content)
                # Add one file.
                self.cache.retrieve_source('dummy-package', '1.0.0')
                self.assert_cache_size_equal(5)
                # Same content in another file
                self.set_stub_acquire_content(content)

                self.cache.retrieve_source('package', '1.0.0')

                # Both files are now saved.
                self.assert_cache_size_equal(10)
コード例 #3
0
    def test_clear_cache(self):
        """
        Tests that the cache removes packages when it exceeds its allocated
        size.
        """
        with make_temp_directory('-pts-cache') as cache_directory:
            with self.settings(
                    PTS_CACHE_DIRECTORY=cache_directory,
                    PTS_APT_CACHE_MAX_SIZE=10):
                self.create_cache()
                # Sanity check: old size is 0 as nothing was ever cached in the
                # brand new directory
                self.assert_cache_size_equal(0)
                initial_content = b'a' * 11
                self.set_stub_acquire_content(initial_content)
                # Set initial source content
                self.cache.retrieve_source('dummy-package', '1.0.0')
                self.assert_cache_size_equal(11)
                content = b'a' * 7
                self.set_stub_acquire_content(content)

                self.cache.retrieve_source('package', '1.0.0')

                # Only the second content is found in the package
                self.assert_cache_size_equal(7)
コード例 #4
0
ファイル: tests.py プロジェクト: pombredanne/DistroTracker
    def test_create_extracted_files(self, mock_cache):
        """
        Tests that the task creates an
        :class:`pts.core.models.ExtractedSourceFile` instance.
        """
        name = SourcePackageName.objects.create(name='dummy-package')
        package = SourcePackage.objects.create(
            source_package_name=name, version='1.0.0')
        self.add_mock_event('new-source-package-version', {
            'pk': package.pk,
        })

        with make_temp_directory('pts-pkg-dir') as pkg_directory:
            debian_dir = os.path.join(pkg_directory, 'debian')
            os.makedirs(debian_dir)
            changelog_path = os.path.join(debian_dir, 'changelog')
            with open(changelog_path, 'w') as f:
                f.write('Contents')
            # This file should not be included in the extracted files.
            other_file = os.path.join(debian_dir, 'some-file')
            with open(other_file, 'w') as f:
                f.write('Contents')

            mock_cache.return_value = os.path.join(pkg_directory)

            self.run_task()

            # Check that the file was created.
            self.assertEqual(1, ExtractedSourceFile.objects.count())
            # Check that it has the correct name
            extracted_file = ExtractedSourceFile.objects.all()[0]
            self.assertEqual('changelog', extracted_file.name)
コード例 #5
0
    def test_signatures_added(self):
        """
        Tests that signatures are correctly added to the news which previously
        didn't have any, despite having signed content.
        """
        # Set up news based on a signed message.
        signed_news = []
        unsigned_news = []
        with make_temp_directory('-pts-keyring') as TEST_KEYRING_DIRECTORY:
            self.TEST_KEYRING_DIRECTORY = TEST_KEYRING_DIRECTORY
            with self.settings(
                    PTS_KEYRING_DIRECTORY=self.TEST_KEYRING_DIRECTORY):
                self.import_key_from_test_file('key1.pub')
                # The content of the test news item is found in a file
                file_path = self.get_test_file_path(
                    'signed-message-quoted-printable')
                with open(file_path, 'rb') as f:
                    content = f.read()
                expected_name = 'PTS Tests'
                expected_email = '*****@*****.**'
                sender_name = 'Some User'
                # The first signed news has the same content as what is found
                # the signed test file.
                signed_news.append(EmailNews.objects.create_email_news(
                    message=message_from_bytes(content),
                    package=self.package))
                # For the second one, add some text after the signature: this
                # should still mean that the correct signature can be extracted!
                signed_news.append(EmailNews.objects.create_email_news(
                    message=message_from_bytes(content + b'\nMore content'),
                    package=self.package))
                # Set up some unsigned news.
                unsigned_news.append(EmailNews.objects.create_email_news(
                    message=message_from_bytes(b'Subject: Hi\n\nPayload.'),
                    package=self.package))
                # A non-email based news item
                unsigned_news.append(News.objects.create(
                    package=self.package,
                    content="Some content.."
                ))
                # Make sure that the signed news do not have associated
                # signature information
                for signed in signed_news:
                    signed.signed_by.clear()

                # Run the command
                call_command("pts_update_news_signatures")

                # The signed news items have associated signature information
                for signed in signed_news:
                    self.assertEqual(1, signed.signed_by.count())
                    signer = signed.signed_by.all()[0]
                    # The signature is actually correct too?
                    self.assertEqual(expected_name, signer.name)
                    self.assertEqual(expected_email, signer.email)
                # The unsigned messages still do not have any signature info
                for unsigned in unsigned_news:
                    self.assertEqual(0, unsigned.signed_by.count())
コード例 #6
0
ファイル: tests.py プロジェクト: pombredanne/DistroTracker
    def test_task_is_initial_existing_files(self, mock_cache):
        """
        Tests the task when it is run as the initial task, but some files for
        the package have already been previously extracted.
        """
        name = SourcePackageName.objects.create(name='dummy-package')
        package = SourcePackage.objects.create(
            source_package_name=name, version='1.0.0')

        with make_temp_directory('pts-pkg-dir') as pkg_directory:
            debian_dir = os.path.join(pkg_directory, 'debian')
            os.makedirs(debian_dir)
            wanted_files = [
                'changelog',
                'copyright',
                'rules',
                'control',
                'watch',
            ]
            all_files = wanted_files + [
                'other-file',
            ]
            for file_name in all_files:
                file_path = os.path.join(debian_dir, file_name)
                with open(file_path, 'w') as f:
                    f.write('Contents')

            mock_cache.return_value = os.path.join(pkg_directory)

            # Make a previously extracted file.
            original_content = 'Original content'
            ExtractedSourceFile.objects.create(
                source_package=package,
                name='changelog',
                extracted_file=ContentFile(original_content, name='changelog'))

            self.run_task(initial_task=True)

            # Check that all the wanted files exist.
            self.assertEqual(len(wanted_files), ExtractedSourceFile.objects.count())
            # Check that the existing file was not changed.
            extracted_file = ExtractedSourceFile.objects.get(
                name='changelog',
                source_package=package)
            extracted_file.extracted_file.open()
            content = extracted_file.extracted_file.read()
            extracted_file.extracted_file.close()
            self.assertEqual(original_content, content)
コード例 #7
0
ファイル: tests.py プロジェクト: pombredanne/DistroTracker
    def test_task_is_initial_existing_file_remove(self, mock_cache):
        """
        Tests the task when it is run as the initial task, but some of the
        already extracted source files should no longer be extracted.
        """
        name = SourcePackageName.objects.create(name='dummy-package')
        package = SourcePackage.objects.create(
            source_package_name=name, version='1.0.0')

        with make_temp_directory('pts-pkg-dir') as pkg_directory:
            debian_dir = os.path.join(pkg_directory, 'debian')
            os.makedirs(debian_dir)
            wanted_files = [
                'changelog',
                'copyright',
                'rules',
                'control',
                'watch',
            ]
            all_files = wanted_files + [
                'other-file',
            ]
            for file_name in all_files:
                file_path = os.path.join(debian_dir, file_name)
                with open(file_path, 'w') as f:
                    f.write('Contents')

            mock_cache.return_value = os.path.join(pkg_directory)

            # Make a previously extracted file.
            original_content = 'Original content'
            ExtractedSourceFile.objects.create(
                source_package=package,
                name='we-dont-want-this-any-more',
                extracted_file=ContentFile(original_content, name='changelog'))

            self.run_task(initial_task=True)

            # Check that all the wanted files exist.
            self.assertEqual(len(wanted_files), ExtractedSourceFile.objects.count())
            # Check that only the wanted files exist.
            extracted_names = [
                extracted_file.name
                for extracted_file in ExtractedSourceFile.objects.all()
            ]
            for wanted_file in wanted_files:
                self.assertIn(wanted_file, extracted_names)
コード例 #8
0
    def test_cache_size_increase_after_acquire(self):
        """
        Tests that the cache correctly increases its size after acquiring new
        files.
        """
        with make_temp_directory('-pts-cache') as cache_directory:
            with self.settings(
                    PTS_CACHE_DIRECTORY=cache_directory,
                    PTS_APT_CACHE_MAX_SIZE=10):
                self.create_cache()
                # Sanity check: old size is 0 as nothing was ever cached in the
                # brand new directory
                self.assert_cache_size_equal(0)
                content = b'a' * 5  # 5 bytes
                self.set_stub_acquire_content(content)

                self.cache.retrieve_source('dummy-package', '1.0.0')

                self.assert_cache_size_equal(5)
コード例 #9
0
ファイル: tests.py プロジェクト: pombredanne/DistroTracker
    def test_create_extracted_files_only_wanted_files(self, mock_cache):
        """
        Tests that the task creates an
        :class:`pts.core.models.ExtractedSourceFile` instance.
        """
        name = SourcePackageName.objects.create(name='dummy-package')
        package = SourcePackage.objects.create(
            source_package_name=name, version='1.0.0')
        self.add_mock_event('new-source-package-version', {
            'pk': package.pk,
        })

        with make_temp_directory('pts-pkg-dir') as pkg_directory:
            debian_dir = os.path.join(pkg_directory, 'debian')
            os.makedirs(debian_dir)
            wanted_files = [
                'changelog',
                'copyright',
                'rules',
                'control',
                'watch',
            ]
            all_files = wanted_files + [
                'other-file',
            ]
            for file_name in all_files:
                file_path = os.path.join(debian_dir, file_name)
                with open(file_path, 'w') as f:
                    f.write('Contents')

            mock_cache.return_value = os.path.join(pkg_directory)

            self.run_task()

            # Check that only the wanted files are created!
            self.assertEqual(len(wanted_files), ExtractedSourceFile.objects.count())
            extracted_names = [
                extracted_file.name
                for extracted_file in ExtractedSourceFile.objects.all()
            ]
            for wanted_file in wanted_files:
                self.assertIn(wanted_file, extracted_names)
コード例 #10
0
ファイル: tests.py プロジェクト: pombredanne/DistroTracker
    def test_task_is_initial_no_existing_files(self, mock_cache):
        """
        Tests the task when it is run as the initial task, but there are no
        extracted files for existing packages.
        """
        name = SourcePackageName.objects.create(name='dummy-package')
        package = SourcePackage.objects.create(
            source_package_name=name, version='1.0.0')

        with make_temp_directory('pts-pkg-dir') as pkg_directory:
            debian_dir = os.path.join(pkg_directory, 'debian')
            os.makedirs(debian_dir)
            wanted_files = [
                'changelog',
                'copyright',
                'rules',
                'control',
                'watch',
            ]
            all_files = wanted_files + [
                'other-file',
            ]
            for file_name in all_files:
                file_path = os.path.join(debian_dir, file_name)
                with open(file_path, 'w') as f:
                    f.write('Contents')

            mock_cache.return_value = os.path.join(pkg_directory)

            self.run_task(initial_task=True)

            # Check that all the wanted files are created!
            self.assertEqual(len(wanted_files), ExtractedSourceFile.objects.count())
            extracted_names = [
                extracted_file.name
                for extracted_file in ExtractedSourceFile.objects.all()
            ]
            for wanted_file in wanted_files:
                self.assertIn(wanted_file, extracted_names)