wintest: moved to top level
[metze/samba/wip.git] / wintest / wintest.py
1 #!/usr/bin/env python
2
3 '''automated testing library for testing Samba against windows'''
4
5 import pexpect, subprocess
6 import sys, os, time, re
7
8 class wintest():
9     '''testing of Samba against windows VMs'''
10
11     def __init__(self):
12         self.vars = {}
13         self.list_mode = False
14         os.putenv('PYTHONUNBUFFERED', '1')
15
16     def setvar(self, varname, value):
17         '''set a substitution variable'''
18         self.vars[varname] = value
19
20     def setwinvars(self, vm, prefix='WIN'):
21         '''setup WIN_XX vars based on a vm name'''
22         for v in ['VM', 'HOSTNAME', 'USER', 'PASS', 'SNAPSHOT', 'BASEDN', 'REALM', 'DOMAIN']:
23             vname = '%s_%s' % (vm, v)
24             if vname in self.vars:
25                 self.setvar("%s_%s" % (prefix,v), self.substitute("${%s}" % vname))
26             else:
27                 self.vars.pop("%s_%s" % (prefix,v), None)
28
29     def info(self, msg):
30         '''print some information'''
31         if not self.list_mode:
32             print(self.substitute(msg))
33
34     def load_config(self, fname):
35         '''load the config file'''
36         f = open(fname)
37         for line in f:
38             line = line.strip()
39             if len(line) == 0 or line[0] == '#':
40                 continue
41             colon = line.find(':')
42             if colon == -1:
43                 raise RuntimeError("Invalid config line '%s'" % line)
44             varname = line[0:colon].strip()
45             value   = line[colon+1:].strip()
46             self.setvar(varname, value)
47
48     def list_steps_mode(self):
49         '''put wintest in step listing mode'''
50         self.list_mode = True
51
52     def set_skip(self, skiplist):
53         '''set a list of tests to skip'''
54         self.skiplist = skiplist.split(',')
55
56     def skip(self, step):
57         '''return True if we should skip a step'''
58         if self.list_mode:
59             print("\t%s" % step)
60             return True
61         return step in self.skiplist
62
63     def substitute(self, text):
64         """Substitute strings of the form ${NAME} in text, replacing
65         with substitutions from vars.
66         """
67         if isinstance(text, list):
68             ret = text[:]
69             for i in range(len(ret)):
70                 ret[i] = self.substitute(ret[i])
71             return ret
72
73         while True:
74             var_start = text.find("${")
75             if var_start == -1:
76                 return text
77             var_end = text.find("}", var_start)
78             if var_end == -1:
79                 return text
80             var_name = text[var_start+2:var_end]
81             if not var_name in self.vars:
82                 raise RuntimeError("Unknown substitution variable ${%s}" % var_name)
83             text = text.replace("${%s}" % var_name, self.vars[var_name])
84         return text
85
86     def have_var(self, varname):
87         '''see if a variable has been set'''
88         return varname in self.vars
89
90
91     def putenv(self, key, value):
92         '''putenv with substitution'''
93         os.putenv(key, self.substitute(value))
94
95     def chdir(self, dir):
96         '''chdir with substitution'''
97         os.chdir(self.substitute(dir))
98
99
100     def run_cmd(self, cmd, dir=".", show=None, output=False, checkfail=True):
101         cmd = self.substitute(cmd)
102         if isinstance(cmd, list):
103             self.info('$ ' + " ".join(cmd))
104         else:
105             self.info('$ ' + cmd)
106         if output:
107             return subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=dir).communicate()[0]
108         if isinstance(cmd, list):
109             shell=False
110         else:
111             shell=True
112         if checkfail:
113             return subprocess.check_call(cmd, shell=shell, cwd=dir)
114         else:
115             return subprocess.call(cmd, shell=shell, cwd=dir)
116
117
118     def cmd_output(self, cmd):
119         '''return output from and command'''
120         cmd = self.substitute(cmd)
121         return self.run_cmd(cmd, output=True)
122
123     def cmd_contains(self, cmd, contains, nomatch=False, ordered=False, regex=False):
124         '''check that command output contains the listed strings'''
125         out = self.cmd_output(cmd)
126         self.info(out)
127         for c in self.substitute(contains):
128             if regex:
129                 m = re.search(c, out)
130                 if m is None:
131                     start = -1
132                     end = -1
133                 else:
134                     start = m.start()
135                     end = m.end()
136             else:
137                 start = out.find(c)
138                 end = start + len(c)
139             if nomatch:
140                 if start != -1:
141                     raise RuntimeError("Expected to not see %s in %s" % (c, cmd))
142             else:
143                 if start == -1:
144                     raise RuntimeError("Expected to see %s in %s" % (c, cmd))
145             if ordered and start != -1:
146                 out = out[end:]
147
148     def retry_cmd(self, cmd, contains, retries=30, delay=2, wait_for_fail=False,
149                   ordered=False, regex=False):
150         '''retry a command a number of times'''
151         while retries > 0:
152             try:
153                 self.cmd_contains(cmd, contains, nomatch=wait_for_fail,
154                                   ordered=ordered, regex=regex)
155                 return
156             except:
157                 time.sleep(delay)
158                 retries = retries - 1
159         raise RuntimeError("Failed to find %s" % contains)
160
161     def pexpect_spawn(self, cmd, timeout=60):
162         '''wrapper around pexpect spawn'''
163         cmd = self.substitute(cmd)
164         self.info("$ " + cmd)
165         ret = pexpect.spawn(cmd, logfile=sys.stdout, timeout=timeout)
166
167         def sendline_sub(line):
168             line = self.substitute(line).replace('\n', '\r\n')
169             return ret.old_sendline(line + '\r')
170
171         def expect_sub(line, timeout=ret.timeout):
172             line = self.substitute(line)
173             return ret.old_expect(line, timeout=timeout)
174
175         ret.old_sendline = ret.sendline
176         ret.sendline = sendline_sub
177         ret.old_expect = ret.expect
178         ret.expect = expect_sub
179
180         return ret
181
182     def vm_poweroff(self, vmname, checkfail=True):
183         '''power off a VM'''
184         self.setvar('VMNAME', vmname)
185         self.run_cmd("${VM_POWEROFF}", checkfail=checkfail)
186
187     def vm_restore(self, vmname, snapshot):
188         '''restore a VM'''
189         self.setvar('VMNAME', vmname)
190         self.setvar('SNAPSHOT', snapshot)
191         self.run_cmd("${VM_RESTORE}")
192
193     def ping_wait(self, hostname):
194         '''wait for a hostname to come up on the network'''
195         hostname = self.substitute(hostname)
196         loops=10
197         while loops > 0:
198             try:
199                 self.run_cmd("ping -c 1 -w 10 %s" % hostname)
200                 break
201             except:
202                 loops = loops - 1
203         if loops == 0:
204             raise RuntimeError("Failed to ping %s" % hostname)
205         self.info("Host %s is up" % hostname)
206
207     def port_wait(self, hostname, port, retries=200, delay=3, wait_for_fail=False):
208         '''wait for a host to come up on the network'''
209         self.retry_cmd("nc -v -z -w 1 %s %u" % (hostname, port), ['succeeded'],
210                        retries=retries, delay=delay, wait_for_fail=wait_for_fail)
211
212     def run_net_time(self, child):
213         '''run net time on windows'''
214         child.sendline("net time \\\\${HOSTNAME} /set")
215         child.expect("Do you want to set the local computer")
216         child.sendline("Y")
217         child.expect("The command completed successfully")
218
219     def run_date_time(self, child, time_tuple=None):
220         '''run date and time on windows'''
221         if time_tuple is None:
222             time_tuple = time.localtime()
223         child.sendline("date")
224         child.expect("Enter the new date:")
225         child.sendline(time.strftime("%m-%d-%y", time_tuple))
226         child.expect("C:")
227         child.sendline("time")
228         child.expect("Enter the new time:")
229         child.sendline(time.strftime("%H:%M:%S", time_tuple))
230         child.expect("C:")
231
232
233     def open_telnet(self, hostname, username, password, retries=60, delay=5, set_time=False):
234         '''open a telnet connection to a windows server, return the pexpect child'''
235         while retries > 0:
236             child = self.pexpect_spawn("telnet " + hostname + " -l '" + username + "'")
237             i = child.expect(["Welcome to Microsoft Telnet Service",
238                               "No more connections are allowed to telnet server",
239                               "Unable to connect to remote host",
240                               "No route to host"])
241             if i != 0:
242                 child.close()
243                 time.sleep(delay)
244                 retries -= 1
245                 continue
246             child.expect("password:")
247             child.sendline(password)
248             child.expect("C:")
249             if set_time:
250                 self.run_date_time(child, None)
251             return child
252         raise RuntimeError("Failed to connect with telnet")
253
254     def kinit(self, username, password):
255         '''use kinit to setup a credentials cache'''
256         self.run_cmd("kdestroy")
257         self.putenv('KRB5CCNAME', "${PREFIX}/ccache.test")
258         username = self.substitute(username)
259         s = username.split('@')
260         if len(s) > 0:
261             s[1] = s[1].upper()
262         username = '@'.join(s)
263         child = self.pexpect_spawn('kinit -V ' + username)
264         child.expect("Password for")
265         child.sendline(password)
266         child.expect("Authenticated to Kerberos")