tests: Add basic test for non-global LoadParm behaviour
[samba.git] / python / samba / tests / loadparm.py
1 # Unix SMB/CIFS implementation.
2 # Copyright (C) Andrew Bartlett <abartlet@samba.org>
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 import TestCaseInTempDir
19 from samba import param
20 import os
21
22 # the python bindings for LoadParm objects map (by default) to a single global
23 # object in the underlying C code. E.g. if we create 2 different LoadParm
24 # objects in python, really they're just the same object underneath.
25 class LoadParmTest(TestCaseInTempDir):
26
27     def test_global_loadparm(self):
28         # create 2 different Loadparm objects (which are really the same
29         # object underneath)
30         lp1 = param.LoadParm()
31         lp2 = param.LoadParm()
32
33         # we can prove this by setting a value on lp1 and assert that the
34         # change is also reflected on lp2
35         lp1_realm = "JUST.A.TEST"
36         self.assertNotEqual(lp2.get('realm'), lp1_realm)
37         lp1.set('realm', lp1_realm)
38         self.assertEqual(lp1.get('realm'), lp1_realm)
39         self.assertEqual(lp2.get('realm'), lp1_realm)
40
41     def touch_temp_file(self, filename):
42         filepath = os.path.join(self.tempdir, filename)
43         open(filepath, 'a').close()
44         # delete the file once the test completes
45         self.addCleanup(os.remove, filepath)
46         return filepath
47
48     def test_non_global_loadparm(self):
49         # create a empty smb.conf file
50         smb_conf = self.touch_temp_file("smb.conf")
51
52         # we can create a non-global Loadparm that overrides the default
53         # behaviour and creates a separate underlying object
54         lp1 = param.LoadParm()
55         lp2 = param.LoadParm(filename_for_non_global_lp=smb_conf)
56
57         # setting a value for the global LP does not affect the non-global LP
58         lp1_realm = "JUST.A.TEST"
59         self.assertNotEqual(lp2.get('realm'), lp1_realm)
60         lp1.set('realm', lp1_realm)
61         self.assertEqual(lp1.get('realm'), lp1_realm)
62         self.assertNotEqual(lp2.get('realm'), lp1_realm)
63
64         # and vice versa
65         lp2_realm = "TEST.REALM.LP2"
66         lp2.set('realm', lp2_realm)
67         self.assertEqual(lp2.get('realm'), lp2_realm)
68         self.assertEqual(lp1.get('realm'), lp1_realm)
69
70     def test_non_global_loadparm_bad_path(self):
71         non_existent_file = os.path.join(self.tempdir, 'not-there')
72
73         # we can create a non-global Loadparm that overrides the default
74         # behaviour and creates a separate underlying object
75         self.assertRaises(ValueError,
76                           param.LoadParm,
77                           filename_for_non_global_lp=non_existent_file)
78
79         # still shouldn't be there
80         self.assertRaises(ValueError,
81                           param.LoadParm,
82                           non_existent_file)