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 = [ 'onelab' ]
19 default_steps = ['uninstall','install','install_rpm',
21 'clear_ssh_config','store_keys', 'initscripts',
22 'sites', 'nodes', 'slices',
23 'bootcd', 'nodegroups',
24 'kill_all_qemus', 'start_nodes',
25 'standby_4', 'nodes_booted',
26 'standby_6','nodes_ssh', 'check_slices','check_initscripts',
29 other_steps = [ 'fresh_install', 'stop',
30 'clean_sites', 'clean_nodes', 'clean_slices', 'clean_keys',
31 'list_all_qemus', 'kill_qemus', 'stop_nodes' ,
32 'db_dump' , 'db_restore',
33 'standby_1 through 20',
35 default_build_url = "http://svn.planet-lab.org/svn/build/trunk"
38 self.path=os.path.dirname(sys.argv[0])
41 def show_env (options, message):
42 utils.header (message)
43 utils.pprint("main options",options)
46 def optparse_list (option, opt, value, parser):
48 setattr(parser.values,option.dest,getattr(parser.values,option.dest)+value.split())
50 setattr(parser.values,option.dest,value.split())
53 steps_message="Defaut steps are\n\t%s"%(" ".join(TestMain.default_steps))
54 steps_message += "\nOther useful steps are\n\t %s"%(" ".join(TestMain.other_steps))
55 usage = """usage: %%prog [options] steps
56 myplc-url defaults to the last value used, as stored in arg-myplc-url,
58 build-url defaults to the last value used, as stored in arg-build-url,
60 config defaults to the last value used, as stored in arg-config,
62 ips defaults to the last value used, as stored in arg-ips,
63 default is to use IP scanning
64 steps refer to a method in TestPlc or to a step_* module
66 """%(TestMain.default_build_url,TestMain.default_config)
67 usage += steps_message
68 parser=OptionParser(usage=usage,version=self.subversion_id)
69 parser.add_option("-u","--url",action="store", dest="myplc_url",
70 help="myplc URL - for locating build output")
71 parser.add_option("-b","--build",action="store", dest="build_url",
72 help="Build URL - for using vtest-init-vserver.sh in native mode")
73 parser.add_option("-c","--config",action="callback", callback=TestMain.optparse_list, dest="config",
74 nargs=1,type="string",
75 help="Config module - can be set multiple times, or use quotes")
76 parser.add_option("-a","--all",action="store_true",dest="all_steps", default=False,
77 help="Run all default steps")
78 parser.add_option("-l","--list",action="store_true",dest="list_steps", default=False,
79 help="List known steps")
80 parser.add_option("-s","--state",action="store",dest="dbname",default=None,
81 help="Used by db_dump and db_restore")
82 parser.add_option("-d","--display", action="store", dest="display", default='bellami.inria.fr:0.0',
83 help="Set DISPLAY for vmplayer")
84 parser.add_option("-i","--ip",action="callback", callback=TestMain.optparse_list, dest="ips",
85 nargs=1,type="string",
86 help="Specify the set of IP addresses to use in vserver mode (disable scanning)")
87 parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False,
88 help="Run in verbose mode")
89 parser.add_option("-n","--dry-run", action="store_true", dest="dry_run", default=False,
90 help="Show environment and exits")
91 parser.add_option("-f","--forcenm", action="store_true", dest="forcenm", default=False,
92 help="Force the NM to restart in check_slices step")
93 (self.options, self.args) = parser.parse_args()
95 if len(self.args) == 0:
96 if self.options.all_steps:
97 self.options.steps=TestMain.default_steps
98 elif self.options.dry_run:
99 self.options.steps=TestMain.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
118 path="%s/%s"%(self.path,filename)
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 utils.header('* Using %s = %s'%(recname,getattr(self.options,recname)))
143 fsave.write(getattr(self.options,recname) + "\n")
145 for value in getattr(self.options,recname):
146 fsave.write(value + "\n")
148 # utils.header('Saved %s into %s'%(recname,filename))
151 if not self.options.steps:
153 #self.options.steps=['dump','clean','install','populate']
154 self.options.steps=TestMain.default_steps
156 # store self.path in options.path for the various callbacks
157 self.options.path = self.path
158 # this is useful when propagating on host boxes, to avoid conflicts
159 self.options.buildname = os.path.basename (os.path.abspath (self.path))
161 if self.options.verbose:
162 self.show_env(self.options,"Verbose")
166 for config in self.options.config:
167 modulename='config_'+config
169 m = __import__(modulename)
170 all_plc_specs = m.config(all_plc_specs,self.options)
172 traceback.print_exc()
173 print 'Cannot load config %s -- ignored'%modulename
176 utils.show_test_spec("Test specifications",all_plc_specs)
177 # build a TestPlc object from the result
178 for spec in all_plc_specs:
179 spec['disabled'] = False
180 all_plcs = [ (x, TestPlc(x)) for x in all_plc_specs]
181 # expose to the various objects
182 for (spec,obj) in all_plcs:
183 obj.options=self.options
185 overall_result = True
186 testplc_method_dict = __import__("TestPlc").__dict__['TestPlc'].__dict__
188 for step in self.options.steps:
189 # try and locate a method in TestPlc
190 if testplc_method_dict.has_key(step):
191 all_step_infos += [ (step, testplc_method_dict[step] )]
192 # otherwise search for the 'run' method in the step_<x> module
194 modulename='step_'+step
196 # locate all methods named run* in the module
197 module_dict = __import__(modulename).__dict__
198 names = [ key for key in module_dict.keys() if key.find("run")==0 ]
200 raise Exception,"No run* method in module %s"%modulename
202 all_step_infos += [ ("%s.%s"%(step,name),module_dict[name]) for name in names ]
204 print 'Step %s -- ignored'%(step)
205 traceback.print_exc()
206 overall_result = False
208 if self.options.dry_run:
209 self.show_env(self.options,"Dry run")
212 # do all steps on all plcs
213 for (stepname,method) in all_step_infos:
214 for (spec,obj) in all_plcs:
217 utils.header("Plc %s is disabled - skipping step %s"%(plcname,stepname))
220 utils.header("Running step %s on plc %s"%(stepname,plcname))
221 step_result = method(obj,self.options)
223 utils.header('********** SUCCESSFUL step %s on %s'%(stepname,plcname))
225 overall_result = False
226 spec['disabled'] = True
227 utils.header('********** Step %s on %s FAILED - discarding that plc from further steps'%(stepname,plcname))
230 spec['disabled'] = True
231 utils.header ('********** Step %s on plc %s FAILED (exception) - discarding this plc from further steps'%(stepname,plcname))
232 traceback.print_exc()
233 return overall_result
235 # wrapper to run, returns a shell-compatible result
246 traceback.print_exc()
249 if __name__ == "__main__":
250 sys.exit(TestMain().main())