23
23
# modules
24
24
#
25
25
# 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
28
29
29
30
import sys
31
+ import base64
30
32
import os
31
33
import subprocess
32
34
import traceback
@@ -60,43 +62,49 @@ def parse():
60
62
else :
61
63
return options , args
62
64
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. """
67
67
argspath = os .path .expanduser ("~/.ansible_test_module_arguments" )
68
68
argsfile = open (argspath , 'w' )
69
69
argsfile .write (argstring )
70
70
argsfile .close ()
71
71
return argspath
72
72
73
- def boilerplate_module ( modfile ):
73
+ def boilerplate_module (modfile , args ):
74
+ """ simulate what ansible does with new style modules """
75
+
74
76
module_fh = open (modfile )
75
77
module_data = module_fh .read ()
76
78
included_boilerplate = module_data .find (module_common .REPLACER ) != - 1
77
79
module_fh .close ()
78
80
79
81
if included_boilerplate :
80
82
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
+
81
86
modfile2_path = os .path .expanduser ("~/.ansible_module_generated" )
82
87
print "* including generated source, if any, saving to: %s" % modfile2_path
83
88
print "* this will offset any line numbers in tracebacks/debuggers!"
84
89
modfile2 = open (modfile2_path , 'w' )
85
90
modfile2 .write (module_data )
86
91
modfile2 .close ()
87
92
modfile = modfile2_path
88
- return modfile2_path
93
+ return ( modfile2_path , included_boilerplate )
89
94
else :
90
95
print "* module boilerplate substitution not requested in module, line numbers will be unaltered"
91
- return modfile
96
+ return ( modfile , included_boilerplate )
92
97
93
98
def runtest ( modfile , argspath ):
94
99
"""Test run a module, piping it's output for reporting."""
100
+
95
101
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 )
100
108
(out , err ) = cmd .communicate ()
101
109
102
110
try :
@@ -105,7 +113,6 @@ def runtest( modfile, argspath):
105
113
print out
106
114
print err
107
115
results = utils .parse_json (out )
108
-
109
116
except :
110
117
print "***********************************"
111
118
print "INVALID OUTPUT FORMAT"
@@ -115,23 +122,27 @@ def runtest( modfile, argspath):
115
122
116
123
print "***********************************"
117
124
print "PARSED OUTPUT"
118
-
119
125
print utils .jsonify (results ,format = True )
120
126
121
127
def rundebug (debugger , modfile , argspath ):
122
128
"""Run interactively with console debugger."""
123
- subprocess .call ( "%s %s %s" % (debugger , modfile , argspath ), shell = True )
124
129
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 )
127
134
128
- argspath = write_argsfile ( options .module_args )
129
- modfile = boilerplate_module ( options .module_path )
135
+ def main ():
130
136
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 )
131
142
if options .debugger :
132
- rundebug ( options .debugger , modfile , argspath )
143
+ rundebug (options .debugger , modfile , argspath )
133
144
else :
134
- runtest ( modfile , argspath )
145
+ runtest (modfile , argspath )
135
146
136
147
if __name__ == "__main__" :
137
148
main ()
0 commit comments