cleanup - renamings - ongoing
[tests.git] / system / TestMain.py
1 #!/usr/bin/env python
2 # $Id$
3
4 import os, sys
5 from optparse import OptionParser
6 import pprint
7
8 import utils
9 from TestPlc import TestPlc
10 from TestSite import TestSite
11 from TestNode import TestNode
12 import TestConfig
13
14 class TestMain:
15
16     subversion_id = "$Id$"
17
18     def __init__ (self):
19         self.path=os.path.dirname(sys.argv[0])
20
21     def main (self):
22         try:
23             usage = """usage: %prog [options] [myplc-url]
24 myplc-url defaults to the last value used, as stored in URL"""
25             parser=OptionParser(usage=usage,version=self.subversion_id)
26             # verbosity
27             parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
28                               help="Run in verbose mode")
29             # debug mode
30             parser.add_option("-g","--debug", action="store", dest="debug", 
31                               help="Run in debug mode for eventual virtual problems")
32             #exporting Display
33             parser.add_option("-d","--display", action="store", dest="Xterm", default='bellami:0.0',
34                               help="sets DISPLAY for vmplayer")
35         
36             (self.options, self.args) = parser.parse_args()
37
38             display=''
39             url=''
40             test_plcs=[]
41             test_nodes=[]
42             pids=[]
43             #test the existence of the URL
44             if (len (self.args) > 2):
45                 parser.print_help()
46                 sys.exit(1)
47             elif (len (self.args) == 1):
48                 url=self.args[0]
49             else:
50                 try:
51                     url_file=open("%s/URL"%self.path)
52                     url=url_file.read().strip()
53                     url_file.close()
54                 except:
55                     print "Cannot determine myplc url"
56                     parser.print_help()
57                     sys.exit(1)
58             utils.header('* Using myplc at url : %s'%url)
59             #check where to display Virtual machines
60             if (self.options.Xterm):
61                 display=self.options.Xterm
62                 utils.header('X11 display : %s'% display)
63             #the debug option 
64             if (self.options.debug):
65                 file=self.path+'/'+self.options.debug+'/node.vmx'
66                 if os.path.exists(file):
67                     print 'vmx file is',file
68                     arg='< /dev/null &>/dev/null &'
69                     os.system('DISPLAY=%s vmplayer %s %s '%(display,file,arg))
70                     sys.exit(0)
71                 else:
72                     print "no way to find the virtual file"
73                     sys.exit(1)
74             
75             utils.header('Saving current myplc url into URL')
76             fsave=open('%s/URL'%self.path,"w")
77             fsave.write(url)
78             fsave.write('\n')
79             fsave.close()
80
81             pp = pprint.PrettyPrinter(indent=4,depth=2)
82             for plc_spec in TestConfig.plc_specs:
83                 utils.header('Creating plc with spec')
84                 pp.pprint(plc_spec)
85                 test_plc = TestPlc(plc_spec)
86                 test_plc.connect()
87                 test_plcs.append(test_plc)
88                 test_plc.cleanup_plc()
89                 utils.header('Installing myplc from url %s'%url)
90                 test_plc.install_plc(url)
91                 test_plc.config_plc(plc_spec)
92                 ##create all the sites under the new plc,and then populate them with
93                 ##nodes,persons and slices
94                 for site_spec in plc_spec['sites']:
95                     utils.header('Creating site')
96                     pp.pprint(site_spec)
97                     test_site = test_plc.init_site(site_spec)
98                     for node_spec in site_spec['nodes']:
99                         utils.header('Creating node')
100                         pp.pprint(node_spec)
101                         test_nodes.append(node_spec)
102                         test_node = test_plc.init_node(test_site,node_spec,self.path)
103                 test_node.create_slice ("pi")
104                 utils.header('Starting vmware nodes')
105                 test_site.run_vmware(test_nodes,display)
106                 utils.header('Checking nodes')
107                 if(test_site.node_check_status(test_nodes,True)):
108                     test_plc.db_dump()
109                     test_site.slice_access()
110                     print "System test successful"
111                     return 0
112                 else :
113                     print "System test failed"
114                     sys.exit(1)
115         except Exception, e:
116             print str(e)
117             sys.exit(1)
118             
119 if __name__ == "__main__":
120     TestMain().main()