2015-09-17 19:58:22 +02:00
|
|
|
#! /usr/bin/env py.test
|
2015-09-20 19:26:22 +02:00
|
|
|
"""
|
|
|
|
Checks an actual pypi-server against various clients.
|
|
|
|
|
|
|
|
The tests below are using 3 ways to startup pypi-servers:
|
|
|
|
|
|
|
|
- "open": a per-module server instance without any authed operations,
|
|
|
|
serving a single `wheel` package, on a fixed port.
|
|
|
|
- "open": a per-module server instance with authed 'download/upload' operations,
|
|
|
|
serving a single `wheel` package, on a fixed port.
|
|
|
|
- "new_server": starting a new server with any configurations on each test.
|
|
|
|
|
|
|
|
"""
|
2015-09-19 01:02:27 +02:00
|
|
|
from __future__ import print_function
|
2015-09-17 19:58:22 +02:00
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
from collections import namedtuple
|
2015-09-17 19:58:22 +02:00
|
|
|
import contextlib
|
2015-09-19 01:02:27 +02:00
|
|
|
import functools
|
|
|
|
import os
|
2015-09-17 19:58:22 +02:00
|
|
|
import subprocess
|
2015-09-18 18:31:12 +02:00
|
|
|
import sys
|
2015-09-19 02:36:43 +02:00
|
|
|
import tempfile
|
2015-09-20 19:26:22 +02:00
|
|
|
from textwrap import dedent
|
2015-09-17 19:58:22 +02:00
|
|
|
import time
|
|
|
|
|
2015-09-18 20:20:01 +02:00
|
|
|
import pip
|
2015-09-17 19:58:22 +02:00
|
|
|
from py import path # @UnresolvedImport
|
|
|
|
import pytest
|
|
|
|
|
2015-09-19 02:36:43 +02:00
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
_BUFF_SIZE = 4096
|
|
|
|
_port = 8090
|
2015-09-18 20:20:01 +02:00
|
|
|
|
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def port():
|
|
|
|
global _port
|
|
|
|
_port += 1
|
|
|
|
return _port
|
2015-09-17 19:58:22 +02:00
|
|
|
|
2015-09-20 18:35:11 +02:00
|
|
|
Srv = namedtuple('Srv', ('proc', 'port', 'package'))
|
2015-09-19 01:02:27 +02:00
|
|
|
|
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
def _run_server(packdir, port, authed, other_cli=''):
|
2015-09-19 01:02:27 +02:00
|
|
|
pswd_opt_choices = {
|
|
|
|
True: "-Ptests/htpasswd.a.a -a update,download",
|
|
|
|
False: "-P. -a."
|
|
|
|
}
|
2015-09-20 19:26:22 +02:00
|
|
|
pswd_opts = pswd_opt_choices[authed]
|
2015-09-21 03:51:14 +02:00
|
|
|
cmd = "%s -m pypiserver.__main__ -vvv --overwrite -p %s %s %s %s" % (
|
2015-09-20 18:35:11 +02:00
|
|
|
sys.executable, port, pswd_opts, other_cli, packdir)
|
2015-09-19 01:02:27 +02:00
|
|
|
proc = subprocess.Popen(cmd.split(), bufsize=_BUFF_SIZE)
|
|
|
|
time.sleep(1)
|
2015-09-19 02:36:43 +02:00
|
|
|
assert proc.poll() is None
|
2015-09-19 01:02:27 +02:00
|
|
|
|
|
|
|
return Srv(proc, int(port), packdir)
|
|
|
|
|
|
|
|
|
|
|
|
def _kill_server(srv):
|
|
|
|
print('Killing %s' % (srv,))
|
2015-09-17 19:58:22 +02:00
|
|
|
try:
|
2015-09-19 01:02:27 +02:00
|
|
|
srv.proc.terminate()
|
|
|
|
time.sleep(1)
|
2015-09-17 19:58:22 +02:00
|
|
|
finally:
|
2015-09-19 01:02:27 +02:00
|
|
|
srv.proc.kill()
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2015-09-20 19:26:22 +02:00
|
|
|
def new_server(packdir, port, authed=False, other_cli=''):
|
2015-09-19 01:02:27 +02:00
|
|
|
srv = _run_server(packdir, port,
|
2015-09-20 19:26:22 +02:00
|
|
|
authed=authed, other_cli=other_cli)
|
2015-09-19 01:02:27 +02:00
|
|
|
try:
|
|
|
|
yield srv
|
|
|
|
finally:
|
|
|
|
_kill_server(srv)
|
|
|
|
|
|
|
|
|
2015-09-19 02:36:43 +02:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def chdir(d):
|
|
|
|
old_d = os.getcwd()
|
|
|
|
try:
|
|
|
|
os.chdir(d)
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(old_d)
|
|
|
|
|
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
def _run_python(cmd):
|
2015-09-20 18:35:11 +02:00
|
|
|
ncmd = '%s %s' % (sys.executable, cmd)
|
|
|
|
return os.system(ncmd)
|
|
|
|
|
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
@pytest.fixture(scope='module')
|
2015-09-20 19:26:22 +02:00
|
|
|
def project(request):
|
2015-09-19 02:36:43 +02:00
|
|
|
def fin():
|
|
|
|
tmpdir.remove(True)
|
|
|
|
tmpdir = path.local(tempfile.mkdtemp())
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
src_setup_py = path.local().join('tests', 'centodeps-setup.py')
|
|
|
|
assert src_setup_py.check()
|
|
|
|
projdir = tmpdir.join('centodeps')
|
|
|
|
projdir.mkdir()
|
|
|
|
dst_setup_py = projdir.join('setup.py')
|
|
|
|
src_setup_py.copy(dst_setup_py)
|
|
|
|
assert dst_setup_py.check()
|
2015-09-19 01:02:27 +02:00
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
return projdir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def package(project, request):
|
|
|
|
with chdir(project.strpath):
|
2015-09-20 18:35:11 +02:00
|
|
|
cmd = 'setup.py bdist_wheel'
|
2015-09-20 19:26:22 +02:00
|
|
|
assert _run_python(cmd) == 0
|
|
|
|
pkgs = list(project.join('dist').visit('centodeps*.whl'))
|
2015-09-19 02:36:43 +02:00
|
|
|
assert len(pkgs) == 1
|
|
|
|
pkg = path.local(pkgs[0])
|
|
|
|
assert pkg.check()
|
|
|
|
|
|
|
|
return pkg
|
2015-09-19 01:02:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def packdir(package):
|
|
|
|
return package.dirpath()
|
|
|
|
|
|
|
|
|
|
|
|
open_port = 8081
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def open_server(packdir, request):
|
2015-09-20 19:26:22 +02:00
|
|
|
srv = _run_server(packdir, open_port, authed=False)
|
2015-09-19 01:02:27 +02:00
|
|
|
fin = functools.partial(_kill_server, srv)
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
|
|
|
|
return srv
|
|
|
|
|
|
|
|
|
|
|
|
protected_port = 8082
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def protected_server(packdir, request):
|
2015-09-20 19:26:22 +02:00
|
|
|
srv = _run_server(packdir, protected_port, authed=True)
|
2015-09-19 01:02:27 +02:00
|
|
|
fin = functools.partial(_kill_server, srv)
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
|
|
|
|
return srv
|
2015-09-17 19:58:22 +02:00
|
|
|
|
|
|
|
|
2015-09-18 20:20:01 +02:00
|
|
|
@pytest.fixture
|
2015-09-19 01:02:27 +02:00
|
|
|
def empty_packdir(tmpdir):
|
|
|
|
return tmpdir.mkdir("dists")
|
|
|
|
|
|
|
|
|
|
|
|
def _build_url(port, user='', pswd=''):
|
|
|
|
auth = '%s:%s@' % (user, pswd) if user or pswd else ''
|
|
|
|
return 'http://%slocalhost:%s' % (auth, port)
|
2015-09-18 19:05:07 +02:00
|
|
|
|
2015-09-17 19:58:22 +02:00
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
def _run_pip(cmd):
|
|
|
|
ncmd = ("--disable-pip-version-check --retries 0 --timeout 5"
|
|
|
|
" --no-input %s"
|
|
|
|
) % cmd
|
|
|
|
print('PIP: %s' % ncmd)
|
2015-09-18 20:20:01 +02:00
|
|
|
return pip.main(ncmd.split())
|
|
|
|
|
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
def _run_pip_install(cmd, port, install_dir, user=None, pswd=None):
|
|
|
|
url = _build_url(port, user, pswd)
|
2015-09-19 02:36:43 +02:00
|
|
|
ncmd = '-vv install --download %s -i %s %s' % (install_dir, url, cmd)
|
2015-09-19 01:02:27 +02:00
|
|
|
return _run_pip(ncmd)
|
|
|
|
|
2015-09-18 20:20:01 +02:00
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def pipdir(tmpdir):
|
|
|
|
return tmpdir.mkdir("pip")
|
|
|
|
|
|
|
|
|
|
|
|
def test_pipInstall_packageNotFound(empty_packdir, port, pipdir, package):
|
|
|
|
with new_server(empty_packdir, port) as srv:
|
|
|
|
cmd = "centodeps"
|
|
|
|
assert _run_pip_install(cmd, port, pipdir) != 0
|
|
|
|
assert not pipdir.listdir()
|
|
|
|
|
|
|
|
|
|
|
|
def test_pipInstall_openOk(open_server, package, pipdir):
|
|
|
|
cmd = "centodeps"
|
|
|
|
assert _run_pip_install(cmd, open_server.port, pipdir) == 0
|
|
|
|
assert pipdir.join(package.basename).check()
|
|
|
|
|
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
def test_pipInstall_authedFails(protected_server, pipdir):
|
2015-09-19 01:02:27 +02:00
|
|
|
cmd = "centodeps"
|
|
|
|
assert _run_pip_install(cmd, protected_server.port, pipdir) != 0
|
|
|
|
assert not pipdir.listdir()
|
|
|
|
|
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
def test_pipInstall_authedOk(protected_server, package, pipdir):
|
2015-09-19 01:02:27 +02:00
|
|
|
cmd = "centodeps"
|
|
|
|
assert _run_pip_install(cmd, protected_server.port, pipdir,
|
|
|
|
user='a', pswd='a') == 0
|
|
|
|
assert pipdir.join(package.basename).check()
|
2015-09-18 20:20:01 +02:00
|
|
|
|
|
|
|
|
2015-09-20 18:35:11 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def pypirc(port):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
def update_pypirc(pypirc, port, user='foo', pswd='bar'):
|
|
|
|
url = _build_url(port, None, None)
|
|
|
|
pypirc.update({
|
|
|
|
'repository': url,
|
|
|
|
'username': user,
|
|
|
|
'password': pswd,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2015-09-20 19:26:22 +02:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def pypirc_file(txt):
|
|
|
|
pypirc_path = path.local('~/.pypirc', expanduser=1)
|
|
|
|
old_pypirc = pypirc_path.read() if pypirc_path.check() else None
|
|
|
|
pypirc_path.write(txt)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
if old_pypirc:
|
|
|
|
pypirc_path.write(old_pypirc)
|
|
|
|
else:
|
|
|
|
pypirc_path.remove()
|
|
|
|
|
|
|
|
|
2015-09-21 03:51:14 +02:00
|
|
|
@pytest.mark.parametrize("pkg_frmt", ['bdist', 'bdist_wheel'])
|
|
|
|
def test_setuptoolsUpload_open(empty_packdir, port, project, package,
|
|
|
|
pkg_frmt):
|
2015-09-20 19:26:22 +02:00
|
|
|
with new_server(empty_packdir, port):
|
|
|
|
with chdir(project.strpath):
|
|
|
|
url = _build_url(port, None, None)
|
2015-09-21 03:51:14 +02:00
|
|
|
cmd = "setup.py -vvv %s upload -r %s" % (pkg_frmt, url)
|
|
|
|
for i in range(5):
|
|
|
|
print('++Attempt #%s' % i)
|
|
|
|
assert _run_python(cmd) == 0
|
2015-09-20 19:26:22 +02:00
|
|
|
time.sleep(1)
|
|
|
|
assert len(empty_packdir.listdir()) == 1
|
|
|
|
|
|
|
|
|
2015-09-21 03:51:14 +02:00
|
|
|
@pytest.mark.parametrize("pkg_frmt", ['bdist', 'bdist_wheel'])
|
2015-09-20 19:26:22 +02:00
|
|
|
def test_setuptoolsUpload_authed(empty_packdir, port, project, package,
|
2015-09-21 03:51:14 +02:00
|
|
|
pkg_frmt, monkeypatch):
|
2015-09-20 19:26:22 +02:00
|
|
|
url = _build_url(port)
|
|
|
|
with pypirc_file(dedent("""\
|
|
|
|
[distutils]
|
|
|
|
index-servers: test
|
|
|
|
|
|
|
|
[test]
|
|
|
|
repository: %s
|
|
|
|
username: a
|
|
|
|
password: a
|
|
|
|
""" % url)):
|
|
|
|
with new_server(empty_packdir, port, authed=True):
|
|
|
|
with chdir(project.strpath):
|
2015-09-21 03:51:14 +02:00
|
|
|
cmd = "setup.py -vvv %s register -r test upload -r test" % pkg_frmt
|
|
|
|
for i in range(5):
|
|
|
|
print('++Attempt #%s' % i)
|
|
|
|
assert _run_python(cmd) == 0
|
2015-09-21 04:38:47 +02:00
|
|
|
time.sleep(1)
|
2015-09-20 19:26:22 +02:00
|
|
|
assert len(empty_packdir.listdir()) == 1
|
|
|
|
|
|
|
|
|
2015-09-19 02:36:43 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def uploader(pypirc, monkeypatch):
|
|
|
|
from twine.commands import upload
|
|
|
|
monkeypatch.setattr(upload.utils, 'get_repository_from_config',
|
|
|
|
lambda *x: pypirc)
|
|
|
|
return upload
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def registerer(pypirc, monkeypatch):
|
|
|
|
from twine.commands import register
|
|
|
|
monkeypatch.setattr(register.utils, 'get_repository_from_config',
|
|
|
|
lambda *x: pypirc)
|
|
|
|
return register
|
|
|
|
|
|
|
|
|
2015-09-18 20:20:01 +02:00
|
|
|
@pytest.mark.skipif(sys.version_info[:2] == (3, 2),
|
|
|
|
reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)")
|
2015-09-20 18:35:11 +02:00
|
|
|
def test_twineUpload_open(empty_packdir, port, package, uploader, pypirc):
|
|
|
|
user, pswd = 'foo', 'bar'
|
|
|
|
update_pypirc(pypirc, port, user=user, pswd=pswd)
|
|
|
|
with new_server(empty_packdir, port):
|
2015-09-20 19:26:22 +02:00
|
|
|
uploader.upload([package.strpath], repository='test',
|
2015-09-18 19:05:07 +02:00
|
|
|
sign=None, identity=None,
|
2015-09-20 18:35:11 +02:00
|
|
|
username=user, password=pswd,
|
|
|
|
comment=None, sign_with=None,
|
|
|
|
config_file=None, skip_existing=None)
|
|
|
|
time.sleep(1)
|
2015-09-20 19:26:22 +02:00
|
|
|
assert len(empty_packdir.listdir()) == 1
|
2015-09-20 18:35:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info[:2] == (3, 2),
|
|
|
|
reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)")
|
2015-09-20 19:26:22 +02:00
|
|
|
def test_twineUpload_authed(empty_packdir, port, package, uploader, pypirc):
|
2015-09-20 18:35:11 +02:00
|
|
|
user, pswd = 'a', 'a'
|
|
|
|
update_pypirc(pypirc, port, user=user, pswd=pswd)
|
2015-09-20 19:26:22 +02:00
|
|
|
with new_server(empty_packdir, port, authed=False):
|
|
|
|
uploader.upload([package.strpath], repository='test',
|
2015-09-20 18:35:11 +02:00
|
|
|
sign=None, identity=None,
|
|
|
|
username=user, password=pswd,
|
2015-09-18 19:05:07 +02:00
|
|
|
comment=None, sign_with=None,
|
|
|
|
config_file=None, skip_existing=None)
|
2015-09-17 19:58:22 +02:00
|
|
|
time.sleep(1)
|
2015-09-20 19:26:22 +02:00
|
|
|
assert len(empty_packdir.listdir()) == 1
|
2015-09-18 19:05:07 +02:00
|
|
|
|
2015-09-19 01:02:27 +02:00
|
|
|
assert empty_packdir.join(
|
|
|
|
package.basename).check(), (package.basename, empty_packdir.listdir())
|
|
|
|
|
|
|
|
|
2015-09-20 18:35:11 +02:00
|
|
|
@pytest.mark.skipif(sys.version_info[:2] == (3, 2),
|
|
|
|
reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)")
|
|
|
|
def test_twineRegister_open(open_server, package, registerer, pypirc):
|
|
|
|
srv = open_server
|
|
|
|
update_pypirc(pypirc, srv.port)
|
|
|
|
registerer.register(package.strpath, repository='test',
|
|
|
|
username='foo', password='bar',
|
|
|
|
comment=None, config_file=None)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(sys.version_info[:2] == (3, 2),
|
|
|
|
reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)")
|
2015-09-20 19:26:22 +02:00
|
|
|
def test_twineRegister_authedOk(protected_server, package, registerer, pypirc):
|
2015-09-20 18:35:11 +02:00
|
|
|
srv = protected_server
|
|
|
|
user, pswd = 'a', 'a'
|
|
|
|
update_pypirc(pypirc, srv.port, user=user, pswd=pswd)
|
|
|
|
registerer.register(package.strpath, repository='test',
|
|
|
|
username=user, password=pswd,
|
|
|
|
comment=None, config_file=None)
|