Skip to content

Commit

Permalink
ref #1281 - add get_downloader() to base importer.
Browse files Browse the repository at this point in the history
  • Loading branch information
jortel committed Sep 28, 2015
1 parent cb6bdfb commit bd189ad
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
31 changes: 31 additions & 0 deletions server/pulp/plugins/importer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import sys

from gettext import gettext as _
from urlparse import urlparse

from nectar.downloaders.local import LocalFileDownloader
from nectar.downloaders.threaded import HTTPThreadedDownloader

from pulp.plugins.util.nectar_config import importer_config_to_nectar_config


class Importer(object):
"""
Expand All @@ -8,6 +16,29 @@ class Importer(object):
importer during discovery.
"""

@staticmethod
def get_downloader(config, url, **options):
"""
Get a configured downloader.
:param config: A plugin configuration.
:type config: pulp.plugins.config.PluginCallConfiguration
:param url: A URL.
:type url: str
:param options: Extended configuration.
:type options: dict
:return: A configured downloader.
:rtype: nectar.downloaders.base.Downloader
:raise ValueError: when the URL scheme is not supported.
"""
url = urlparse(url)
nectar_config = importer_config_to_nectar_config(config.flatten())
if url.scheme == 'file':
return LocalFileDownloader(nectar_config)
if url.scheme in ('http', 'https'):
return HTTPThreadedDownloader(nectar_config)
raise ValueError(_('Scheme "{s}" not supported').format(s=url.scheme))

# -- plugin lifecycle -----------------------------------------------------

@classmethod
Expand Down
55 changes: 51 additions & 4 deletions server/test/unit/plugins/test_importer.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
# -*- coding: utf-8 -*-

import unittest
from unittest import TestCase

import mock
from mock import patch, Mock

from pulp.plugins.importer import Importer


class TestImporter(unittest.TestCase):
class TestImporter(TestCase):

@mock.patch('pulp.plugins.importer.sys.exit', autospec=True)
@patch('pulp.plugins.importer.LocalFileDownloader')
@patch('pulp.plugins.importer.importer_config_to_nectar_config')
def test_get_local_downloader(self, to_nectar, local):
url = 'file:///martin.com/test'
config = Mock()

# test
downloader = Importer.get_downloader(config, url)

# validation
to_nectar.assert_called_once_with(config.flatten.return_value)
local.assert_called_once_with(to_nectar.return_value)
self.assertEqual(downloader, local.return_value)

@patch('pulp.plugins.importer.HTTPThreadedDownloader')
@patch('pulp.plugins.importer.importer_config_to_nectar_config')
def test_get_http_downloader(self, to_nectar, http):
url = 'http://martin.com/test'
config = Mock()

# test
downloader = Importer.get_downloader(config, url)

# validation
to_nectar.assert_called_once_with(config.flatten.return_value)
http.assert_called_once_with(to_nectar.return_value)
self.assertEqual(downloader, http.return_value)

@patch('pulp.plugins.importer.HTTPThreadedDownloader')
@patch('pulp.plugins.importer.importer_config_to_nectar_config')
def test_get_https_downloader(self, to_nectar, http):
url = 'https://martin.com/test'
config = Mock()

# test
downloader = Importer.get_downloader(config, url)

# validation
to_nectar.assert_called_once_with(config.flatten.return_value)
http.assert_called_once_with(to_nectar.return_value)
self.assertEqual(downloader, http.return_value)

@patch('pulp.plugins.importer.importer_config_to_nectar_config', Mock())
def test_get_downloader_invalid_scheme(self):
url = 'ftpx://martin.com/test'
self.assertRaises(ValueError, Importer.get_downloader, Mock(), url)

@patch('pulp.plugins.importer.sys.exit', autospec=True)
def test_cancel_sync_repo_calls_sys_exit(self, mock_sys_exit):
Importer().cancel_sync_repo()
mock_sys_exit.assert_called_once_with()

0 comments on commit bd189ad

Please sign in to comment.