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_steps = ['uninstall','install','install_rpm',
22 'configure', 'start', SEP,
23 'store_keys', 'initscripts',
24 'sites', 'nodes', 'slices', 'nodegroups', SEP,
25 'init_node','bootcd', 'configure_qemu', SEP,
26 'kill_all_qemus', 'start_nodes', SEP,
28 'nodes_booted', 'nodes_ssh', 'check_slices',
29 'check_initscripts', 'check_tcp',SEP,
30 'force_kill_qemus', 'force_gather_logs' ]
31 other_steps = [ 'stop_all_vservers','fresh_install', 'cache_rpm', 'stop', SEP,
32 'clean_sites', 'clean_nodes', 'clean_slices', 'clean_keys', SEP,
33 'show_boxes', 'list_all_qemus', 'list_qemus', SEP,
34 'db_dump' , 'db_restore',
35 'standby_1 through 20', SEP,
37 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
40 self.path=os.path.dirname(sys.argv[0])
44 def show_env (options, message):
45 utils.header (message)
46 utils.show_options("main options",options)
49 def optparse_list (option, opt, value, parser):
51 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
53 setattr(parser.values,option.dest,value.split())
56 def printable_steps (list):
57 return " ".join(list).replace(" "+SEP+" ","\n")
60 steps_message=20*'x'+" Defaut steps are\n"+TestMain.printable_steps(TestMain.default_steps)
61 steps_message += "\n"+20*'x'+" Other useful steps are\n"+TestMain.printable_steps(TestMain.other_steps)
62 usage = """usage: %%prog [options] steps
63 myplc-url defaults to the last value used, as stored in arg-myplc-url,
65 build-url defaults to the last value used, as stored in arg-build-url,
67 config defaults to the last value used, as stored in arg-config,
69 ips defaults to the last value used, as stored in arg-ips,
70 default is to use IP scanning
71 steps refer to a method in TestPlc or to a step_* module
73 """%(TestMain.default_build_url,TestMain.default_config)
74 usage += steps_message
75 parser=OptionParser(usage=usage,version=self.subversion_id)
76 parser.add_option("-u","--url",action="store", dest="myplc_url",
77 help="myplc URL - for locating build output")
78 parser.add_option("-b","--build",action="store", dest="build_url",
79 help="Build URL - for using vtest-init-vserver.sh in native mode")
80 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
81 nargs=1,type="string",
82 help="Config module - can be set multiple times, or use quotes")
83 parser.add_option("-x","--exclude",action="callback", callback=TestMain.optparse_list, dest="exclude",
84 nargs=1,type="string",default=[],
85 help="steps to exclude - can be set multiple times, or use quotes")
86 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
87 help="Run all default steps")
88 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
89 help="List known steps")
90 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
91 nargs=1,type="string",
92 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
93 parser.add_option("-s","--small",action="store_true",dest="small_test",default=False,
94 help="run a small test -- typically only one node")
95 parser.add_option("-d","--dbname",action="store",dest="dbname",default=None,
96 help="Used by db_dump and db_restore")
97 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
98 help="Run in verbose mode")
99 parser.add_option("-q","--quiet", action="store_true", dest="quiet", default=False,
100 help="Run in quiet mode")
101 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
102 help="Show environment and exits")
103 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
104 help="Force the NM to restart in check_slices step")
105 (self.options, self.args) = parser.parse_args()
107 # tmp : force small test
108 utils.header("XXX WARNING : forcing small tests")
109 self.options.small_test = True
111 if len(self.args) == 0:
112 if self.options.all_steps:
113 self.options.steps=TestMain.default_steps
114 elif self.options.dry_run:
115 self.options.steps=TestMain.default_steps
116 elif self.options.list_steps:
120 print 'No step found (do you mean -a ? )'
121 print "Run %s --help for help"%sys.argv[0]
124 self.options.steps = self.args
126 # handle defaults and option persistence
127 for (recname,filename,default) in (
128 ('build_url','arg-build-url',TestMain.default_build_url) ,
129 ('ips','arg-ips',[]) ,
130 ('config','arg-config',TestMain.default_config) ,
131 ('myplc_url','arg-myplc-url',"") ,
133 # print 'handling',recname
135 is_list = isinstance(default,list)
136 if not getattr(self.options,recname):
138 parsed=file(path).readlines()
139 if not is_list: # strings
141 print "%s - error when parsing %s"%(sys.argv[1],path)
143 parsed=parsed[0].strip()
145 parsed=[x.strip() for x in parsed]
146 setattr(self.options,recname,parsed)
149 setattr(self.options,recname,default)
151 print "Cannot determine",recname
152 print "Run %s --help for help"%sys.argv[0]
154 if not self.options.quiet:
155 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
160 fsave.write(getattr(self.options,recname) + "\n")
162 for value in getattr(self.options,recname):
163 fsave.write(value + "\n")
165 # utils.header('Saved %s into %s'%(recname,filename))
168 if not self.options.steps:
170 #self.options.steps=['dump','clean','install','populate']
171 self.options.steps=TestMain.default_steps
175 for step in self.options.steps:
177 for exclude in self.options.exclude:
178 if utils.match(step,exclude):
181 if keep: selected.append(step)
182 self.options.steps=selected
184 # this is useful when propagating on host boxes, to avoid conflicts
185 self.options.buildname = os.path.basename (os.path.abspath (self.path))
187 if self.options.verbose:
188 self.show_env(self.options,"Verbose")
192 for config in self.options.config:
193 modulename='config_'+config
195 m = __import__(modulename)
196 all_plc_specs = m.config(all_plc_specs,self.options)
198 traceback.print_exc()
199 print 'Cannot load config %s -- ignored'%modulename
202 if not self.options.quiet:
203 utils.show_test_spec("Test specifications",all_plc_specs)
204 # build a TestPlc object from the result, passing options
205 for spec in all_plc_specs:
206 spec['disabled'] = False
207 all_plcs = [ (x, TestPlc(x,self.options)) for x in all_plc_specs]
209 # pass options to utils as well
210 utils.init_options(self.options)
212 overall_result = True
213 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
215 for step in self.options.steps:
218 if step.find("force_") == 0:
219 step=step.replace("force_","")
221 # try and locate a method in TestPlc
222 if testplc_method_dict.has_key(step):
223 all_step_infos += [ (step, testplc_method_dict[step] , force)]
224 # otherwise search for the 'run' method in the step_<x> module
226 modulename='step_'+step
228 # locate all methods named run* in the module
229 module_dict = __import__(modulename).__dict__
230 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
232 raise Exception,"No run* method in module %s"%modulename
234 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name],force) for name in names ]
236 print 'Step %s -- ignored'%(step)
237 traceback.print_exc()
238 overall_result = False
240 if self.options.dry_run:
241 self.show_env(self.options,"Dry run")
243 # do all steps on all plcs
244 for (stepname,method,force) in all_step_infos:
245 for (spec,obj) in all_plcs:
249 if not spec['disabled'] or force:
252 if force: force_msg=" (forced)"
253 utils.header("********** RUNNING step %s%s on plc %s"%(stepname,force_msg,plcname))
254 step_result = method(obj)
256 utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
258 overall_result = False
259 spec['disabled'] = True
260 utils.header('********** FAILED Step %s on %s - discarding that plc from further steps'%(stepname,plcname))
263 spec['disabled'] = True
264 traceback.print_exc()
265 utils.header ('********** FAILED (exception) Step %s on plc %s - discarding this plc from further steps'%(stepname,plcname))
267 # do not run, just display it's skipped
269 utils.header("********** IGNORED Plc %s is disabled - skipping step %s"%(plcname,stepname))
271 return overall_result
273 # wrapper to run, returns a shell-compatible result
284 traceback.print_exc()
287 if __name__ == "__main__":
288 sys.exit(TestMain().main())