예제 #1
0
    def test_cancel(self):
        dlstep = DownloadStep('fake-step')
        dlstep.parent = MagicMock()
        dlstep.initialize()

        dlstep.cancel()

        self.assertTrue(dlstep.downloader.is_canceled)
예제 #2
0
class DownloadStepTests(unittest.TestCase):

    TYPE_ID_FOO = 'foo'

    def get_basic_config(*arg, **kwargs):
        plugin_config = {"num_retries": 0, "retry_delay": 0}
        repo_plugin_config = {}
        for key in kwargs:
            repo_plugin_config[key] = kwargs[key]
        config = PluginCallConfiguration(plugin_config,
                                         repo_plugin_config=repo_plugin_config)
        return config

    def get_sync_conduit(type_id=None, existing_units=None, pkg_dir=None):
        def build_failure_report(summary, details):
            return SyncReport(False, sync_conduit._added_count, sync_conduit._updated_count,
                              sync_conduit._removed_count, summary, details)

        def build_success_report(summary, details):
            return SyncReport(True, sync_conduit._added_count, sync_conduit._updated_count,
                              sync_conduit._removed_count, summary, details)

        def side_effect(type_id, key, metadata, rel_path):
            if rel_path and pkg_dir:
                rel_path = os.path.join(pkg_dir, rel_path)
                if not os.path.exists(os.path.dirname(rel_path)):
                    os.makedirs(os.path.dirname(rel_path))
            unit = Unit(type_id, key, metadata, rel_path)
            return unit

        def get_units(criteria=None):
            ret_val = []
            if existing_units:
                for u in existing_units:
                    if criteria:
                        if u.type_id in criteria.type_ids:
                            ret_val.append(u)
                    else:
                        ret_val.append(u)
            return ret_val

        def search_all_units(type_id, criteria):
            ret_val = []
            if existing_units:
                for u in existing_units:
                    if u.type_id == type_id:
                        if u.unit_key['id'] == criteria['filters']['id']:
                            ret_val.append(u)
            return ret_val

        sync_conduit = Mock(spec=RepoSyncConduit)
        sync_conduit._added_count = sync_conduit._updated_count = sync_conduit._removed_count = 0
        sync_conduit.init_unit.side_effect = side_effect
        sync_conduit.get_units.side_effect = get_units
        sync_conduit.save_unit = Mock()
        sync_conduit.search_all_units.side_effect = search_all_units
        sync_conduit.build_failure_report = MagicMock(side_effect=build_failure_report)
        sync_conduit.build_success_report = MagicMock(side_effect=build_success_report)
        sync_conduit.set_progress = MagicMock()

        return sync_conduit

    def setUp(self):

        conf_dict = {
            importer_constants.KEY_FEED: 'http://fake.com/file_feed/',
            importer_constants.KEY_MAX_SPEED: 500.0,
            importer_constants.KEY_MAX_DOWNLOADS: 5,
            importer_constants.KEY_SSL_VALIDATION: False,
            importer_constants.KEY_SSL_CLIENT_CERT: "Trust me, I'm who I say I am.",
            importer_constants.KEY_SSL_CLIENT_KEY: "Secret Key",
            importer_constants.KEY_SSL_CA_CERT: "Uh, I guess that's the right server.",
            importer_constants.KEY_PROXY_HOST: 'proxy.com',
            importer_constants.KEY_PROXY_PORT: 1234,
            importer_constants.KEY_PROXY_USER: "******",
            importer_constants.KEY_PROXY_PASS: '******',
            importer_constants.KEY_VALIDATE: False,
        }
        self.real_config = self.get_basic_config(**conf_dict)
        self.real_conduit = self.get_sync_conduit()

        self.mock_repo = Mock()
        self.mock_conduit = Mock()
        self.mock_config = Mock()
        self.mock_working_dir = Mock()
        self.dlstep = DownloadStep("fake_download", repo=self.mock_repo, conduit=self.mock_conduit,
                                   config=self.mock_config, working_dir=self.mock_working_dir,
                                   plugin_type="fake plugin", description='foo')

    def test_init(self):
        self.assertEquals(self.dlstep.get_repo(), self.mock_repo)
        self.assertEquals(self.dlstep.get_conduit(), self.mock_conduit)
        self.assertEquals(self.dlstep.get_config(), self.mock_config)
        self.assertEquals(self.dlstep.get_working_dir(), self.mock_working_dir)
        self.assertEquals(self.dlstep.get_plugin_type(), "fake plugin")
        self.assertEqual(self.dlstep.description, 'foo')

    def test_initalize(self):
        # override mock config with real config dict
        self.dlstep.config = self.real_config
        self.dlstep.conduit = self.get_sync_conduit()

        self.dlstep.initialize()

        # Now let's assert that all the right things happened during initialization
        self.assertEqual(self.dlstep._repo_url, 'http://fake.com/file_feed/')
        # Validation of downloads should be disabled by default
        self.assertEqual(self.dlstep._validate_downloads, False)

        # Inspect the downloader
        downloader = self.dlstep.downloader
        # The dlstep should be the event listener for the downloader
        self.assertEqual(downloader.event_listener, self.dlstep)
        # Inspect the downloader config
        expected_downloader_config = {
            'max_speed': 500.0,
            'max_concurrent': 5,
            'ssl_client_cert': "Trust me, I'm who I say I am.",
            'ssl_client_key': 'Secret Key',
            'ssl_ca_cert': "Uh, I guess that's the right server.",
            'ssl_validation': False,
            'proxy_url': 'proxy.com',
            'proxy_port': 1234,
            'proxy_username': '******',
            'proxy_password': '******'}
        for key, value in expected_downloader_config.items():
            self.assertEquals(getattr(downloader.config, key), value)

    def test__init___with_feed_lacking_trailing_slash(self):
        """
        tests https://bugzilla.redhat.com/show_bug.cgi?id=949004
        """
        slash_config = self.get_basic_config(
            **{importer_constants.KEY_FEED: 'http://fake.com/no_trailing_slash'})

        # override mock config with real config dict
        self.dlstep.config = slash_config
        self.dlstep.initialize()
        # Humorously enough, the _repo_url attribute named no_trailing_slash
        # should now have a trailing slash
        self.assertEqual(self.dlstep._repo_url, 'http://fake.com/no_trailing_slash/')

    def test__init___file_downloader(self):
        slash_config = self.get_basic_config(
            **{importer_constants.KEY_FEED: 'file:///some/path/'})
        # override mock config with real config dict
        self.dlstep.config = slash_config
        self.dlstep.initialize()
        self.assertTrue(isinstance(self.dlstep.downloader, LocalFileDownloader))

    def test__init___ssl_validation(self):
        """
        Make sure the SSL validation is on by default.
        """
        # It should default to True
        self.dlstep.config = self.get_basic_config(
            **{importer_constants.KEY_FEED: 'http://fake.com/iso_feed/'})
        self.dlstep.initialize()
        self.assertEqual(self.dlstep.downloader.config.ssl_validation, True)

        # It should be possible to explicitly set it to False
        self.dlstep.config = self.get_basic_config(
            **{importer_constants.KEY_FEED: 'http://fake.com/iso_feed/',
               importer_constants.KEY_SSL_VALIDATION: False})
        self.dlstep.initialize()
        self.assertEqual(self.dlstep.downloader.config.ssl_validation, False)

        # It should be possible to explicitly set it to True
        self.dlstep.config = self.get_basic_config(
            **{importer_constants.KEY_FEED: 'http://fake.com/iso_feed/',
               importer_constants.KEY_SSL_VALIDATION: True})
        self.dlstep.initialize()
        self.assertEqual(self.dlstep.downloader.config.ssl_validation, True)

    def test__get_total(self):
        mock_downloads = ['fake', 'downloads']
        dlstep = DownloadStep('fake-step', downloads=mock_downloads)
        self.assertEquals(dlstep._get_total(), 2)

    def test__process_block(self):
        mock_downloader = Mock()
        mock_downloads = ['fake', 'downloads']
        dlstep = DownloadStep('fake-step', downloads=mock_downloads)
        dlstep.downloader = mock_downloader
        dlstep._process_block()
        mock_downloader.download.assert_called_once_with(['fake', 'downloads'])

    def test_download_succeeded(self):
        dlstep = DownloadStep('fake-step')
        mock_report = Mock()
        mock_report_progress = Mock()
        dlstep.report_progress = mock_report_progress
        dlstep.download_succeeded(mock_report)
        self.assertEquals(dlstep.progress_successes, 1)
        # assert report_progress was called with no args
        mock_report_progress.assert_called_once_with()

    def test_download_failed(self):
        dlstep = DownloadStep('fake-step')
        mock_report = Mock()
        mock_report_progress = Mock()
        dlstep.report_progress = mock_report_progress
        dlstep.download_failed(mock_report)
        self.assertEquals(dlstep.progress_failures, 1)
        # assert report_progress was called with no args
        mock_report_progress.assert_called_once_with()

    def test_downloads_property(self):
        generator = (DownloadRequest(url, '/a/b/c') for url in ['http://pulpproject.org'])
        dlstep = DownloadStep('fake-step', downloads=generator)

        downloads = dlstep.downloads

        self.assertTrue(isinstance(downloads, list))
        self.assertEqual(len(downloads), 1)
        self.assertTrue(isinstance(downloads[0], DownloadRequest))

    def test_cancel(self):
        dlstep = DownloadStep('fake-step')
        dlstep.parent = MagicMock()
        dlstep.initialize()

        dlstep.cancel()

        self.assertTrue(dlstep.downloader.is_canceled)