2010-08-24 15:11:01 +02:00
|
|
|
import unittest
|
|
|
|
import osc.core
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import sys
|
2010-08-30 18:29:24 +02:00
|
|
|
from xml.etree import cElementTree as ET
|
2010-08-24 15:11:01 +02:00
|
|
|
EXPECTED_REQUESTS = []
|
|
|
|
|
2013-04-10 11:34:59 +02:00
|
|
|
if sys.version_info[0:2] in ((2, 6), (2, 7)):
|
|
|
|
bytes = lambda x, *args: x
|
|
|
|
|
2013-04-09 14:22:45 +02:00
|
|
|
try:
|
2013-04-10 11:34:59 +02:00
|
|
|
#python 2.x
|
2013-04-09 14:22:45 +02:00
|
|
|
from cStringIO import StringIO
|
|
|
|
from urllib2 import HTTPHandler, addinfourl, build_opener
|
2013-04-10 11:34:59 +02:00
|
|
|
from urlparse import urlparse, parse_qs
|
2013-04-09 14:22:45 +02:00
|
|
|
except ImportError:
|
|
|
|
from io import StringIO
|
|
|
|
from urllib.request import HTTPHandler, addinfourl, build_opener
|
2013-04-10 11:34:59 +02:00
|
|
|
from urllib.parse import urlparse, parse_qs
|
|
|
|
|
|
|
|
def urlcompare(url, *args):
|
|
|
|
"""compare all components of url except query string - it is converted to
|
|
|
|
dict, therefor different ordering does not makes url's different, as well
|
|
|
|
as quoting of a query string"""
|
|
|
|
|
|
|
|
components = urlparse(url)
|
|
|
|
query_args = parse_qs(components.query)
|
|
|
|
components = components._replace(query=None)
|
|
|
|
|
|
|
|
if not args:
|
|
|
|
return False
|
|
|
|
|
|
|
|
for url in args:
|
|
|
|
components2 = urlparse(url)
|
|
|
|
query_args2 = parse_qs(components2.query)
|
|
|
|
components2 = components2._replace(query=None)
|
|
|
|
|
|
|
|
if components != components2 or \
|
|
|
|
query_args != query_args2:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2013-04-09 14:22:45 +02:00
|
|
|
|
2010-08-24 15:11:01 +02:00
|
|
|
class RequestWrongOrder(Exception):
|
2010-08-30 18:29:24 +02:00
|
|
|
"""raised if an unexpected request is issued to urllib2"""
|
2010-08-24 15:11:01 +02:00
|
|
|
def __init__(self, url, exp_url, method, exp_method):
|
|
|
|
Exception.__init__(self)
|
|
|
|
self.url = url
|
|
|
|
self.exp_url = exp_url
|
|
|
|
self.method = method
|
|
|
|
self.exp_method = exp_method
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s, %s, %s, %s' % (self.url, self.exp_url, self.method, self.exp_method)
|
|
|
|
|
2010-08-30 18:29:24 +02:00
|
|
|
class RequestDataMismatch(Exception):
|
|
|
|
"""raised if POSTed or PUTed data doesn't match with the expected data"""
|
|
|
|
def __init__(self, url, got, exp):
|
|
|
|
self.url = url
|
|
|
|
self.got = got
|
|
|
|
self.exp = exp
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '%s, %s, %s' % (self.url, self.got, self.exp)
|
|
|
|
|
2013-04-09 14:22:45 +02:00
|
|
|
class MyHTTPHandler(HTTPHandler):
|
2010-08-24 15:11:01 +02:00
|
|
|
def __init__(self, exp_requests, fixtures_dir):
|
2013-04-09 14:22:45 +02:00
|
|
|
HTTPHandler.__init__(self)
|
2010-08-24 15:11:01 +02:00
|
|
|
self.__exp_requests = exp_requests
|
|
|
|
self.__fixtures_dir = fixtures_dir
|
|
|
|
|
|
|
|
def http_open(self, req):
|
|
|
|
r = self.__exp_requests.pop(0)
|
2013-04-10 11:34:59 +02:00
|
|
|
if not urlcompare(req.get_full_url(), r[1]) or req.get_method() != r[0]:
|
2010-08-24 15:11:01 +02:00
|
|
|
raise RequestWrongOrder(req.get_full_url(), r[1], req.get_method(), r[0])
|
2010-08-30 18:29:24 +02:00
|
|
|
if req.get_method() in ('GET', 'DELETE'):
|
2010-08-24 15:11:01 +02:00
|
|
|
return self.__mock_GET(r[1], **r[2])
|
2010-08-30 18:29:24 +02:00
|
|
|
elif req.get_method() in ('PUT', 'POST'):
|
|
|
|
return self.__mock_PUT(req, **r[2])
|
2010-08-24 15:11:01 +02:00
|
|
|
|
|
|
|
def __mock_GET(self, fullurl, **kwargs):
|
|
|
|
return self.__get_response(fullurl, **kwargs)
|
|
|
|
|
2010-08-30 18:29:24 +02:00
|
|
|
def __mock_PUT(self, req, **kwargs):
|
|
|
|
exp = kwargs.get('exp', None)
|
2013-04-09 14:22:45 +02:00
|
|
|
if exp is not None and 'expfile' in kwargs:
|
2010-08-30 18:29:24 +02:00
|
|
|
raise RuntimeError('either specify exp or expfile')
|
2013-04-09 14:22:45 +02:00
|
|
|
elif 'expfile' in kwargs:
|
2010-08-30 18:29:24 +02:00
|
|
|
exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'r').read()
|
2010-09-03 17:46:01 +02:00
|
|
|
elif exp is None:
|
|
|
|
raise RuntimeError('exp or expfile required')
|
2010-08-30 18:29:24 +02:00
|
|
|
if exp is not None:
|
2015-08-13 14:13:08 +02:00
|
|
|
# use req.data instead of req.get_data() for python3 compatiblity
|
2015-08-13 13:40:16 +02:00
|
|
|
if req.data != bytes(exp, "utf-8"):
|
2010-09-03 17:46:01 +02:00
|
|
|
raise RequestDataMismatch(req.get_full_url(), repr(req.get_data()), repr(exp))
|
2010-08-30 18:29:24 +02:00
|
|
|
return self.__get_response(req.get_full_url(), **kwargs)
|
|
|
|
|
2010-08-24 15:11:01 +02:00
|
|
|
def __get_response(self, url, **kwargs):
|
|
|
|
f = None
|
2013-04-09 14:22:45 +02:00
|
|
|
if 'exception' in kwargs:
|
2010-08-30 18:29:24 +02:00
|
|
|
raise kwargs['exception']
|
2013-04-09 14:22:45 +02:00
|
|
|
if 'text' not in kwargs and 'file' in kwargs:
|
|
|
|
f = StringIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'r').read())
|
|
|
|
elif 'text' in kwargs and 'file' not in kwargs:
|
|
|
|
f = StringIO(kwargs['text'])
|
2010-08-24 15:11:01 +02:00
|
|
|
else:
|
|
|
|
raise RuntimeError('either specify text or file')
|
2013-04-09 14:22:45 +02:00
|
|
|
resp = addinfourl(f, {}, url)
|
2011-03-21 16:52:13 +01:00
|
|
|
resp.code = kwargs.get('code', 200)
|
2010-08-24 15:11:01 +02:00
|
|
|
resp.msg = ''
|
|
|
|
return resp
|
|
|
|
|
2010-08-30 18:29:24 +02:00
|
|
|
def urldecorator(method, fullurl, **kwargs):
|
2010-08-24 15:11:01 +02:00
|
|
|
def decorate(test_method):
|
|
|
|
def wrapped_test_method(*args):
|
2010-08-30 18:29:24 +02:00
|
|
|
addExpectedRequest(method, fullurl, **kwargs)
|
2010-08-24 15:11:01 +02:00
|
|
|
test_method(*args)
|
2010-08-24 19:06:49 +02:00
|
|
|
# "rename" method otherwise we cannot specify a TestCaseClass.testName
|
|
|
|
# cmdline arg when using unittest.main()
|
|
|
|
wrapped_test_method.__name__ = test_method.__name__
|
2010-08-24 15:11:01 +02:00
|
|
|
return wrapped_test_method
|
|
|
|
return decorate
|
|
|
|
|
2010-08-30 18:29:24 +02:00
|
|
|
def GET(fullurl, **kwargs):
|
|
|
|
return urldecorator('GET', fullurl, **kwargs)
|
|
|
|
|
|
|
|
def PUT(fullurl, **kwargs):
|
|
|
|
return urldecorator('PUT', fullurl, **kwargs)
|
|
|
|
|
|
|
|
def POST(fullurl, **kwargs):
|
|
|
|
return urldecorator('POST', fullurl, **kwargs)
|
|
|
|
|
|
|
|
def DELETE(fullurl, **kwargs):
|
|
|
|
return urldecorator('DELETE', fullurl, **kwargs)
|
|
|
|
|
2010-08-24 15:11:01 +02:00
|
|
|
def addExpectedRequest(method, url, **kwargs):
|
|
|
|
global EXPECTED_REQUESTS
|
|
|
|
EXPECTED_REQUESTS.append((method, url, kwargs))
|
|
|
|
|
|
|
|
class OscTestCase(unittest.TestCase):
|
2010-12-30 01:57:41 +01:00
|
|
|
def setUp(self, copytree=True):
|
2013-01-18 22:58:53 +01:00
|
|
|
oscrc = os.path.join(self._get_fixtures_dir(), 'oscrc')
|
|
|
|
osc.core.conf.get_config(override_conffile=oscrc,
|
2013-01-18 14:27:01 +01:00
|
|
|
override_no_keyring=True, override_no_gnome_keyring=True)
|
2013-01-18 22:58:53 +01:00
|
|
|
os.environ['OSC_CONFIG'] = oscrc
|
|
|
|
|
2010-08-24 15:11:01 +02:00
|
|
|
self.tmpdir = tempfile.mkdtemp(prefix='osc_test')
|
2010-12-30 01:57:41 +01:00
|
|
|
if copytree:
|
|
|
|
shutil.copytree(os.path.join(self._get_fixtures_dir(), 'osctest'), os.path.join(self.tmpdir, 'osctest'))
|
2010-08-24 15:11:01 +02:00
|
|
|
global EXPECTED_REQUESTS
|
|
|
|
EXPECTED_REQUESTS = []
|
2013-04-09 14:22:45 +02:00
|
|
|
osc.core.conf._build_opener = lambda u: build_opener(MyHTTPHandler(EXPECTED_REQUESTS, self._get_fixtures_dir()))
|
2010-08-24 15:11:01 +02:00
|
|
|
self.stdout = sys.stdout
|
2013-04-09 14:22:45 +02:00
|
|
|
sys.stdout = StringIO()
|
2010-08-24 15:11:01 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.assertTrue(len(EXPECTED_REQUESTS) == 0)
|
|
|
|
sys.stdout = self.stdout
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.tmpdir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _get_fixtures_dir(self):
|
|
|
|
raise NotImplementedError('subclasses should implement this method')
|
2010-08-24 16:06:10 +02:00
|
|
|
|
|
|
|
def _change_to_pkg(self, name):
|
|
|
|
os.chdir(os.path.join(self.tmpdir, 'osctest', name))
|
2010-08-24 18:06:47 +02:00
|
|
|
|
2010-08-30 13:46:49 +02:00
|
|
|
def _check_list(self, fname, exp):
|
2010-08-24 18:06:47 +02:00
|
|
|
fname = os.path.join('.osc', fname)
|
|
|
|
self.assertTrue(os.path.exists(fname))
|
|
|
|
self.assertEqual(open(fname, 'r').read(), exp)
|
|
|
|
|
2010-08-25 11:20:25 +02:00
|
|
|
def _check_addlist(self, exp):
|
2010-08-30 13:46:49 +02:00
|
|
|
self._check_list('_to_be_added', exp)
|
2010-08-25 11:20:25 +02:00
|
|
|
|
|
|
|
def _check_deletelist(self, exp):
|
2010-08-30 13:46:49 +02:00
|
|
|
self._check_list('_to_be_deleted', exp)
|
2010-08-25 11:20:25 +02:00
|
|
|
|
|
|
|
def _check_conflictlist(self, exp):
|
2010-08-30 13:46:49 +02:00
|
|
|
self._check_list('_in_conflict', exp)
|
2010-08-25 11:20:25 +02:00
|
|
|
|
2010-08-24 18:06:47 +02:00
|
|
|
def _check_status(self, p, fname, exp):
|
|
|
|
self.assertEqual(p.status(fname), exp)
|
2010-08-30 18:29:24 +02:00
|
|
|
|
|
|
|
def _check_digests(self, fname, *skipfiles):
|
|
|
|
fname = os.path.join(self._get_fixtures_dir(), fname)
|
|
|
|
self.assertEqual(open(os.path.join('.osc', '_files'), 'r').read(), open(fname, 'r').read())
|
|
|
|
root = ET.parse(fname).getroot()
|
|
|
|
for i in root.findall('entry'):
|
|
|
|
if i.get('name') in skipfiles:
|
|
|
|
continue
|
|
|
|
self.assertTrue(os.path.exists(os.path.join('.osc', i.get('name'))))
|
|
|
|
self.assertEqual(osc.core.dgst(os.path.join('.osc', i.get('name'))), i.get('md5'))
|
2013-01-18 20:38:25 +01:00
|
|
|
|
|
|
|
def assertEqualMultiline(self, got, exp):
|
|
|
|
if (got + exp).find('\n') == -1:
|
|
|
|
self.assertEqual(got, exp)
|
|
|
|
else:
|
|
|
|
start_delim = "\n" + (" 8< ".join(["-----"] * 8)) + "\n"
|
|
|
|
end_delim = "\n" + (" >8 ".join(["-----"] * 8)) + "\n\n"
|
|
|
|
self.assertEqual(got, exp,
|
|
|
|
"got:" + start_delim + got + end_delim +
|
|
|
|
"expected:" + start_delim + exp + end_delim)
|