PEP8: fix E302: expected 2 blank lines, found 1
[bbaumbach/samba-autobuild/.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
26
27 class LoadParmTest(TestCaseInTempDir):
28
29     def test_global_loadparm(self):
30         # create 2 different Loadparm objects (which are really the same
31         # object underneath)
32         lp1 = param.LoadParm()
33         lp2 = param.LoadParm()
34
35         # we can prove this by setting a value on lp1 and assert that the
36         # change is also reflected on lp2
37         lp1_realm = "JUST.A.TEST"
38         self.assertNotEqual(lp2.get('realm'), lp1_realm)
39         lp1.set('realm', lp1_realm)
40         self.assertEqual(lp1.get('realm'), lp1_realm)
41         self.assertEqual(lp2.get('realm'), lp1_realm)
42
43     def touch_temp_file(self, filename):
44         filepath = os.path.join(self.tempdir, filename)
45         open(filepath, 'a').close()
46         # delete the file once the test completes
47         self.addCleanup(os.remove, filepath)
48         return filepath
49
50     def test_non_global_loadparm(self):
51         # create a empty smb.conf file
52         smb_conf = self.touch_temp_file("smb.conf")
53
54         # we can create a non-global Loadparm that overrides the default
55         # behaviour and creates a separate underlying object
56         lp1 = param.LoadParm()
57         lp2 = param.LoadParm(filename_for_non_global_lp=smb_conf)
58
59         # setting a value for the global LP does not affect the non-global LP
60         lp1_realm = "JUST.A.TEST"
61         self.assertNotEqual(lp2.get('realm'), lp1_realm)
62         lp1.set('realm', lp1_realm)
63         self.assertEqual(lp1.get('realm'), lp1_realm)
64         self.assertNotEqual(lp2.get('realm'), lp1_realm)
65
66         # and vice versa
67         lp2_realm = "TEST.REALM.LP2"
68         lp2.set('realm', lp2_realm)
69         self.assertEqual(lp2.get('realm'), lp2_realm)
70         self.assertEqual(lp1.get('realm'), lp1_realm)
71
72     def test_non_global_loadparm_bad_path(self):
73         non_existent_file = os.path.join(self.tempdir, 'not-there')
74
75         # we can create a non-global Loadparm that overrides the default
76         # behaviour and creates a separate underlying object
77         self.assertRaises(ValueError,
78                           param.LoadParm,
79                           filename_for_non_global_lp=non_existent_file)
80
81         # still shouldn't be there
82         self.assertRaises(ValueError,
83                           param.LoadParm,
84                           non_existent_file)