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 ProvisionLmdbSizeTestCase(SambaToolCmdTest
):
24 """Test setting of the lmdb map size during provision"""
28 self
.tempsambadir
= os
.path
.join(self
.tempdir
, "samba")
29 os
.mkdir(self
.tempsambadir
)
31 # provision a domain and set the lmdb map size to size
33 # returns the tuple (ret, stdout, stderr)
34 def provision(self
, size
=None):
38 "--realm=foo.example.com " +
40 ("--targetdir=%s " % self
.tempsambadir
) +
41 "--backend-store=mdb " +
45 command
+= ("--backend-store-size=%s" % size
)
47 return self
.run_command(command
)
50 # Get the lmdb map size for the specified command
52 # While there is a python lmdb package available we use the lmdb command
53 # line utilities to avoid introducing a dependency.
55 def get_lmdb_environment_size(self
, path
):
56 (result
, out
, err
) = self
.run_command("mdb_stat -ne %s" % path
)
58 self
.fail("Unable to run mdb_stat\n")
59 for line
in out
.split("\n"):
61 if line
.startswith("Map size:"):
62 line
= line
.replace(" ", "")
63 (label
, size
) = line
.split(":")
67 # Check the lmdb files created by provision and ensure that the map size
68 # has been set to size.
70 # Currently this is all the *.ldb files in private/sam.ldb.d
72 def check_lmdb_environment_sizes(self
, size
):
73 directory
= os
.path
.join(self
.tempsambadir
, "private", "sam.ldb.d")
74 for name
in os
.listdir(directory
):
75 if name
.endswith(".ldb"):
76 path
= os
.path
.join(directory
, name
)
77 s
= self
.get_lmdb_environment_size(path
)
79 self
.fail("File %s, size=%d larger than %d" %
83 # Ensure that if --backend-store-size is not specified the default of
85 def test_default(self
):
86 (result
, out
, err
) = self
.provision()
87 self
.assertEqual(0, result
)
88 self
.check_lmdb_environment_sizes(8 * 1024 * 1024 * 1024)
91 (result
, out
, err
) = self
.provision("64Mb")
92 self
.assertEqual(0, result
)
93 self
.check_lmdb_environment_sizes(64 * 1024 * 1024)
96 (result
, out
, err
) = self
.provision("1Gb")
97 self
.assertEqual(0, result
)
98 self
.check_lmdb_environment_sizes(1 * 1024 * 1024 * 1024)
100 # 128Mb specified in bytes.
102 def test_134217728b(self
):
103 (result
, out
, err
) = self
.provision("134217728b")
104 self
.assertEqual(0, result
)
105 self
.check_lmdb_environment_sizes(134217728)
107 def test_no_unit_suffix(self
):
108 (result
, out
, err
) = self
.run_command(
109 'samba-tool domain provision --backend-store-size "2"')
110 self
.assertGreater(result
, 0)
111 self
.assertRegex(err
,
112 r
"--backend-store-size invalid suffix ''")
114 def test_invalid_unit_suffix(self
):
115 (result
, out
, err
) = self
.run_command(
116 'samba-tool domain provision --backend-store-size "2 cd"')
117 self
.assertGreater(result
, 0)
118 self
.assertRegex(err
,
119 r
"--backend-store-size invalid suffix 'cd'")
121 def test_non_numeric(self
):
122 (result
, out
, err
) = self
.run_command(
123 'samba-tool domain provision --backend-store-size "two Gb"')
124 self
.assertGreater(result
, 0)
127 r
"backend-store-size option requires a numeric value, with an"
128 " optional unit suffix")
132 shutil
.rmtree(self
.tempsambadir
)