2015-09-17 19:58:22 +02:00
|
|
|
#! /usr/bin/env py.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-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 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-19 01:02:27 +02:00
|
|
|
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."
|
|
|
|
}
|
2015-09-18 20:20:01 +02:00
|
|
|
pswd_opts = pswd_opt_choices[with_password]
|
2015-09-19 01:02:27 +02:00
|
|
|
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,))
|
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
|
|
|
|
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 srv
|
|
|
|
finally:
|
|
|
|
_kill_server(srv)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def package():
|
|
|
|
dist_path = path.local('tests/centodeps/wheelhouse')
|
|
|
|
pkgs = list(dist_path.visit('centodeps*.whl'))
|
|
|
|
assert len(pkgs) == 1
|
|
|
|
|
|
|
|
pkg = path.local(pkgs[0])
|
|
|
|
assert pkg.check()
|
|
|
|
|
|
|
|
return pkg
|
|
|
|
|
|
|
|
|
|
|
|
@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
|
2015-09-17 19:58:22 +02:00
|
|
|
|
|
|
|
|
2015-09-18 19:05:07 +02:00
|
|
|
@pytest.fixture
|
2015-09-19 01:02:27 +02:00
|
|
|
def pypirc(port):
|
|
|
|
return {
|
|
|
|
"repository": "http://localhost:%s" % port,
|
|
|
|
"username": 'a',
|
|
|
|
"password": 'a'
|
|
|
|
}
|
2015-09-17 19:58:22 +02:00
|
|
|
|
2015-09-18 19:05:07 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
2015-09-19 01:02:27 +02:00
|
|
|
def uploader(pypirc, monkeypatch):
|
2015-09-18 19:05:07 +02:00
|
|
|
from twine.commands import upload
|
2015-09-17 20:18:09 +02:00
|
|
|
monkeypatch.setattr(upload.utils, 'get_repository_from_config',
|
2015-09-18 19:05:07 +02:00
|
|
|
lambda *x: pypirc)
|
|
|
|
return upload
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
ncmd = 'install --download %s -i %s %s' % (install_dir, url, cmd)
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
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-19 01:02:27 +02:00
|
|
|
def test_upload(empty_packdir, port, package, uploader):
|
|
|
|
with new_server(empty_packdir, port) as srv:
|
2015-09-18 20:20:01 +02:00
|
|
|
uploader.upload([str(package)], repository='test',
|
2015-09-18 19:05:07 +02:00
|
|
|
sign=None, identity=None,
|
|
|
|
username='a', password='a',
|
|
|
|
comment=None, sign_with=None,
|
|
|
|
config_file=None, skip_existing=None)
|
2015-09-17 19:58:22 +02:00
|
|
|
time.sleep(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())
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
# @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
|