3 '''automated testing library for testing Samba against windows'''
5 import pexpect, subprocess
6 import sys, os, time, re
9 '''testing of Samba against windows VMs'''
13 self.list_mode = False
14 os.putenv('PYTHONUNBUFFERED', '1')
16 def setvar(self, varname, value):
17 '''set a substitution variable'''
18 self.vars[varname] = value
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))
27 self.vars.pop("%s_%s" % (prefix,v), None)
30 '''print some information'''
31 if not self.list_mode:
32 print(self.substitute(msg))
34 def load_config(self, fname):
35 '''load the config file'''
39 if len(line) == 0 or line[0] == '#':
41 colon = line.find(':')
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)
48 def list_steps_mode(self):
49 '''put wintest in step listing mode'''
52 def set_skip(self, skiplist):
53 '''set a list of tests to skip'''
54 self.skiplist = skiplist.split(',')
57 '''return True if we should skip a step'''
61 return step in self.skiplist
63 def substitute(self, text):
64 """Substitute strings of the form ${NAME} in text, replacing
65 with substitutions from vars.
67 if isinstance(text, list):
69 for i in range(len(ret)):
70 ret[i] = self.substitute(ret[i])
73 """We may have objects such as pexpect.EOF that are not strings"""
74 if not isinstance(text, str):
77 var_start = text.find("${")
80 var_end = text.find("}", var_start)
83 var_name = text[var_start+2:var_end]
84 if not var_name in self.vars:
85 raise RuntimeError("Unknown substitution variable ${%s}" % var_name)
86 text = text.replace("${%s}" % var_name, self.vars[var_name])
89 def have_var(self, varname):
90 '''see if a variable has been set'''
91 return varname in self.vars
94 def putenv(self, key, value):
95 '''putenv with substitution'''
96 os.putenv(key, self.substitute(value))
99 '''chdir with substitution'''
100 os.chdir(self.substitute(dir))
102 def del_files(self, dirs):
103 '''delete all files in the given directory'''
105 self.run_cmd("find %s -type f | xargs rm -f" % d)
107 def write_file(self, filename, text, mode='w'):
108 '''write to a file'''
109 f = open(self.substitute(filename), mode=mode)
110 f.write(self.substitute(text))
113 def run_cmd(self, cmd, dir=".", show=None, output=False, checkfail=True):
114 cmd = self.substitute(cmd)
115 if isinstance(cmd, list):
116 self.info('$ ' + " ".join(cmd))
118 self.info('$ ' + cmd)
120 return subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=dir).communicate()[0]
121 if isinstance(cmd, list):
126 return subprocess.check_call(cmd, shell=shell, cwd=dir)
128 return subprocess.call(cmd, shell=shell, cwd=dir)
131 def cmd_output(self, cmd):
132 '''return output from and command'''
133 cmd = self.substitute(cmd)
134 return self.run_cmd(cmd, output=True)
136 def cmd_contains(self, cmd, contains, nomatch=False, ordered=False, regex=False,
138 '''check that command output contains the listed strings'''
140 if isinstance(contains, str):
141 contains = [contains]
143 out = self.cmd_output(cmd)
145 for c in self.substitute(contains):
147 m = re.search(c, out)
155 start = out.upper().find(c.upper())
162 raise RuntimeError("Expected to not see %s in %s" % (c, cmd))
165 raise RuntimeError("Expected to see %s in %s" % (c, cmd))
166 if ordered and start != -1:
169 def retry_cmd(self, cmd, contains, retries=30, delay=2, wait_for_fail=False,
170 ordered=False, regex=False, casefold=False):
171 '''retry a command a number of times'''
174 self.cmd_contains(cmd, contains, nomatch=wait_for_fail,
175 ordered=ordered, regex=regex, casefold=casefold)
179 retries = retries - 1
180 raise RuntimeError("Failed to find %s" % contains)
182 def pexpect_spawn(self, cmd, timeout=60):
183 '''wrapper around pexpect spawn'''
184 cmd = self.substitute(cmd)
185 self.info("$ " + cmd)
186 ret = pexpect.spawn(cmd, logfile=sys.stdout, timeout=timeout)
188 def sendline_sub(line):
189 line = self.substitute(line).replace('\n', '\r\n')
190 return ret.old_sendline(line + '\r')
192 def expect_sub(line, timeout=ret.timeout):
193 line = self.substitute(line)
194 return ret.old_expect(line, timeout=timeout)
196 ret.old_sendline = ret.sendline
197 ret.sendline = sendline_sub
198 ret.old_expect = ret.expect
199 ret.expect = expect_sub
203 def vm_poweroff(self, vmname, checkfail=True):
205 self.setvar('VMNAME', vmname)
206 self.run_cmd("${VM_POWEROFF}", checkfail=checkfail)
208 def vm_restore(self, vmname, snapshot):
210 self.setvar('VMNAME', vmname)
211 self.setvar('SNAPSHOT', snapshot)
212 self.run_cmd("${VM_RESTORE}")
214 def ping_wait(self, hostname):
215 '''wait for a hostname to come up on the network'''
216 hostname = self.substitute(hostname)
220 self.run_cmd("ping -c 1 -w 10 %s" % hostname)
225 raise RuntimeError("Failed to ping %s" % hostname)
226 self.info("Host %s is up" % hostname)
228 def port_wait(self, hostname, port, retries=200, delay=3, wait_for_fail=False):
229 '''wait for a host to come up on the network'''
230 self.retry_cmd("nc -v -z -w 1 %s %u" % (hostname, port), ['succeeded'],
231 retries=retries, delay=delay, wait_for_fail=wait_for_fail)
233 def run_net_time(self, child):
234 '''run net time on windows'''
235 child.sendline("net time \\\\${HOSTNAME} /set")
236 child.expect("Do you want to set the local computer")
238 child.expect("The command completed successfully")
240 def run_date_time(self, child, time_tuple=None):
241 '''run date and time on windows'''
242 if time_tuple is None:
243 time_tuple = time.localtime()
244 child.sendline("date")
245 child.expect("Enter the new date:")
246 i = child.expect(["dd-mm-yy", "mm-dd-yy"])
248 child.sendline(time.strftime("%d-%m-%y", time_tuple))
250 child.sendline(time.strftime("%m-%d-%y", time_tuple))
252 child.sendline("time")
253 child.expect("Enter the new time:")
254 child.sendline(time.strftime("%H:%M:%S", time_tuple))
257 def get_ipconfig(self, child):
258 '''get the IP configuration of the child'''
259 child.sendline("ipconfig /all")
260 child.expect('Ethernet adapter ')
261 child.expect("[\w\s]+")
262 self.setvar("WIN_NIC", child.after)
263 child.expect(['DHCP Enabled', 'Dhcp Enabled'])
264 i = child.expect(['Yes', 'No'])
266 self.setvar("WIN_DHCP", True)
268 self.setvar("WIN_DHCP", False)
269 child.expect(['IPv4 Address', 'IP Address'])
270 child.expect('\d+.\d+.\d+.\d+')
271 self.setvar('WIN_IPV4_ADDRESS', child.after)
272 child.expect('Subnet Mask')
273 child.expect('\d+.\d+.\d+.\d+')
274 self.setvar('WIN_SUBNET_MASK', child.after)
275 child.expect('Default Gateway')
276 child.expect('\d+.\d+.\d+.\d+')
277 self.setvar('WIN_DEFAULT_GATEWAY', child.after)
280 def run_tlntadmn(self, child):
281 '''remove the annoying telnet restrictions'''
282 child.sendline('tlntadmn config maxconn=1024')
283 child.expect("The settings were successfully updated")
286 def disable_firewall(self, child):
287 '''remove the annoying firewall'''
288 child.sendline('netsh advfirewall set allprofiles state off')
289 i = child.expect(["Ok", "The following command was not found: advfirewall set allprofiles state off"])
292 child.sendline('netsh firewall set opmode mode = DISABLE profile = ALL')
296 def set_dns(self, child):
297 child.sendline('netsh interface ip set dns "${WIN_NIC}" static ${DNSSERVER} primary')
298 i = child.expect(['C:', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
304 def set_ip(self, child):
305 '''fix the IP address to the same value it had when we
306 connected, but don't use DHCP, and force the DNS server to our
307 DNS server. This allows DNS updates to run'''
308 self.get_ipconfig(child)
309 if self.vars['WIN_DHCP'] is False:
311 child.sendline('netsh')
312 child.expect('netsh>')
313 child.sendline('offline')
314 child.expect('netsh>')
315 child.sendline('routing ip add persistentroute dest=0.0.0.0 mask=0.0.0.0 name="${WIN_NIC}" nhop=${WIN_DEFAULT_GATEWAY}')
316 child.expect('netsh>')
317 child.sendline('interface ip set address "${WIN_NIC}" static ${WIN_IPV4_ADDRESS} ${WIN_SUBNET_MASK} ${WIN_DEFAULT_GATEWAY} 1 store=persistent')
318 i = child.expect(['The syntax supplied for this command is not valid. Check help for the correct syntax', 'netsh>', pexpect.EOF, pexpect.TIMEOUT], timeout=5)
320 child.sendline('interface ip set address "${WIN_NIC}" static ${WIN_IPV4_ADDRESS} ${WIN_SUBNET_MASK} ${WIN_DEFAULT_GATEWAY} 1')
321 child.expect('netsh>')
322 child.sendline('commit')
323 child.sendline('online')
324 child.sendline('exit')
326 child.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=5)
329 def open_telnet(self, hostname, username, password, retries=60, delay=5, set_time=False, set_ip=False, disable_firewall=True, run_tlntadmn=True):
330 '''open a telnet connection to a windows server, return the pexpect child'''
334 child = self.pexpect_spawn("telnet " + hostname + " -l '" + username + "'")
335 i = child.expect(["Welcome to Microsoft Telnet Service",
336 "Denying new connections due to the limit on number of connections",
337 "No more connections are allowed to telnet server",
338 "Unable to connect to remote host",
340 "Connection refused",
347 child.expect("password:")
348 child.sendline(password)
349 i = child.expect(["C:",
350 "Denying new connections due to the limit on number of connections",
351 "No more connections are allowed to telnet server",
352 "Unable to connect to remote host",
354 "Connection refused",
363 if self.set_dns(child):
366 child.sendline('route add 0.0.0.0 mask 0.0.0.0 ${WIN_DEFAULT_GATEWAY}')
370 self.run_date_time(child, None)
373 self.run_tlntadmn(child)
376 self.disable_firewall(child)
377 disable_firewall = False
380 if self.set_ip(child):
385 raise RuntimeError("Failed to connect with telnet")
387 def kinit(self, username, password):
388 '''use kinit to setup a credentials cache'''
389 self.run_cmd("kdestroy")
390 self.putenv('KRB5CCNAME', "${PREFIX}/ccache.test")
391 username = self.substitute(username)
392 s = username.split('@')
395 username = '@'.join(s)
396 child = self.pexpect_spawn('kinit -V ' + username)
397 child.expect("Password for")
398 child.sendline(password)
399 child.expect("Authenticated to Kerberos")