Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / trunk / plctest / TestPlc.py
1 import os
2 import sys
3 import xmlrpclib
4 import datetime
5 from TestSite import TestSite
6 from TestNode import TestNode
7
8 class TestPlc:
9
10     def __init__ (self,plc_spec):
11         self.plc_spec=plc_spec
12         self.url="https://%s:443/PLCAPI/"%plc_spec['hostname']
13         self.server=xmlrpclib.Server(self.url,allow_none=True)
14         self.path=os.path.dirname(sys.argv[0])
15         
16     def connect (self):
17         # tricky : define les methodes de l'API sur cet object
18         pass
19     
20     def auth_root (self):
21         return {'Username':self.plc_spec['PLC_ROOT_USER'],
22                 'AuthMethod':'password',
23                 'AuthString':self.plc_spec['PLC_ROOT_PASSWORD'],
24                 'Role' : self.plc_spec['role']
25                 }
26     def affiche_results(self, test_case_name, status, timers):
27         timers=datetime.datetime.now()
28         fileHandle = open (self.path+'/results.txt', 'a' )
29         fileHandle.write ( str(test_case_name)+'                    ' +str(status)+'                    '+str(timers))
30         fileHandle.close()
31
32         
33
34     def config_plc(self,plc_spec):
35 # Thierry 2007-07-05 
36 # now plc-config-tty silently creates needed directories
37 #        os.system('mkdir -p /etc/planetlab/configs')
38
39         fileconf=open('tty_conf','w')
40         for var in [ 'PLC_NAME',
41                      'PLC_ROOT_PASSWORD',
42                      'PLC_ROOT_USER',
43                      'PLC_MAIL_ENABLED',
44                      'PLC_MAIL_SUPPORT_ADDRESS',
45                      'PLC_DB_HOST',
46                      'PLC_API_HOST',
47                      'PLC_WWW_HOST',
48                      'PLC_BOOT_HOST',
49                      'PLC_NET_DNS1',
50                      'PLC_NET_DNS2']:
51             fileconf.write ('e %s\n%s\n'%(var,plc_spec[var]))
52         fileconf.write('w\n')
53         fileconf.write('q\n')
54         fileconf.close()
55         os.system('set -x ; cat tty_conf')
56         os.system('set -x ; chroot /plc/root  plc-config-tty < tty_conf')
57         os.system('set -x ; service plc start')
58         os.system('set -x; service sendmail stop')
59         os.system('set -x; chroot /plc/root service sendmail restart')
60         
61     def cleanup_plc(self):
62         os.system('service plc safestop')
63         #####detecting the last myplc version installed and remove it
64         os.system('set -x; rpm -e myplc')
65         print "=======================>Remove Myplc DONE!"
66         ##### Clean up the /plc directory
67         os.system('set -x; rm -rf  /plc/data')
68         print "=======================>Clean up  DONE!"
69         
70     def install_plc(self,url):
71         print url
72         os.system('set -x; rpm -ivh '+url)
73         os.system('set -x; service plc mount')
74       
75     def init_site (self,site_spec):
76         test_site = TestSite (self,site_spec)
77         test_site.create_site()
78         for key in site_spec['users']:
79             test_site.create_user(key)
80             test_site.enable_user(key)
81             test_site.add_key_user(key)            
82         return test_site
83
84     def init_node (self,test_site,node_spec,path):
85
86         test_node = TestNode(self, test_site, node_spec)
87         test_node.create_node ("pi")
88         test_node.create_node ("tech")
89         test_node.create_boot_cd(node_spec,path)
90         return test_node
91     
92     def db_dump(self):
93         
94         t=datetime.datetime.now()
95         d=t.date()
96         dump='/var/lib/pgsql/backups/planetlab4-'+str(d)+'-2nodes'
97         os.system('chroot /plc/root pg_dump -U pgsqluser planetlab4 -f '+ dump)
98         print 'dump is done',dump
99         
100
101