Teach the test-module script about the new way MODULE_ARGS works in new-style modules.
This commit is contained in:
parent
ec12cc4154
commit
fc96b88205
1 changed files with 33 additions and 22 deletions
|
@ -23,10 +23,12 @@
|
||||||
# modules
|
# modules
|
||||||
#
|
#
|
||||||
# example:
|
# example:
|
||||||
# test-module ../library/command /bin/sleep 3
|
# test-module -m ../library/command -a "/bin/sleep 3"
|
||||||
# test-module ../library/service name=httpd ensure=restarted
|
# test-module -m ../library/service -a "name=httpd ensure=restarted"
|
||||||
|
# test-module -m ../library/service -a "name=httpd ensure=restarted" --debugger /usr/bin/pdb
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import base64
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import traceback
|
import traceback
|
||||||
|
@ -60,17 +62,17 @@ def parse():
|
||||||
else:
|
else:
|
||||||
return options, args
|
return options, args
|
||||||
|
|
||||||
def write_argsfile( argstring):
|
def write_argsfile(argstring):
|
||||||
"""Write args to a file for the module's use.
|
""" Write args to a file for old-style module's use. """
|
||||||
|
|
||||||
:return: full path to args file"""
|
|
||||||
argspath = os.path.expanduser("~/.ansible_test_module_arguments")
|
argspath = os.path.expanduser("~/.ansible_test_module_arguments")
|
||||||
argsfile = open(argspath, 'w')
|
argsfile = open(argspath, 'w')
|
||||||
argsfile.write(argstring)
|
argsfile.write(argstring)
|
||||||
argsfile.close()
|
argsfile.close()
|
||||||
return argspath
|
return argspath
|
||||||
|
|
||||||
def boilerplate_module( modfile):
|
def boilerplate_module(modfile, args):
|
||||||
|
""" simulate what ansible does with new style modules """
|
||||||
|
|
||||||
module_fh = open(modfile)
|
module_fh = open(modfile)
|
||||||
module_data = module_fh.read()
|
module_data = module_fh.read()
|
||||||
included_boilerplate = module_data.find(module_common.REPLACER) != -1
|
included_boilerplate = module_data.find(module_common.REPLACER) != -1
|
||||||
|
@ -78,6 +80,9 @@ def boilerplate_module( modfile):
|
||||||
|
|
||||||
if included_boilerplate:
|
if included_boilerplate:
|
||||||
module_data = module_data.replace(module_common.REPLACER, module_common.MODULE_COMMON)
|
module_data = module_data.replace(module_common.REPLACER, module_common.MODULE_COMMON)
|
||||||
|
encoded_args = base64.b64encode(args)
|
||||||
|
module_data = module_data.replace(module_common.REPLACER_ARGS, encoded_args)
|
||||||
|
|
||||||
modfile2_path = os.path.expanduser("~/.ansible_module_generated")
|
modfile2_path = os.path.expanduser("~/.ansible_module_generated")
|
||||||
print "* including generated source, if any, saving to: %s" % modfile2_path
|
print "* including generated source, if any, saving to: %s" % modfile2_path
|
||||||
print "* this will offset any line numbers in tracebacks/debuggers!"
|
print "* this will offset any line numbers in tracebacks/debuggers!"
|
||||||
|
@ -85,18 +90,21 @@ def boilerplate_module( modfile):
|
||||||
modfile2.write(module_data)
|
modfile2.write(module_data)
|
||||||
modfile2.close()
|
modfile2.close()
|
||||||
modfile = modfile2_path
|
modfile = modfile2_path
|
||||||
return modfile2_path
|
return (modfile2_path, included_boilerplate)
|
||||||
else:
|
else:
|
||||||
print "* module boilerplate substitution not requested in module, line numbers will be unaltered"
|
print "* module boilerplate substitution not requested in module, line numbers will be unaltered"
|
||||||
return modfile
|
return (modfile, included_boilerplate)
|
||||||
|
|
||||||
def runtest( modfile, argspath):
|
def runtest( modfile, argspath):
|
||||||
"""Test run a module, piping it's output for reporting."""
|
"""Test run a module, piping it's output for reporting."""
|
||||||
|
|
||||||
os.system("chmod +x %s" % modfile)
|
os.system("chmod +x %s" % modfile)
|
||||||
cmd = subprocess.Popen("%s %s" % (modfile, argspath),
|
|
||||||
shell=True,
|
invoke = "%s" % (modfile)
|
||||||
stdout=subprocess.PIPE,
|
if argspath is not None:
|
||||||
stderr=subprocess.PIPE)
|
invoke = "%s %s" % (modfile, argspath)
|
||||||
|
|
||||||
|
cmd = subprocess.Popen(invoke, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
(out, err) = cmd.communicate()
|
(out, err) = cmd.communicate()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -105,7 +113,6 @@ def runtest( modfile, argspath):
|
||||||
print out
|
print out
|
||||||
print err
|
print err
|
||||||
results = utils.parse_json(out)
|
results = utils.parse_json(out)
|
||||||
|
|
||||||
except:
|
except:
|
||||||
print "***********************************"
|
print "***********************************"
|
||||||
print "INVALID OUTPUT FORMAT"
|
print "INVALID OUTPUT FORMAT"
|
||||||
|
@ -115,23 +122,27 @@ def runtest( modfile, argspath):
|
||||||
|
|
||||||
print "***********************************"
|
print "***********************************"
|
||||||
print "PARSED OUTPUT"
|
print "PARSED OUTPUT"
|
||||||
|
|
||||||
print utils.jsonify(results,format=True)
|
print utils.jsonify(results,format=True)
|
||||||
|
|
||||||
def rundebug(debugger, modfile, argspath):
|
def rundebug(debugger, modfile, argspath):
|
||||||
"""Run interactively with console debugger."""
|
"""Run interactively with console debugger."""
|
||||||
subprocess.call( "%s %s %s" % (debugger, modfile, argspath), shell=True)
|
|
||||||
|
if argspath is not None:
|
||||||
|
subprocess.call("%s %s %s" % (debugger, modfile, argspath), shell=True)
|
||||||
|
else:
|
||||||
|
subprocess.call("%s %s" % (debugger, modfile), shell=True)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
options, args = parse()
|
options, args = parse()
|
||||||
|
(modfile, is_new_style) = boilerplate_module(options.module_path, options.module_args)
|
||||||
argspath = write_argsfile( options.module_args)
|
argspath=None
|
||||||
modfile = boilerplate_module( options.module_path)
|
if not is_new_style:
|
||||||
|
argspath = write_argsfile(options.module_args)
|
||||||
if options.debugger:
|
if options.debugger:
|
||||||
rundebug( options.debugger, modfile, argspath)
|
rundebug(options.debugger, modfile, argspath)
|
||||||
else:
|
else:
|
||||||
runtest( modfile, argspath)
|
runtest(modfile, argspath)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue