Added redis pipeline, cmd arg for gui, register plugin

1. Used redis pipeline for consecutive writes when possible

2. Add --status_gui option for py.test. So, removed explicit plugin from self test as it creates conflict.

3. Register as plugin in setup.py using pytest11

4. Update todo
This commit is contained in:
2015-12-21 20:48:57 +05:30
parent 2494bc9923
commit bc06862a7f
6 changed files with 51 additions and 29 deletions
+4 -1
View File
@@ -8,7 +8,7 @@ Todo
5. use logging.debug for more prints 5. use logging.debug for more prints
6. Think about popen stdouts 6. Think about popen stdouts
7. Print redis not connected in gui itself instead of console 7. Print redis not connected in gui itself instead of console
8. Allow config for tmpl, redis_arg, status_gui_arg, 8. Allow config for tmpl, redis_arg, status_gui_arg
Done Done
------ ------
@@ -17,3 +17,6 @@ Done
2. (---) responsive width of test 2. (---) responsive width of test
3. (---) Time should update automatically 3. (---) Time should update automatically
4. (---) Launch only 1 instance of gui from plugin with pid check from redis_db 4. (---) Launch only 1 instance of gui from plugin with pid check from redis_db
5. (-) use pipelines when possible
6. (---) Self Tests also pop up gui, add option for plugin
9. (---) Register as pytest plugin in setup.py
+41 -19
View File
@@ -5,6 +5,8 @@ import sys
import redis import redis
import os import os
import psutil import psutil
import datetime
env_redis_port = os.environ.get("pytest_status_port") env_redis_port = os.environ.get("pytest_status_port")
if env_redis_port: if env_redis_port:
@@ -29,6 +31,7 @@ command_status_gui_gen = "pytest_gui_status \"{norm_dir_name}\""
# {hash_a}_fail = [test_b_name,...] # {hash_a}_fail = [test_b_name,...]
# {hash_a}_skip = [test_c_name,...] # {hash_a}_skip = [test_c_name,...]
# {hash_a}_gui_pid = pid # check this before launching gui # {hash_a}_gui_pid = pid # check this before launching gui
# make bulk changes with pipeline
class Helpers(object): class Helpers(object):
@@ -36,7 +39,7 @@ class Helpers(object):
@staticmethod @staticmethod
def on_start(dir_name): def on_start(dir_name):
''' '''
Init gui_status and redis on start of pytest. Init redis on start of pytest.
dir_name - Name of directory from which pytest started dir_name - Name of directory from which pytest started
''' '''
@@ -59,14 +62,15 @@ class Helpers(object):
print("** Not found existing redis, couldnt connect, check! **") print("** Not found existing redis, couldnt connect, check! **")
sys.exit() sys.exit()
# todo : start and check gui with dir name and hash hash_dir_name = hash(dir_name)
# craete redis connection and set sample key # craete redis connection and set sample key
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
redis_db.set("PYTEST_STATUS_DB", "1") redis_pipe = redis_db.pipeline()
redis_pipe.set("PYTEST_STATUS_DB", "1")
hash_dir_name = hash(dir_name) redis_pipe.hset("directories_to_hash", dir_name, hash_dir_name)
redis_db.hset("directories_to_hash", dir_name, hash_dir_name) redis_pipe.execute()
Helpers.on_start_reset(dir_name) Helpers.on_start_reset(dir_name)
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "start") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "start")
@@ -76,6 +80,9 @@ class Helpers(object):
@staticmethod @staticmethod
def start_gui(dir_name): def start_gui(dir_name):
'''
Init status_gui at start
'''
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
@@ -98,6 +105,7 @@ class Helpers(object):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "collect") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "collect")
@@ -109,6 +117,7 @@ class Helpers(object):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
redis_db.rpush("{hash_a}_collect".format(hash_a=hash_dir_name), *list_test_name) redis_db.rpush("{hash_a}_collect".format(hash_a=hash_dir_name), *list_test_name)
@@ -120,6 +129,7 @@ class Helpers(object):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "runtest") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "runtest")
@@ -131,6 +141,7 @@ class Helpers(object):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
list_testname_pass = [test_result.nodeid for test_result in list_testname_pass = [test_result.nodeid for test_result in
list_test_result if test_result.outcome == "passed"] list_test_result if test_result.outcome == "passed"]
@@ -141,12 +152,16 @@ class Helpers(object):
list_testname_skip = [test_result.nodeid for test_result in list_testname_skip = [test_result.nodeid for test_result in
list_test_result if test_result.outcome == "skipped"] list_test_result if test_result.outcome == "skipped"]
redis_pipe = redis_db.pipeline()
if list_testname_pass: if list_testname_pass:
redis_db.lpush("{hash_a}_pass".format(hash_a=hash_dir_name), *list_testname_pass) redis_pipe.lpush("{hash_a}_pass".format(hash_a=hash_dir_name), *list_testname_pass)
if list_testname_fail: if list_testname_fail:
redis_db.lpush("{hash_a}_fail".format(hash_a=hash_dir_name), *list_testname_fail) redis_pipe.lpush("{hash_a}_fail".format(hash_a=hash_dir_name), *list_testname_fail)
if list_testname_skip: if list_testname_skip:
redis_db.lpush("{hash_a}_skip".format(hash_a=hash_dir_name), *list_testname_skip) redis_pipe.lpush("{hash_a}_skip".format(hash_a=hash_dir_name), *list_testname_skip)
redis_pipe.execute()
# set last updated # set last updated
Helpers.modify_last_updated(dir_name) Helpers.modify_last_updated(dir_name)
@@ -156,6 +171,7 @@ class Helpers(object):
# craete redis connection # craete redis connection
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "end") redis_db.set("{hash_a}_state".format(hash_a=hash_dir_name), "end")
@@ -165,7 +181,9 @@ class Helpers(object):
@staticmethod @staticmethod
def on_start_reset(dir_name): def on_start_reset(dir_name):
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
list_gen_varname = ["{hash_a}_state", "{hash_a}_last_updated", "{hash_a}_collect", "{hash_a}_pass", "{hash_a}_fail", "{hash_a}_skip"] list_gen_varname = ["{hash_a}_state", "{hash_a}_last_updated", "{hash_a}_collect", "{hash_a}_pass", "{hash_a}_fail", "{hash_a}_skip"]
list_cur_varname = [varname.format(hash_a=hash_dir_name) for varname in list_gen_varname] list_cur_varname = [varname.format(hash_a=hash_dir_name) for varname in list_gen_varname]
@@ -175,12 +193,13 @@ class Helpers(object):
@staticmethod @staticmethod
def modify_last_updated(dir_name): def modify_last_updated(dir_name):
import datetime # update_last_updated in redis. If pipe provided, queue the command instead.
redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_PORT, db=0)
hash_dir_name = redis_db.hget("directories_to_hash", dir_name) hash_dir_name = redis_db.hget("directories_to_hash", dir_name)
assert hash_dir_name is not None
cur_iso_datetime = datetime.datetime.now().isoformat() cur_iso_datetime = datetime.datetime.now().isoformat()
redis_db.set("{hash_a}_last_updated".format(hash_a=hash_dir_name), cur_iso_datetime) redis_db.set("{hash_a}_last_updated".format(hash_a=hash_dir_name), cur_iso_datetime)
@@ -189,9 +208,21 @@ class PYTEST_DATA(object):
data = {} data = {}
def pytest_addoption(parser):
parser.addoption("--status_gui", dest="show_status_gui", action="store_true")
def pytest_configure(config):
pass
def pytest_sessionstart(session): def pytest_sessionstart(session):
config = session.config
PYTEST_DATA.data["dir_name_start"] = str(session.startdir) PYTEST_DATA.data["dir_name_start"] = str(session.startdir)
Helpers.on_start(PYTEST_DATA.data["dir_name_start"]) Helpers.on_start(PYTEST_DATA.data["dir_name_start"])
if config.getoption("show_status_gui"):
Helpers.start_gui(PYTEST_DATA.data["dir_name_start"]) Helpers.start_gui(PYTEST_DATA.data["dir_name_start"])
@@ -214,12 +245,3 @@ def pytest_runtest_logreport(report):
def pytest_sessionfinish(session, exitstatus): def pytest_sessionfinish(session, exitstatus):
Helpers.on_end(PYTEST_DATA.data["dir_name_start"]) Helpers.on_end(PYTEST_DATA.data["dir_name_start"])
# pytest_sessionstart(session)
# pytest_collectstart(collector) | collector.config
# pytest_itemcollected
## pytest_collection_modifyitems(session, config, items)
# pytest_collectreport
# pytest_runtest_logstart(nodeid, location) - single test start
# pytest_runtest_logreport(report) - single test end
# pytest_sessionfinish(session, exit_status) - session end
+3 -3
View File
@@ -24,9 +24,9 @@ setup(
platforms='any', platforms='any',
install_requires=open('requirements.txt').readlines(), install_requires=open('requirements.txt').readlines(),
entry_points={ entry_points={
# 'pytest11': [ 'pytest11': [
# 'pytest_gui_status = pytest_gui_status.main', 'pytest_gui_status = pytest_gui_status.status_plugin.plugin',
# ], ],
'console_scripts': [ 'console_scripts': [
'pytest_gui_status = pytest_gui_status.status_gui.gui_frontend:main', 'pytest_gui_status = pytest_gui_status.status_gui.gui_frontend:main',
] ]
-3
View File
@@ -2,7 +2,6 @@ import pytest_gui_status.status_plugin as status_plugin
import subprocess import subprocess
from mock import patch from mock import patch
import redis import redis
import pytest
import os import os
import tempfile import tempfile
@@ -54,8 +53,6 @@ def test_whole_1(tmpdir):
popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True) popen_pytest = subprocess.Popen(["py.test", "-s"], shell=True)
popen_pytest.wait() popen_pytest.wait()
# pytest.main(["-s"])
dir_name = os.getcwd() dir_name = os.getcwd()
assert (dir_name == path_case) assert (dir_name == path_case)
redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0) redis_db = redis.StrictRedis(host='localhost', port=REDIS_TEST_PORT, db=0)
+1 -1
View File
@@ -1 +1 @@
pytest_plugins = "pytest_gui_status.status_plugin" # pytest_plugins = "pytest_gui_status.status_plugin"
+1 -1
View File
@@ -1 +1 @@
pytest_plugins = "pytest_gui_status.status_plugin" # pytest_plugins = "pytest_gui_status.status_plugin"