wintest: added README
[kai/samba.git] / wintest / wintest.py
index effc77e95ef8e0f6546894e0d0da5a82cf7b1bbe..b8e6ea2dd19d69aa69e04402f86ecf1af52c0262 100644 (file)
@@ -11,6 +11,7 @@ class wintest():
     def __init__(self):
         self.vars = {}
         self.list_mode = False
+        self.vms = None
         os.putenv('PYTHONUNBUFFERED', '1')
 
     def setvar(self, varname, value):
@@ -25,13 +26,19 @@ class wintest():
 
     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:
@@ -59,6 +66,11 @@ class wintest():
         '''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:
@@ -96,6 +108,13 @@ class wintest():
         '''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'''
@@ -158,7 +177,7 @@ class wintest():
         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):
@@ -168,6 +187,9 @@ class wintest():
         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
@@ -191,7 +213,7 @@ class wintest():
                 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:
@@ -204,23 +226,31 @@ class wintest():
                 self.info("retrying (retries=%u delay=%u)" % (retries, delay))
         raise RuntimeError("Failed to find %s" % contains)
 
-    def pexpect_spawn(self, cmd, timeout=60, crlf=True):
+    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)
 
-        if crlf:
-            ret.old_sendline = ret.sendline
-            ret.sendline = sendline_sub
+        ret.old_sendline = ret.sendline
+        ret.sendline = sendline_sub
         ret.old_expect = ret.expect
         ret.expect = expect_sub
 
@@ -312,6 +342,33 @@ class wintest():
         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 run_tlntadmn(self, child):
         '''remove the annoying telnet restrictions'''
         child.sendline('tlntadmn config maxconn=1024')
@@ -453,11 +510,13 @@ class wintest():
         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'''
@@ -486,3 +545,11 @@ class wintest():
                 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