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