コード例 #1
0
ファイル: test_resolver.py プロジェクト: youprofit/pants
class TestRESTfulResolver(unittest.TestCase):

    TEST_TIMEOUT = 1
    TEST_RETRIES = 3

    RESOLVED_URL_1 = 'http://10.0.0.1:1'
    RESOLVED_URL_2 = 'http://10.0.0.2:2'
    URLS = [RESOLVED_URL_1, RESOLVED_URL_2]

    TEST_RESOLVED_FROM = 'http://test-resolver'

    TEST_REQUEST_EXCEPTION = requests.exceptions.ConnectionError()

    def setUp(self):
        self.parser = Mock(spec=ResponseParser)
        self.resolver = RESTfulResolver(self.TEST_TIMEOUT, self.TEST_RETRIES,
                                        self.parser)

    def mock_response(self, status_code, urls=None):
        response = Mock()
        response.status_code = status_code
        self.parser.parse = Mock(return_value=urls)
        return response

    def testResolveSuccess(self):
        with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
            mock_get.return_value = self.mock_response(requests.codes.ok,
                                                       urls=self.URLS)
            self.assertEquals(self.URLS,
                              self.resolver.resolve(self.TEST_RESOLVED_FROM))

    def testResolveErrorEmptyReturn(self):
        with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
            mock_get.return_value = self.mock_response(requests.codes.ok,
                                                       urls=[])
            with self.assertRaises(Resolver.ResolverError):
                self.resolver.resolve(self.TEST_RESOLVED_FROM)

    def testResolveParseError(self):
        with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
            mock_get.return_value = self.mock_response(
                requests.codes.ok, urls='this is a garbage string')
            self.parser.parse.side_effect = ResponseParser.ResponseParserError(
            )
            with self.assertRaises(Resolver.ResolverError):
                self.resolver.resolve(self.TEST_RESOLVED_FROM)

    def testResolveResponseError(self):
        with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
            mock_get.return_value = self.mock_response(
                requests.codes.service_unavailable)
            with self.assertRaises(Resolver.ResolverError):
                self.resolver.resolve(self.TEST_RESOLVED_FROM)

    def testResolveConnectionError(self):
        with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
            mock_get.side_effect = self.TEST_REQUEST_EXCEPTION
            with self.assertRaises(Resolver.ResolverError):
                self.resolver.resolve(self.TEST_RESOLVED_FROM)
コード例 #2
0
ファイル: cache_setup.py プロジェクト: Gointer/pants
    def __init__(self, options, log, stable_name, pinger=None, resolver=None):
        """Create a cache factory from settings.

    :param options: Task's scoped options.
    :param log: Task's context log.
    :param stable_name: Task's stable name.
    :param pinger: Pinger to choose the best remote artifact cache URL.
    :param resolver: Resolver to look up remote artifact cache URLs.
    :return: cache factory.
    """
        self._options = options
        self._log = log
        self._stable_name = stable_name

        # Created on-demand.
        self._read_cache = None
        self._write_cache = None

        # Protects local filesystem setup, and assignment to the references above.
        self._cache_setup_lock = threading.Lock()

        # Caches are supposed to be close, and we don't want to waste time pinging on no-op builds.
        # So we ping twice with a short timeout.
        # TODO: Make lazy.
        self._pinger = pinger or Pinger(timeout=self._options.pinger_timeout,
                                        tries=self._options.pinger_tries)

        # resolver is also close but failing to resolve might have broader impact than
        # single ping failure, therefore use a higher timeout with more retries.
        self._resolver = resolver or \
                         (RESTfulResolver(timeout=1.0, tries=3) if self._options.resolver == 'rest' else \
                          NoopResolver())
コード例 #3
0
ファイル: test_resolver.py プロジェクト: CaitieM20/pants
class TestRESTfulResolver(unittest.TestCase):

  TEST_TIMEOUT = 1
  TEST_RETRIES = 3

  RESOLVED_URL_1 = 'http://10.0.0.1:1'
  RESOLVED_URL_2 = 'http://10.0.0.2:2'
  URLS = [RESOLVED_URL_1, RESOLVED_URL_2]

  TEST_RESOLVED_FROM = 'http://test-resolver'

  TEST_REQUEST_EXCEPTION =  requests.exceptions.ConnectionError()

  def setUp(self):
    self.parser = Mock(spec=ResponseParser)
    self.resolver = RESTfulResolver(self.TEST_TIMEOUT, self.TEST_RETRIES, self.parser)

  def mock_response(self, status_code, urls=None):
    response = Mock()
    response.status_code = status_code
    self.parser.parse = Mock(return_value=urls)
    return response

  def testResolveSuccess(self):
    with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
      mock_get.return_value = self.mock_response(requests.codes.ok, urls=self.URLS)
      self.assertEquals(self.URLS, self.resolver.resolve(self.TEST_RESOLVED_FROM))

  def testResolveErrorEmptyReturn(self):
    with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
      mock_get.return_value = self.mock_response(requests.codes.ok, urls=[])
      with self.assertRaises(Resolver.ResolverError):
        self.resolver.resolve(self.TEST_RESOLVED_FROM)

  def testResolveParseError(self):
    with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
      mock_get.return_value = self.mock_response(requests.codes.ok, urls='this is a garbage string')
      self.parser.parse.side_effect = ResponseParser.ResponseParserError()
      with self.assertRaises(Resolver.ResolverError):
        self.resolver.resolve(self.TEST_RESOLVED_FROM)

  def testResolveResponseError(self):
    with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
      mock_get.return_value = self.mock_response(requests.codes.service_unavailable)
      with self.assertRaises(Resolver.ResolverError):
        self.resolver.resolve(self.TEST_RESOLVED_FROM)

  def testResolveConnectionError(self):
    with patch.object(requests.Session, 'get', **PATCH_OPTS) as mock_get:
      mock_get.side_effect = self.TEST_REQUEST_EXCEPTION
      with self.assertRaises(Resolver.ResolverError):
        self.resolver.resolve(self.TEST_RESOLVED_FROM)
コード例 #4
0
ファイル: test_resolver.py プロジェクト: youprofit/pants
 def setUp(self):
     self.parser = Mock(spec=ResponseParser)
     self.resolver = RESTfulResolver(self.TEST_TIMEOUT, self.TEST_RETRIES,
                                     self.parser)
コード例 #5
0
ファイル: test_resolver.py プロジェクト: CaitieM20/pants
 def setUp(self):
   self.parser = Mock(spec=ResponseParser)
   self.resolver = RESTfulResolver(self.TEST_TIMEOUT, self.TEST_RETRIES, self.parser)