Skip to content

Commit b335509

Browse files
committed
[#28] Add unit test runner
1 parent 3f7f6f8 commit b335509

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

irods_testing_environment/context.py

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ def python():
130130
return 'python3'
131131

132132

133+
def unit_tests():
134+
"""Return the path to the directory containing packaged unit tests."""
135+
import os
136+
return os.path.join(irods_home(), 'unit_tests')
137+
138+
133139
def sanitize(repo_or_tag):
134140
"""Sanitize the input from special characters rejected by docker-compose.
135141

irods_testing_environment/test_runner.py

+16
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,19 @@ def execute_test(self, test, options=None):
143143
user='irods',
144144
workdir=context.irods_home(),
145145
stream_output=True)
146+
147+
148+
class test_runner_irods_unit_tests(test_runner):
149+
def __init__(self, executing_container, tests=None):
150+
super(test_runner_irods_unit_tests, self).__init__(executing_container, tests)
151+
152+
153+
def execute_test(self, test):
154+
"""Execute `test` and return the command run and the return code."""
155+
import os
156+
cmd = [os.path.join(context.unit_tests(), test)]
157+
return cmd, execute.execute_command(self.executor,
158+
' '.join(cmd),
159+
user='irods',
160+
workdir=context.irods_home(),
161+
stream_output=True)

irods_testing_environment/test_utils.py

+37
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ def make_output_directory(dirname, basename):
4747

4848
return directory
4949

50+
def run_unit_tests(containers, test_list=None, fail_fast=True):
51+
"""Run a set of tests from the python test suite for iRODS.
52+
53+
Arguments:
54+
containers -- target containers on which the tests will run
55+
test_list -- a list of strings of the tests to be run
56+
options -- list of strings representing script options to pass to the run_tests.py script
57+
fail_fast -- if True, stop running after first failure; else, runs all tests
58+
"""
59+
tests = test_list or get_unit_test_list(containers[0])
60+
61+
tm = test_manager.test_manager(containers, tests, test_type='irods_unit_tests')
62+
63+
try:
64+
tm.run(fail_fast)
65+
66+
finally:
67+
logging.error(tm.result_string())
68+
69+
return tm.return_code()
70+
71+
5072
def run_specific_tests(containers, test_list=None, options=None, fail_fast=True):
5173
"""Run a set of tests from the python test suite for iRODS.
5274
@@ -68,6 +90,7 @@ def run_specific_tests(containers, test_list=None, options=None, fail_fast=True)
6890

6991
return tm.return_code()
7092

93+
7194
def run_python_test_suite(container, options=None):
7295
"""Run the entire python test suite for iRODS.
7396
@@ -148,6 +171,20 @@ def run_test_hook_file_in_container(container, path_to_test_hook, options=None):
148171
return ec
149172

150173

174+
def get_unit_test_list(container):
175+
"""Return list of unit tests extracted from unit_tests_list.json file in `container`.
176+
177+
Arguments:
178+
container -- target container from which test list will be extracted
179+
"""
180+
from . import json_utils
181+
return json_utils.get_json_from_file(container,
182+
os.path.join(
183+
context.unit_tests(),
184+
'unit_tests_list.json')
185+
)
186+
187+
151188
def get_test_list(container):
152189
"""Return list of tests extracted from core_tests_list.json file in `container`.
153190

run_unit_tests.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# grown-up modules
2+
import compose.cli.command
3+
import docker
4+
import logging
5+
import os
6+
7+
# local modules
8+
from irods_testing_environment import archive
9+
from irods_testing_environment import context
10+
from irods_testing_environment import irods_config
11+
from irods_testing_environment import services
12+
from irods_testing_environment import test_utils
13+
14+
if __name__ == "__main__":
15+
import argparse
16+
import textwrap
17+
import time
18+
19+
import cli
20+
from irods_testing_environment import logs
21+
22+
parser = argparse.ArgumentParser(description='Run iRODS tests in a consistent environment.')
23+
24+
cli.add_common_args(parser)
25+
cli.add_compose_args(parser)
26+
cli.add_database_config_args(parser)
27+
cli.add_irods_package_args(parser)
28+
cli.add_irods_test_args(parser)
29+
30+
args = parser.parse_args()
31+
32+
if args.package_directory and args.package_version:
33+
print('--package-directory and --package-version are incompatible')
34+
exit(1)
35+
36+
project_directory = os.path.abspath(args.project_directory or os.getcwd())
37+
38+
ctx = context.context(docker.from_env(),
39+
compose.cli.command.get_project(
40+
project_dir=project_directory,
41+
project_name=args.project_name))
42+
43+
if args.output_directory:
44+
dirname = args.output_directory
45+
else:
46+
import tempfile
47+
dirname = tempfile.mkdtemp(prefix=ctx.compose_project.name)
48+
49+
job_name = test_utils.job_name(ctx.compose_project.name, args.job_name)
50+
51+
output_directory = test_utils.make_output_directory(dirname, job_name)
52+
53+
logs.configure(args.verbosity, os.path.join(output_directory, 'script_output.log'))
54+
55+
rc = 0
56+
last_command_to_fail = None
57+
58+
try:
59+
if args.do_setup:
60+
# Bring up the services
61+
logging.debug('bringing up project [{}]'.format(ctx.compose_project.name))
62+
consumer_count = 0
63+
services.create_topologies(ctx,
64+
zone_count=args.executor_count,
65+
externals_directory=args.irods_externals_package_directory,
66+
package_directory=args.package_directory,
67+
package_version=args.package_version,
68+
odbc_driver=args.odbc_driver,
69+
consumer_count=consumer_count)
70+
71+
# Configure the containers for running iRODS automated tests
72+
logging.info('configuring iRODS containers for testing')
73+
irods_config.configure_irods_testing(ctx.docker_client, ctx.compose_project)
74+
75+
# Get the container on which the command is to be executed
76+
containers = [
77+
ctx.docker_client.containers.get(
78+
context.container_name(ctx.compose_project.name,
79+
context.irods_catalog_provider_service(),
80+
service_instance=i + 1)
81+
)
82+
for i in range(args.executor_count)
83+
]
84+
85+
start_time = time.time()
86+
87+
rc = test_utils.run_unit_tests(containers, args.tests, args.fail_fast)
88+
89+
end_time = time.time()
90+
91+
logging.error('tests completed; time [{}] seconds, success [{}]'.format(end_time - start_time, rc is 0))
92+
93+
except Exception as e:
94+
logging.critical(e)
95+
96+
raise
97+
98+
finally:
99+
logging.warning('collecting logs [{}]'.format(output_directory))
100+
logs.collect_logs(ctx.docker_client, ctx.irods_containers(), output_directory)
101+
102+
ctx.compose_project.down(include_volumes=True, remove_image_type=False)
103+
104+
exit(rc)

0 commit comments

Comments
 (0)