diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/APIExerciser.py | 164 | ||||
-rw-r--r-- | tests/clonedigger.sh | 4 | ||||
-rw-r--r-- | tests/code_analysis.sh | 15 | ||||
-rw-r--r-- | tests/test_api.py | 18 | ||||
-rw-r--r-- | tests/test_json.py | 41 |
5 files changed, 218 insertions, 24 deletions
diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py new file mode 100644 index 000000000..d2a069c78 --- /dev/null +++ b/tests/APIExerciser.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import with_statement + +import gc +import math +import random +import string +import threading +import time +import traceback + +from pyload.remote.thriftbackend.ThriftClient import ThriftClient, Destination + + +def createURLs(): + """ create some urls, some may fail """ + urls = [] + for x in xrange(0, random.randint(20, 100)): + name = "DEBUG_API" + if random.randint(0, 5) == 5: + name = "" #: this link will fail + + urls.append(name + "".join(random.sample(string.ascii_letters, random.randint(10, 20)))) + + return urls + + +AVOID = (0, 3, 8) + +idPool = 0 +sumCalled = 0 + + +def startApiExerciser(core, n): + for _i in xrange(n): + APIExerciser(core).start() + + +class APIExerciser(threading.Thread): + + def __init__(self, core, thrift=False, user=None, pw=None): + global idPool + + threading.Thread.__init__(self) + self.setDaemon(True) + self.core = core + self.count = 0 #: number of methods + self.time = time.time() + + self.api = ThriftClient(user=user, password=pw) if thrift else core.api + + self.id = idPool + + idPool += 1 + + # self.start() + + + def run(self): + + self.core.log.info("API Excerciser started %d" % self.id) + + with open("error.log", "ab") as out: + # core errors are not logged of course + out.write("\n" + "Starting\n") + out.flush() + + while True: + try: + self.testAPI() + except Exception: + self.core.log.error("Excerciser %d throw an execption" % self.id) + traceback.print_exc() + out.write(traceback.format_exc() + 2 * "\n") + out.flush() + + if not self.count % 100: + self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count)) + if not self.count % 1000: + out.flush() + + if not sumCalled % 1000: #: not thread safe + self.core.log.info("Exercisers tested %d api calls" % sumCalled) + persec = sumCalled / (time.time() - self.time) + self.core.log.info("Approx. %.2f calls per second." % persec) + self.core.log.info("Approx. %.2f ms per call." % (1000 / persec)) + self.core.log.info("Collected garbage: %d" % gc.collect()) + # time.sleep(random() / 500) + + + def testAPI(self): + global sumCalled + + m = ["statusDownloads", "statusServer", "addPackage", "getPackageData", "getFileData", "deleteFiles", + "deletePackages", "getQueue", "getCollector", "getQueueData", "getCollectorData", "isCaptchaWaiting", + "getCaptchaTask", "stopAllDownloads", "getAllInfo", "getServices", "getAccounts", "getAllUserData"] + + method = random.choice(m) + # print "Testing:", method + + if hasattr(self, method): + res = getattr(self, method)() + else: + res = getattr(self.api, method)() + + self.count += 1 + sumCalled += 1 + + # print res + + + def addPackage(self): + name = "".join(random.sample(string.ascii_letters, 10)) + urls = createURLs() + + self.api.addPackage(name, urls, random.choice([Destination.Queue.Queue, Destination.Collector])) + + + def deleteFiles(self): + info = self.api.getQueueData() + if not info: + return + + pack = random.choice(info) + fids = pack.links + + if len(fids): + fids = [f.fid for f in random.sample(fids, random.randint(1, max(len(fids) / 2, 1)))] + self.api.deleteFiles(fids) + + + def deletePackages(self): + info = random.choice([self.api.getQueue(), self.api.getCollector()]) + if not info: + return + + pids = [p.pid for p in info] + if pids: + pids = random.sample(pids, random.randint(1, max(math.floor(len(pids) / 2.5), 1))) + self.api.deletePackages(pids) + + + def getFileData(self): + info = self.api.getQueueData() + if info: + p = random.choice(info) + if p.links: + self.api.getFileData(random.choice(p.links).fid) + + + def getPackageData(self): + info = self.api.getQueue() + if info: + self.api.getPackageData(random.choice(info).pid) + + + def getAccounts(self): + self.api.getAccounts(False) + + + def getCaptchaTask(self): + self.api.getCaptchaTask(False) diff --git a/tests/clonedigger.sh b/tests/clonedigger.sh new file mode 100644 index 000000000..e7fd17eb6 --- /dev/null +++ b/tests/clonedigger.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +PYLOAD="../pyload" #: Check pyload directory +clonedigger -o cpd.xml --cpd-output --fast --ignore-dir="remote" ${PYLOAD} diff --git a/tests/code_analysis.sh b/tests/code_analysis.sh new file mode 100644 index 000000000..aaf6bb6a4 --- /dev/null +++ b/tests/code_analysis.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +PYLOAD="../pyload" #: Check pyload directory + +echo "Running sloccount ..." +REPORT="sloccount.sc" +sloccount --duplicates --wide --details ${PYLOAD} > ${REPORT} && echo "Done. Report saved to $REPORT" + +echo "Running pep8 ..." +REPORT="pep8.txt" +pep8 ${PYLOAD} > ${REPORT} && echo "Done. Report saved to $REPORT" + +echo "Running pylint ..." +REPORT="pylint.txt" +pylint --reports=no ${PYLOAD} > ${REPORT} && echo "Done. Report saved to $REPORT" diff --git a/tests/test_api.py b/tests/test_api.py index f8901f731..ca02f7a2c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,20 +1,24 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- -from module.common import APIExerciser -from nose.tools import nottest +import nose +import APIExerciser -class TestApi: + +class TestApi(object): def __init__(self): self.api = APIExerciser.APIExerciser(None, True, "TestUser", "pwhere") + def test_login(self): assert self.api.api.login("crapp", "wrong pw") is False - #takes really long, only test when needed - @nottest - def test_random(self): + # takes really long, only test when needed - for i in range(0, 100): + + @nose.tools.nottest + def test_random(self): + for _i in xrange(0, 100): self.api.testAPI() diff --git a/tests/test_json.py b/tests/test_json.py index ff56e8f5a..de2914182 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1,48 +1,55 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- -from urllib import urlencode -from urllib2 import urlopen, HTTPError -from json import loads +import json +import logging +import urllib +import urllib2 -from logging import log url = "http://localhost:8001/api/%s" -class TestJson: + +class TestJson(object): def call(self, name, post=None): if not post: post = {} - post["session"] = self.key - u = urlopen(url % name, data=urlencode(post)) - return loads(u.read()) + post['session'] = self.key + u = urllib2.urlopen(url % name, data=urlencode(post)) + return json.loads(u.read()) + def setUp(self): - u = urlopen(url % "login", data=urlencode({"username": "TestUser", "password": "pwhere"})) - self.key = loads(u.read()) + u = urllib2.urlopen(url % "login", data=urlencode({"username": "TestUser", "password": "pwhere"})) + self.key = json.loads(u.read()) assert self.key is not False + def test_wronglogin(self): - u = urlopen(url % "login", data=urlencode({"username": "crap", "password": "wrongpw"})) - assert loads(u.read()) is False + u = urllib2.urlopen(url % "login", data=urlencode({"username": "crap", "password": "wrongpw"})) + assert json.loads(u.read()) is False + def test_access(self): try: - urlopen(url % "getServerVersion") - except HTTPError, e: + urllib2.urlopen(url % "getServerVersion") + except urllib2.HTTPError, e: assert e.code == 403 else: assert False + def test_status(self): ret = self.call("statusServer") - log(1, str(ret)) + logging.log(1, str(ret)) assert "pause" in ret assert "queue" in ret + def test_unknown_method(self): try: self.call("notExisting") - except HTTPError, e: + except urllib2.HTTPError, e: assert e.code == 404 else: - assert False
\ No newline at end of file + assert False |