lib/smbconf: add set_parameter method to SMBConf
[Samba.git] / python / samba / tests / smbconf.py
blob749a6fd398578c04c86ae887226455bb3cf6cd14
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
3 # Copyright (C) John Mulligan <phlogistonjohn@asynchrono.us> 2022
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 """
20 Tests for samba.smbconf module
21 """
23 from samba.samba3 import param as s3param
24 import samba.tests
27 class SMBConfTests(samba.tests.TestCase):
28 _smbconf = None
29 _s3smbconf = None
31 @property
32 def smbconf(self):
33 """Property to access module under test without
34 importing it at test module load-time.
35 """
36 if self._smbconf is not None:
37 return self._smbconf
39 import samba.smbconf
41 self._smbconf = samba.smbconf
42 return self._smbconf
44 @property
45 def s3smbconf(self):
46 if self._s3smbconf is not None:
47 return self._s3smbconf
49 import samba.samba3.smbconf
51 self._s3smbconf = samba.samba3.smbconf
52 return self._s3smbconf
54 @property
55 def example_conf_default(self):
56 return "./testdata/samba3/smb.conf"
58 def setUp(self):
59 super().setUp()
60 # fetch the configuration in the same style as other test suites
61 self.lp_ctx = samba.tests.env_loadparm()
62 # apply the configuration to the samba3 configuration
63 # (because there are two... and they're independent!)
64 # this is needed to make use of the registry
65 s3_lp = s3param.get_context()
66 s3_lp.load(self.lp_ctx.configfile)
68 def test_uninitalized_smbconf(self):
69 sconf = self.smbconf.SMBConf()
70 self.assertRaises(RuntimeError, sconf.requires_messaging)
71 self.assertRaises(RuntimeError, sconf.is_writeable)
72 self.assertRaises(RuntimeError, sconf.share_names)
73 self.assertRaises(RuntimeError, sconf.get_share, "foo")
75 def test_txt_backend_properties(self):
76 sconf = self.smbconf.init_txt(self.example_conf_default)
77 self.assertFalse(sconf.requires_messaging())
78 self.assertFalse(sconf.is_writeable())
80 def test_share_names(self):
81 sconf = self.smbconf.init_txt(self.example_conf_default)
82 names = sconf.share_names()
83 self.assertEqual(names, ["global", "cd1", "cd2", "media", "tmp"])
85 def test_get_share_cd1(self):
86 sconf = self.smbconf.init_txt(self.example_conf_default)
87 s1 = sconf.get_share("cd1")
88 self.assertEqual(s1, ("cd1", [("path", "/mnt/cd1"), ("public", "yes")]))
90 def test_get_share_cd2(self):
91 sconf = self.smbconf.init_txt(self.example_conf_default)
92 s1 = sconf.get_share("cd2")
93 self.assertEqual(s1, ("cd2", [("path", "/mnt/cd2"), ("public", "yes")]))
95 def test_get_config(self):
96 sconf = self.smbconf.init_txt(self.example_conf_default)
97 services = sconf.get_config()
98 self.assertEqual(len(services), 5)
99 self.assertEqual(
100 services[0],
102 "global",
104 ("workgroup", "SAMBA"),
105 ("security", "user"),
107 "passdb backend",
108 "smbpasswd:../testdata/samba3/smbpasswd "
109 "tdbsam:../testdata/samba3/passdb.tdb ldapsam:tdb://samba3.ldb",
111 ("debug level", "5"),
112 ("netbios name", "BEDWYR"),
116 self.assertEqual(
117 services[1], ("cd1", [("path", "/mnt/cd1"), ("public", "yes")])
120 def test_init_reg(self):
121 sconf = self.s3smbconf.init_reg(None)
122 self.assertTrue(sconf.is_writeable())
124 def test_init_str_reg(self):
125 sconf = self.s3smbconf.init("registry:")
126 self.assertTrue(sconf.is_writeable())
128 def test_init_str_file(self):
129 sconf = self.s3smbconf.init(f"file:{self.example_conf_default}")
130 self.assertFalse(sconf.is_writeable())
132 def test_create_share(self):
133 sconf = self.s3smbconf.init_reg(None)
134 sconf.drop()
135 sconf.create_share("alice")
136 sconf.create_share("bob")
137 names = sconf.share_names()
138 self.assertEqual(names, ["alice", "bob"])
139 self.assertRaises(
140 self.smbconf.SMBConfError, sconf.create_share, "alice"
143 def test_create_share(self):
144 sconf = self.s3smbconf.init_reg(None)
145 sconf.drop()
146 sconf.create_share("alice")
147 sconf.drop()
148 names = sconf.share_names()
149 self.assertEqual(names, [])
151 def test_set_parameter(self):
152 sconf = self.s3smbconf.init_reg(None)
153 sconf.drop()
154 sconf.create_share("foobar")
155 sconf.set_parameter("foobar", "path", "/mnt/foobar")
156 sconf.set_parameter("foobar", "browseable", "no")
158 s1 = sconf.get_share("foobar")
159 self.assertEqual(
160 s1, ("foobar", [("path", "/mnt/foobar"), ("browseable", "no")])
164 if __name__ == "__main__":
165 import unittest
167 unittest.main()