4 import sys, os, os.path
5 from optparse import OptionParser
7 from time import strftime
10 from TestPlc import TestPlc
11 from TestSite import TestSite
12 from TestNode import TestNode
16 subversion_id = "$Id$"
18 default_config = [ 'default' ]
20 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
23 self.path=os.path.dirname(sys.argv[0]) or "."
27 def show_env (options, message):
28 utils.header (message)
29 utils.show_options("main options",options)
32 def optparse_list (option, opt, value, parser):
34 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
36 setattr(parser.values,option.dest,value.split())
39 steps_message=20*'x'+" Defaut steps are\n"+TestPlc.printable_steps(TestPlc.default_steps)
40 steps_message += "\n"+20*'x'+" Other useful steps are\n"+TestPlc.printable_steps(TestPlc.other_steps)
41 usage = """usage: %%prog [options] steps
42 myplc-url defaults to the last value used, as stored in arg-myplc-url,
44 build-url defaults to the last value used, as stored in arg-build-url,
46 config defaults to the last value used, as stored in arg-config,
48 ips defaults to the last value used, as stored in arg-ips,
49 default is to use IP scanning
50 steps refer to a method in TestPlc or to a step_* module
52 """%(TestMain.default_build_url,TestMain.default_config)
53 usage += steps_message
54 parser=OptionParser(usage=usage,version=self.subversion_id)
55 parser.add_option("-u","--url",action="store", dest="myplc_url",
56 help="myplc URL - for locating build output")
57 parser.add_option("-b","--build",action="store", dest="build_url",
58 help="Build URL - for using vtest-init-vserver.sh in native mode")
59 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
60 nargs=1,type="string",
61 help="Config module - can be set multiple times, or use quotes")
62 parser.add_option("-x","--exclude",action="callback", callback=TestMain.optparse_list, dest="exclude",
63 nargs=1,type="string",default=[],
64 help="steps to exclude - can be set multiple times, or use quotes")
65 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
66 help="Run all default steps")
67 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
68 help="List known steps")
69 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
70 nargs=1,type="string",
71 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
72 parser.add_option("-s","--vserver",action="store_true",dest="native",default=False,
73 help="deploy myplc-native rather than former chroot-based package")
74 parser.add_option("-1","--small",action="store_true",dest="small_test",default=False,
75 help="run a small test -- typically only one node")
76 parser.add_option("-d","--dbname",action="store",dest="dbname",default=None,
77 help="Used by db_dump and db_restore")
78 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
79 help="Run in verbose mode")
80 parser.add_option("-q","--quiet", action="store_true", dest="quiet", default=False,
81 help="Run in quiet mode")
82 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
83 help="Show environment and exits")
84 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
85 help="Force the NM to restart in check_slices step")
86 parser.add_option("-t","--trace", action="store", dest="trace_file", default=None,
87 #default="logs/trace-@TIME@.txt",
88 help="Trace file location")
89 (self.options, self.args) = parser.parse_args()
91 if len(self.args) == 0:
92 if self.options.all_steps:
93 self.options.steps=TestPlc.default_steps
94 elif self.options.dry_run:
95 self.options.steps=TestPlc.default_steps
96 elif self.options.list_steps:
100 print 'No step found (do you mean -a ? )'
101 print "Run %s --help for help"%sys.argv[0]
104 self.options.steps = self.args
106 # handle defaults and option persistence
107 for (recname,filename,default) in (
108 ('build_url','arg-build-url',TestMain.default_build_url) ,
109 ('ips','arg-ips',[]) ,
110 ('config','arg-config',TestMain.default_config) ,
111 ('myplc_url','arg-myplc-url',"") ,
113 # print 'handling',recname
115 is_list = isinstance(default,list)
116 if not getattr(self.options,recname):
118 parsed=file(path).readlines()
119 if not is_list: # strings
121 print "%s - error when parsing %s"%(sys.argv[1],path)
123 parsed=parsed[0].strip()
125 parsed=[x.strip() for x in parsed]
126 setattr(self.options,recname,parsed)
129 setattr(self.options,recname,default)
131 print "Cannot determine",recname
132 print "Run %s --help for help"%sys.argv[0]
134 if not self.options.quiet:
135 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
140 fsave.write(getattr(self.options,recname) + "\n")
142 for value in getattr(self.options,recname):
143 fsave.write(value + "\n")
145 # utils.header('Saved %s into %s'%(recname,filename))
147 self.options.arch = "i386"
148 if self.options.myplc_url.find("x86_64") >= 0:
149 self.options.arch="x86_64"
151 if not self.options.steps:
153 #self.options.steps=['dump','clean','install','populate']
154 self.options.steps=TestPlc.default_steps
158 for step in self.options.steps:
160 for exclude in self.options.exclude:
161 if utils.match(step,exclude):
164 if keep: selected.append(step)
165 self.options.steps=selected
167 # this is useful when propagating on host boxes, to avoid conflicts
168 self.options.buildname = os.path.basename (os.path.abspath (self.path))
170 if self.options.verbose:
171 self.show_env(self.options,"Verbose")
175 for config in self.options.config:
176 modulename='config_'+config
178 m = __import__(modulename)
179 all_plc_specs = m.config(all_plc_specs,self.options)
181 traceback.print_exc()
182 print 'Cannot load config %s -- ignored'%modulename
185 if not self.options.quiet:
186 utils.show_test_spec("Test specifications",all_plc_specs)
187 # build a TestPlc object from the result, passing options
188 for spec in all_plc_specs:
189 spec['disabled'] = False
190 all_plcs = [ (x, TestPlc(x,self.options)) for x in all_plc_specs]
192 # pass options to utils as well
193 utils.init_options(self.options)
195 overall_result = True
196 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
198 for step in self.options.steps:
199 if not TestPlc.valid_step(step):
203 if step.find("force_") == 0:
204 step=step.replace("force_","")
206 # try and locate a method in TestPlc
207 if testplc_method_dict.has_key(step):
208 all_step_infos += [ (step, testplc_method_dict[step] , force)]
209 # otherwise search for the 'run' method in the step_<x> module
211 modulename='step_'+step
213 # locate all methods named run* in the module
214 module_dict = __import__(modulename).__dict__
215 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
217 raise Exception,"No run* method in module %s"%modulename
219 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name],force) for name in names ]
221 print '********** step %s NOT FOUND -- ignored'%(step)
222 traceback.print_exc()
223 overall_result = False
225 if self.options.dry_run:
226 self.show_env(self.options,"Dry run")
228 # init & open trace file if provided
229 if self.options.trace_file and not self.options.dry_run:
230 time=strftime("%H-%M")
231 date=strftime("%Y-%m-%d")
232 trace_file=self.options.trace_file
233 trace_file=trace_file.replace("@TIME@",time)
234 trace_file=trace_file.replace("@DATE@",date)
235 self.options.trace_file=trace_file
236 # create dir if needed
237 trace_dir=os.path.dirname(trace_file)
238 if trace_dir and not os.path.isdir(trace_dir):
239 os.makedirs(trace_dir)
240 trace=open(trace_file,"w")
242 # do all steps on all plcs
243 TRACE_FORMAT="TRACE: time=%(time)s plc=%(plcname)s step=%(stepname)s status=%(status)s force=%(force)s\n"
244 for (stepname,method,force) in all_step_infos:
245 for (spec,obj) in all_plcs:
249 time=strftime("%Y-%m-%d-%H-%M")
250 if not spec['disabled'] or force:
253 if force: force_msg=" (forced)"
254 utils.header("********** RUNNING step %s%s on plc %s"%(stepname,force_msg,plcname))
255 step_result = method(obj)
257 utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
260 overall_result = False
261 spec['disabled'] = True
262 utils.header('********** FAILED Step %s on %s - discarding that plc from further steps'%(stepname,plcname))
266 spec['disabled'] = True
267 traceback.print_exc()
268 utils.header ('********** FAILED (exception) Step %s on plc %s - discarding this plc from further steps'%(stepname,plcname))
271 # do not run, just display it's skipped
273 utils.header("********** IGNORED Plc %s is disabled - skipping step %s"%(plcname,stepname))
275 if not self.options.dry_run:
276 # alwas do this on stdout
277 print TRACE_FORMAT%locals()
278 # duplicate on trace_file if provided
279 if self.options.trace_file:
280 trace.write(TRACE_FORMAT%locals())
282 if self.options.trace_file and not self.options.dry_run:
285 return overall_result
287 # wrapper to run, returns a shell-compatible result
298 traceback.print_exc()
301 if __name__ == "__main__":
302 sys.exit(TestMain().main())