Skip to content

Commit 7e1cdcc

Browse files
committed
Silence noise when running the test suite.
When the testcore boilerplate was copied for other legacy tests the main methods were not made to fit to the same extent, particularly around the print override. Meanwhile, we now use only PY3 which allows shadowing the print function. So, use this to avoid filling the console with arbitrary messages while the tests are run. While here make things consistent - always pass in a test configuration, use the passed in value and try to consistently use legacy main naming.
1 parent a6fe6b0 commit 7e1cdcc

12 files changed

Lines changed: 82 additions & 81 deletions

mig/lib/events.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,9 @@ def run_events_command(
557557
# ret_msg, txt_out))
558558

559559

560-
def main(_exit=sys.exit, _print=print):
560+
def main(conf, print=print, _exit=sys.exit):
561561
"""Run module self-tests"""
562-
from mig.shared.conf import get_configuration_object
563562

564-
conf = get_configuration_object()
565563
client_id = "/C=DK/ST=NA/L=NA/O=NBI/OU=NA/CN=Jonas Bardino/emailAddress=bardino@nbi.ku.dk"
566564
now = datetime.datetime.now()
567565
now = now.replace(second=0, microsecond=0)
@@ -623,4 +621,6 @@ def main(_exit=sys.exit, _print=print):
623621

624622

625623
if __name__ == "__main__":
626-
main()
624+
from mig.shared.conf import get_configuration_object
625+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
626+
main(conf)

mig/shared/base.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ def auth_type_description(configuration, auth_type=keyword_all):
951951
return auth_map.get(auth_type, 'UNKNOWN')
952952

953953

954-
def legacy_main(_exit=sys.exit, _print=print):
954+
def legacy_main(configuration, print=print, _exit=sys.exit):
955955
"""Run module self-tests"""
956956
orig_id = '/C=DK/ST=NA/L=NA/O=Ajax Inc/OU=NA/CN=John Doe/emailAddress=john.doe@ajaxinc.org'
957957
client_dir = client_id_dir(orig_id)
@@ -986,9 +986,6 @@ def legacy_main(_exit=sys.exit, _print=print):
986986
for path in legal:
987987
print(" %s: %s" % (path, not invisible_path(path)))
988988

989-
from mig.shared.conf import get_configuration_object
990-
configuration = get_configuration_object(
991-
skip_log=True, disable_auth_log=True)
992989
print("check script restrictions:")
993990
for script_name in ['reqoid.py', 'ls.py', 'sharelink.py', 'put']:
994991
(allow, msg) = allow_script(configuration, script_name, '')
@@ -1035,6 +1032,10 @@ def legacy_main(_exit=sys.exit, _print=print):
10351032
print("Found unsafe page for %r : %s" %
10361033
(script_uri, requested_page(include_unsafe=True)))
10371034

1035+
_exit(0)
1036+
10381037

10391038
if __name__ == '__main__':
1040-
legacy_main()
1039+
from mig.shared.conf import get_configuration_object
1040+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
1041+
legacy_main(conf)

mig/shared/pwcrypto.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,10 +1028,8 @@ def generate_random_password(configuration, tries=42):
10281028
raise ValueError("Failed to generate suitable password!")
10291029

10301030

1031-
def main(_exit=sys.exit, _print=print):
1031+
def main(configuration, print=print, _exit=sys.exit):
10321032
"""Run module self-tests"""
1033-
from mig.shared.conf import get_configuration_object
1034-
configuration = get_configuration_object()
10351033
dummy_user = {'distinguished_name': 'Test User', 'password_hash': ''}
10361034
pw_tests = (
10371035
'', 'abc', 'dbey3h', 'abcdefgh', '12345678', 'test1234',
@@ -1117,6 +1115,10 @@ def main(_exit=sys.exit, _print=print):
11171115
print(
11181116
"Failed to handle aesgcm static encrypt/decrypt %s : %s" % (pw, exc))
11191117

1118+
_exit(0)
1119+
11201120

11211121
if __name__ == "__main__":
1122-
main()
1122+
from mig.shared.conf import get_configuration_object
1123+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
1124+
main(conf)

mig/shared/userio.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -574,20 +574,18 @@ def __clean_test_files(configuration, test_path):
574574
exc))
575575

576576

577-
def main(_exit=sys.exit, _print=print):
577+
def main(configuration, print=print, _exit=sys.exit, _argv=sys.argv):
578578
"""Run module self-tests"""
579579
from mig.shared.base import client_id_dir
580-
from mig.shared.conf import get_configuration_object
581580
from mig.shared.defaults import htaccess_filename
582581
print("Unit testing user I/O")
583582
client_id = "/C=DK/ST=NA/L=NA/O=NBI/OU=NA/CN=Jonas Bardino/emailAddress=bardino@nbi.ku.dk"
584583
sub_dir = '.'
585-
if sys.argv[1:]:
586-
client_id = sys.argv[1]
587-
if sys.argv[2:]:
588-
sub_dir = sys.argv[2]
584+
if _argv[1:]:
585+
client_id = _argv[1]
586+
if _argv[2:]:
587+
sub_dir = _argv[2]
589588
client_dir = client_id_dir(client_id)
590-
configuration = get_configuration_object()
591589
configuration.mig_server_id = "localhost"
592590
tmp_dir = os.path.join("userio-testdir", sub_dir)
593591
real_tmp = os.path.normpath(os.path.join(configuration.user_home,
@@ -679,6 +677,10 @@ def main(_exit=sys.exit, _print=print):
679677
events_path = os.path.join(configuration.events_home, name)
680678
print("%s : %db" % (name, os.stat(events_path).st_size))
681679

680+
_exit(0)
681+
682682

683683
if __name__ == "__main__":
684-
main()
684+
from mig.shared.conf import get_configuration_object
685+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
686+
main(conf)

mig/shared/vgrid.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,9 +2321,7 @@ def vgrid_rm_entry(configuration, vgrid):
23212321
return (success, msg)
23222322

23232323

2324-
def legacy_main(_exit=sys.exit, _print=print):
2325-
from mig.shared.conf import get_configuration_object
2326-
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
2324+
def legacy_main(conf, print=print, _exit=sys.exit):
23272325
client_id = '/C=DK/CN=John Doe/emailAddress=john@doe.org'
23282326
if sys.argv[1:]:
23292327
client_id = sys.argv[1]
@@ -2445,6 +2443,10 @@ def legacy_main(_exit=sys.exit, _print=print):
24452443
print(vgrid_set_members(conf, dummy_vgrid, orig_members))
24462444
print(vgrid_members(dummy_vgrid, conf))
24472445

2446+
_exit(0)
2447+
24482448

24492449
if __name__ == "__main__":
2450-
legacy_main()
2450+
from mig.shared.conf import get_configuration_object
2451+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
2452+
legacy_main(conf)

mig/unittest/testcore.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,48 +36,11 @@
3636
import time
3737
import logging
3838

39-
sys.path.append(os.path.realpath(
40-
os.path.join(os.path.dirname(__file__), "../..")))
41-
from tests.support import MIG_BASE, PY2, is_path_within
42-
4339
from mig.shared.base import client_id_dir, client_dir_id, get_short_id, \
4440
invisible_path, allow_script, brief_list
4541

4642

47-
_TEST_CONF_FILE = os.environ['MIG_CONF']
48-
_TEST_CONF_DIR = os.path.dirname(_TEST_CONF_FILE)
49-
_TEST_CONF_SYMLINK = os.path.join(MIG_BASE, "envhelp/output/testconfs")
50-
51-
52-
def _assert_local_config():
53-
try:
54-
#link_stat = os.lstat(_TEST_CONF_SYMLINK)
55-
#assert stat.S_ISLNK(link_stat.st_mode)
56-
_test_conf_dir = os.path.dirname(_TEST_CONF_DIR)
57-
configdir_stat = os.stat(_test_conf_dir)
58-
assert stat.S_ISDIR(configdir_stat.st_mode)
59-
config = ConfigParser()
60-
config.read([_TEST_CONF_FILE])
61-
return config
62-
except Exception as exc:
63-
raise AssertionError(
64-
'local configuration invalid or missing: %s' % (str(exc),))
65-
66-
67-
def _assert_local_config_global_values(config):
68-
config_global_values = dict(config.items('GLOBAL'))
69-
70-
for path in ('mig_path', 'certs_path', 'state_path'):
71-
path_value = config_global_values.get(path)
72-
if not is_path_within(path_value, start=MIG_BASE):
73-
raise AssertionError('local config contains bad path: %s=%s' % (path, path_value))
74-
75-
return config_global_values
76-
77-
78-
def main(configuration, _exit=sys.exit):
79-
config = _assert_local_config()
80-
config_global_values = _assert_local_config_global_values(config)
43+
def main(configuration, print=print, _exit=sys.exit):
8144

8245
print("Running unit test on shared core functions ..")
8346

@@ -188,4 +151,5 @@ def main(configuration, _exit=sys.exit):
188151

189152
if __name__ == "__main__":
190153
from mig.shared.conf import get_configuration_object
191-
main(get_configuration_object())
154+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
155+
main(conf)

tests/test_mig_lib_events.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,15 +3395,14 @@ def raise_on_error_exit(exit_code):
33953395
else:
33963396
identifying_message = "unknown"
33973397
raise AssertionError(
3398-
"failure in unittest/testcore: %s" % (identifying_message,)
3399-
)
3398+
'legacy test failure: %s' % (identifying_message,))
34003399

34013400
raise_on_error_exit.last_print = None
34023401

34033402
def record_last_print(value):
34043403
raise_on_error_exit.last_print = value
34053404

3406-
events_main(_exit=raise_on_error_exit, _print=record_last_print)
3405+
events_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit)
34073406

34083407

34093408
if __name__ == "__main__":

tests/test_mig_shared_base.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,25 +1347,28 @@ def test_brief_list_edge_cases(self):
13471347
['a', 'b', ' ... shortened ... ', 'f', 'g'])
13481348

13491349

1350-
class TestMigSharedBase__legacy(MigTestCase):
1350+
class TestMigSharedBase__legacy_main(MigTestCase):
13511351
"""Run mig.shared.base legacy self-test"""
13521352

1353+
def _provide_configuration(self):
1354+
return 'testconfig'
1355+
13531356
# TODO: migrate all legacy self-check functionality into the above?
13541357
def test_existing_main(self):
13551358
"""Run built-in self-tests and check output"""
1359+
13561360
def raise_on_error_exit(exit_code):
13571361
if exit_code != 0:
13581362
if raise_on_error_exit.last_print is not None:
13591363
identifying_message = raise_on_error_exit.last_print
13601364
else:
13611365
identifying_message = 'unknown'
13621366
raise AssertionError(
1363-
'failure in unittest/testcore: %s' %
1364-
(identifying_message,))
1367+
'legacy test failure: %s' % (identifying_message,))
13651368
raise_on_error_exit.last_print = None
13661369

13671370
def record_last_print(value):
13681371
"""Keep track of printed output"""
13691372
raise_on_error_exit.last_print = value
13701373

1371-
legacy_main(_exit=raise_on_error_exit, _print=record_last_print)
1374+
legacy_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit)

tests/test_mig_shared_pwcrypto.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,12 @@ def test_generate_random_password_fixed_seed(self):
708708
self.assertEqual(expected, result,
709709
"failed generate password with fixed seed")
710710

711+
712+
class MigSharedPwCrypto__legacy_main(MigTestCase):
713+
714+
def _provide_configuration(self):
715+
return 'testconfig'
716+
711717
# TODO: migrate remaining inline checks from module here instead
712718
def test_existing_main(self):
713719
def raise_on_error_exit(exit_code):
@@ -717,13 +723,13 @@ def raise_on_error_exit(exit_code):
717723
else:
718724
identifying_message = 'unknown'
719725
raise AssertionError(
720-
'failure in unittest/testcore: %s' % (identifying_message,))
726+
'legacy test failure: %s' % (identifying_message,))
721727
raise_on_error_exit.last_print = None
722728

723729
def record_last_print(value):
724730
raise_on_error_exit.last_print = value
725731

726-
pwcrypto_main(_exit=raise_on_error_exit, _print=record_last_print)
732+
pwcrypto_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit)
727733

728734

729735
if __name__ == '__main__':

tests/test_mig_shared_userio.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,29 @@
3636
from mig.shared.userio import main as userio_main
3737

3838

39-
class MigSharedUserIO(MigTestCase):
39+
class MigSharedUserIO__legacy_main(MigTestCase):
4040
"""Unit tests for userio related helper functions"""
4141

42+
def _provide_configuration(self):
43+
return 'testconfig'
44+
4245
def test_existing_main(self):
46+
self.logger.forgive_errors()
47+
4348
def raise_on_error_exit(exit_code):
4449
if exit_code != 0:
4550
if raise_on_error_exit.last_print is not None:
4651
identifying_message = raise_on_error_exit.last_print
4752
else:
4853
identifying_message = 'unknown'
4954
raise AssertionError(
50-
'failure in unittest/testcore: %s' % (identifying_message,))
55+
'legacy test failure: %s' % (identifying_message,))
5156
raise_on_error_exit.last_print = None
5257

5358
def record_last_print(value):
5459
raise_on_error_exit.last_print = value
5560

56-
userio_main(_exit=raise_on_error_exit, _print=record_last_print)
61+
userio_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit, _argv=[])
5762

5863

5964
if __name__ == '__main__':

0 commit comments

Comments
 (0)