ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / tests / samdb_api.py
blob5a720aa2399266180bd21936bb8557be09040e0e
1 # Tests for the samba samdb api
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
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 from samba.tests import TestCaseInTempDir
20 from samba.samdb import SamDB
21 from ldb import LdbError, ERR_OPERATIONS_ERROR
22 import errno
25 class SamDBApiTestCase(TestCaseInTempDir):
27 def tearDown(self):
28 self.rm_files("test.db", "existing.db", allow_missing=True)
30 super().tearDown()
32 # Attempt to open and existing non tdb file as a tdb file.
33 # Don't create new db is set, the default
35 # Should fail to open
36 # And the existing file should be left intact.
38 def test_dont_create_db_existing_non_tdb_file(self):
39 existing_name = self.tempdir + "/existing.db"
40 existing = open(existing_name, "w")
41 existing.write("This is not a tdb file!!!!!!\n")
42 existing.close()
44 try:
45 SamDB(url="tdb://" + existing_name)
46 self.fail("Exception not thrown ")
47 except LdbError as e:
48 (err, _) = e.args
49 self.assertEqual(err, ERR_OPERATIONS_ERROR)
51 existing = open(existing_name, "r")
52 contents = existing.readline()
53 self.assertEqual("This is not a tdb file!!!!!!\n", contents)
55 # Attempt to open and existing non tdb file as a tdb file.
56 # Don't create new db is cleared
58 # Should open as a tdb file
59 # And the existing file should be over written
61 def test_create_db_existing_file_non_tdb_file(self):
62 existing_name = self.tempdir + "/existing.db"
63 existing = open(existing_name, "wb")
64 existing.write(b"This is not a tdb file!!!!!!")
65 existing.close()
67 SamDB(url="tdb://" + existing_name, flags=0)
69 existing = open(existing_name, "rb")
70 contents = existing.readline()
71 self.assertEqual(b"TDB file\n", contents)
74 # Attempt to open an existing tdb file as a tdb file.
75 # Don't create new db is set, the default
77 # Should open successfully
78 # And the existing file should be left intact.
80 def test_dont_create_db_existing_tdb_file(self):
81 existing_name = self.tempdir + "/existing.db"
82 initial = SamDB(url="tdb://" + existing_name, flags=0)
83 dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
84 initial.add({
85 "dn": dn,
86 "cn": "test_dont_create_db_existing_tdb_file"
89 cn = initial.searchone("cn", dn)
90 self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
92 second = SamDB(url="tdb://" + existing_name)
93 cn = second.searchone("cn", dn)
94 self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
97 # Attempt to open an existing tdb file as a tdb file.
98 # Don't create new db is explicitly cleared
100 # Should open successfully
101 # And the existing file should be left intact.
103 def test_create_db_existing_file_tdb_file(self):
104 existing_name = self.tempdir + "/existing.db"
105 initial = SamDB(url="tdb://" + existing_name, flags=0)
106 dn = "dn=,cn=test_dont_create_db_existing_tdb_file"
107 initial.add({
108 "dn": dn,
109 "cn": "test_dont_create_db_existing_tdb_file"
112 cn = initial.searchone("cn", dn)
113 self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
115 second = SamDB(url="tdb://" + existing_name, flags=0)
116 cn = second.searchone("cn", dn)
117 self.assertEqual(b"test_dont_create_db_existing_tdb_file", cn)
119 # Open a non existent TDB file.
120 # Don't create new db is set, the default
122 # Should fail
123 # and the database file should not be created
124 def test_dont_create_db_new_file(self):
125 try:
126 SamDB(url="tdb://" + self.tempdir + "/test.db")
127 self.fail("Exception not thrown ")
128 except LdbError as e1:
129 (err, _) = e1.args
130 self.assertEqual(err, ERR_OPERATIONS_ERROR)
132 try:
133 file = open(self.tempdir + "/test.db", "r")
134 self.fail("New database file created")
135 except IOError as e:
136 self.assertEqual(e.errno, errno.ENOENT)
138 # Open a SamDB with the don't create new DB flag cleared.
139 # The underlying database file does not exist.
141 # Should successful open the SamDB creating a new database file.
144 def test_create_db_new_file(self):
145 SamDB(url="tdb://" + self.tempdir + "/test.db", flags=0)
146 existing = open(self.tempdir + "/test.db", mode="rb")
147 contents = existing.readline()
148 self.assertEqual(b"TDB file\n", contents)