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

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

@ -9,7 +9,7 @@ if sys.version_info >= (3, 0):
else: else:
exec("def do_exec(co, loc): exec co in loc\n") 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): if sys.version_info <= (3, 2):
tests_require.append('mock') tests_require.append('mock')

1
tests/htpasswd.a.a Normal file

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

@ -1,6 +1,10 @@
#! /usr/bin/env py.test #! /usr/bin/env py.test
from __future__ import print_function
from collections import namedtuple
import contextlib import contextlib
import functools
import os
import subprocess import subprocess
import sys import sys
import time import time
@ -9,73 +13,169 @@ import pip
from py import path # @UnresolvedImport from py import path # @UnresolvedImport
import pytest import pytest
localhost = "http://localhost:8080" _BUFF_SIZE = 4096
_port = 8090
pypirc = {
"repository": localhost, @pytest.fixture
"username": 'a', def port():
"password": 'a' 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 @contextlib.contextmanager
def server(packdir, with_password=False): def new_server(packdir, port, with_password=False, other_cli=''):
pswd_opt_choices = {True: "-Phtpaswd.a.a", False: "-P. -a."} srv = _run_server(packdir, port,
pswd_opts = pswd_opt_choices[with_password] with_password=with_password, other_cli=other_cli)
cmd = "python -m pypiserver.__main__ -v %s %s" % (pswd_opts, packdir)
proc = subprocess.Popen(cmd.split())
try: try:
yield proc yield srv
finally: finally:
try: _kill_server(srv)
proc.terminate()
time.sleep(1)
finally:
proc.kill()
@pytest.fixture @pytest.fixture(scope='module')
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
def package(): def package():
dist_path = path.local('tests/centodeps/wheelhouse') dist_path = path.local('tests/centodeps/wheelhouse')
pkgs = list(dist_path.visit('centodeps*.whl')) pkgs = list(dist_path.visit('centodeps*.whl'))
assert len(pkgs) == 1 assert len(pkgs) == 1
return pkgs[0]
pkg = path.local(pkgs[0])
assert pkg.check()
return pkg
def run_pip(cmd): @pytest.fixture(scope='module')
ncmd = "--disable-pip-version-check %s" % cmd 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()) return pip.main(ncmd.split())
def test_pip(srv_packdir, package): def _run_pip_install(cmd, port, install_dir, user=None, pswd=None):
with server(package.dirname): url = _build_url(port, user, pswd)
time.sleep(1) ncmd = 'install --download %s -i %s %s' % (install_dir, url, cmd)
cmd = "install -i %s centodeps" % localhost return _run_pip(ncmd)
assert pip.main(cmd.split()) == 0
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), @pytest.mark.skipif(sys.version_info[:2] == (3, 2),
reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)") reason="urllib3 fails on twine (see https://travis-ci.org/ankostis/pypiserver/builds/81044993)")
def test_upload(srv_packdir, package, uploader): def test_upload(empty_packdir, port, package, uploader):
with server(srv_packdir): with new_server(empty_packdir, port) as srv:
time.sleep(1)
uploader.upload([str(package)], repository='test', uploader.upload([str(package)], repository='test',
sign=None, identity=None, sign=None, identity=None,
username='a', password='a', username='a', password='a',
@ -83,14 +183,24 @@ def test_upload(srv_packdir, package, uploader):
config_file=None, skip_existing=None) config_file=None, skip_existing=None)
time.sleep(1) 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) # @contextlib.contextmanager
# cmd = "pip register -i %s upload centodeps" % localhost # def chdir(d):
# assert pip.main(cmd.split()) == 0 # old_d = os.getcwd()
# # try:
# cmd = "uninstall centodeps --yes" # os.chdir('tests/centodeps')
# assert pip.main(cmd.split()) == 0 # 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