e6f57841e888a1dc4666f3a13e98d12350a7f049
[tests.git] / system / TestNode.py
1 import sys, os, os.path, time, base64
2 import xmlrpclib
3
4 import utils
5 from TestUser import TestUser
6 from TestBox import TestBox
7 from TestSsh import TestSsh
8
9 class TestNode:
10
11     def __init__ (self,test_plc,test_site,node_spec):
12         self.test_plc=test_plc
13         self.test_site=test_site
14         self.node_spec=node_spec
15         
16     def name(self):
17         return self.node_spec['node_fields']['hostname']
18     
19     @staticmethod
20     def is_qemu_model (model):
21         return model.find("qemu") >= 0
22     def is_qemu (self):
23         return TestNode.is_qemu_model(self.node_spec['node_fields']['model'])
24
25     @staticmethod
26     def is_real_model (model):
27         return not TestNode.is_qemu_model(model)
28     def is_real (self):
29         return TestNode.is_real_model (self.node_spec['node_fields']['model'])
30
31     def buildname(self):
32         return self.test_plc.options.buildname
33         
34     def nodedir (self):
35         if self.is_qemu():
36             return "qemu-%s"%self.name()
37         else:
38             return "real-%s"%self.name()
39
40     # this returns a hostname
41     def host_box (self):
42         if self.is_real ():
43             return 'localhost'
44         else:
45             try:
46                 return self.node_spec['host_box']
47             except:
48                 utils.header("WARNING : qemu nodes need a host box")
49                 return 'localhost'
50
51     # this returns a TestBox instance - cached in .test_box_value
52     def test_box (self):
53         try:
54             return self.test_box_value
55         except:
56             self.test_box_value = TestBox (self.host_box(),self.buildname())
57             return self.test_box_value
58
59     def create_node (self):
60         ownername = self.node_spec['owner']
61         user_spec = self.test_site.locate_user(ownername)
62         test_user = TestUser(self.test_plc,self.test_site,user_spec)
63         userauth = test_user.auth()
64         utils.header("node %s created by user %s"%(self.name(),test_user.name()))
65         rootauth=self.test_plc.auth_root()
66         server = self.test_plc.apiserver
67         server.AddNode(userauth,
68                        self.test_site.site_spec['site_fields']['login_base'],
69                        self.node_spec['node_fields'])
70         server.SetNodePlainBootstrapfs(userauth,
71                                        self.node_spec['node_fields']['hostname'],
72                                        'YES')
73         # create as reinstall to avoid user confirmation
74         server.UpdateNode(userauth, self.name(), {'boot_state':'reinstall'})
75         # populate network interfaces - primary
76         server.AddInterface(userauth,self.name(),
77                                             self.node_spec['interface_fields'])
78         # populate network interfaces - others
79         if self.node_spec.has_key('extra_interfaces'):
80             for interface in self.node_spec['extra_interfaces']:
81                 server.AddInterface(userauth,self.name(),
82                                                     interface['interface_fields'])
83                 if interface.has_key('settings'):
84                     for (attribute,value) in interface['settings'].iteritems():
85                         # locate node network
86                         nn = server.GetInterfaces(userauth,{'ip':interface['interface_fields']['ip']})[0]
87                         nnid=nn['interface_id']
88                         # locate or create node network attribute type
89                         try:
90                             nnst = server.GetTagTypes(userauth,{'name':attribute})[0]
91                         except:
92                             nnst = server.AddTagType(rootauth,{'category':'test',
93                                                                'tagname':attribute})
94                         # attach value
95                         server.AddInterfaceSetting(userauth,nnid,attribute,value)
96
97     def delete_node (self):
98         # uses the right auth as far as poss.
99         try:
100             ownername = self.node_spec['owner']
101             user_spec = self.test_site.locate_user(ownername)
102             test_user = TestUser(self.test_plc,self.test_site,user_spec)
103             auth = test_user.auth()
104         except:
105             auth=self.test_plc.auth_root()
106         self.test_plc.apiserver.DeleteNode(auth,self.name())
107
108     # Do most of the stuff locally - will be pushed on host_box - *not* the plc - later if needed
109     def init_node(self):
110         utils.system("rm -rf %s"%self.nodedir())
111         utils.system("mkdir %s"%self.nodedir())
112         if not self.is_qemu():
113             return True
114         return utils.system("rsync -v -a --exclude .svn template-qemu/ %s/"%self.nodedir())==0
115
116     def bootcd(self):
117         utils.header("Calling GetBootMedium for %s"%self.name())
118         options = []
119         if self.is_qemu():
120             options.append('serial')
121             options.append('no-hangcheck')
122         encoded=self.test_plc.apiserver.GetBootMedium(self.test_plc.auth_root(), 
123                                                       self.name(), 'node-iso', '', options)
124         if (encoded == ''):
125             raise Exception, 'GetBootmedium failed'
126
127         filename="%s/%s.iso"%(self.nodedir(),self.name())
128         utils.header('Storing boot medium into %s'%filename)
129         if self.test_plc.options.dry_run:
130             print "Dry_run: skipped writing of iso image"
131             return True
132         else:
133             file(filename,'w').write(base64.b64decode(encoded))
134             return True
135
136     def reinstall_node (self):
137         self.test_plc.apiserver.UpdateNode(self.test_plc.auth_root(),
138                                            self.name(),{'boot_state':'reinstall'})
139         return True
140     
141     def configure_qemu(self):
142         if not self.is_qemu():
143             return
144         mac=self.node_spec['interface_fields']['mac']
145         hostname=self.node_spec['node_fields']['hostname']
146         ip=self.node_spec['interface_fields']['ip']
147         auth=self.test_plc.auth_root()
148         target_arch=self.test_plc.apiserver.GetPlcRelease(auth)['build']['target-arch']
149         conf_filename="%s/qemu.conf"%(self.nodedir())
150         if self.test_plc.options.dry_run:
151             print "dry_run: skipped actual storage of qemu.conf"
152             return True
153         utils.header('Storing qemu config for %s in %s'%(self.name(),conf_filename))
154         file=open(conf_filename,'w')
155         file.write('MACADDR=%s\n'%mac)
156         file.write('NODE_ISO=%s.iso\n'%self.name())
157         file.write('HOSTNAME=%s\n'%hostname)
158         file.write('IP=%s\n'%ip)
159         file.write('TARGET_ARCH=%s\n'%target_arch)
160         file.close()
161         return True
162
163     def export_qemu (self):
164         # if relevant, push the qemu area onto the host box
165         if self.test_box().is_local():
166             return True
167         utils.header ("Cleaning any former sequel of %s on %s"%(self.name(),self.host_box()))
168         self.test_box().run_in_buildname("rm -rf %s"%self.nodedir())
169         utils.header ("Transferring configuration files for node %s onto %s"%(self.name(),self.host_box()))
170         return self.test_box().copy(self.nodedir(),recursive=True)==0
171             
172     def start_node (self):
173         model=self.node_spec['node_fields']['model']
174         #starting the Qemu nodes before 
175         if self.is_qemu():
176             self.start_qemu()
177         else:
178             utils.header("TestNode.start_node : %s model %s taken as real node"%(self.name(),model))
179         return True
180
181     def start_qemu (self):
182         options = self.test_plc.options
183         test_box = self.test_box()
184         utils.header("Starting qemu node %s on %s"%(self.name(),test_box.hostname()))
185
186         test_box.run_in_buildname("%s/qemu-bridge-init start >> %s/log.txt"%(self.nodedir(),self.nodedir()))
187         # kick it off in background, as it would otherwise hang
188         test_box.run_in_buildname("%s/qemu-start-node 2>&1 >> %s/log.txt"%(self.nodedir(),self.nodedir()))
189
190     def list_qemu (self):
191         utils.header("Listing qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
192         command="%s/qemu-kill-node -l %s"%(self.nodedir(),self.name())
193         self.test_box().run_in_buildname(command)
194         return True
195
196     def kill_qemu (self):
197         #Prepare the log file before killing the nodes
198         test_box = self.test_box()
199         # kill the right processes 
200         utils.header("Stopping qemu for host %s on box %s"%(self.name(),self.test_box().hostname()))
201         command="%s/qemu-kill-node %s"%(self.nodedir(),self.name())
202         self.test_box().run_in_buildname(command)
203         return True
204
205     def gather_qemu_logs (self):
206         if not self.is_qemu():
207             return True
208         remote_log="%s/log.txt"%self.nodedir()
209         local_log="logs/node.qemu.%s.log"%self.name()
210         self.test_box().test_ssh.fetch(remote_log,local_log)
211
212     def clear_known_hosts (self):
213         TestSsh(self.name()).clear_known_hosts()
214         return True
215
216     def create_test_ssh(self):
217         # get the plc's keys for entering the node
218         vservername=self.test_plc.vservername
219         # assuming we've run testplc.fetch_keys()
220         key = "keys/%(vservername)s.rsa"%locals()
221         return TestSsh(self.name(), buildname=self.buildname(), key=key)
222
223     def check_sanity (self):
224         extensions = [ 'py','pl','sh' ]
225         path='tests/qaapi/qa/tests/node/'
226         scripts=utils.locate_sanity_scripts ('node '+self.name(), path,extensions)
227         overall = True
228         for script in scripts:
229             if not self.check_sanity_script (script):
230                 overall = False
231         return overall
232
233     def check_sanity_script (self,local_script):
234         # push the script on the node's root context
235         ssh_handle=self.create_test_ssh()
236         ssh_handle.copy_home(local_script)
237         script_name=os.path.basename(local_script)
238         if ssh_handle.run("./"+script_name) != 0:
239             print "WARNING: sanity check script %s FAILED"%script_name
240             # xxx - temporary : always return true for now
241             #return False
242         return True
243