Skip to content

Commit d68489b

Browse files
committed
Manually merge PR128 to add unit testing of the cat functionality backend. Made a number of smaller adjustments in line with comments on github. Spliced the new _temppath function back into the existing temppath function with a new skip_output_anchor=False argument instead. Changed the semi-private _ensuredirs to a public ensure_dirs_exist function for general use. It overlaps with shared.fileio.makedirs_rec but it's probably better to keep tests independent of code there. Renamed and eliminated a few leftover WSGI and CGI references. Unwrapped a one-liner conditional and changed default prefix to empty string to eliminate None vs empty consideration. Added missing docstrings. Wrapped some long lines and fixed a comment typo.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6148 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent 5748ddd commit d68489b

File tree

5 files changed

+349
-11
lines changed

5 files changed

+349
-11
lines changed

mig/shared/accountstate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
from __future__ import print_function
3333
from __future__ import absolute_import
34+
from past.builtins import basestring
3435

3536
import os
3637
import time

mig/shared/functionality/cat.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,25 @@ def signature():
5454

5555

5656
def main(client_id, user_arguments_dict, environ=None):
57-
"""Main function used by front end"""
57+
"""Main function wrapper used by front end"""
5858

5959
if environ is None:
6060
environ = os.environ
6161

6262
(configuration, logger, output_objects, op_name) = \
6363
initialize_main_variables(client_id)
64+
65+
return _main(configuration, logger, op_name=op_name,
66+
output_objects=output_objects, client_id=client_id,
67+
user_arguments_dict=user_arguments_dict)
68+
69+
70+
def _main(configuration, logger, op_name='', output_objects=[], client_id=None,
71+
user_arguments_dict=None, environ=None):
72+
"""Actual main function to generate contents for the front end"""
73+
if logger is None:
74+
logger = configuration.logger
75+
6476
client_dir = client_id_dir(client_id)
6577
defaults = signature()[1]
6678
status = returnvalues.OK
@@ -71,6 +83,7 @@ def main(client_id, user_arguments_dict, environ=None):
7183
client_id,
7284
configuration,
7385
allow_rejects=False,
86+
environ=environ,
7487
# NOTE: path can use wildcards, dst cannot
7588
typecheck_overrides={'path': valid_path_pattern},
7689
)
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
(dp0
2+
V/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=b'Test User'/emailAddress=dummy-user
3+
p1
4+
(dp2
5+
Vfull_name
6+
p3
7+
Vb'Test User'
8+
p4
9+
sVorganization
10+
p5
11+
VTest Org
12+
p6
13+
sVstate
14+
p7
15+
VNA
16+
p8
17+
sVcountry
18+
p9
19+
VDK
20+
p10
21+
sVemail
22+
p11
23+
Vdummy-user
24+
p12
25+
sVcomment
26+
p13
27+
VThis is the create comment
28+
p14
29+
sVpassword
30+
p15
31+
V
32+
p16
33+
sVpassword_hash
34+
p17
35+
VPBKDF2$sha256$10000$b't0JM/JjkQ347th0Q'$b'QupJt53hA5KhESEeqDhTQTCPOrCBvZ6H'
36+
p18
37+
sVdistinguished_name
38+
p19
39+
V/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=b'Test User'/emailAddress=dummy-user
40+
p20
41+
sVlocality
42+
p21
43+
g16
44+
sVorganizational_unit
45+
p22
46+
g16
47+
sVexpire
48+
p23
49+
I1757925298
50+
sVcreated
51+
p24
52+
F1726233828.2676349
53+
sVunique_id
54+
p25
55+
VktyCKIRg9HvsVzXMQ22EaKS67t9atchv9JKTiJqrtBiGN3qksKrbTTYIH8mitY2K
56+
p26
57+
sVopenid_names
58+
p27
59+
(lp28
60+
sVold_password_hash
61+
p29
62+
VPBKDF2$sha256$10000$b'GL7Qq92iLe/hZXBo'$b'ZwB/5IZqgU7onP+ZqZk9zcHVZOx7jmWz'
63+
p30
64+
sVrenewed
65+
p31
66+
F1726389298.7801197
67+
ssV/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=dummy-user
68+
p32
69+
(dp33
70+
Vfull_name
71+
p34
72+
VTest User
73+
p35
74+
sVorganization
75+
p36
76+
VTest Org
77+
p37
78+
sVstate
79+
p38
80+
VNA
81+
p39
82+
sVcountry
83+
p40
84+
VDK
85+
p41
86+
sVemail
87+
p42
88+
Vdummy-user
89+
p43
90+
sVcomment
91+
p44
92+
VThis is the create comment
93+
p45
94+
sVpassword
95+
p46
96+
g16
97+
sVpassword_hash
98+
p47
99+
VPBKDF2$sha256$10000$b'kZ8WgLNH+wg3X11d'$b't1d08MV4g215WYW7S7EbkjHqDF+MCjMa'
100+
p48
101+
sVdistinguished_name
102+
p49
103+
V/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=dummy-user
104+
p50
105+
sVlocality
106+
p51
107+
g16
108+
sVorganizational_unit
109+
p52
110+
g16
111+
sVexpire
112+
p53
113+
I1759243332
114+
sVcreated
115+
p54
116+
F1726602273.7987707
117+
sVunique_id
118+
p55
119+
VKdYHJ21t37jAoHUmBq6t8Xnsnih6JWR5i0QepHoVXfDpQxz9fQGnEmegoDNrPzbe
120+
p56
121+
sVopenid_names
122+
p57
123+
(lp58
124+
sVold_password_hash
125+
p59
126+
VPBKDF2$sha256$10000$b'yObizsUepZvvJ0/r'$b'uKIt7n6Lf/7WXD6pKDGyvT30L2uowBnV'
127+
p60
128+
sVrenewed
129+
p61
130+
F1727707333.0969944
131+
ssV/C=DK/ST=NA/L=NA/O=Test Org/OU=NA/CN=Test User/emailAddress=test@example.com
132+
p62
133+
(dp63
134+
Vfull_name
135+
p64
136+
VTest User
137+
p65
138+
sVorganization
139+
p66
140+
VTest Org
141+
p67
142+
sVstate
143+
p68
144+
VNA
145+
p69
146+
sVcountry
147+
p70
148+
VDK
149+
p71
150+
sVemail
151+
p72
152+
Vtest@example.com
153+
p73
154+
sVcomment
155+
p74
156+
VThis is the create comment
157+
p75
158+
sVpassword
159+
p76
160+
g16
161+
sVpassword_hash
162+
p77
163+
VPBKDF2$sha256$10000$b'/TkhLk4yMGf6XhaY'$b'7HUeQ9iwCkE4YMQAaCd+ZdrN+y8EzkJH'
164+
p78
165+
sVdistinguished_name
166+
p79
167+
g62
168+
sVlocality
169+
p80
170+
g16
171+
sVorganizational_unit
172+
p81
173+
g16
174+
sVexpire
175+
p82
176+
I1758970812
177+
sVcreated
178+
p83
179+
F1727434813.0792377
180+
sVunique_id
181+
p84
182+
VaTza92klrnN2wfylm6HnphCy9C3PReGpQ6jklJ7zF3xjeaUDw36tW95Avx43vtba
183+
p85
184+
sVopenid_names
185+
p86
186+
(lp87
187+
ss.

tests/support/__init__.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,17 @@ def is_path_within(path, start=None, _msg=None):
276276
return not relative.startswith('..')
277277

278278

279-
def fixturefile(relative_path, fixture_format=None):
279+
def ensure_dirs_exist(absolute_dir):
280+
"""A simple helper to create absolute_dir and any parents if missing"""
281+
try:
282+
os.makedirs(absolute_dir)
283+
except OSError as oserr:
284+
if oserr.errno != errno.EEXIST:
285+
raise
286+
return absolute_dir
287+
288+
289+
def fixturefile(relative_path, fixture_format=None, include_path=False):
280290
"""Support function for loading fixtures from their serialised format.
281291
282292
Doing so is a little more involved than it may seem because serialisation
@@ -296,12 +306,27 @@ def fixturefile(relative_path, fixture_format=None):
296306
#_, extension = os.path.splitext(os.path.basename(tmp_path))
297307
#assert fixture_format == extension, "fixture file does not match format"
298308

299-
if fixture_format == 'json':
300-
return _fixturefile_json(tmp_path)
309+
data = None
310+
311+
if fixture_format == 'binary':
312+
with open(tmp_path, 'rb') as binfile:
313+
data = binfile.read()
314+
elif fixture_format == 'json':
315+
data = _fixturefile_json(tmp_path)
301316
else:
302317
raise AssertionError(
303318
"unsupported fixture format: %s" % (fixture_format,))
304319

320+
return (data, tmp_path) if include_path else data
321+
322+
323+
def fixturefile_normname(relative_path, prefix=''):
324+
"""Grab normname from relative_path and optionally add a path prefix"""
325+
normname, _ = relative_path.split('--')
326+
if prefix:
327+
return os.path.join(prefix, normname)
328+
return normname
329+
305330

306331
_FIXTUREFILE_HINTAPPLIERS = {
307332
'array_of_tuples': lambda value: [tuple(x) for x in value]
@@ -311,7 +336,7 @@ def fixturefile(relative_path, fixture_format=None):
311336
def _fixturefile_json(json_path):
312337
hints = ConfigParser()
313338

314-
# leti's see if there are loading hints
339+
# let's see if there are loading hints
315340
try:
316341
hints_path = "%s.ini" % (json_path,)
317342
with open(hints_path) as hints_file:
@@ -336,16 +361,25 @@ def fixturepath(relative_path):
336361
return tmp_path
337362

338363

339-
def temppath(relative_path, test_case, ensure_dir=False, skip_clean=False):
340-
"""Get absolute temp path for relative_path"""
364+
def temppath(relative_path, test_case, ensure_dir=False, skip_clean=False,
365+
skip_output_anchor=False):
366+
"""Register relative_path as a temp path and schedule automatic clean up
367+
after unit tests unless skip_clean is set. Anchors the temp path in
368+
internal test output dir unless skip_output_anchor is set. Returns
369+
resulting temp path.
370+
"""
341371
assert isinstance(test_case, MigTestCase)
342-
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
372+
if not skip_output_anchor:
373+
tmp_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
374+
else:
375+
tmp_path = relative_path
343376
if ensure_dir:
344377
try:
345378
os.mkdir(tmp_path)
346-
except FileExistsError:
347-
raise AssertionError(
348-
"ABORT: use of unclean output path: %s" % relative_path)
379+
except OSError as oserr:
380+
if oserr.errno == errno.EEXIST:
381+
raise AssertionError(
382+
"ABORT: use of unclean output path: %s" % tmp_path)
349383
if not skip_clean:
350384
test_case._cleanup_paths.add(tmp_path)
351385
return tmp_path

0 commit comments

Comments
 (0)