2016-03-21 04:26:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-12-29 03:21:58 +00:00
|
|
|
"""
|
|
|
|
Test Library Functions
|
|
|
|
"""
|
2016-03-21 04:26:02 +00:00
|
|
|
import sys, traceback
|
2014-12-29 03:21:58 +00:00
|
|
|
import datetime, time
|
|
|
|
from plexapi import server
|
2016-04-04 03:17:29 +00:00
|
|
|
from plexapi.client import PlexClient
|
|
|
|
from plexapi.exceptions import NotFound
|
2016-04-07 05:39:04 +00:00
|
|
|
from plexapi.myplex import MyPlexAccount
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2016-03-15 15:04:53 +00:00
|
|
|
COLORS = {'blue':'\033[94m', 'green':'\033[92m', 'red':'\033[91m', 'yellow':'\033[93m', 'end':'\033[0m'}
|
|
|
|
|
|
|
|
|
|
|
|
registered = []
|
|
|
|
def register(tags=''):
|
|
|
|
def wrap2(func):
|
|
|
|
registered.append({'name':func.__name__, 'tags':tags.split(','), 'func':func})
|
2016-03-21 04:26:02 +00:00
|
|
|
def wrap1(*args, **kwargs): # noqa
|
2016-03-15 15:04:53 +00:00
|
|
|
func(*args, **kwargs)
|
|
|
|
return wrap1
|
|
|
|
return wrap2
|
2014-12-29 03:21:58 +00:00
|
|
|
|
2015-06-09 04:17:55 +00:00
|
|
|
|
|
|
|
def log(indent, message, color=None):
|
2014-12-29 03:21:58 +00:00
|
|
|
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
|
2015-06-09 04:17:55 +00:00
|
|
|
if color:
|
2016-03-15 15:04:53 +00:00
|
|
|
return sys.stdout.write('%s: %s%s%s%s\n' % (dt, ' '*indent, COLORS[color], message, COLORS['end']))
|
|
|
|
return sys.stdout.write('%s: %s%s\n' % (dt, ' '*indent, message))
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fetch_server(args):
|
2016-03-14 04:57:04 +00:00
|
|
|
if args.resource and args.username and args.password:
|
2016-04-07 05:39:04 +00:00
|
|
|
log(0, 'Signing in as MyPlex account %s..' % args.username)
|
|
|
|
account = MyPlexAccount.signin(args.username, args.password)
|
2016-04-02 06:19:32 +00:00
|
|
|
log(0, 'Connecting to Plex server %s..' % args.resource)
|
2016-04-07 05:39:04 +00:00
|
|
|
return account.resource(args.resource).connect(), account
|
2016-04-02 06:19:32 +00:00
|
|
|
elif args.baseurl and args.token:
|
|
|
|
log(0, 'Connecting to Plex server %s..' % args.baseurl)
|
|
|
|
return server.PlexServer(args.baseurl, args.token), None
|
2015-02-17 20:35:17 +00:00
|
|
|
return server.PlexServer(), None
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
2016-04-04 03:17:29 +00:00
|
|
|
def safe_client(name, baseurl, server):
|
|
|
|
try:
|
|
|
|
return server.client(name)
|
|
|
|
except NotFound as err:
|
|
|
|
log(2, 'Warning: %s' % err)
|
|
|
|
return PlexClient(baseurl, server=server)
|
|
|
|
|
|
|
|
|
2016-03-15 15:04:53 +00:00
|
|
|
def iter_tests(query):
|
|
|
|
tags = query[5:].split(',') if query and query.startswith('tags:') else ''
|
|
|
|
for test in registered:
|
|
|
|
if not query:
|
|
|
|
yield test
|
|
|
|
elif tags:
|
|
|
|
matching_tags = [t for t in tags if t in test['tags']]
|
|
|
|
if matching_tags: yield test
|
2016-03-25 02:36:25 +00:00
|
|
|
elif query == test['name']:
|
2016-03-15 15:04:53 +00:00
|
|
|
yield test
|
2014-12-29 03:21:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def run_tests(module, args):
|
2016-04-07 05:39:04 +00:00
|
|
|
plex, account = fetch_server(args)
|
2014-12-29 03:21:58 +00:00
|
|
|
tests = {'passed':0, 'failed':0}
|
2016-03-15 15:04:53 +00:00
|
|
|
for test in iter_tests(args.query):
|
2014-12-29 03:21:58 +00:00
|
|
|
starttime = time.time()
|
2016-03-15 15:04:53 +00:00
|
|
|
log(0, '%s (%s)' % (test['name'], ','.join(test['tags'])))
|
2014-12-29 03:21:58 +00:00
|
|
|
try:
|
2016-04-07 05:39:04 +00:00
|
|
|
test['func'](plex, account)
|
2016-03-14 04:19:48 +00:00
|
|
|
runtime = time.time() - starttime
|
2016-04-02 06:19:32 +00:00
|
|
|
log(2, 'PASS! (runtime: %.3fs)' % runtime, 'blue')
|
2014-12-29 03:21:58 +00:00
|
|
|
tests['passed'] += 1
|
2015-02-17 20:35:17 +00:00
|
|
|
except Exception as err:
|
2016-03-15 18:36:59 +00:00
|
|
|
errstr = str(err)
|
2016-03-17 04:51:20 +00:00
|
|
|
errstr += '\n%s' % traceback.format_exc() if args.verbose else ''
|
2016-03-15 18:36:59 +00:00
|
|
|
log(2, 'FAIL!: %s' % errstr, 'red')
|
2014-12-29 03:21:58 +00:00
|
|
|
tests['failed'] += 1
|
|
|
|
log(0, '')
|
|
|
|
log(0, 'Tests Run: %s' % sum(tests.values()))
|
|
|
|
log(0, 'Tests Passed: %s' % tests['passed'])
|
2016-03-15 18:36:59 +00:00
|
|
|
if tests['failed']:
|
|
|
|
log(0, 'Tests Failed: %s' % tests['failed'], 'red')
|
2014-12-29 03:21:58 +00:00
|
|
|
if not tests['failed']:
|
|
|
|
log(0, '')
|
|
|
|
log(0, 'EVERYTHING OK!! :)')
|
|
|
|
raise SystemExit(tests['failed'])
|