Skip to content

Commit fc96b88

Browse files
committed
Teach the test-module script about the new way MODULE_ARGS works in new-style modules.
1 parent ec12cc4 commit fc96b88

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

hacking/test-module

+33-22
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
# modules
2424
#
2525
# example:
26-
# test-module ../library/command /bin/sleep 3
27-
# test-module ../library/service name=httpd ensure=restarted
26+
# test-module -m ../library/command -a "/bin/sleep 3"
27+
# test-module -m ../library/service -a "name=httpd ensure=restarted"
28+
# test-module -m ../library/service -a "name=httpd ensure=restarted" --debugger /usr/bin/pdb
2829

2930
import sys
31+
import base64
3032
import os
3133
import subprocess
3234
import traceback
@@ -60,43 +62,49 @@ def parse():
6062
else:
6163
return options, args
6264

63-
def write_argsfile( argstring):
64-
"""Write args to a file for the module's use.
65-
66-
:return: full path to args file"""
65+
def write_argsfile(argstring):
66+
""" Write args to a file for old-style module's use. """
6767
argspath = os.path.expanduser("~/.ansible_test_module_arguments")
6868
argsfile = open(argspath, 'w')
6969
argsfile.write(argstring)
7070
argsfile.close()
7171
return argspath
7272

73-
def boilerplate_module( modfile):
73+
def boilerplate_module(modfile, args):
74+
""" simulate what ansible does with new style modules """
75+
7476
module_fh = open(modfile)
7577
module_data = module_fh.read()
7678
included_boilerplate = module_data.find(module_common.REPLACER) != -1
7779
module_fh.close()
7880

7981
if included_boilerplate:
8082
module_data = module_data.replace(module_common.REPLACER, module_common.MODULE_COMMON)
83+
encoded_args = base64.b64encode(args)
84+
module_data = module_data.replace(module_common.REPLACER_ARGS, encoded_args)
85+
8186
modfile2_path = os.path.expanduser("~/.ansible_module_generated")
8287
print "* including generated source, if any, saving to: %s" % modfile2_path
8388
print "* this will offset any line numbers in tracebacks/debuggers!"
8489
modfile2 = open(modfile2_path, 'w')
8590
modfile2.write(module_data)
8691
modfile2.close()
8792
modfile = modfile2_path
88-
return modfile2_path
93+
return (modfile2_path, included_boilerplate)
8994
else:
9095
print "* module boilerplate substitution not requested in module, line numbers will be unaltered"
91-
return modfile
96+
return (modfile, included_boilerplate)
9297

9398
def runtest( modfile, argspath):
9499
"""Test run a module, piping it's output for reporting."""
100+
95101
os.system("chmod +x %s" % modfile)
96-
cmd = subprocess.Popen("%s %s" % (modfile, argspath),
97-
shell=True,
98-
stdout=subprocess.PIPE,
99-
stderr=subprocess.PIPE)
102+
103+
invoke = "%s" % (modfile)
104+
if argspath is not None:
105+
invoke = "%s %s" % (modfile, argspath)
106+
107+
cmd = subprocess.Popen(invoke, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
100108
(out, err) = cmd.communicate()
101109

102110
try:
@@ -105,7 +113,6 @@ def runtest( modfile, argspath):
105113
print out
106114
print err
107115
results = utils.parse_json(out)
108-
109116
except:
110117
print "***********************************"
111118
print "INVALID OUTPUT FORMAT"
@@ -115,23 +122,27 @@ def runtest( modfile, argspath):
115122

116123
print "***********************************"
117124
print "PARSED OUTPUT"
118-
119125
print utils.jsonify(results,format=True)
120126

121127
def rundebug(debugger, modfile, argspath):
122128
"""Run interactively with console debugger."""
123-
subprocess.call( "%s %s %s" % (debugger, modfile, argspath), shell=True)
124129

125-
def main():
126-
options, args = parse()
130+
if argspath is not None:
131+
subprocess.call("%s %s %s" % (debugger, modfile, argspath), shell=True)
132+
else:
133+
subprocess.call("%s %s" % (debugger, modfile), shell=True)
127134

128-
argspath = write_argsfile( options.module_args)
129-
modfile = boilerplate_module( options.module_path)
135+
def main():
130136

137+
options, args = parse()
138+
(modfile, is_new_style) = boilerplate_module(options.module_path, options.module_args)
139+
argspath=None
140+
if not is_new_style:
141+
argspath = write_argsfile(options.module_args)
131142
if options.debugger:
132-
rundebug( options.debugger, modfile, argspath)
143+
rundebug(options.debugger, modfile, argspath)
133144
else:
134-
runtest( modfile, argspath)
145+
runtest(modfile, argspath)
135146

136147
if __name__ == "__main__":
137148
main()

0 commit comments

Comments
 (0)