Skip to content

Commit

Permalink
Merge a7a4cb1 into e85a25d
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouhei committed Nov 3, 2018
2 parents e85a25d + a7a4cb1 commit 48366e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 102 deletions.
53 changes: 15 additions & 38 deletions bootstrap_py/pypi.py
@@ -1,15 +1,12 @@
# -*- coding: utf-8 -*-
"""bootstrap_py.pypi."""
import sys
import requests
import socket
from requests.exceptions import Timeout, HTTPError
from bootstrap_py.exceptions import BackendFailure, Conflict
if sys.version_info < (3, 0):
import xmlrpclib as xmlrpc_client
else:
from xmlrpc import client as xmlrpc_client

#: PyPI XML-RPC API url
PYPI_URL = 'https://pypi.python.org/pypi'
#: PyPI JSONC API url
PYPI_URL = 'https://pypi.org/pypi/{0}/json'


def package_existent(name):
Expand All @@ -23,34 +20,14 @@ def package_existent(name):
:param str name: package name
"""
if sys.version_info < (3, 0):
try:
result = search_package(name)
except (socket.error,
xmlrpc_client.ProtocolError) as exc:
raise BackendFailure(exc)
else:
try:
result = search_package(name)
except (socket.gaierror,
TimeoutError,
ConnectionRefusedError,
xmlrpc_client.ProtocolError) as exc:
raise BackendFailure(exc)
if result:
msg = ('[error] "{0}" is registered already in PyPI.\n'
'\tSpecify another package name.').format(name)
raise Conflict(msg)


def search_package(name):
"""Search package.
:param str name: package name
:rtype: list
:return: package name list
"""
client = xmlrpc_client.ServerProxy(PYPI_URL)
return [pkg for pkg in client.search({'name': name})
if pkg.get('name') == name]
try:
response = requests.get(PYPI_URL.format(name))
if response.ok:
msg = ('[error] "{0}" is registered already in PyPI.\n'
'\tSpecify another package name.').format(name)
raise Conflict(msg)
except (socket.gaierror,
Timeout,
ConnectionError,
HTTPError) as exc:
raise BackendFailure(exc)
15 changes: 7 additions & 8 deletions bootstrap_py/tests/test_control.py
@@ -1,12 +1,11 @@
# -*- coding: utf-8 -*-
"""bootstrap_py.tests.test_control."""
import unittest
import mock
import os
import tempfile
import shutil
import requests_mock
from bootstrap_py import control, exceptions
from bootstrap_py import control, exceptions, pypi
from bootstrap_py.classifiers import Classifiers
from bootstrap_py.tests.test_package import Dummy

Expand Down Expand Up @@ -43,10 +42,10 @@ def test_check_repository_existence(self):
with self.assertRaises(exceptions.Conflict):
control.check_repository_existence(self.params)

@mock.patch('bootstrap_py.pypi.search_package')
def test_check_package_existence(self, _mock):
def test_check_package_existence(self):
"""check_package_existence."""
_mock.return_value = [{'name': 'py-deps'}]
setattr(self.params, 'no_check', False)
with self.assertRaises(exceptions.Conflict):
control.check_package_existence(self.params)
with requests_mock.Mocker() as _mock:
_mock.get(pypi.PYPI_URL.format(self.params.name), status_code=200)
setattr(self.params, 'no_check', False)
with self.assertRaises(exceptions.Conflict):
control.check_package_existence(self.params)
81 changes: 25 additions & 56 deletions bootstrap_py/tests/test_pypi.py
Expand Up @@ -2,14 +2,11 @@
"""bootstrap_py.tests.test_pypi."""
import unittest
import json
import sys
import socket
import mock
import requests
import socket
import requests_mock
from bootstrap_py import pypi, exceptions
if sys.version_info < (3, 0):
import xmlrpclib as xmlrpc_client
else:
from xmlrpc import client as xmlrpc_client


def search_result():
Expand All @@ -21,76 +18,48 @@ def search_result():
class PyPITests(unittest.TestCase):
"""bootstrap_py.pypi tests."""

if sys.version_info < (3, 0):
@mock.patch('xmlrpclib.ServerProxy')
def test_search_package(self, _mock):
"""search package for Python2."""
client_mock = _mock.return_value
client_mock.search.return_value = search_result()
self.assertListEqual(pypi.search_package('py-deps'),
search_result())
else:
@mock.patch('xmlrpc.client.ServerProxy')
def test_search_package(self, _mock):
"""search package for Python3."""
client_mock = _mock.return_value
client_mock.search.return_value = search_result()
self.assertListEqual(pypi.search_package('py-deps'),
search_result())

@mock.patch('bootstrap_py.pypi.search_package')
def test_package_existent(self, _mock):
def test_package_existent(self):
"""checking package existent."""
_mock.return_value = [{'name': 'py-deps'}]
with self.assertRaises(exceptions.Conflict):
pypi.package_existent('py-deps')
package_name = 'py-deps'
with requests_mock.Mocker() as _mock:
_mock.get(pypi.PYPI_URL.format(package_name), status_code=404)
self.assertEqual(pypi.package_existent(package_name), None)

@mock.patch('bootstrap_py.pypi.search_package')
def test_package_existent_duplicate(self, _mock):
def test_package_existent_duplicate(self):
"""checking package existent, but this case does not occur."""
_mock.return_value = [{'name': 'py-deps'},
{'name': 'py-deps-dummy'}]
with self.assertRaises(exceptions.Conflict):
pypi.package_existent('py-deps')
package_name = 'py-deps'
with requests_mock.Mocker() as _mock:
_mock.get(pypi.PYPI_URL.format(package_name), status_code=200)
with self.assertRaises(exceptions.Conflict):
pypi.package_existent(package_name)

@mock.patch('bootstrap_py.pypi.search_package')
@mock.patch('requests.get')
def test_pypi_slow_response(self, _mock):
"""pypi slow response."""
if sys.version_info < (3, 0):
# pylint: disable=undefined-variable
_mock.side_effect = socket.error
else:
# pylint: disable=undefined-variable
_mock.side_effect = TimeoutError
# pylint: disable=undefined-variable
_mock.side_effect = requests.exceptions.Timeout
with self.assertRaises(exceptions.BackendFailure):
pypi.package_existent('py-deps')

@mock.patch('bootstrap_py.pypi.search_package')
@mock.patch('requests.get')
def test_pypi_internal_server_down(self, _mock):
"""pypi internal server down."""
if sys.version_info < (3, 0):
# pylint: disable=undefined-variable
_mock.side_effect = socket.error
else:
# pylint: disable=undefined-variable
_mock.side_effect = ConnectionRefusedError
# pylint: disable=undefined-variable
_mock.side_effect = ConnectionError
with self.assertRaises(exceptions.BackendFailure):
pypi.package_existent('py-deps')

@mock.patch('bootstrap_py.pypi.search_package')
@mock.patch('requests.get')
def test_pypi_interface_down(self, _mock):
"""pypi interface down."""
# pylint: disable=undefined-variable
_mock.side_effect = socket.gaierror
with self.assertRaises(exceptions.BackendFailure):
pypi.package_existent('py-deps')

@mock.patch('bootstrap_py.pypi.search_package')
def test_pypi_protocol_error(self, _mock):
"""pypi protocol error."""
_mock.side_effect = xmlrpc_client.ProtocolError(pypi.PYPI_URL,
400,
'dummy',
{})
@mock.patch('requests.get')
def test_pypi_http_error(self, _mock):
"""pypi http error."""
_mock.side_effect = requests.exceptions.HTTPError
with self.assertRaises(exceptions.BackendFailure):
pypi.package_existent('py-deps')

0 comments on commit 48366e1

Please sign in to comment.