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 | 14 | ||||
-rw-r--r-- | tests/test_json.py | 13 |
5 files changed, 202 insertions, 8 deletions
diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py new file mode 100644 index 000000000..f4b082479 --- /dev/null +++ b/tests/APIExerciser.py @@ -0,0 +1,164 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from __future__ import with_statement + +import string +from threading import Thread +from random import choice, sample, randint +from time import time +from math import floor +import gc +from traceback import print_exc, format_exc + +from pyload.remote.thriftbackend.ThriftClient import ThriftClient, Destination + + +def createURLs(): + """ create some urls, some may fail """ + urls = [] + for x in xrange(0, randint(20, 100)): + name = "DEBUG_API" + if randint(0, 5) == 5: + name = "" #: this link will fail + + urls.append(name + "".join(sample(string.ascii_letters, 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(Thread): + + def __init__(self, core, thrift=False, user=None, pw=None): + global idPool + + Thread.__init__(self) + self.setDaemon(True) + self.core = core + self.count = 0 #: number of methods + self.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) + print_exc() + out.write(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() - 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()) + # 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 = 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(sample(string.ascii_letters, 10)) + urls = createURLs() + + self.api.addPackage(name, urls, choice([Destination.Queue, Destination.Collector])) + + + def deleteFiles(self): + info = self.api.getQueueData() + if not info: + return + + pack = choice(info) + fids = pack.links + + if len(fids): + fids = [f.fid for f in sample(fids, randint(1, max(len(fids) / 2, 1)))] + self.api.deleteFiles(fids) + + + def deletePackages(self): + info = choice([self.api.getQueue(), self.api.getCollector()]) + if not info: + return + + pids = [p.pid for p in info] + if pids: + pids = sample(pids, randint(1, max(floor(len(pids) / 2.5), 1))) + self.api.deletePackages(pids) + + + def getFileData(self): + info = self.api.getQueueData() + if info: + p = choice(info) + if p.links: + self.api.getFileData(choice(p.links).fid) + + + def getPackageData(self): + info = self.api.getQueue() + if info: + self.api.getPackageData(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..1e02d8aa3 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 +import APIExerciser + from nose.tools import nottest -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 + # takes really long, only test when needed + + @nottest def test_random(self): - - for i in range(0, 100): + for _i in xrange(0, 100): self.api.testAPI() diff --git a/tests/test_json.py b/tests/test_json.py index ff56e8f5a..a83ef0a24 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # -*- coding: utf-8 -*- from urllib import urlencode @@ -8,23 +9,27 @@ 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 + post['session'] = self.key u = urlopen(url % name, data=urlencode(post)) return loads(u.read()) + def setUp(self): u = urlopen(url % "login", data=urlencode({"username": "TestUser", "password": "pwhere"})) self.key = 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 + def test_access(self): try: urlopen(url % "getServerVersion") @@ -33,16 +38,18 @@ class TestJson: else: assert False + def test_status(self): ret = self.call("statusServer") log(1, str(ret)) assert "pause" in ret assert "queue" in ret + def test_unknown_method(self): try: self.call("notExisting") except HTTPError, e: assert e.code == 404 else: - assert False
\ No newline at end of file + assert False |