Skip to content

Commit edbfb42

Browse files
committed
Merge remote-tracking branch 'origin/master' into edge
2 parents 3b6e9d1 + b94f047 commit edbfb42

13 files changed

+126
-137
lines changed

mig/install/generateconfs.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def usage(options):
6464
''' % (sys.argv[0], '\n'.join(lines)))
6565

6666

67-
def main(argv, _generate_confs=generate_confs):
67+
def main(argv, _generate_confs=generate_confs, _print=print):
6868
str_names = [
6969
'source',
7070
'destination',
@@ -317,7 +317,7 @@ def main(argv, _generate_confs=generate_confs):
317317
elif opt_name in bool_names and val:
318318
settings[opt_name] = (val.strip().lower() in ['1', 'true', 'yes'])
319319
else:
320-
print('Error: environment options %r not supported!' % opt_name)
320+
_print('Error: environment options %r not supported!' % opt_name)
321321
usage(names)
322322
return 1
323323

@@ -327,7 +327,7 @@ def main(argv, _generate_confs=generate_confs):
327327
try:
328328
(opts, args) = getopt.getopt(argv, flag_str, opts_str)
329329
except getopt.GetoptError as exc:
330-
print('Error: ', exc.msg)
330+
_print('Error: ', exc.msg)
331331
usage(names)
332332
return 1
333333

@@ -343,13 +343,13 @@ def main(argv, _generate_confs=generate_confs):
343343
elif opt_name in bool_names:
344344
settings[opt_name] = (val.strip().lower() in ['1', 'true', 'yes'])
345345
else:
346-
print('Error: command line option %r not supported!' % opt_name)
346+
_print('Error: command line option %r not supported!' % opt_name)
347347
usage(names)
348348
return 1
349349

350350
if args:
351-
print('Error: non-option arguments are no longer supported!')
352-
print(" ... found: %s" % args)
351+
_print('Error: non-option arguments are no longer supported!')
352+
_print(" ... found: %s" % args)
353353
usage(names)
354354
return 1
355355
if settings['destination_suffix'] == 'DEFAULT':
@@ -364,10 +364,10 @@ def main(argv, _generate_confs=generate_confs):
364364
else:
365365
# ... but use verbatim passthrough for absolute destination
366366
output_path = settings['destination']
367-
print('# Creating confs with:')
367+
_print('# Creating confs with:')
368368
# NOTE: force list to avoid problems with in-line edits
369369
for (key, val) in list(settings.items()):
370-
print('%s: %s' % (key, val))
370+
_print('%s: %s' % (key, val))
371371
# Remove default values to use generate_confs default values
372372
if val == 'DEFAULT':
373373
del settings[key]
@@ -381,9 +381,9 @@ def main(argv, _generate_confs=generate_confs):
381381
instructions_fd = open(instructions_path, "r")
382382
instructions = instructions_fd.read()
383383
instructions_fd.close()
384-
print(instructions)
384+
_print(instructions)
385385
except Exception as exc:
386-
print("ERROR: could not read generated instructions: %s" % exc)
386+
_print("ERROR: could not read generated instructions: %s" % exc)
387387
return 1
388388
return 0
389389

mig/shared/jupyter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def gen_balancer_proxy_template(url, define, name, member_hosts,
106106
return template
107107

108108

109-
def gen_openid_template(url, define, auth_type):
109+
def gen_openid_template(url, define, auth_type, _print=print):
110110
"""Generates an openid 2.0 or connect apache configuration section template
111111
for a particular jupyter service.
112112
url: Setting the url_path to where the jupyter service is to be located.
@@ -124,7 +124,7 @@ def gen_openid_template(url, define, auth_type):
124124
'define': define,
125125
'auth_type': auth_type
126126
}
127-
print("filling in jupyter gen_openid_template with helper: (%s)" % fill_helpers)
127+
_print("filling in jupyter gen_openid_template with helper: (%s)" % fill_helpers)
128128

129129
template = """
130130
<IfDefine %(define)s>

mig/unittest/testcore.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ def main(_exit=sys.exit):
8484
format="%(asctime)s %(levelname)s %(message)s")
8585
configuration.logger = logging
8686

87-
print("Running unit test on shared core functions")
88-
89-
print("Testing shared base functions")
87+
print("Running unit test on shared core functions ..")
9088

9189
short_alias = 'email'
9290
test_clients = [
@@ -189,9 +187,8 @@ def main(_exit=sys.exit):
189187
(size, outlen, len(shortened)))
190188
_exit(1)
191189

192-
print("Completed shared base functions tests")
190+
print("Running unit test on shared core functions DONE")
193191

194-
print("Done running unit test on shared core functions")
195192
_exit(0)
196193

197194
if __name__ == "__main__":

tests/support/__init__.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def assertFileContentIdentical(self, file_actual, file_expected):
167167
os.path.relpath(file_actual, MIG_BASE),
168168
''.join(different_lines)))
169169

170+
def assertFileExists(self, relative_path):
171+
"""Make sure relative_path exists and is a file"""
172+
path_kind = self.assertPathExists(relative_path)
173+
assert path_kind == "file", "expected a file but found %s" % (
174+
path_kind, )
175+
return os.path.join(TEST_OUTPUT_DIR, relative_path)
176+
170177
def assertPathExists(self, relative_path):
171178
"""Make sure file in relative_path exists"""
172179
assert not os.path.isabs(
@@ -208,12 +215,6 @@ def cleanpath(relative_path, test_case, ensure_dir=False):
208215
"""Register post-test clean up of file in relative_path"""
209216
assert isinstance(test_case, MigTestCase)
210217
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
211-
if ensure_dir:
212-
try:
213-
os.mkdir(tmp_path)
214-
except FileExistsError:
215-
raise AssertionError(
216-
"ABORT: use of unclean output path: %s" % relative_path)
217218
test_case._cleanup_paths.add(tmp_path)
218219
return tmp_path
219220

@@ -224,17 +225,21 @@ def fixturepath(relative_path):
224225
return tmp_path
225226

226227

227-
def outputpath(relative_path):
228-
"""Get absolute output path for relative_path"""
229-
assert not os.path.isabs(relative_path)
230-
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
231-
return tmp_path
232-
233-
234-
def temppath(relative_path, test_case, skip_clean=False):
228+
def temppath(relative_path, test_case, ensure_dir=False, skip_clean=False):
235229
"""Get absolute temp path for relative_path"""
236230
assert isinstance(test_case, MigTestCase)
237231
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
232+
if ensure_dir:
233+
try:
234+
os.mkdir(tmp_path)
235+
except FileExistsError:
236+
raise AssertionError(
237+
"ABORT: use of unclean output path: %s" % relative_path)
238238
if not skip_clean:
239239
test_case._cleanup_paths.add(tmp_path)
240240
return tmp_path
241+
242+
243+
# compatibility alias
244+
def cleanpath(relative_path, test_case, **kwargs):
245+
return temppath(relative_path, test_case, **kwargs)

tests/test_mig_install_generateconfs.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def _generate_confs(*args, **kwargs):
5959
return _generate_confs
6060

6161

62+
def noop(*args):
63+
pass
64+
65+
6266
class MigInstallGenerateconfs__main(MigTestCase):
6367
"""Unit test helper for the migrid code pointed to in class name"""
6468

@@ -72,7 +76,7 @@ def test_option_permanent_freeze(self):
7276
dict(destination_dir=expected_generated_dir))
7377
test_arguments = ['--permanent_freeze', 'yes']
7478

75-
exit_code = main(test_arguments, _generate_confs=fake_generate_confs)
79+
exit_code = main(test_arguments, _generate_confs=fake_generate_confs, _print=noop)
7680
self.assertEqual(exit_code, 0)
7781

7882

tests/test_mig_shared_compat.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
import os
3232
import sys
3333

34-
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
35-
from support import MigTestCase, testmain
34+
from tests.support import MigTestCase, testmain
3635

3736
from mig.shared.compat import PY2, ensure_native_string
3837

tests/test_mig_shared_install.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@
3636
import pwd
3737
import sys
3838

39-
sys.path.append(os.path.realpath(
40-
os.path.join(os.path.dirname(__file__), "..")))
41-
42-
from support import MIG_BASE, TEST_OUTPUT_DIR, MigTestCase, \
43-
testmain, temppath, cleanpath, fixturepath, is_path_within, outputpath
39+
from tests.support import MIG_BASE, TEST_OUTPUT_DIR, MigTestCase, \
40+
testmain, temppath, cleanpath, fixturepath, is_path_within
4441

4542
from mig.shared.defaults import keyword_auto
4643
from mig.shared.install import determine_timezone, generate_confs
@@ -223,10 +220,8 @@ def test_creates_output_files_with_datasafety(self):
223220
_getpwnam=create_dummy_gpwnam(4321, 1234),
224221
)
225222

226-
relative_file = 'confs-stdlocal/MiGserver.conf'
227-
self.assertPathExists('confs-stdlocal/MiGserver.conf')
223+
actual_file = self.assertFileExists('confs-stdlocal/MiGserver.conf')
228224

229-
actual_file = outputpath(relative_file)
230225
self.assertConfigKey(
231226
actual_file, 'SITE', 'datasafety_link', expected='TEST_DATASAFETY_LINK')
232227
self.assertConfigKey(
@@ -246,10 +241,8 @@ def test_creates_output_files_with_permanent_freeze(self):
246241
_getpwnam=create_dummy_gpwnam(4321, 1234),
247242
)
248243

249-
relative_file = 'confs-stdlocal/MiGserver.conf'
250-
self.assertPathExists('confs-stdlocal/MiGserver.conf')
244+
actual_file = self.assertFileExists('confs-stdlocal/MiGserver.conf')
251245

252-
actual_file = outputpath(relative_file)
253246
self.assertConfigKey(
254247
actual_file, 'SITE', 'permanent_freeze', expected=arg_val)
255248

tests/test_mig_shared_jupyter.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@
3939
from mig.shared.jupyter import gen_openid_template
4040

4141

42+
def noop(*args):
43+
pass
44+
45+
4246
class MigSharedJupyter(MigTestCase):
4347
"""Wrap unit tests for the corresponding module"""
4448

4549
def test_jupyter_gen_openid_template_openid_auth(self):
46-
filled = gen_openid_template("/some-jupyter-url", "MyDefine", "OpenID")
50+
filled = gen_openid_template(
51+
"/some-jupyter-url", "MyDefine", "OpenID", _print=noop)
4752
expected = """
4853
<IfDefine MyDefine>
4954
<Location /some-jupyter-url>
@@ -57,8 +62,9 @@ def test_jupyter_gen_openid_template_openid_auth(self):
5762
self.assertEqual(filled, expected)
5863

5964
def test_jupyter_gen_openid_template_oidc_auth(self):
60-
filled = gen_openid_template("/some-jupyter-url", "MyDefine",
61-
"openid-connect")
65+
filled = gen_openid_template(
66+
"/some-jupyter-url", "MyDefine", "openid-connect", _print=noop)
67+
6268
expected = """
6369
<IfDefine MyDefine>
6470
<Location /some-jupyter-url>
@@ -72,40 +78,24 @@ def test_jupyter_gen_openid_template_oidc_auth(self):
7278
self.assertEqual(filled, expected)
7379

7480
def test_jupyter_gen_openid_template_invalid_url_type(self):
75-
rejected = False
76-
try:
81+
with self.assertRaises(AssertionError):
7782
filled = gen_openid_template(None, "MyDefine",
78-
"no-such-auth-type")
79-
except AssertionError as err:
80-
rejected = True
81-
self.assertTrue(rejected)
83+
"openid-connect")
8284

8385
def test_jupyter_gen_openid_template_invalid_define_type(self):
84-
rejected = False
85-
try:
86+
with self.assertRaises(AssertionError):
8687
filled = gen_openid_template("/some-jupyter-url", None,
8788
"no-such-auth-type")
88-
except AssertionError as err:
89-
rejected = True
90-
self.assertTrue(rejected)
9189

92-
def test_jupyter_gen_openid_template_invalid_auth_type(self):
93-
rejected = False
94-
try:
90+
def test_jupyter_gen_openid_template_missing_auth_type(self):
91+
with self.assertRaises(AssertionError):
9592
filled = gen_openid_template("/some-jupyter-url", "MyDefine",
9693
None)
97-
except AssertionError as err:
98-
rejected = True
99-
self.assertTrue(rejected)
10094

101-
def test_jupyter_gen_openid_template_invalid_auth_val(self):
102-
rejected = False
103-
try:
95+
def test_jupyter_gen_openid_template_invalid_auth_type(self):
96+
with self.assertRaises(AssertionError):
10497
filled = gen_openid_template("/some-jupyter-url", "MyDefine",
10598
"no-such-auth-type")
106-
except AssertionError as err:
107-
rejected = True
108-
self.assertTrue(rejected)
10999

110100
# TODO: add more coverage of module
111101

tests/test_mig_shared_safeinput.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
import sys
3434
from past.builtins import basestring
3535

36-
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
36+
from tests.support import MigTestCase, testmain
3737

38-
from support import MigTestCase, testmain
3938
from mig.shared.safeinput import main as safeinput_main, InputException, \
4039
filter_commonname, valid_commonname
4140

tests/test_mig_shared_serial.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
import os
3131
import sys
3232

33-
sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), ".")))
33+
from tests.support import MigTestCase, temppath, testmain
3434

35-
from support import MigTestCase, temppath, testmain
3635
from mig.shared.serial import *
3736

3837
class BasicSerial(MigTestCase):

0 commit comments

Comments
 (0)