PIP test-server.

+ Rework test_server fictures with ports, pswds, and package-dirs.
+ Increase dep: pip>=7
This commit is contained in:
ankostis on tokoti 2015-09-19 01:02:27 +02:00
parent db24b882f3
commit 4bb758da30
4 changed files with 168 additions and 57 deletions

View File

@ -5,7 +5,7 @@
-r exe.pip
pip>6
pip>=7
setuptools
setuptools-git>=0.3
tox

View File

@ -9,7 +9,7 @@ if sys.version_info >= (3, 0):
else:
exec("def do_exec(co, loc): exec co in loc\n")
tests_require = ['pytest>=2.3', 'tox', 'twine']
tests_require = ['pytest>=2.3', 'tox', 'twine', 'pip>=7']
if sys.version_info <= (3, 2):
tests_require.append('mock')

1
tests/htpasswd.a.a Normal file
View File

@ -0,0 +1 @@
a:$apr1$RefAZFGO$plN5ZXI60KJCKk5oijFvD.

View File

@ -1,6 +1,10 @@
#! /usr/bin/env py.test
from __future__ import print_function
from collections import namedtuple
import contextlib
import functools
import os
import subprocess
import sys
import time
@ -9,73 +13,169 @@ import pip
from py import path # @UnresolvedImport
import pytest
localhost = "http://localhost:8080"
_BUFF_SIZE = 4096
_port = 8090
pypirc = {
"repository": localhost,
"username": 'a',
"password": 'a'
}
@pytest.fixture
def port():
global _port
_port += 1
return _port
Srv = namedtuple('Srv', ('proc', 'port', 'pdir'))
def _run_server(packdir, port, with_password, other_cli=''):
pswd_opt_choices = {
True: "-Ptests/htpasswd.a.a -a update,download",
False: "-P. -a."
}
pswd_opts = pswd_opt_choices[with_password]
cmd = "python -m pypiserver.__main__ -v --overwrite -p %s %s %s %s" % (
port, pswd_opts, other_cli, packdir)
proc = subprocess.Popen(cmd.split(), bufsize=_BUFF_SIZE)
time.sleep(1)
return Srv(proc, int(port), packdir)
def _kill_server(srv):
print('Killing %s' % (srv,))
try:
srv.proc.terminate()
time.sleep(1)
finally:
srv.proc.kill()
@contextlib.contextmanager
def server(packdir, with_password=False):
pswd_opt_choices = {True: "-Phtpaswd.a.a", False: "-P. -a."}
pswd_opts = pswd_opt_choices[with_password]
cmd = "python -m pypiserver.__main__ -v %s %s" % (pswd_opts, packdir)
proc = subprocess.Popen(cmd.split())
def new_server(packdir, port, with_password=False, other_cli=''):
srv = _run_server(packdir, port,
with_password=with_password, other_cli=other_cli)
try:
yield proc
yield srv
finally:
try:
proc.terminate()
time.sleep(1)
finally:
proc.kill()
_kill_server(srv)
@pytest.fixture
def srv_packdir(tmpdir):
return tmpdir.mkdir("dists")
@pytest.fixture
def uploader(monkeypatch):
from twine.commands import upload
monkeypatch.setattr(upload.utils, 'get_repository_from_config',
lambda *x: pypirc)
return upload
@pytest.fixture
@pytest.fixture(scope='module')
def package():
dist_path = path.local('tests/centodeps/wheelhouse')
pkgs = list(dist_path.visit('centodeps*.whl'))
assert len(pkgs) == 1
return pkgs[0]
pkg = path.local(pkgs[0])
assert pkg.check()
return pkg
def run_pip(cmd):
ncmd = "--disable-pip-version-check %s" % cmd
@pytest.fixture(scope='module')
def packdir(package):
return package.dirpath()
open_port = 8081
@pytest.fixture(scope='module')
def open_server(packdir, request):
srv = _run_server(packdir, open_port, with_password=False)
fin = functools.partial(_kill_server, srv)
request.addfinalizer(fin)
return srv
protected_port = 8082
@pytest.fixture(scope='module')
def protected_server(packdir, request):
srv = _run_server(packdir, protected_port, with_password=True)
fin = functools.partial(_kill_server, srv)
request.addfinalizer(fin)
return srv
@pytest.fixture
def pypirc(port):
return {
"repository": "http://localhost:%s" % port,
"username": 'a',
"password": 'a'
}
@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 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)
def _run_pip(cmd):
ncmd = ("--disable-pip-version-check --retries 0 --timeout 5"
" --no-input %s"
) % cmd
print('PIP: %s' % ncmd)
return pip.main(ncmd.split())
def test_pip(srv_packdir, package):
with server(package.dirname):
time.sleep(1)
cmd = "install -i %s centodeps" % localhost
assert pip.main(cmd.split()) == 0
def _run_pip_install(cmd, port, install_dir, user=None, pswd=None):
url = _build_url(port, user, pswd)
ncmd = 'install --download %s -i %s %s' % (install_dir, url, cmd)
return _run_pip(ncmd)
cmd = "uninstall centodeps --yes"
assert pip.main(cmd.split()) == 0
@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()
def test_pipInstall_protectedFails(protected_server, pipdir):
cmd = "centodeps"
assert _run_pip_install(cmd, protected_server.port, pipdir) != 0
assert not pipdir.listdir()
def test_pipInstall_protectedOk(protected_server, package, pipdir):
cmd = "centodeps"
assert _run_pip_install(cmd, protected_server.port, pipdir,
user='a', pswd='a') == 0
assert pipdir.join(package.basename).check()
@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_upload(srv_packdir, package, uploader):
with server(srv_packdir):
time.sleep(1)
def test_upload(empty_packdir, port, package, uploader):
with new_server(empty_packdir, port) as srv:
uploader.upload([str(package)], repository='test',
sign=None, identity=None,
username='a', password='a',
@ -83,14 +183,24 @@ def test_upload(srv_packdir, package, uploader):
config_file=None, skip_existing=None)
time.sleep(1)
assert srv_packdir.join(package.basename).check(), srv_packdir.listdir()
assert empty_packdir.join(
package.basename).check(), (package.basename, empty_packdir.listdir())
#
# def test_register_upload(srv_packdir, package):
# with server(package.dirname) as srv:
# time.sleep(1)
# cmd = "pip register -i %s upload centodeps" % localhost
# assert pip.main(cmd.split()) == 0
#
# cmd = "uninstall centodeps --yes"
# assert pip.main(cmd.split()) == 0
#
# @contextlib.contextmanager
# def chdir(d):
# old_d = os.getcwd()
# try:
# os.chdir('tests/centodeps')
# yield
# finally:
# os.chdir(old_d)
# def test_register_upload(open_server, pypirc, package, pipdir):
# with chdir('tests/centodeps'):
# url = _build_url(open_server.port, user='a', pswd='a')
# cmd = "python setup.py register sdist upload -r %s" % url
# assert subprocess.Popen(cmd.split(), bufsize=_BUFF_SIZE) == 0