samba-tool: Support preloading multiple users
[nivanova/samba-autobuild/.git] / python / samba / tests / samba_tool / rodc.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd. 2015
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17
18 import os
19 import ldb
20 import samba
21 from samba.samdb import SamDB
22 from samba.tests import delete_force
23 from samba.tests.samba_tool.base import SambaToolCmdTest
24 from samba.credentials import Credentials
25 from samba.auth import system_session
26
27 class RodcCmdTestCase(SambaToolCmdTest):
28     def setUp(self):
29         super(RodcCmdTestCase, self).setUp()
30         self.lp = samba.param.LoadParm()
31         self.lp.load(os.environ["SMB_CONF_PATH"])
32         self.creds = Credentials()
33         self.creds.set_username(os.environ["DC_USERNAME"])
34         self.creds.set_password(os.environ["DC_PASSWORD"])
35         self.creds.guess(self.lp)
36         self.session = system_session()
37         self.ldb = SamDB("ldap://" + os.environ["DC_SERVER"],
38             session_info=self.session, credentials=self.creds,lp=self.lp)
39
40         self.base_dn = self.ldb.domain_dn()
41
42         self.ldb.newuser("sambatool1", "1qazXSW@")
43         self.ldb.newuser("sambatool2", "2wsxCDE#")
44         self.ldb.newuser("sambatool3", "3edcVFR$")
45         self.ldb.newuser("sambatool4", "4rfvBGT%")
46
47         self.ldb.add_remove_group_members("Allowed RODC Password Replication Group",
48                                           ["sambatool1", "sambatool2", "sambatool3",
49                                            "sambatool4"],
50                                           add_members_operation=True)
51
52     def tearDown(self):
53         super(RodcCmdTestCase, self).tearDown()
54         self.ldb.deleteuser("sambatool1")
55         self.ldb.deleteuser("sambatool2")
56         self.ldb.deleteuser("sambatool3")
57         self.ldb.deleteuser("sambatool4")
58         (result, out, err) = self.runsubcmd("drs", "replicate", "--local", "unused",
59                                             os.environ["DC_SERVER"], self.base_dn)
60
61
62     def test_single_by_account_name(self):
63         (result, out, err) = self.runsubcmd("rodc", "preload", "sambatool1",
64                                             "--server", os.environ["DC_SERVER"])
65         self.assertCmdSuccess(result, "ensuring rodc prefetch ran successfully")
66         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\n" % self.base_dn)
67         self.assertEqual(err, "")
68
69     def test_single_by_dn(self):
70         (result, out, err) = self.runsubcmd("rodc", "preload", "cn=sambatool2,cn=users,%s" % self.base_dn,
71                                             "--server", os.environ["DC_SERVER"])
72         self.assertCmdSuccess(result, "ensuring rodc prefetch ran successfully")
73         self.assertEqual(out, "Replicating DN CN=sambatool2,CN=Users,%s\n" % self.base_dn)
74
75     def test_multi_by_account_name(self):
76         (result, out, err) = self.runsubcmd("rodc", "preload", "sambatool1", "sambatool2",
77                                             "--server", os.environ["DC_SERVER"])
78         self.assertCmdSuccess(result, "ensuring rodc prefetch ran successfully")
79         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\nReplicating DN CN=sambatool2,CN=Users,%s\n" % (self.base_dn, self.base_dn))
80
81     def test_multi_by_dn(self):
82         (result, out, err) = self.runsubcmd("rodc", "preload", "cn=sambatool3,cn=users,%s" % self.base_dn, "cn=sambatool4,cn=users,%s" % self.base_dn,
83                                             "--server", os.environ["DC_SERVER"])
84         self.assertCmdSuccess(result, "ensuring rodc prefetch ran successfully")
85         self.assertEqual(out, "Replicating DN CN=sambatool3,CN=Users,%s\nReplicating DN CN=sambatool4,CN=Users,%s\n" % (self.base_dn, self.base_dn))
86
87     def test_multi_in_file(self):
88         tempf = os.path.join(self.tempdir, "accountlist")
89         open(tempf, 'w').write("sambatool1\nsambatool2")
90         (result, out, err) = self.runsubcmd("rodc", "preload", "--file", tempf,
91                                             "--server", os.environ["DC_SERVER"])
92         self.assertCmdSuccess(result, "ensuring rodc prefetch ran successfully")
93         self.assertEqual(out, "Replicating DN CN=sambatool1,CN=Users,%s\nReplicating DN CN=sambatool2,CN=Users,%s\n" % (self.base_dn, self.base_dn))
94         os.unlink(tempf)