wintest: cope with w2k3 form of dcdiag output
[kai/samba.git] / wintest / test-s4-howto.py
index 8f57553929bc673d3c654e67e07272c36b1d9a61..840901b379015083cb58c15ad3fc9f94c429f1fe 100755 (executable)
@@ -4,7 +4,7 @@
 
 import sys, os
 import optparse
-import wintest, pexpect
+import wintest, pexpect, time
 
 def check_prerequesites(t):
     t.info("Checking prerequesites")
@@ -17,7 +17,6 @@ def check_prerequesites(t):
         t.run_cmd('ifconfig ${INTERFACE} inet6 del ${INTERFACE_IPV6}/64', checkfail=False)
         t.run_cmd('ifconfig ${INTERFACE} inet6 add ${INTERFACE_IPV6}/64 up')
 
-
 def build_s4(t):
     '''build samba4'''
     t.info('Building s4')
@@ -63,6 +62,13 @@ def start_s4(t):
              '--option', 'panic action=gnome-terminal -e "gdb --pid %PID%"'])
     t.port_wait("${INTERFACE_IP}", 139)
 
+def stop_vms(t):
+    '''Shut down any existing alive VMs, so they do not collide with what we are doing'''
+    t.info('Shutting down any of our VMs already running')
+    vms = t.get_vms()
+    for v in vms:
+        t.vm_poweroff(v, checkfail=False)
+
 def test_smbclient(t):
     '''test smbclient'''
     t.info('Testing smbclient')
@@ -123,19 +129,13 @@ def restore_resolv_conf(t):
         t.info("restoring /etc/resolv.conf")
         t.run_cmd("mv -f %s /etc/resolv.conf" % t.resolv_conf_backup)
 
+
 def rndc_cmd(t, cmd, checkfail=True):
     '''run a rndc command'''
     t.run_cmd("${RNDC} -c ${PREFIX}/etc/rndc.conf %s" % cmd, checkfail=checkfail)
 
-
-def restart_bind(t):
-    '''restart the test environment version of bind'''
-    t.info("Restarting bind9")
-    t.putenv('KEYTAB_FILE', '${PREFIX}/private/dns.keytab')
-    t.putenv('KRB5_KTNAME', '${PREFIX}/private/dns.keytab')
+def configure_bind(t):
     t.chdir('${PREFIX}')
-    t.run_cmd("mkdir -p var/named/data")
-    t.run_cmd("chown -R ${BIND_USER} var/named")
 
     nameserver = t.get_nameserver()
     if nameserver == t.getvar('INTERFACE_IP'):
@@ -216,13 +216,36 @@ options {
 
     set_nameserver(t, t.getvar('INTERFACE_IP'))
 
+
+def stop_bind(t):
+    '''Stop our private BIND from listening and operating'''
     rndc_cmd(t, "stop", checkfail=False)
     t.port_wait("${INTERFACE_IP}", 53, wait_for_fail=True)
+
+    t.run_cmd("rm -rf var/named")
+
+
+def start_bind(t):
+    '''restart the test environment version of bind'''
+    t.info("Restarting bind9")
+    t.putenv('KEYTAB_FILE', '${PREFIX}/private/dns.keytab')
+    t.putenv('KRB5_KTNAME', '${PREFIX}/private/dns.keytab')
+    t.chdir('${PREFIX}')
+
+    set_nameserver(t, t.getvar('INTERFACE_IP'))
+
+    t.run_cmd("mkdir -p var/named/data")
+    t.run_cmd("chown -R ${BIND_USER} var/named")
+
     t.bind_child = t.run_child("${BIND9} -u ${BIND_USER} -n 1 -c ${PREFIX}/etc/named.conf -g")
 
     t.port_wait("${INTERFACE_IP}", 53)
     rndc_cmd(t, "flush")
 
+def restart_bind(t):
+    configure_bind(t)
+    stop_bind(t)
+    start_bind(t)
 
 def test_dns(t):
     '''test that DNS is OK'''
@@ -326,12 +349,15 @@ def test_dcpromo(t, vm):
     t.info("Checking the dcpromo join is OK")
     t.chdir('${PREFIX}')
     t.port_wait("${WIN_IP}", 139)
+    t.retry_cmd("host -t A ${WIN_HOSTNAME}.${LCREALM}. ${INTERFACE_IP}",
+                ['${WIN_HOSTNAME}.${LCREALM} has address'],
+                retries=30, delay=10, casefold=True)
     t.retry_cmd('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
     t.cmd_contains("host -t A ${WIN_HOSTNAME}.${LCREALM}.", ['has address'])
     t.cmd_contains('bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utestallowed@${LCREALM}%${PASSWORD1}', ["C$", "IPC$", "Sharename"])
 
     t.cmd_contains("bin/samba-tool drs kcc ${HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}", ['Consistency check', 'successful'])
-    t.cmd_contains("bin/samba-tool drs kcc ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}", ['Consistency check', 'successful'])
+    t.retry_cmd("bin/samba-tool drs kcc ${WIN_HOSTNAME}.${LCREALM} -Uadministrator@${LCREALM}%${PASSWORD1}", ['Consistency check', 'successful'])
 
     t.kinit("administrator@${REALM}", "${PASSWORD1}")
 
@@ -380,7 +406,15 @@ def test_dcpromo(t, vm):
 
     child = t.open_telnet("${WIN_HOSTNAME}", "${DOMAIN}\\administrator", "${PASSWORD1}", set_time=True)
     child.sendline("net use t: \\\\${HOSTNAME}.${LCREALM}\\test")
-    child.expect("The command completed successfully")
+
+    retries = 10
+    i = child.expect(["The command completed successfully", "The network path was not found"])
+    while i == 1 and retries > 0:
+        child.expect("C:")
+        time.sleep(2)
+        child.sendline("net use t: \\\\${HOSTNAME}.${LCREALM}\\test")
+        i = child.expect(["The command completed successfully", "The network path was not found"])
+        retries -=1
 
     t.run_net_time(child)
 
@@ -394,7 +428,7 @@ def test_dcpromo(t, vm):
     child.expect("was successful")
 
     t.info("Checking if new users propogate to windows")
-    t.run_cmd('bin/samba-tool newuser test2 ${PASSWORD2}')
+    t.retry_cmd('bin/samba-tool newuser test2 ${PASSWORD2}', ["created successfully"])
     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k no", ['Sharename', 'Remote IPC'])
     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${LCREALM} -Utest2%${PASSWORD2} -k yes", ['Sharename', 'Remote IPC'])
 
@@ -498,10 +532,10 @@ def test_dcpromo_rodc(t, vm):
     t.vm_poweroff("${WIN_VM}")
 
 
-def join_as_dc(t, vm):
-    '''join a windows domain as a DC'''
+def prep_join_as_dc(t, vm):
+    '''start VM and shutdown Samba in preperation to join a windows domain as a DC'''
     t.setwinvars(vm)
-    t.info("Joining ${WIN_VM} as a second DC using samba-tool join DC")
+    t.info("Starting VMs for joining ${WIN_VM} as a second DC using samba-tool join DC")
     t.chdir('${PREFIX}')
     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
     t.vm_poweroff("${WIN_VM}", checkfail=False)
@@ -510,6 +544,14 @@ def join_as_dc(t, vm):
     t.run_cmd("rm -rf etc/smb.conf private")
     child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
     t.get_ipconfig(child)
+
+def join_as_dc(t, vm):
+    '''join a windows domain as a DC'''
+    t.setwinvars(vm)
+    t.info("Joining ${WIN_VM} as a second DC using samba-tool join DC")
+    t.port_wait("${WIN_IP}", 389)
+    t.retry_cmd("host -t SRV _ldap._tcp.${WIN_REALM} ${WIN_IP}", ['has SRV record'] )
+
     t.retry_cmd("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator%${WIN_PASS}", ['INBOUND NEIGHBORS'] )
     t.run_cmd('bin/samba-tool join ${WIN_REALM} DC -Uadministrator%${WIN_PASS} -d${DEBUGLEVEL} --option=interfaces=${INTERFACE}')
     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
@@ -532,8 +574,15 @@ def test_join_as_dc(t, vm):
         t.cmd_contains("bin/samba-tool drs replicate ${HOSTNAME}.${WIN_REALM} ${WIN_HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
         t.cmd_contains("bin/samba-tool drs replicate ${WIN_HOSTNAME}.${WIN_REALM} ${HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
 
-    child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
-    child.expect("The command completed successfully")
+    retries = 10
+    i = 1
+    while i == 1 and retries > 0:
+        child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
+        i = child.expect(["The command completed successfully", "The network path was not found"])
+        child.expect("C:")
+        if i == 1:
+            time.sleep(2)
+        retries -=1
 
     t.info("Checking if showrepl is happy")
     child.sendline("repadmin /showrepl")
@@ -545,7 +594,7 @@ def test_join_as_dc(t, vm):
     child.expect("was successful")
 
     t.info("Checking if new users propogate to windows")
-    t.run_cmd('bin/samba-tool newuser test2 ${PASSWORD2}')
+    t.retry_cmd('bin/samba-tool newuser test2 ${PASSWORD2}', ["created successfully"])
     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k no", ['Sharename', 'Remote IPC'])
     t.retry_cmd("bin/smbclient -L ${WIN_HOSTNAME}.${WIN_REALM} -Utest2%${PASSWORD2} -k yes", ['Sharename', 'Remote IPC'])
 
@@ -571,14 +620,8 @@ def join_as_rodc(t, vm):
     '''join a windows domain as a RODC'''
     t.setwinvars(vm)
     t.info("Joining ${WIN_VM} as a RODC using samba-tool join DC")
-    t.chdir('${PREFIX}')
-    t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
-    t.vm_poweroff("${WIN_VM}", checkfail=False)
-    t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
-    rndc_cmd(t, 'flush')
-    t.run_cmd("rm -rf etc/smb.conf private")
-    child = t.open_telnet("${WIN_HOSTNAME}", "${WIN_DOMAIN}\\administrator", "${WIN_PASS}", set_time=True)
-    t.get_ipconfig(child)
+    t.port_wait("${WIN_IP}", 389)
+    t.retry_cmd("host -t SRV _ldap._tcp.${WIN_REALM} ${WIN_IP}", ['has SRV record'] )
     t.retry_cmd("bin/samba-tool drs showrepl ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator%${WIN_PASS}", ['INBOUND NEIGHBORS'] )
     t.run_cmd('bin/samba-tool join ${WIN_REALM} RODC -Uadministrator%${WIN_PASS} -d${DEBUGLEVEL} --option=interfaces=${INTERFACE}')
     t.run_cmd('bin/samba-tool drs kcc ${WIN_HOSTNAME}.${WIN_REALM} -Uadministrator@${WIN_REALM}%${WIN_PASS}')
@@ -600,8 +643,15 @@ def test_join_as_rodc(t, vm):
     for nc in [ '${WIN_BASEDN}', 'CN=Configuration,${WIN_BASEDN}', 'CN=Schema,CN=Configuration,${WIN_BASEDN}' ]:
         t.cmd_contains("bin/samba-tool drs replicate ${HOSTNAME}.${WIN_REALM} ${WIN_HOSTNAME}.${WIN_REALM} %s -k yes" % nc, ["was successful"])
 
-    child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
-    child.expect("The command completed successfully")
+    retries = 10
+    i = 1
+    while i == 1 and retries > 0:
+        child.sendline("net use t: \\\\${HOSTNAME}.${WIN_REALM}\\test")
+        i = child.expect(["The command completed successfully", "The network path was not found"])
+        child.expect("C:")
+        if i == 1:
+            time.sleep(2)
+        retries -=1
 
     t.info("Checking if showrepl is happy")
     child.sendline("repadmin /showrepl")
@@ -638,6 +688,56 @@ def test_join_as_rodc(t, vm):
     t.vm_poweroff("${WIN_VM}")
 
 
+def run_dcpromo_as_first_dc(t, vm, func_level=None):
+    t.setwinvars(vm)
+    t.info("Configuring a windows VM ${WIN_VM} at the first DC in the domain using dcpromo")
+    child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_time=True)
+    if t.get_is_dc(child):
+        return
+
+    if func_level == '2008r2':
+        t.setvar("FUNCTION_LEVEL_INT", str(4))
+    elif func_level == '2003':
+        t.setvar("FUNCTION_LEVEL_INT", str(1))
+    else:
+        t.setvar("FUNCTION_LEVEL_INT", str(0))
+
+    child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_ip=True)
+
+    """This server must therefore not yet be a directory server, so we must promote it"""
+    child.sendline("copy /Y con answers.txt")
+    child.sendline('''
+[DCInstall]
+; New forest promotion
+ReplicaOrNewDomain=Domain
+NewDomain=Forest
+NewDomainDNSName=${WIN_REALM}
+ForestLevel=${FUNCTION_LEVEL_INT}
+DomainNetbiosName=${WIN_DOMAIN}
+DomainLevel=${FUNCTION_LEVEL_INT}
+InstallDNS=Yes
+ConfirmGc=Yes
+CreateDNSDelegation=No
+DatabasePath="C:\Windows\NTDS"
+LogPath="C:\Windows\NTDS"
+SYSVOLPath="C:\Windows\SYSVOL"
+; Set SafeModeAdminPassword to the correct value prior to using the unattend file
+SafeModeAdminPassword=${WIN_PASS}
+; Run-time flags (optional)
+RebootOnCompletion=No
+\1a
+''')
+    child.expect("copied.")
+    child.expect("C:")
+    child.expect("C:")
+    child.sendline("dcpromo /answer:answers.txt")
+    i = child.expect(["You must restart this computer", "failed", "Active Directory Domain Services was not installed", "C:"], timeout=120)
+    if i == 1 or i == 2:
+        raise Exception("dcpromo failed")
+    child.sendline("shutdown -r -t 0")
+    t.port_wait("${WIN_IP}", 139, wait_for_fail=True)
+    t.port_wait("${WIN_IP}", 139)
+
 def test_howto(t):
     '''test the Samba4 howto'''
 
@@ -646,6 +746,13 @@ def test_howto(t):
     # we don't need fsync safety in these tests
     t.putenv('TDB_NO_FSYNC', '1')
 
+    if not t.skip("configure_bind"):
+        configure_bind(t)
+    if not t.skip("stop_bind"):
+        stop_bind(t)
+    if not t.skip("stop_vms"):
+        stop_vms(t)
+
     if not t.skip("build"):
         build_s4(t)
 
@@ -659,8 +766,10 @@ def test_howto(t):
         start_s4(t)
     if not t.skip("smbclient"):
         test_smbclient(t)
-    if not t.skip("startbind"):
-        restart_bind(t)
+    if not t.skip("configure_bind2"):
+        configure_bind(t)
+    if not t.skip("start_bind"):
+        start_bind(t)
     if not t.skip("dns"):
         test_dns(t)
     if not t.skip("kerberos"):
@@ -668,30 +777,30 @@ def test_howto(t):
     if not t.skip("dyndns"):
         test_dyndns(t)
 
-    if t.have_var('WINDOWS7_VM') and not t.skip("windows7"):
+    if t.have_vm('WINDOWS7') and not t.skip("windows7"):
         run_winjoin(t, "WINDOWS7")
         test_winjoin(t, "WINDOWS7")
 
-    if t.have_var('WINXP_VM') and not t.skip("winxp"):
+    if t.have_vm('WINXP') and not t.skip("winxp"):
         run_winjoin(t, "WINXP")
         test_winjoin(t, "WINXP")
 
-    if t.have_var('W2K8R2C_VM') and not t.skip("dcpromo_rodc"):
+    if t.have_vm('W2K8R2C') and not t.skip("dcpromo_rodc"):
         t.info("Testing w2k8r2 RODC dcpromo")
         run_dcpromo_rodc(t, "W2K8R2C")
         test_dcpromo_rodc(t, "W2K8R2C")
 
-    if t.have_var('W2K8R2B_VM') and not t.skip("dcpromo_w2k8r2"):
+    if t.have_vm('W2K8R2B') and not t.skip("dcpromo_w2k8r2"):
         t.info("Testing w2k8r2 dcpromo")
         run_dcpromo(t, "W2K8R2B")
         test_dcpromo(t, "W2K8R2B")
 
-    if t.have_var('W2K8B_VM') and not t.skip("dcpromo_w2k8"):
+    if t.have_vm('W2K8B') and not t.skip("dcpromo_w2k8"):
         t.info("Testing w2k8 dcpromo")
         run_dcpromo(t, "W2K8B")
         test_dcpromo(t, "W2K8B")
 
-    if t.have_var('W2K3B_VM') and not t.skip("dcpromo_w2k3"):
+    if t.have_vm('W2K3B') and not t.skip("dcpromo_w2k3"):
         t.info("Testing w2k3 dcpromo")
         t.info("Changing to 2003 functional level")
         provision_s4(t, func_level='2003')
@@ -705,21 +814,27 @@ def test_howto(t):
         run_dcpromo(t, "W2K3B")
         test_dcpromo(t, "W2K3B")
 
-    if t.have_var('W2K8R2A_VM') and not t.skip("join_w2k8r2"):
+    if t.have_vm('W2K8R2A') and not t.skip("join_w2k8r2"):
+        prep_join_as_dc(t, "W2K8R2A")
+        run_dcpromo_as_first_dc(t, "W2K8R2A", func_level='2008r2')
         join_as_dc(t, "W2K8R2A")
         create_shares(t)
         start_s4(t)
         test_dyndns(t)
         test_join_as_dc(t, "W2K8R2A")
 
-    if t.have_var('W2K8R2A_VM') and not t.skip("join_rodc"):
+    if t.have_vm('W2K8R2A') and not t.skip("join_rodc"):
+        prep_join_as_dc(t, "W2K8R2A")
+        run_dcpromo_as_first_dc(t, "W2K8R2A", func_level='2008r2')
         join_as_rodc(t, "W2K8R2A")
         create_shares(t)
         start_s4(t)
         test_dyndns(t)
         test_join_as_rodc(t, "W2K8R2A")
 
-    if t.have_var('W2K3A_VM') and not t.skip("join_w2k3"):
+    if t.have_vm('W2K3A') and not t.skip("join_w2k3"):
+        prep_join_as_dc(t, "W2K3A")
+        run_dcpromo_as_first_dc(t, "W2K3A", func_level='2003')
         join_as_dc(t, "W2K3A")
         create_shares(t)
         start_s4(t)
@@ -741,6 +856,7 @@ if __name__ == '__main__':
     parser = optparse.OptionParser("test-howto.py")
     parser.add_option("--conf", type='string', default='', help='config file')
     parser.add_option("--skip", type='string', default='', help='list of steps to skip (comma separated)')
+    parser.add_option("--vms", type='string', default=None, help='list of VMs to use (comma separated)')
     parser.add_option("--list", action='store_true', default=False, help='list the available steps')
     parser.add_option("--rebase", action='store_true', default=False, help='do a git pull --rebase')
     parser.add_option("--clean", action='store_true', default=False, help='clean the tree')
@@ -756,7 +872,9 @@ if __name__ == '__main__':
 
     t = wintest.wintest()
     t.load_config(opts.conf)
+
     t.set_skip(opts.skip)
+    t.set_vms(opts.vms)
 
     if opts.list:
         t.list_steps_mode()