'''automated testing library for testing Samba against windows'''
import pexpect, subprocess
+import optparse
import sys, os, time, re
class wintest():
def __init__(self):
self.vars = {}
self.list_mode = False
+ self.vms = None
os.putenv('PYTHONUNBUFFERED', '1')
+ self.parser = optparse.OptionParser("wintest")
def setvar(self, varname, value):
'''set a substitution variable'''
def setwinvars(self, vm, prefix='WIN'):
'''setup WIN_XX vars based on a vm name'''
- for v in ['VM', 'HOSTNAME', 'USER', 'PASS', 'SNAPSHOT', 'BASEDN', 'REALM', 'DOMAIN', 'IP']:
+ for v in ['VM', 'HOSTNAME', 'USER', 'PASS', 'SNAPSHOT', 'REALM', 'DOMAIN', 'IP']:
vname = '%s_%s' % (vm, v)
if vname in self.vars:
self.setvar("%s_%s" % (prefix,v), self.substitute("${%s}" % vname))
else:
self.vars.pop("%s_%s" % (prefix,v), None)
+ if self.getvar("WIN_REALM"):
+ self.setvar("WIN_REALM", self.getvar("WIN_REALM").upper())
+ self.setvar("WIN_LCREALM", self.getvar("WIN_REALM").lower())
+ dnsdomain = self.getvar("WIN_REALM")
+ self.setvar("WIN_BASEDN", "DC=" + dnsdomain.replace(".", ",DC="))
+
def info(self, msg):
'''print some information'''
if not self.list_mode:
'''set a list of tests to skip'''
self.skiplist = skiplist.split(',')
+ def set_vms(self, vms):
+ '''set a list of VMs to test'''
+ if vms is not None:
+ self.vms = vms.split(',')
+
def skip(self, step):
'''return True if we should skip a step'''
if self.list_mode:
'''see if a variable has been set'''
return varname in self.vars
+ def have_vm(self, vmname):
+ '''see if a VM should be used'''
+ if not self.have_var(vmname + '_VM'):
+ return False
+ if self.vms is None:
+ return True
+ return vmname in self.vms
def putenv(self, key, value):
'''putenv with substitution'''
f.close()
def run_cmd(self, cmd, dir=".", show=None, output=False, checkfail=True):
+ '''run a command'''
cmd = self.substitute(cmd)
if isinstance(cmd, list):
self.info('$ ' + " ".join(cmd))
else:
return subprocess.call(cmd, shell=shell, cwd=dir)
+
def run_child(self, cmd, dir="."):
+ '''create a child and return the Popen handle to it'''
cwd = os.getcwd()
cmd = self.substitute(cmd)
if isinstance(cmd, list):
else:
shell=True
os.chdir(dir)
- ret = subprocess.Popen(cmd, shell=shell)
+ ret = subprocess.Popen(cmd, shell=shell, stderr=subprocess.STDOUT)
os.chdir(cwd)
return ret
return self.run_cmd(cmd, output=True)
def cmd_contains(self, cmd, contains, nomatch=False, ordered=False, regex=False,
- casefold=False):
+ casefold=True):
'''check that command output contains the listed strings'''
if isinstance(contains, str):
self.info(out)
for c in self.substitute(contains):
if regex:
+ if casefold:
+ c = c.upper()
+ out = out.upper()
m = re.search(c, out)
if m is None:
start = -1
out = out[end:]
def retry_cmd(self, cmd, contains, retries=30, delay=2, wait_for_fail=False,
- ordered=False, regex=False, casefold=False):
+ ordered=False, regex=False, casefold=True):
'''retry a command a number of times'''
while retries > 0:
try:
return
except:
time.sleep(delay)
- retries = retries - 1
+ retries -= 1
+ self.info("retrying (retries=%u delay=%u)" % (retries, delay))
raise RuntimeError("Failed to find %s" % contains)
- def pexpect_spawn(self, cmd, timeout=60):
+ def pexpect_spawn(self, cmd, timeout=60, crlf=True, casefold=True):
'''wrapper around pexpect spawn'''
cmd = self.substitute(cmd)
self.info("$ " + cmd)
ret = pexpect.spawn(cmd, logfile=sys.stdout, timeout=timeout)
def sendline_sub(line):
- line = self.substitute(line).replace('\n', '\r\n')
- return ret.old_sendline(line + '\r')
+ line = self.substitute(line)
+ if crlf:
+ line = line.replace('\n', '\r\n') + '\r'
+ return ret.old_sendline(line)
- def expect_sub(line, timeout=ret.timeout):
+ def expect_sub(line, timeout=ret.timeout, casefold=casefold):
line = self.substitute(line)
+ if casefold:
+ if isinstance(line, list):
+ for i in range(len(line)):
+ if isinstance(line[i], str):
+ line[i] = '(?i)' + line[i]
+ elif isinstance(line, str):
+ line = '(?i)' + line
return ret.old_expect(line, timeout=timeout)
ret.old_sendline = ret.sendline
def get_nameserver(self):
'''Get the current nameserver from /etc/resolv.conf'''
- child = self.pexpect_spawn('cat /etc/resolv.conf')
- child.expect('nameserver')
+ child = self.pexpect_spawn('cat /etc/resolv.conf', crlf=False)
+ i = child.expect(['Generated by wintest', 'nameserver'])
+ if i == 0:
+ child.expect('your original resolv.conf')
+ child.expect('nameserver')
child.expect('\d+.\d+.\d+.\d+')
return child.after
self.setvar('VMNAME', vmname)
self.run_cmd("${VM_POWEROFF}", checkfail=checkfail)
+ def vm_reset(self, vmname):
+ '''reset a VM'''
+ self.setvar('VMNAME', vmname)
+ self.run_cmd("${VM_RESET}")
+
def vm_restore(self, vmname, snapshot):
'''restore a VM'''
self.setvar('VMNAME', vmname)
self.setvar('WIN_DEFAULT_GATEWAY', child.after)
child.expect("C:")
+ def get_is_dc(self, child):
+ '''check if a windows machine is a domain controller'''
+ child.sendline("dcdiag")
+ i = child.expect(["is not a Directory Server",
+ "is not recognized as an internal or external command",
+ "Home Server = ",
+ "passed test Replications"])
+ if i == 0:
+ return False
+ if i == 1 or i == 3:
+ child.expect("C:")
+ child.sendline("net config Workstation")
+ child.expect("Workstation domain")
+ child.expect('[\S]+')
+ domain = child.after
+ i = child.expect(["Workstation Domain DNS Name", "Logon domain"])
+ '''If we get the Logon domain first, we are not in an AD domain'''
+ if i == 1:
+ return False
+ if domain.upper() == self.getvar("WIN_DOMAIN").upper():
+ return True
+
+ child.expect('[\S]+')
+ hostname = child.after
+ if hostname.upper() == self.getvar("WIN_HOSTNAME").upper():
+ return True
+
+ def set_noexpire(self, child, username):
+ '''Ensure this user's password does not expire'''
+ child.sendline('wmic useraccount where name="%s" set PasswordExpires=FALSE' % username)
+ child.expect("update successful")
+ child.expect("C:")
+
def run_tlntadmn(self, child):
'''remove the annoying telnet restrictions'''
child.sendline('tlntadmn config maxconn=1024')
child.expect("C:")
if i == 1:
child.sendline('netsh firewall set opmode mode = DISABLE profile = ALL')
- child.expect("Ok")
+ i = child.expect(["Ok", "The following command was not found"])
+ if i != 0:
+ self.info("Firewall disable failed - ignoring")
child.expect("C:")
def set_dns(self, child):
return child.after
retries -= 1
time.sleep(delay)
+ self.info("retrying (retries=%u delay=%u)" % (retries, delay))
raise RuntimeError("Failed to resolve IP of %s" % hostname)
def open_telnet(self, hostname, username, password, retries=60, delay=5, set_time=False, set_ip=False,
- disable_firewall=True, run_tlntadmn=True):
+ disable_firewall=True, run_tlntadmn=True, set_noexpire=False):
'''open a telnet connection to a windows server, return the pexpect child'''
set_route = False
set_dns = False
child.close()
time.sleep(delay)
retries -= 1
+ self.info("retrying (retries=%u delay=%u)" % (retries, delay))
continue
child.expect("password:")
child.sendline(password)
child.close()
time.sleep(delay)
retries -= 1
+ self.info("retrying (retries=%u delay=%u)" % (retries, delay))
continue
if set_dns:
set_dns = False
if run_tlntadmn:
self.run_tlntadmn(child)
run_tlntadmn = False
+ if set_noexpire:
+ self.set_noexpire(child, username)
+ set_noexpire = False
if disable_firewall:
self.disable_firewall(child)
disable_firewall = False
if len(s) > 0:
s[1] = s[1].upper()
username = '@'.join(s)
- child = self.pexpect_spawn('kinit -V ' + username)
- child.expect("Password for")
+ child = self.pexpect_spawn('kinit ' + username)
+ child.expect("Password")
child.sendline(password)
- child.expect("Authenticated to Kerberos")
-
+ child.expect(pexpect.EOF)
+ child.close()
+ if child.exitstatus != 0:
+ raise RuntimeError("kinit failed with status %d" % child.exitstatus)
def get_domains(self):
'''return a dictionary of DNS domains and IPs for named.conf'''
if base + '_IP' in self.vars:
ret[self.vars[base + '_REALM']] = self.vars[base + '_IP']
return ret
+
+ def wait_reboot(self, retries=3):
+ '''wait for a VM to reboot'''
+
+ # first wait for it to shutdown
+ self.port_wait("${WIN_IP}", 139, wait_for_fail=True, delay=6)
+
+ # now wait for it to come back. If it fails to come back
+ # then try resetting it
+ while retries > 0:
+ try:
+ self.port_wait("${WIN_IP}", 139)
+ return
+ except:
+ retries -= 1
+ self.vm_reset("${WIN_VM}")
+ self.info("retrying reboot (retries=%u)" % retries)
+ raise RuntimeError(self.substitute("VM ${WIN_VM} failed to reboot"))
+
+ def get_vms(self):
+ '''return a dictionary of all the configured VM names'''
+ ret = []
+ for v in self.vars:
+ if v[-3:] == "_VM":
+ ret.append(self.vars[v])
+ return ret
+
+ def setup(self, testname, subdir):
+ '''setup for main tests, parsing command line'''
+ self.parser.add_option("--conf", type='string', default='', help='config file')
+ self.parser.add_option("--skip", type='string', default='', help='list of steps to skip (comma separated)')
+ self.parser.add_option("--vms", type='string', default=None, help='list of VMs to use (comma separated)')
+ self.parser.add_option("--list", action='store_true', default=False, help='list the available steps')
+ self.parser.add_option("--rebase", action='store_true', default=False, help='do a git pull --rebase')
+ self.parser.add_option("--clean", action='store_true', default=False, help='clean the tree')
+ self.parser.add_option("--prefix", type='string', default=None, help='override install prefix')
+ self.parser.add_option("--sourcetree", type='string', default=None, help='override sourcetree location')
+ self.parser.add_option("--nocleanup", action='store_true', default=False, help='disable cleanup code')
+
+ self.opts, self.args = self.parser.parse_args()
+
+ if not self.opts.conf:
+ print("Please specify a config file with --conf")
+ sys.exit(1)
+
+ # we don't need fsync safety in these tests
+ self.putenv('TDB_NO_FSYNC', '1')
+
+ self.load_config(self.opts.conf)
+
+ self.set_skip(self.opts.skip)
+ self.set_vms(self.opts.vms)
+
+ if self.opts.list:
+ self.list_steps_mode()
+
+ if self.opts.prefix:
+ self.setvar('PREFIX', self.opts.prefix)
+
+ if self.opts.sourcetree:
+ self.setvar('SOURCETREE', self.opts.sourcetree)
+
+ if self.opts.rebase:
+ self.info('rebasing')
+ self.chdir('${SOURCETREE}')
+ self.run_cmd('git pull --rebase')
+
+ if self.opts.clean:
+ self.info('cleaning')
+ self.chdir('${SOURCETREE}/' + subdir)
+ self.run_cmd('make clean')