Skip to content

Commit 4e99185

Browse files
Silence noise when running the test suite. (#505)
* 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. * Minor: added missing class docstrings on a couple of legacy self test classes. --------- Signed-off-by: Jonas Bardino <bardino@science.ku.dk> Co-authored-by: Jonas Bardino <bardino@science.ku.dk>
1 parent f53fe9d commit 4e99185

12 files changed

Lines changed: 84 additions & 91 deletions

mig/lib/events.py

Lines changed: 6 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 legacy_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)
@@ -621,6 +619,10 @@ def main(_exit=sys.exit, _print=print):
621619
"At %s job is %dm in the future for rule" % (timestamp, remain)
622620
)
623621

622+
_exit(0)
623+
624624

625625
if __name__ == "__main__":
626-
main()
626+
from mig.shared.conf import get_configuration_object
627+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
628+
legacy_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 legacy_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+
legacy_main(conf)

mig/shared/userio.py

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

577577

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

681+
_exit(0)
682+
683683

684684
if __name__ == "__main__":
685-
legacy_main()
685+
from mig.shared.conf import get_configuration_object
686+
conf = get_configuration_object(skip_log=True, disable_auth_log=True)
687+
legacy_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 legacy_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+
legacy_main(conf)

tests/test_mig_lib_events.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434
# Imports of the code under test
3535
from mig.lib.events import _restore_env, _save_env, at_remain, cron_match, \
3636
get_path_expand_map, get_time_expand_map, load_atjobs, load_crontab
37-
from mig.lib.events import main as events_main
3837
from mig.lib.events import parse_and_save_atjobs, parse_and_save_crontab, \
3938
parse_atjobs, parse_atjobs_contents, parse_crontab, \
40-
parse_crontab_contents, run_cron_command, run_events_command
39+
parse_crontab_contents, run_cron_command, run_events_command, legacy_main
4140
# Imports required for the unit tests themselves
4241
from tests.support import MigTestCase, ensure_dirs_exist
4342

@@ -3395,15 +3394,14 @@ def raise_on_error_exit(exit_code):
33953394
else:
33963395
identifying_message = "unknown"
33973396
raise AssertionError(
3398-
"failure in unittest/testcore: %s" % (identifying_message,)
3399-
)
3397+
'legacy test failure: %s' % (identifying_message,))
34003398

34013399
raise_on_error_exit.last_print = None
34023400

34033401
def record_last_print(value):
34043402
raise_on_error_exit.last_print = value
34053403

3406-
events_main(_exit=raise_on_error_exit, _print=record_last_print)
3404+
legacy_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit)
34073405

34083406

34093407
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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
from mig.shared.defaults import POLICY_NONE, POLICY_WEAK, POLICY_MEDIUM, \
3939
POLICY_HIGH, POLICY_MODERN, POLICY_CUSTOM, PASSWORD_POLICIES
40-
from mig.shared.pwcrypto import main as pwcrypto_main
4140
from mig.shared.pwcrypto import *
4241

4342
DUMMY_USER = "dummy-user"
@@ -708,6 +707,13 @@ def test_generate_random_password_fixed_seed(self):
708707
self.assertEqual(expected, result,
709708
"failed generate password with fixed seed")
710709

710+
711+
class MigSharedPwCrypto__legacy_main(MigTestCase):
712+
"""Legacy tests for corresponding module self-checks"""
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+
legacy_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: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
TEST_OWNER_DN = "/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test Owner/emailAddress=owner@example.org"
6767

6868

69-
class TestMigSharedUserIO_legacy_main(MigTestCase, UserAssertMixin):
69+
class TestMigSharedUserIO(MigTestCase, UserAssertMixin):
7070
"""Unit tests for userio related helper functions"""
7171

7272
def _provide_configuration(self):
@@ -520,29 +520,32 @@ def test_touch_path(self):
520520

521521

522522
class TestMigSharedUserIO__legacy_main(MigTestCase):
523-
"""Legacy tests for safeinput module self-checks"""
523+
"""Legacy tests for corresponding module self-checks"""
524+
525+
def _provide_configuration(self):
526+
return 'testconfig'
524527

525528
# TODO: migrate all legacy self-check functionality into the above?
526529
def test_existing_main(self):
527530
"""Wrap old inline self-test as an additional unit test"""
528531

532+
self.logger.forgive_errors()
533+
529534
def raise_on_error_exit(exit_code):
530535
if exit_code != 0:
531536
if raise_on_error_exit.last_print is not None:
532537
identifying_message = raise_on_error_exit.last_print
533538
else:
534539
identifying_message = "unknown"
535540
raise AssertionError(
536-
"failure in unittest/testcore: %s" % (identifying_message,)
537-
)
538-
541+
'legacy test failure: %s' % (identifying_message,))
539542
raise_on_error_exit.last_print = None
540543

541544
def record_last_print(value):
542545
"""Helper to show last print on error"""
543546
raise_on_error_exit.last_print = value
544547

545-
legacy_main(_exit=raise_on_error_exit, _print=record_last_print)
548+
legacy_main(self.configuration, print=record_last_print, _exit=raise_on_error_exit, _argv=[])
546549

547550

548551
if __name__ == "__main__":

0 commit comments

Comments
 (0)