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
23 class DrsCloneDcDataLmdbSizeTestCase(SambaToolCmdTest):
24 """Test setting of the lmdb map size during drs clone-dc-data"""
28 self.tempsambadir = os.path.join(self.tempdir, "samba")
29 os.mkdir(self.tempsambadir)
31 # clone a domain and set the lmdb map size to size
33 # returns the tuple (ret, stdout, stderr)
34 def clone(self, size=None):
37 "drs clone-dc-database " +
38 os.environ["REALM"] + " " +
39 ("-U%s%%%s " % (os.environ["USERNAME"], os.environ["PASSWORD"])) +
40 ("--targetdir=%s " % self.tempsambadir) +
41 "--backend-store=mdb "
44 command += ("--backend-store-size=%s" % size)
46 return self.run_command(command)
49 # Get the lmdb map size for the specified command
51 # While there is a python lmdb package available we use the lmdb command
52 # line utilities to avoid introducing a dependency.
54 def get_lmdb_environment_size(self, path):
55 (result, out, err) = self.run_command("mdb_stat -ne %s" % path)
57 self.fail("Unable to run mdb_stat\n")
58 for line in out.split("\n"):
60 if line.startswith("Map size:"):
61 line = line.replace(" ", "")
62 (label, size) = line.split(":")
66 # Check the lmdb files created by provision and ensure that the map size
67 # has been set to size.
69 # Currently this is all the *.ldb files in private/sam.ldb.d
71 def check_lmdb_environment_sizes(self, size):
72 directory = os.path.join(self.tempsambadir, "private", "sam.ldb.d")
73 for name in os.listdir(directory):
74 if name.endswith(".ldb"):
75 path = os.path.join(directory, name)
76 s = self.get_lmdb_environment_size(path)
78 self.fail("File %s, size=%d larger than %d" %
82 # Ensure that if --backend-store-size is not specified the default of
84 def test_default(self):
85 (result, out, err) = self.clone()
86 self.assertEqual(0, result)
87 self.check_lmdb_environment_sizes(8 * 1024 * 1024 * 1024)
90 (result, out, err) = self.clone("64Mb")
91 self.assertEqual(0, result)
92 self.check_lmdb_environment_sizes(64 * 1024 * 1024)
94 def test_no_unit_suffix(self):
95 (result, out, err) = self.run_command(
96 'samba-tool drs clone-dc-database --backend-store-size "2"')
97 self.assertGreater(result, 0)
99 r"--backend-store-size invalid suffix ''")
101 def test_invalid_unit_suffix(self):
102 (result, out, err) = self.run_command(
103 'samba-tool drs clone-dc-database --backend-store-size "2 cd"')
104 self.assertGreater(result, 0)
105 self.assertRegex(err,
106 r"--backend-store-size invalid suffix 'cd'")
108 def test_non_numeric(self):
109 (result, out, err) = self.run_command(
110 'samba-tool drs clone-dc-database --backend-store-size "two Gb"')
111 self.assertGreater(result, 0)
114 r"backend-store-size option requires a numeric value, with an"
115 " optional unit suffix")
119 shutil.rmtree(self.tempsambadir)