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
15 subversion_id = "$Id$"
17 default_config = [ 'default' ]
19 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
22 self.path=os.path.dirname(sys.argv[0]) or "."
26 def show_env (options, message):
27 utils.header (message)
28 utils.show_options("main options",options)
31 def optparse_list (option, opt, value, parser):
33 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
35 setattr(parser.values,option.dest,value.split())
38 steps_message=20*'x'+" Defaut steps are\n"+TestPlc.printable_steps(TestPlc.default_steps)
39 steps_message += "\n"+20*'x'+" Other useful steps are\n"+TestPlc.printable_steps(TestPlc.other_steps)
40 usage = """usage: %%prog [options] steps
41 myplc-url defaults to the last value used, as stored in arg-myplc-url,
43 build-url defaults to the last value used, as stored in arg-build-url,
45 config defaults to the last value used, as stored in arg-config,
47 ips defaults to the last value used, as stored in arg-ips,
48 default is to use IP scanning
49 steps refer to a method in TestPlc or to a step_* module
51 """%(TestMain.default_build_url,TestMain.default_config)
52 usage += steps_message
53 parser=OptionParser(usage=usage,version=self.subversion_id)
54 parser.add_option("-u","--url",action="store", dest="myplc_url",
55 help="myplc URL - for locating build output")
56 parser.add_option("-b","--build",action="store", dest="build_url",
57 help="Build URL - for using vtest-init-vserver.sh in native mode")
58 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
59 nargs=1,type="string",
60 help="Config module - can be set multiple times, or use quotes")
61 parser.add_option("-x","--exclude",action="callback", callback=TestMain.optparse_list, dest="exclude",
62 nargs=1,type="string",default=[],
63 help="steps to exclude - can be set multiple times, or use quotes")
64 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
65 help="Run all default steps")
66 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
67 help="List known steps")
68 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
69 nargs=1,type="string",
70 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
71 parser.add_option("-s","--vserver",action="store_true",dest="native",default=False,
72 help="deploy myplc-native rather than former chroot-based package")
73 parser.add_option("-1","--small",action="store_true",dest="small_test",default=False,
74 help="run a small test -- typically only one node")
75 parser.add_option("-d","--dbname",action="store",dest="dbname",default=None,
76 help="Used by db_dump and db_restore")
77 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
78 help="Run in verbose mode")
79 parser.add_option("-q","--quiet", action="store_true", dest="quiet", default=False,
80 help="Run in quiet mode")
81 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
82 help="Show environment and exits")
83 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
84 help="Force the NM to restart in check_slices step")
85 (self.options, self.args) = parser.parse_args()
87 if len(self.args) == 0:
88 if self.options.all_steps:
89 self.options.steps=TestPlc.default_steps
90 elif self.options.dry_run:
91 self.options.steps=TestPlc.default_steps
92 elif self.options.list_steps:
96 print 'No step found (do you mean -a ? )'
97 print "Run %s --help for help"%sys.argv[0]
100 self.options.steps = self.args
102 # handle defaults and option persistence
103 for (recname,filename,default) in (
104 ('build_url','arg-build-url',TestMain.default_build_url) ,
105 ('ips','arg-ips',[]) ,
106 ('config','arg-config',TestMain.default_config) ,
107 ('myplc_url','arg-myplc-url',"") ,
109 # print 'handling',recname
111 is_list = isinstance(default,list)
112 if not getattr(self.options,recname):
114 parsed=file(path).readlines()
115 if not is_list: # strings
117 print "%s - error when parsing %s"%(sys.argv[1],path)
119 parsed=parsed[0].strip()
121 parsed=[x.strip() for x in parsed]
122 setattr(self.options,recname,parsed)
125 setattr(self.options,recname,default)
127 print "Cannot determine",recname
128 print "Run %s --help for help"%sys.argv[0]
130 if not self.options.quiet:
131 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
136 fsave.write(getattr(self.options,recname) + "\n")
138 for value in getattr(self.options,recname):
139 fsave.write(value + "\n")
141 # utils.header('Saved %s into %s'%(recname,filename))
143 self.options.arch = "i386"
144 if self.options.myplc_url.find("x86_64") >= 0:
145 self.options.arch="x86_64"
147 if not self.options.steps:
149 #self.options.steps=['dump','clean','install','populate']
150 self.options.steps=TestPlc.default_steps
154 for step in self.options.steps:
156 for exclude in self.options.exclude:
157 if utils.match(step,exclude):
160 if keep: selected.append(step)
161 self.options.steps=selected
163 # this is useful when propagating on host boxes, to avoid conflicts
164 self.options.buildname = os.path.basename (os.path.abspath (self.path))
166 if self.options.verbose:
167 self.show_env(self.options,"Verbose")
171 for config in self.options.config:
172 modulename='config_'+config
174 m = __import__(modulename)
175 all_plc_specs = m.config(all_plc_specs,self.options)
177 traceback.print_exc()
178 print 'Cannot load config %s -- ignored'%modulename
181 if not self.options.quiet:
182 utils.show_test_spec("Test specifications",all_plc_specs)
183 # build a TestPlc object from the result, passing options
184 for spec in all_plc_specs:
185 spec['disabled'] = False
186 all_plcs = [ (x, TestPlc(x,self.options)) for x in all_plc_specs]
188 # pass options to utils as well
189 utils.init_options(self.options)
191 overall_result = True
192 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
194 for step in self.options.steps:
195 if not TestPlc.valid_step(step):
199 if step.find("force_") == 0:
200 step=step.replace("force_","")
202 # try and locate a method in TestPlc
203 if testplc_method_dict.has_key(step):
204 all_step_infos += [ (step, testplc_method_dict[step] , force)]
205 # otherwise search for the 'run' method in the step_<x> module
207 modulename='step_'+step
209 # locate all methods named run* in the module
210 module_dict = __import__(modulename).__dict__
211 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
213 raise Exception,"No run* method in module %s"%modulename
215 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name],force) for name in names ]
217 print '********** step %s NOT FOUND -- ignored'%(step)
218 traceback.print_exc()
219 overall_result = False
221 if self.options.dry_run:
222 self.show_env(self.options,"Dry run")
224 # do all steps on all plcs
225 for (stepname,method,force) in all_step_infos:
226 for (spec,obj) in all_plcs:
230 if not spec['disabled'] or force:
233 if force: force_msg=" (forced)"
234 utils.header("********** RUNNING step %s%s on plc %s"%(stepname,force_msg,plcname))
235 step_result = method(obj)
237 utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
239 overall_result = False
240 spec['disabled'] = True
241 utils.header('********** FAILED Step %s on %s - discarding that plc from further steps'%(stepname,plcname))
244 spec['disabled'] = True
245 traceback.print_exc()
246 utils.header ('********** FAILED (exception) Step %s on plc %s - discarding this plc from further steps'%(stepname,plcname))
248 # do not run, just display it's skipped
250 utils.header("********** IGNORED Plc %s is disabled - skipping step %s"%(plcname,stepname))
252 return overall_result
254 # wrapper to run, returns a shell-compatible result
265 traceback.print_exc()
268 if __name__ == "__main__":
269 sys.exit(TestMain().main())