38c0f5f7f8f8070935b959c237056b84be800d3f
[plcapi.git] / plctest / TestMain.py
1 #!/usr/bin/env python
2
3 import os, sys, time
4 from optparse import OptionParser
5 from TestPlc import TestPlc
6 from TestSite import TestSite
7 from TestNode import TestNode
8 import TestConfig
9 import threading
10
11 class TestMain:
12
13     subversion_id = "$Id: TestMain.py 767 2007-08-06 08:32:04Z amine $"
14
15     def __init__ (self):
16         self.path=os.path.dirname(sys.argv[0])
17
18     def main (self):
19         try:
20             usage = """usage: %prog [options] MyplcURL"""
21             parser=OptionParser(usage=usage,version=self.subversion_id)
22             # verbosity
23             parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
24                               help="Run in verbose mode")
25             # debug mode
26             parser.add_option("-g","--debug", action="store", dest="debug", 
27                               help="Run in debug mode for eventual virtual problems")
28             #exporting Display
29             parser.add_option("-d","--display", action="store", dest="Xterm", default='bellami:0.0',
30                               help="export the display on the mentionneted one")
31         
32             (self.options, self.args) = parser.parse_args()
33
34             display=''
35             url=''
36             test_plcs=[]
37             test_nodes=[]
38             pids=[]
39             timset=time.strftime("%H:%M:%S", time.localtime())
40             #test the existence of the URL
41             if (len (self.args)):
42                 url=self.args[0]
43                 print 'the myplc url is ',url
44             else:
45                 print "PLease introduce a right URL for the myplc instal"
46                 sys.exit(1)
47             #check where to display Virtual machines
48             if (self.options.Xterm):
49                 display=self.options.Xterm
50                 print 'the display is', display
51             #the debug option 
52             if (self.options.debug):
53                 file=self.path+'/'+self.options.debug+'/My_Virtual_Machine.vmx'
54                 if os.path.exists(file):
55                     print 'vmx file is',file
56                     arg='< /dev/null &>/dev/null &'
57                     os.system('DISPLAY=%s vmplayer %s %s '%(display,file,arg))
58                     sys.exit(0)
59                 else:
60                     print "no way to find the virtual file"
61                     sys.exit(1)
62             
63             for plc_spec in TestConfig.plc_specs:
64                 print '========>Creating plc at '+timset+':',plc_spec
65                 test_plc = TestPlc(plc_spec)
66                 test_plc.connect()
67                 test_plcs.append(test_plc)
68                 test_plc.cleanup_plc()
69                 print '========>Installing myplc at: ', timset
70                 if (len(sys.argv) > 1):
71                     test_plc.install_plc(url)
72                     test_plc.config_plc(plc_spec)
73                 else :
74                     print "========>PLease insert a valid url for the myplc install"
75                 ##create all the sites under the new plc,and then populate them with
76                 ##nodes,persons and slices
77                 for site_spec in plc_spec['sites']:
78                     print '========>Creating site at '+timset+ ':',site_spec
79                     test_site = test_plc.init_site(site_spec)
80                     for node_spec in site_spec['nodes']:
81                         print '========>Creating node at  '+ timset+' :',node_spec
82                         test_nodes.append(node_spec)
83                         test_node = test_plc.init_node(test_site,node_spec,self.path)
84                 test_node.create_slice ("pi")
85                 print 'Runing Checkers and Vmwares for Site nodes at :',timset
86                 test_site.run_vmware(test_nodes,display)
87                 if(test_site.node_check_status(test_nodes,True)):
88                     test_plc.db_dump()
89                     test_site.slice_access(test_nodes)
90                     print "all is alright"
91                     return 0
92                 else :
93                     print "There is something wrong"
94                     sys.exit(1)
95         except Exception, e:
96             print str(e)
97             sys.exit(1)
98             
99 if __name__ == "__main__":
100     TestMain().main()