python: tests: update all super calls to python 3 style in tests
[samba.git] / python / samba / tests / samba_tool / drs_clone_dc_data_lmdb_size.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Catalyst IT Ltd. 2019
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 from samba.tests.samba_tool.base import SambaToolCmdTest
19 import os
20 import shutil
21
22
23 class DrsCloneDcDataLmdbSizeTestCase(SambaToolCmdTest):
24     """Test setting of the lmdb map size during drs clone-dc-data"""
25
26     def setUp(self):
27         super().setUp()
28         self.tempsambadir = os.path.join(self.tempdir, "samba")
29         os.mkdir(self.tempsambadir)
30
31     # clone a domain and set the lmdb map size to size
32     #
33     # returns the tuple (ret, stdout, stderr)
34     def clone(self, size=None):
35         command = (
36             "samba-tool " +
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 "
42         )
43         if size:
44             command += ("--backend-store-size=%s" % size)
45
46         return self.run_command(command)
47
48     #
49     # Get the lmdb map size for the specified command
50     #
51     # While there is a python lmdb package available we use the lmdb command
52     # line utilities to avoid introducing a dependency.
53     #
54     def get_lmdb_environment_size(self, path):
55         (result, out, err) = self.run_command("mdb_stat -ne %s" % path)
56         if result:
57             self.fail("Unable to run mdb_stat\n")
58         for line in out.split("\n"):
59             line = line.strip()
60             if line.startswith("Map size:"):
61                 line = line.replace(" ", "")
62                 (label, size) = line.split(":")
63                 return int(size)
64
65     #
66     # Check the lmdb files created by provision and ensure that the map size
67     # has been set to size.
68     #
69     # Currently this is all the *.ldb files in private/sam.ldb.d
70     #
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)
77                 if s != size:
78                     self.fail("File %s, size=%d larger than %d" %
79                               (name, s, size))
80
81     #
82     # Ensure that if --backend-store-size is not specified the default of
83     # 8Gb is used
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)
88
89     def test_64Mb(self):
90         (result, out, err) = self.clone("64Mb")
91         self.assertEqual(0, result)
92         self.check_lmdb_environment_sizes(64 * 1024 * 1024)
93
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)
98         self.assertRegex(err,
99                          r"--backend-store-size invalid suffix ''")
100
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'")
107
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)
112         self.assertRegex(
113             err,
114             r"backend-store-size option requires a numeric value, with an"
115             " optional unit suffix")
116
117     def tearDown(self):
118         super().tearDown()
119         shutil.rmtree(self.tempsambadir)