wintest Share more of the S4 test code with the s3 test
[kai/samba.git] / wintest / test-s3.py
1 #!/usr/bin/env python
2
3 '''automated testing of Samba3 against windows'''
4
5 import sys, os
6 import optparse
7 import wintest
8
9 def set_libpath(t):
10     t.putenv("LD_LIBRARY_PATH", "${PREFIX}/lib")
11
12 def build_s3(t):
13     '''build samba3'''
14     t.info('Building s3')
15     t.chdir('${SOURCETREE}/source3')
16     t.putenv('CC', 'ccache gcc')
17     t.run_cmd("./autogen.sh")
18     t.run_cmd("./configure -C --prefix=${PREFIX} --enable-developer")
19     t.run_cmd('make basics')
20     t.run_cmd('make -j4')
21     t.run_cmd('rm -rf ${PREFIX}')
22     t.run_cmd('make install')
23
24 def start_s3(t):
25     t.info('Starting Samba3')
26     t.chdir("${PREFIX}")
27     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
28     t.run_cmd("rm -f var/locks/*.pid")
29     t.run_cmd(['sbin/nmbd', "-D"])
30     t.run_cmd(['sbin/winbindd', "-D"])
31     t.run_cmd(['sbin/smbd', "-D"])
32     t.port_wait("localhost", 139)
33
34
35 def test_wbinfo(t):
36     t.info('Testing wbinfo')
37     t.chdir('${PREFIX}')
38     t.cmd_contains("bin/wbinfo --version", ["Version 3."])
39     t.cmd_contains("bin/wbinfo -p", ["Ping to winbindd succeeded"])
40     t.retry_cmd("bin/wbinfo --online-status",
41                 ["BUILTIN : online",
42                  "${HOSTNAME} : online",
43                  "${WIN_DOMAIN} : online"],
44                 casefold=True)
45     t.cmd_contains("bin/wbinfo -u",
46                    ["${WIN_DOMAIN}/administrator",
47                     "${WIN_DOMAIN}/krbtgt" ],
48                    casefold=True)
49     t.cmd_contains("bin/wbinfo -g",
50                    ["${WIN_DOMAIN}/domain users",
51                     "${WIN_DOMAIN}/domain guests",
52                     "${WIN_DOMAIN}/domain admins"],
53                    casefold=True)
54     t.cmd_contains("bin/wbinfo --name-to-sid administrator",
55                    "S-1-5-.*-500 SID_USER .1",
56                    regex=True)
57     t.cmd_contains("bin/wbinfo --name-to-sid 'domain users'",
58                    "S-1-5-.*-513 SID_DOM_GROUP .2",
59                    regex=True)
60
61     t.retry_cmd("bin/wbinfo --authenticate=administrator%${WIN_PASS}",
62                 ["plaintext password authentication succeeded",
63                  "challenge/response password authentication succeeded"])
64
65
66 def test_smbclient(t):
67     t.info('Testing smbclient')
68     t.chdir('${PREFIX}')
69     t.cmd_contains("bin/smbclient --version", ["Version 3."])
70     t.cmd_contains('bin/smbclient -L localhost -U%', ["Domain=[${WIN_DOMAIN}]", "test", "IPC$", "Samba 3."],
71                    casefold=True)
72     child = t.pexpect_spawn('bin/smbclient //${HOSTNAME}/test -Uadministrator%${WIN_PASS}')
73     child.expect("smb:")
74     child.sendline("dir")
75     child.expect("blocks available")
76     child.sendline("mkdir testdir")
77     child.expect("smb:")
78     child.sendline("cd testdir")
79     child.expect('testdir')
80     child.sendline("cd ..")
81     child.sendline("rmdir testdir")
82
83
84 def create_shares(t):
85     t.info("Adding test shares")
86     t.chdir('${PREFIX}')
87     t.write_file("lib/smb.conf", '''
88 [test]
89        path = ${PREFIX}/test
90        read only = no
91        ''',
92                  mode='a')
93     t.run_cmd("mkdir -p test")
94
95
96 def prep_join_as_member(t, vm):
97     '''prepare to join a windows domain as a member server'''
98     t.setwinvars(vm)
99     t.info("Starting VMs for joining ${WIN_VM} as a member using net ads join")
100     t.chdir('${PREFIX}')
101     t.run_cmd('killall -9 -q samba smbd nmbd winbindd', checkfail=False)
102     t.vm_poweroff("${WIN_VM}", checkfail=False)
103     t.vm_restore("${WIN_VM}", "${WIN_SNAPSHOT}")
104     child = t.open_telnet("${WIN_HOSTNAME}", "administrator", "${WIN_PASS}", set_time=True)
105     t.get_ipconfig(child)
106     t.del_files(["var", "private"])
107     t.write_file("lib/smb.conf", '''
108 [global]
109         netbios name = ${HOSTNAME}
110         log level = ${DEBUGLEVEL}
111         realm = ${WIN_REALM}
112         workgroup = ${WIN_DOMAIN}
113         security = ADS
114         interfaces = ${INTERFACE}
115         winbind separator = /
116         idmap uid = 1000000-2000000
117         idmap gid = 1000000-2000000
118         winbind enum users = yes
119         winbind enum groups = yes
120         max protocol = SMB2
121         map hidden = no
122         map system = no
123         ea support = yes
124         panic action = xterm -e gdb --pid %d
125     ''')
126
127 def join_as_member(t, vm):
128     '''join a windows domain as a member server'''
129     t.setwinvars(vm)
130     t.info("Joining ${WIN_VM} as a member using net ads join")
131     t.port_wait("${WIN_IP}", 389)
132     t.retry_cmd("host -t SRV _ldap._tcp.${WIN_REALM} ${WIN_IP}", ['has SRV record'] )
133     t.cmd_contains("bin/net ads join -Uadministrator%${WIN_PASS}", ["Joined"])
134     t.cmd_contains("bin/net ads testjoin", ["Join is OK"])
135
136
137 def test_join_as_member(t, vm):
138     '''test the domain join'''
139     t.setwinvars(vm)
140     t.info('Testing join as member')
141     t.chdir('${PREFIX}')
142     t.run_cmd('bin/net ads user add root -Uadministrator%${WIN_PASS}')
143     test_wbinfo(t)
144     test_smbclient(t)
145
146
147 def test_s3(t):
148     '''basic s3 testing'''
149
150     t.check_prerequesites()
151     set_libpath(t)
152
153     if not t.skip("configure_bind"):
154         t.configure_bind()
155     if not t.skip("stop_bind"):
156         t.stop_bind()
157     if not t.skip("stop_vms"):
158         t.stop_vms()
159
160     if not t.skip("build"):
161         build_s3(t)
162
163     if not t.skip("configure_bind2"):
164         t.configure_bind()
165     if not t.skip("start_bind"):
166         t.start_bind()
167
168     if t.have_var('W2K8R2A_VM') and not t.skip("join_w2k8r2"):
169         prep_join_as_member(t, "W2K8R2A")
170         t.run_dcpromo_as_first_dc("W2K8R2A", func_level='2008r2')
171         join_as_member(t, "W2K8R2A")
172         create_shares(t)
173         start_s3(t)
174         test_join_as_member(t, "W2K8R2A")
175
176     t.info("S3 test: All OK")
177
178
179 def test_cleanup(t):
180     '''cleanup after tests'''
181     t.info("Cleaning up ...")
182     t.restore_resolv_conf()
183     if getattr(t, 'bind_child', False):
184         t.bind_child.kill()
185
186
187 if __name__ == '__main__':
188     t = wintest.wintest()
189
190     t.setup("test-s3.py", "source3")
191
192     try:
193         test_s3(t)
194     except:
195         if not t.opts.nocleanup:
196             test_cleanup(t)
197         raise
198
199     if not t.opts.nocleanup:
200         test_cleanup(t)
201     t.info("S3 test: All OK")