b0fc82e5d23da7e6f9edc84e69a04e10ebebc841
[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 MYPLC-URL"""
25             parser=OptionParser(usage=usage,version=self.subversion_id)
26
27             parser.add_option("-d","--display", action="store", dest="Xdisplay", default='bellami:0.0',
28                               help="sets DISPLAY for vmplayer")
29             parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
30                               help="Run in verbose mode")
31             parser.add_option("-r","--run", action="store", dest="run_node", 
32                               help="Only starts vmplayer for the specified node")
33             parser.add_option("-h","--help", action="store_true", dest="help", default=False,
34                               help="Displays this message")
35             (self.options, self.args) = parser.parse_args()
36             if self.options.help:
37                 parser.print_help()
38                 sys.exit(1)
39
40             display=''
41             url=''
42             test_plcs=[]
43             test_nodes=[]
44             pids=[]
45             #test the existence of the url
46             if (len (self.args) > 2):
47                 parser.print_help()
48                 sys.exit(1)
49             elif (len (self.args) == 1):
50                 url=self.args[0]
51             else:
52                 try:
53                     url_file=open("%s/MYPLC-URL"%self.path)
54                     url=url_file.read().strip()
55                     url_file.close()
56                 except:
57                     print "Cannot determine myplc url"
58                     parser.print_help()
59                     sys.exit(1)
60             utils.header('* Using myplc at url : %s'%url)
61             #check where to display Virtual machines
62             if (self.options.Xdisplay):
63                 display=self.options.Xdisplay
64                 utils.header('X11 display : %s'% display)
65             #the run option 
66             if (self.options.run_node):
67                 file=self.path+'/vmplayer-'+self.options.run_node+'/node.vmx'
68                 if os.path.exists(file):
69                     utils.header('starting vmplayer for node %s'%self.options.run_node)
70                     os.system('DISPLAY=%s vmplayer %s '%(display,file))
71                     sys.exit(0)
72                 else:
73                     utils.header ('File not found %s - exiting'%file)
74                     sys.exit(1)
75             
76             utils.header('Saving current myplc url into MYPLC-URL')
77             fsave=open('%s/MYPLC-URL'%self.path,"w")
78             fsave.write(url)
79             fsave.write('\n')
80             fsave.close()
81
82             pp = pprint.PrettyPrinter(indent=4,depth=2)
83             for plc_spec in TestConfig.plc_specs:
84                 utils.header('Creating plc with spec')
85                 pp.pprint(plc_spec)
86                 test_plc = TestPlc(plc_spec)
87                 test_plc.connect()
88                 test_plcs.append(test_plc)
89                 test_plc.cleanup_plc()
90                 utils.header('Installing myplc from url %s'%url)
91                 test_plc.install_plc(url)
92                 test_plc.config_plc(plc_spec)
93                 ##create all the sites under the new plc,and then populate them with
94                 ##nodes,persons and slices(with initscripts)
95                 for site_spec in plc_spec['sites']:
96                     utils.header('Creating site')
97                     pp.pprint(site_spec)
98                     test_site = test_plc.init_site(site_spec)
99                     for node_spec in site_spec['nodes']:
100                         utils.header('Creating node')
101                         pp.pprint(node_spec)
102                         test_nodes.append(node_spec)
103                         test_node = test_plc.init_node(test_site,node_spec,self.path)
104                 test_node.add_initscripts()
105                 test_node.create_slice ("pi")
106                 utils.header('Starting vmware nodes')
107                 test_site.run_vmware(test_nodes,display)
108                 utils.header('Checking nodes')
109                 if(test_site.node_check_status(test_nodes,True)):
110                     test_plc.db_dump()
111                     test_site.slice_access()
112                     print "System test successful"
113                     return 0
114                 else :
115                     print "System test failed"
116                     sys.exit(1)
117         except Exception, e:
118             print str(e)
119             sys.exit(1)
120             
121 if __name__ == "__main__":
122     TestMain().main()