diff options
author | Walter Purcaro <vuolter@gmail.com> | 2014-09-08 01:08:03 +0200 |
---|---|---|
committer | Walter Purcaro <vuolter@gmail.com> | 2014-09-14 11:03:20 +0200 |
commit | 91115fd577f20704ef7f2e74c4527ffbb0730a09 (patch) | |
tree | 198fc68f1eb6f42e220d0f1c68499f48ae70543c /tests | |
parent | Project __init__ with info (diff) | |
download | pyload-91115fd577f20704ef7f2e74c4527ffbb0730a09.tar.xz |
Restructure pyload file tree (1)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/APIExerciser.py | 157 | ||||
-rw-r--r-- | tests/clonedigger.sh | 4 | ||||
-rw-r--r-- | tests/code_analysis.sh | 25 | ||||
-rw-r--r-- | tests/test_api.py | 21 | ||||
-rw-r--r-- | tests/test_json.py | 48 |
5 files changed, 255 insertions, 0 deletions
diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py new file mode 100644 index 000000000..886c72a4a --- /dev/null +++ b/tests/APIExerciser.py @@ -0,0 +1,157 @@ +# -*- coding: utf-8 -*- + +import string +from threading import Thread +from random import choice, random, sample, randint +from time import time, sleep +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 range(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 range(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() + + if thrift: + self.api = ThriftClient(user=user, password=pw) + else: + self.api = core.api + + + self.id = idPool + + idPool += 1 + + #self.start() + + def run(self): + + self.core.log.info("API Excerciser started %d" % self.id) + + out = open("error.log", "ab") + #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 len(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..358c1b68b --- /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="lib" --ignore-dir="remote" $PYLOAD diff --git a/tests/code_analysis.sh b/tests/code_analysis.sh new file mode 100644 index 000000000..c853652bf --- /dev/null +++ b/tests/code_analysis.sh @@ -0,0 +1,25 @@ +#!/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" + +#echo "Running pyflakes ..." +#REPORT="pyflakes.txt" +#{ + # pyflakes to pylint syntak +# find $PYLOAD -type f -name "*.py" | egrep -v '^\./lib' | xargs pyflakes > pyflakes.log || : + # Filter warnings and strip ./ from path +# cat pyflakes.log | awk -F\: '{printf "%s:%s: [E]%s\n", $1, $2, $3}' | grep -i -E -v "'_'|pypath|webinterface|pyreq|addonmanager" > $REPORT +# sed -i 's/^.\///g' $REPORT +#} && echo "Done. Report saved to $REPORT" diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 000000000..a4229cfe6 --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +import APIExerciser + +from nose.tools import nottest + + +class TestApi: + + 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): + + for i in range(0, 100): + self.api.testAPI() diff --git a/tests/test_json.py b/tests/test_json.py new file mode 100644 index 000000000..320a42d4f --- /dev/null +++ b/tests/test_json.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +from urllib import urlencode +from urllib2 import urlopen, HTTPError +from json import loads + +from logging import log + +url = "http://localhost:8001/api/%s" + +class TestJson: + + 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()) + + 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") + except HTTPError, e: + assert e.code == 403 + 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 |