1 |
commit: 8a0a42ceb530b277e17220614f8c34bc8d0e503c |
2 |
Author: David Palao <david.palao <AT> gmail <DOT> com> |
3 |
AuthorDate: Tue May 31 15:30:48 2022 +0000 |
4 |
Commit: Mike Gilbert <floppym <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Jun 7 23:47:55 2022 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=8a0a42ce |
7 |
|
8 |
test(MtimeDB): added some unit tests for __write_to_disk and for _load |
9 |
|
10 |
Signed-off-by: David Palao <david.palao <AT> gmail.com> |
11 |
Signed-off-by: Mike Gilbert <floppym <AT> gentoo.org> |
12 |
|
13 |
lib/portage/tests/util/test_mtimedb.py | 122 +++++++++++++++++++++++++++------ |
14 |
lib/portage/util/mtimedb.py | 1 + |
15 |
2 files changed, 102 insertions(+), 21 deletions(-) |
16 |
|
17 |
diff --git a/lib/portage/tests/util/test_mtimedb.py b/lib/portage/tests/util/test_mtimedb.py |
18 |
index ecc0e7135..09d72049f 100644 |
19 |
--- a/lib/portage/tests/util/test_mtimedb.py |
20 |
+++ b/lib/portage/tests/util/test_mtimedb.py |
21 |
@@ -2,31 +2,28 @@ |
22 |
# Distributed under the terms of the GNU General Public License v2 |
23 |
|
24 |
from unittest.mock import patch, mock_open |
25 |
+import json |
26 |
|
27 |
from portage.tests import TestCase |
28 |
|
29 |
+import portage |
30 |
+from portage.data import portage_gid, uid |
31 |
from portage.util.mtimedb import MtimeDB, _MTIMEDBKEYS |
32 |
from portage.exception import DigestException |
33 |
|
34 |
|
35 |
-# Some random data for the fixtures: |
36 |
+# Some data for the fixtures: |
37 |
|
38 |
_ONE_RESUME_LIST_JSON = b"""{ |
39 |
"info": { |
40 |
- "/tmp/stage1root/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711785090, |
41 |
- "/tmp/stage1root/usr/share/gcc-data/x86_64-pc-linux-gnu/9.3.0/info": 1711785090, |
42 |
- "/tmp/stage1root/usr/share/info": 1611785090, |
43 |
- "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711787325, |
44 |
+ "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": 1711787325, |
45 |
"/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": 1735158257, |
46 |
- "/usr/share/gcc-data/x86_64-pc-linux-gnu/9.3.0/info": 1711787325, |
47 |
"/usr/share/info": 1650633847 |
48 |
}, |
49 |
"ldpath": { |
50 |
"/lib": 1748456830, |
51 |
"/lib64": 1750523381, |
52 |
"/usr/lib": 1750461195, |
53 |
- "/usr/lib/llvm/11/lib64": 1723048948, |
54 |
- "/usr/lib/llvm/12/lib64": 1730499781, |
55 |
"/usr/lib/llvm/13/lib64": 1747003135, |
56 |
"/usr/lib/rust/lib": 1750461173, |
57 |
"/usr/lib64": 1750881821, |
58 |
@@ -66,12 +63,8 @@ _ONE_RESUME_LIST_JSON = b"""{ |
59 |
}, |
60 |
"starttime": 0, |
61 |
"updates": { |
62 |
- "/var/db/repos/gentoo/profiles/updates/1Q-2021": 1739992409, |
63 |
"/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791, |
64 |
- "/var/db/repos/gentoo/profiles/updates/2Q-2021": 1724404379, |
65 |
"/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209, |
66 |
- "/var/db/repos/gentoo/profiles/updates/3Q-2021": 1741119203, |
67 |
- "/var/db/repos/gentoo/profiles/updates/4Q-2020": 1709167362, |
68 |
"/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797 |
69 |
}, |
70 |
"version": "3.0.30" |
71 |
@@ -85,8 +78,6 @@ _PARTIAL_FILE_JSON = b"""{ |
72 |
"/lib": 1748456830, |
73 |
"/lib64": 1750523381, |
74 |
"/usr/lib": 1750461195, |
75 |
- "/usr/lib/llvm/11/lib64": 1723048948, |
76 |
- "/usr/lib/llvm/12/lib64": 1730499781, |
77 |
"/usr/lib/llvm/13/lib64": 1747003135, |
78 |
"/usr/lib/rust/lib": 1750461173, |
79 |
"/usr/lib64": 1750881821, |
80 |
@@ -130,7 +121,7 @@ _PARTIAL_FILE_JSON = b"""{ |
81 |
|
82 |
_TWO_RESUME_LISTS_JSON = b"""{ |
83 |
"info": { |
84 |
- "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711787325, |
85 |
+ "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": 1711787325, |
86 |
"/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": 1735158257, |
87 |
"/usr/share/info": 1650633847 |
88 |
}, |
89 |
@@ -199,12 +190,8 @@ _TWO_RESUME_LISTS_JSON = b"""{ |
90 |
}, |
91 |
"starttime": 0, |
92 |
"updates": { |
93 |
- "/var/db/repos/gentoo/profiles/updates/1Q-2021": 1739992409, |
94 |
"/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791, |
95 |
- "/var/db/repos/gentoo/profiles/updates/2Q-2021": 1724404379, |
96 |
"/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209, |
97 |
- "/var/db/repos/gentoo/profiles/updates/3Q-2021": 1741119203, |
98 |
- "/var/db/repos/gentoo/profiles/updates/4Q-2020": 1709167362, |
99 |
"/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797 |
100 |
}, |
101 |
"version": "3.0.30" |
102 |
@@ -215,7 +202,7 @@ _TWO_RESUME_LISTS_JSON = b"""{ |
103 |
class MtimeDBTestCase(TestCase): |
104 |
text = b"Unit tests for MtimeDB" |
105 |
|
106 |
- def test_instances_are_created_with_only_expected_keys(self): |
107 |
+ def test_instances_created_with_only_expected_keys(self): |
108 |
all_fixtures = ( |
109 |
_ONE_RESUME_LIST_JSON, |
110 |
_EMPTY_FILE, |
111 |
@@ -229,7 +216,7 @@ class MtimeDBTestCase(TestCase): |
112 |
mtimedb = MtimeDB("/path/to/mtimedb") |
113 |
self.assertLessEqual(set(mtimedb.keys()), _MTIMEDBKEYS) |
114 |
|
115 |
- def test_instances_have_default_values(self): |
116 |
+ def test_default_values(self): |
117 |
with patch("portage.util.mtimedb.open", |
118 |
mock_open(read_data=_EMPTY_FILE)): |
119 |
mtimedb = MtimeDB("/some/path/mtimedb") |
120 |
@@ -246,6 +233,78 @@ class MtimeDBTestCase(TestCase): |
121 |
self.assertEqual(dict(mtimedb), dict(mtimedb._clean_data)) |
122 |
self.assertIsNot(mtimedb, mtimedb._clean_data) |
123 |
|
124 |
+ def test_load_data_called_at_instance_creation_time(self): |
125 |
+ with patch("portage.util.mtimedb.open", |
126 |
+ mock_open(read_data=_ONE_RESUME_LIST_JSON)): |
127 |
+ mtimedb = MtimeDB("/some/path/mtimedb") |
128 |
+ self.assertEqual( |
129 |
+ mtimedb["info"], |
130 |
+ { |
131 |
+ "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": ( |
132 |
+ 1711787325), |
133 |
+ "/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": ( |
134 |
+ 1735158257), |
135 |
+ "/usr/share/info": 1650633847 |
136 |
+ } |
137 |
+ ) |
138 |
+ self.assertEqual( |
139 |
+ mtimedb["ldpath"], |
140 |
+ { |
141 |
+ "/lib": 1748456830, |
142 |
+ "/lib64": 1750523381, |
143 |
+ "/usr/lib": 1750461195, |
144 |
+ "/usr/lib/llvm/13/lib64": 1747003135, |
145 |
+ "/usr/lib/rust/lib": 1750461173, |
146 |
+ "/usr/lib64": 1750881821, |
147 |
+ "/usr/local/lib": 1711784303, |
148 |
+ "/usr/local/lib64": 1711784303 |
149 |
+ } |
150 |
+ ) |
151 |
+ self.assertEqual( |
152 |
+ mtimedb["resume"], |
153 |
+ { |
154 |
+ "favorites": [ |
155 |
+ "@world" |
156 |
+ ], |
157 |
+ "mergelist": [ |
158 |
+ [ |
159 |
+ "ebuild", |
160 |
+ "/", |
161 |
+ "some-cat/some-package-1.2.3-r4", |
162 |
+ "merge" |
163 |
+ ], |
164 |
+ [ |
165 |
+ "ebuild", |
166 |
+ "/", |
167 |
+ "another-cat/another-package-4.3.2-r1", |
168 |
+ "merge" |
169 |
+ ] |
170 |
+ ], |
171 |
+ "myopts": { |
172 |
+ "--buildpkg": True, |
173 |
+ "--deep": True, |
174 |
+ "--getbinpkg": True, |
175 |
+ "--keep-going": True, |
176 |
+ "--newuse": True, |
177 |
+ "--quiet": True, |
178 |
+ "--regex-search-auto": "y", |
179 |
+ "--update": True, |
180 |
+ "--usepkg": True, |
181 |
+ "--verbose": True |
182 |
+ } |
183 |
+ } |
184 |
+ ) |
185 |
+ self.assertEqual(mtimedb["starttime"], 0) |
186 |
+ self.assertEqual( |
187 |
+ mtimedb["updates"], |
188 |
+ { |
189 |
+ "/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791, |
190 |
+ "/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209, |
191 |
+ "/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797 |
192 |
+ } |
193 |
+ ) |
194 |
+ self.assertEqual(mtimedb["version"], "3.0.30") |
195 |
+ |
196 |
@patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk") |
197 |
def test_commit_writes_to_disk_if_needed_and_possible(self, pwrite2disk): |
198 |
with patch("portage.util.mtimedb.open", |
199 |
@@ -296,3 +355,24 @@ class MtimeDBTestCase(TestCase): |
200 |
mtimedb.make_readonly() |
201 |
self.assertTrue(mtimedb.is_readonly) |
202 |
self.assertIs(mtimedb.filename, None) |
203 |
+ |
204 |
+ @patch("portage.util.mtimedb.apply_secpass_permissions") |
205 |
+ @patch("portage.util.mtimedb.atomic_ofstream") |
206 |
+ def test_write_to_disk(self, matomic_ofstream, mapply_perms): |
207 |
+ with patch("portage.util.mtimedb.open", |
208 |
+ mock_open(read_data=_ONE_RESUME_LIST_JSON)): |
209 |
+ mtimedb = MtimeDB("/some/path/mtimedb") |
210 |
+ d = {"z": "zome", "a": "AAA"} |
211 |
+ encoding = portage._encodings["repo.content"] |
212 |
+ # I'm interested here in unit testing, but the method is private |
213 |
+ # and shouldn't be called directly from outside, obviously: |
214 |
+ mtimedb._MtimeDB__write_to_disk(d) |
215 |
+ self.assertEqual(d["version"], str(portage.VERSION)) |
216 |
+ matomic_ofstream.return_value.write.assert_called_once_with( |
217 |
+ json.dumps(d, **mtimedb._json_write_opts).encode(encoding) |
218 |
+ ) |
219 |
+ mapply_perms.assert_called_once_with( |
220 |
+ mtimedb.filename, uid=uid, gid=portage_gid, mode=0o644 |
221 |
+ ) |
222 |
+ self.assertEqual(d, mtimedb._clean_data) |
223 |
+ self.assertIsNot(d, mtimedb._clean_data) |
224 |
|
225 |
diff --git a/lib/portage/util/mtimedb.py b/lib/portage/util/mtimedb.py |
226 |
index 95da2235f..9884746f3 100644 |
227 |
--- a/lib/portage/util/mtimedb.py |
228 |
+++ b/lib/portage/util/mtimedb.py |
229 |
@@ -133,6 +133,7 @@ class MtimeDB(dict): |
230 |
self.__write_to_disk(d) |
231 |
|
232 |
def __write_to_disk(self, d): |
233 |
+ """Private method used by the ``commit`` method.""" |
234 |
d["version"] = str(portage.VERSION) |
235 |
try: |
236 |
f = atomic_ofstream(self.filename, mode="wb") |