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