4 import sys, os, os.path
5 from optparse import OptionParser
9 from TestPlc import TestPlc
10 from TestSite import TestSite
11 from TestNode import TestNode
17 subversion_id = "$Id$"
19 default_config = [ 'main' , '1vnodes' , '1testbox64' ]
21 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
24 self.path=os.path.dirname(sys.argv[0]) or "."
28 def show_env (options, message):
29 utils.header (message)
30 utils.show_options("main options",options)
33 def optparse_list (option, opt, value, parser):
35 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
37 setattr(parser.values,option.dest,value.split())
40 def printable_steps (list):
41 return " ".join(list).replace(" "+SEP+" ","\n")
44 steps_message=20*'x'+" Defaut steps are\n"+TestMain.printable_steps(TestPlc.default_steps)
45 steps_message += "\n"+20*'x'+" Other useful steps are\n"+TestMain.printable_steps(TestPlc.other_steps)
46 usage = """usage: %%prog [options] steps
47 myplc-url defaults to the last value used, as stored in arg-myplc-url,
49 build-url defaults to the last value used, as stored in arg-build-url,
51 config defaults to the last value used, as stored in arg-config,
53 ips defaults to the last value used, as stored in arg-ips,
54 default is to use IP scanning
55 steps refer to a method in TestPlc or to a step_* module
57 """%(TestMain.default_build_url,TestMain.default_config)
58 usage += steps_message
59 parser=OptionParser(usage=usage,version=self.subversion_id)
60 parser.add_option("-u","--url",action="store", dest="myplc_url",
61 help="myplc URL - for locating build output")
62 parser.add_option("-b","--build",action="store", dest="build_url",
63 help="Build URL - for using vtest-init-vserver.sh in native mode")
64 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
65 nargs=1,type="string",
66 help="Config module - can be set multiple times, or use quotes")
67 parser.add_option("-x","--exclude",action="callback", callback=TestMain.optparse_list, dest="exclude",
68 nargs=1,type="string",default=[],
69 help="steps to exclude - can be set multiple times, or use quotes")
70 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
71 help="Run all default steps")
72 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
73 help="List known steps")
74 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
75 nargs=1,type="string",
76 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
77 parser.add_option("-s","--small",action="store_true",dest="small_test",default=False,
78 help="run a small test -- typically only one node")
79 parser.add_option("-d","--dbname",action="store",dest="dbname",default=None,
80 help="Used by db_dump and db_restore")
81 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
82 help="Run in verbose mode")
83 parser.add_option("-q","--quiet", action="store_true", dest="quiet", default=False,
84 help="Run in quiet mode")
85 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
86 help="Show environment and exits")
87 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
88 help="Force the NM to restart in check_slices step")
89 (self.options, self.args) = parser.parse_args()
91 # tmp : force small test
92 utils.header("XXX WARNING : forcing small tests")
93 self.options.small_test = True
95 if len(self.args) == 0:
96 if self.options.all_steps:
97 self.options.steps=TestPlc.default_steps
98 elif self.options.dry_run:
99 self.options.steps=TestPlc.default_steps
100 elif self.options.list_steps:
104 print 'No step found (do you mean -a ? )'
105 print "Run %s --help for help"%sys.argv[0]
108 self.options.steps = self.args
110 # handle defaults and option persistence
111 for (recname,filename,default) in (
112 ('build_url','arg-build-url',TestMain.default_build_url) ,
113 ('ips','arg-ips',[]) ,
114 ('config','arg-config',TestMain.default_config) ,
115 ('myplc_url','arg-myplc-url',"") ,
117 # print 'handling',recname
119 is_list = isinstance(default,list)
120 if not getattr(self.options,recname):
122 parsed=file(path).readlines()
123 if not is_list: # strings
125 print "%s - error when parsing %s"%(sys.argv[1],path)
127 parsed=parsed[0].strip()
129 parsed=[x.strip() for x in parsed]
130 setattr(self.options,recname,parsed)
133 setattr(self.options,recname,default)
135 print "Cannot determine",recname
136 print "Run %s --help for help"%sys.argv[0]
138 if not self.options.quiet:
139 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
144 fsave.write(getattr(self.options,recname) + "\n")
146 for value in getattr(self.options,recname):
147 fsave.write(value + "\n")
149 # utils.header('Saved %s into %s'%(recname,filename))
152 if not self.options.steps:
154 #self.options.steps=['dump','clean','install','populate']
155 self.options.steps=TestPlc.default_steps
159 for step in self.options.steps:
161 for exclude in self.options.exclude:
162 if utils.match(step,exclude):
165 if keep: selected.append(step)
166 self.options.steps=selected
168 # this is useful when propagating on host boxes, to avoid conflicts
169 self.options.buildname = os.path.basename (os.path.abspath (self.path))
171 if self.options.verbose:
172 self.show_env(self.options,"Verbose")
176 for config in self.options.config:
177 modulename='config_'+config
179 m = __import__(modulename)
180 all_plc_specs = m.config(all_plc_specs,self.options)
182 traceback.print_exc()
183 print 'Cannot load config %s -- ignored'%modulename
186 if not self.options.quiet:
187 utils.show_test_spec("Test specifications",all_plc_specs)
188 # build a TestPlc object from the result, passing options
189 for spec in all_plc_specs:
190 spec['disabled'] = False
191 all_plcs = [ (x, TestPlc(x,self.options)) for x in all_plc_specs]
193 # pass options to utils as well
194 utils.init_options(self.options)
196 overall_result = True
197 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
199 for step in self.options.steps:
204 if step.find("force_") == 0:
205 step=step.replace("force_","")
207 # try and locate a method in TestPlc
208 if testplc_method_dict.has_key(step):
209 all_step_infos += [ (step, testplc_method_dict[step] , force)]
210 # otherwise search for the 'run' method in the step_<x> module
212 modulename='step_'+step
214 # locate all methods named run* in the module
215 module_dict = __import__(modulename).__dict__
216 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
218 raise Exception,"No run* method in module %s"%modulename
220 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name],force) for name in names ]
222 print '********** step %s NOT FOUND -- ignored'%(step)
223 traceback.print_exc()
224 overall_result = False
226 if self.options.dry_run:
227 self.show_env(self.options,"Dry run")
229 # do all steps on all plcs
230 for (stepname,method,force) in all_step_infos:
231 for (spec,obj) in all_plcs:
235 if not spec['disabled'] or force:
238 if force: force_msg=" (forced)"
239 utils.header("********** RUNNING step %s%s on plc %s"%(stepname,force_msg,plcname))
240 step_result = method(obj)
242 utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
244 overall_result = False
245 spec['disabled'] = True
246 utils.header('********** FAILED Step %s on %s - discarding that plc from further steps'%(stepname,plcname))
249 spec['disabled'] = True
250 traceback.print_exc()
251 utils.header ('********** FAILED (exception) Step %s on plc %s - discarding this plc from further steps'%(stepname,plcname))
253 # do not run, just display it's skipped
255 utils.header("********** IGNORED Plc %s is disabled - skipping step %s"%(plcname,stepname))
257 return overall_result
259 # wrapper to run, returns a shell-compatible result
270 traceback.print_exc()
273 if __name__ == "__main__":
274 sys.exit(TestMain().main())