2017-10-19 00:22:29 -05:00
|
|
|
import os
|
2017-10-24 18:03:31 -05:00
|
|
|
from lxml import etree as ET
|
2017-10-19 00:22:29 -05:00
|
|
|
from osc import conf
|
|
|
|
from osc.core import get_request
|
2017-10-24 18:03:31 -05:00
|
|
|
from osc.core import http_GET
|
|
|
|
from osc.core import makeurl
|
2017-10-19 00:22:29 -05:00
|
|
|
import subprocess
|
|
|
|
import unittest
|
|
|
|
|
2017-10-24 18:01:48 -05:00
|
|
|
OSCRC = os.path.expanduser('~/.oscrc-test')
|
2018-01-03 16:19:28 -06:00
|
|
|
OSCCOOKIEJAR = os.path.expanduser('~/.osc_cookiejar-test')
|
2017-10-19 00:22:29 -05:00
|
|
|
APIURL = 'local-test'
|
|
|
|
|
|
|
|
class OBSLocalTestCase(unittest.TestCase):
|
|
|
|
script = None
|
|
|
|
script_apiurl = True
|
|
|
|
script_debug = True
|
|
|
|
script_debug_osc = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-10-24 18:00:47 -05:00
|
|
|
# TODO #1214: Workaround for tests/obs.py's lack of cleanup.
|
|
|
|
import httpretty
|
|
|
|
httpretty.disable()
|
2017-10-19 00:22:29 -05:00
|
|
|
|
|
|
|
def setUp(self):
|
2018-01-03 16:19:28 -06:00
|
|
|
if os.path.exists(OSCCOOKIEJAR):
|
|
|
|
# Avoid stale cookiejar since local OBS may be completely reset.
|
|
|
|
os.remove(OSCCOOKIEJAR)
|
|
|
|
|
2018-01-03 16:22:13 -06:00
|
|
|
self.osc_user('Admin')
|
2017-10-24 18:01:48 -05:00
|
|
|
self.apiurl = conf.config['apiurl']
|
2017-10-24 18:03:31 -05:00
|
|
|
self.assertOBS()
|
2017-10-19 00:22:29 -05:00
|
|
|
|
2017-10-24 18:03:31 -05:00
|
|
|
def assertOBS(self):
|
|
|
|
url = makeurl(self.apiurl, ['about'])
|
|
|
|
root = ET.parse(http_GET(url)).getroot()
|
|
|
|
self.assertEqual(root.tag, 'about')
|
2017-10-19 00:22:29 -05:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def oscrc(userid):
|
|
|
|
with open(OSCRC, 'w+') as f:
|
|
|
|
f.write('\n'.join([
|
|
|
|
'[general]',
|
|
|
|
'apiurl = http://0.0.0.0:3000',
|
2018-01-03 16:19:28 -06:00
|
|
|
'cookiejar = {}'.format(OSCCOOKIEJAR),
|
2017-10-19 00:22:29 -05:00
|
|
|
'[http://0.0.0.0:3000]',
|
2017-10-24 18:02:20 -05:00
|
|
|
'user = {}'.format(userid),
|
|
|
|
'pass = opensuse',
|
|
|
|
'email = {}@example.com'.format(userid),
|
|
|
|
'aliases = {}'.format(APIURL),
|
2017-10-19 00:22:29 -05:00
|
|
|
'',
|
|
|
|
]))
|
|
|
|
|
|
|
|
def osc_user(self, userid):
|
|
|
|
self.oscrc(userid)
|
|
|
|
|
2018-01-03 16:22:13 -06:00
|
|
|
# Rather than modify userid and email, just re-parse entire config and
|
|
|
|
# reset authentication by clearing opener to avoid edge-cases.
|
|
|
|
self.oscParse()
|
|
|
|
|
|
|
|
def oscParse(self):
|
|
|
|
# Otherwise, will stick to first user for a given apiurl.
|
|
|
|
conf._build_opener.last_opener = (None, None)
|
|
|
|
|
|
|
|
# Otherwise, will not re-parse same config file.
|
|
|
|
if 'cp' in conf.get_configParser.__dict__:
|
|
|
|
del conf.get_configParser.cp
|
|
|
|
|
|
|
|
conf.get_config(override_conffile=OSCRC,
|
|
|
|
override_no_keyring=True,
|
|
|
|
override_no_gnome_keyring=True)
|
|
|
|
|
2017-10-24 18:04:30 -05:00
|
|
|
def execute_script(self, args):
|
2017-10-19 00:22:29 -05:00
|
|
|
if self.script:
|
|
|
|
args.insert(0, self.script)
|
|
|
|
if self.script_debug:
|
|
|
|
args.insert(1, '--debug')
|
|
|
|
if self.script_debug_osc:
|
|
|
|
args.insert(1, '--osc-debug')
|
2017-10-24 18:06:33 -05:00
|
|
|
args.insert(0, '-p')
|
|
|
|
args.insert(0, 'run')
|
|
|
|
args.insert(0, 'coverage')
|
2017-10-19 00:22:29 -05:00
|
|
|
|
2017-10-24 18:04:30 -05:00
|
|
|
self.execute(args)
|
|
|
|
|
|
|
|
def execute_osc(self, args):
|
|
|
|
# The wrapper allows this to work properly when osc installed via pip.
|
|
|
|
args.insert(0, 'osc-wrapper.py')
|
|
|
|
self.execute(args)
|
|
|
|
|
|
|
|
def execute(self, args):
|
2017-10-19 00:22:29 -05:00
|
|
|
print('$ ' + ' '.join(args)) # Print command for debugging.
|
|
|
|
try:
|
2017-10-24 18:04:30 -05:00
|
|
|
env = os.environ
|
|
|
|
env['OSC_CONFIG'] = OSCRC
|
|
|
|
self.output = subprocess.check_output(args, stderr=subprocess.STDOUT, env=env)
|
2017-10-19 00:22:29 -05:00
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
print(e.output)
|
|
|
|
raise e
|
|
|
|
print(self.output) # For debugging assertion failures.
|
|
|
|
|
|
|
|
def assertOutput(self, string):
|
|
|
|
self.assertTrue(string in self.output, '[MISSING] ' + string)
|
|
|
|
|
|
|
|
def assertReview(self, rid, **kwargs):
|
|
|
|
request = get_request(self.apiurl, rid)
|
|
|
|
for review in request.reviews:
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
if hasattr(review, key) and getattr(review, key) == value[0]:
|
|
|
|
self.assertEqual(review.state, value[1], '{}={} not {}'.format(key, value[0], value[1]))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.fail('{} not found'.format(kwargs))
|