From 4bb758da30969bc40ccbc3cd260d4d422a12c288 Mon Sep 17 00:00:00 2001 From: ankostis on tokoti Date: Sat, 19 Sep 2015 01:02:27 +0200 Subject: [PATCH] PIP test-server. + Rework test_server fictures with ports, pswds, and package-dirs. + Increase dep: pip>=7 --- requirements/dev.pip | 2 +- setup.py | 2 +- tests/htpasswd.a.a | 1 + tests/test_server.py | 220 ++++++++++++++++++++++++++++++++----------- 4 files changed, 168 insertions(+), 57 deletions(-) create mode 100644 tests/htpasswd.a.a diff --git a/requirements/dev.pip b/requirements/dev.pip index 2b23adb..0f29e86 100644 --- a/requirements/dev.pip +++ b/requirements/dev.pip @@ -5,7 +5,7 @@ -r exe.pip -pip>6 +pip>=7 setuptools setuptools-git>=0.3 tox diff --git a/setup.py b/setup.py index 7feaf07..201c8fd 100644 --- a/setup.py +++ b/setup.py @@ -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') diff --git a/tests/htpasswd.a.a b/tests/htpasswd.a.a new file mode 100644 index 0000000..4e895ba --- /dev/null +++ b/tests/htpasswd.a.a @@ -0,0 +1 @@ +a:$apr1$RefAZFGO$plN5ZXI60KJCKk5oijFvD. diff --git a/tests/test_server.py b/tests/test_server.py index 3b84025..0ea78c3 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -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