ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / tests / samba_tool / promote_dc_lmdb_size.py
blob88e9d7cac632d9ee7afb585b8e24fa1a093a20ed
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd. 2019
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.
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.
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/>.
18 from samba.tests.samba_tool.base import SambaToolCmdTest
19 import os
20 import shutil
23 class PromoteDcLmdbSizeTestCase(SambaToolCmdTest):
24 """Test setting of the lmdb map size during a promote dc"""
26 def setUp(self):
27 super().setUp()
28 self.tempsambadir = os.path.join(self.tempdir, "samba")
29 os.mkdir(self.tempsambadir)
30 (_, name) = os.path.split(self.tempdir)
31 self.netbios_name = name
33 # join a domain as a member server
35 # returns the tuple (ret, stdout, stderr)
36 def join_member(self):
37 command = (
38 "samba-tool " +
39 "domain join " +
40 os.environ["REALM"] + " " +
41 "member " +
42 ("-U%s%%%s " % (os.environ["USERNAME"], os.environ["PASSWORD"])) +
43 ("--option=netbiosname=%s " % self.netbios_name) +
44 ("--targetdir=%s " % self.tempsambadir))
45 return self.run_command(command)
48 # Promote a member server to a domain controller
49 def promote(self, size=None, role=None):
50 command = (
51 "samba-tool " +
52 "domain dcpromo " +
53 os.environ["REALM"] + " " +
54 role + " " +
55 ("-U%s%%%s " % (os.environ["USERNAME"], os.environ["PASSWORD"])) +
56 ("--option=netbiosname=%s " % self.netbios_name) +
57 ("--targetdir=%s " % self.tempsambadir) +
58 "--backend-store=mdb "
60 if size:
61 command += ("--backend-store-size=%s" % size)
63 (ret, stdout, stderr) = self.run_command(command)
64 if ret == 0:
65 self.cleanup_join(self.netbios_name)
67 return (ret, stdout, stderr)
69 def is_rodc(self):
70 url = "ldb://%s/private/sam.ldb" % self.tempsambadir
71 samdb = self.getSamDB("-H", url)
72 return samdb.am_rodc()
75 # Get the lmdb map size for the specified command
77 # While there is a python lmdb package available we use the lmdb command
78 # line utilities to avoid introducing a dependency.
80 def get_lmdb_environment_size(self, path):
81 (result, out, err) = self.run_command("mdb_stat -ne %s" % path)
82 if result:
83 self.fail("Unable to run mdb_stat\n")
84 for line in out.split("\n"):
85 line = line.strip()
86 if line.startswith("Map size:"):
87 line = line.replace(" ", "")
88 (label, size) = line.split(":")
89 return int(size)
92 # Check the lmdb files created by join and ensure that the map size
93 # has been set to size.
95 # Currently this is all the *.ldb files in private/sam.ldb.d
97 def check_lmdb_environment_sizes(self, size):
98 directory = os.path.join(self.tempsambadir, "private", "sam.ldb.d")
99 for name in os.listdir(directory):
100 if name.endswith(".ldb"):
101 path = os.path.join(directory, name)
102 s = self.get_lmdb_environment_size(path)
103 if s != size:
104 self.fail("File %s, size=%d larger than %d" %
105 (name, s, size))
108 # Ensure that if --backend-store-size is not specified the default of
109 # 8Gb is used
110 def test_promote_dc_default(self):
111 (result, out, err) = self.join_member()
112 self.assertEqual(0, result)
113 (result, out, err) = self.promote(role="DC")
114 self.assertEqual(0, result)
115 self.check_lmdb_environment_sizes(8 * 1024 * 1024 * 1024)
116 self.assertFalse(self.is_rodc())
119 # Ensure that if --backend-store-size is not specified the default of
120 # 8Gb is used
121 def test_promote_rodc_default(self):
122 (result, out, err) = self.join_member()
123 self.assertEqual(0, result)
124 (result, out, err) = self.promote(role="RODC")
125 self.assertEqual(0, result)
126 self.check_lmdb_environment_sizes(8 * 1024 * 1024 * 1024)
127 self.assertTrue(self.is_rodc())
130 # Promote to a DC with a backend size of 96Mb
131 def test_promote_dc_96Mb(self):
132 (result, out, err) = self.join_member()
133 self.assertEqual(0, result)
134 (result, out, err) = self.promote(role="DC", size="96Mb")
135 self.assertEqual(0, result)
136 self.check_lmdb_environment_sizes(96 * 1024 * 1024)
137 self.assertFalse(self.is_rodc())
140 # Promote to an RODC with a backend size of 256Mb
141 def test_promote_rodc_256Mb(self):
142 (result, out, err) = self.join_member()
143 self.assertEqual(0, result)
144 (result, out, err) = self.promote(role="RODC", size="256Mb")
145 self.assertEqual(0, result)
146 self.check_lmdb_environment_sizes(256 * 1024 * 1024)
147 self.assertTrue(self.is_rodc())
149 def test_no_unit_suffix(self):
150 (result, out, err) = self.run_command(
151 'samba-tool domain dcpromo --backend-store-size "2"')
152 self.assertGreater(result, 0)
153 self.assertRegex(err,
154 r"--backend-store-size invalid suffix ''")
156 def test_invalid_unit_suffix(self):
157 (result, out, err) = self.run_command(
158 'samba-tool domain dcpromo --backend-store-size "2 cd"')
159 self.assertGreater(result, 0)
160 self.assertRegex(err,
161 r"--backend-store-size invalid suffix 'cd'")
163 def test_non_numeric(self):
164 (result, out, err) = self.run_command(
165 'samba-tool domain dcpromo --backend-store-size "two Gb"')
166 self.assertGreater(result, 0)
167 self.assertRegex(
168 err,
169 r"backend-store-size option requires a numeric value, with an"
170 " optional unit suffix")
172 def tearDown(self):
173 super().tearDown()
174 shutil.rmtree(self.tempsambadir)