aboutsummaryrefslogtreecommitdiffstats
path: root/lib/portage/tests/util/test_mtimedb.py
blob: 09d72049f83205cd9e9e6d89a0ecf8eb639d64df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Copyright 2022 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from unittest.mock import patch, mock_open
import json

from portage.tests import TestCase

import portage
from portage.data import portage_gid, uid
from portage.util.mtimedb import MtimeDB, _MTIMEDBKEYS
from portage.exception import DigestException


# Some data for the fixtures:

_ONE_RESUME_LIST_JSON = b"""{
	"info": {
		"/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": 1711787325,
		"/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": 1735158257,
		"/usr/share/info": 1650633847
	},
	"ldpath": {
		"/lib": 1748456830,
		"/lib64": 1750523381,
		"/usr/lib": 1750461195,
		"/usr/lib/llvm/13/lib64": 1747003135,
		"/usr/lib/rust/lib": 1750461173,
		"/usr/lib64": 1750881821,
		"/usr/local/lib": 1711784303,
		"/usr/local/lib64": 1711784303
	},
	"resume": {
		"favorites": [
			"@world"
		],
		"mergelist": [
			[
				"ebuild",
				"/",
				"some-cat/some-package-1.2.3-r4",
				"merge"
			],
			[
				"ebuild",
				"/",
				"another-cat/another-package-4.3.2-r1",
				"merge"
			]
		],
		"myopts": {
			"--buildpkg": true,
			"--deep": true,
			"--getbinpkg": true,
			"--keep-going": true,
			"--newuse": true,
			"--quiet": true,
			"--regex-search-auto": "y",
			"--update": true,
			"--usepkg": true,
			"--verbose": true
		}
	},
	"starttime": 0,
	"updates": {
		"/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791,
		"/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209,
		"/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797
	},
	"version": "3.0.30"
}
"""

_EMPTY_FILE = b""

_PARTIAL_FILE_JSON = b"""{
	"ldpath": {
		"/lib": 1748456830,
		"/lib64": 1750523381,
		"/usr/lib": 1750461195,
		"/usr/lib/llvm/13/lib64": 1747003135,
		"/usr/lib/rust/lib": 1750461173,
		"/usr/lib64": 1750881821,
		"/usr/local/lib": 1711784303,
		"/usr/local/lib64": 1711784303
	},
	"resume": {
		"favorites": [
			"@world"
		],
		"mergelist": [
			[
				"ebuild",
				"/",
				"some-cat/some-package-1.2.3-r4",
				"merge"
			],
			[
				"ebuild",
				"/",
				"another-cat/another-package-4.3.2-r1",
				"merge"
			]
		],
		"myopts": {
			"--buildpkg": true,
			"--deep": true,
			"--getbinpkg": true,
			"--keep-going": true,
			"--newuse": true,
			"--quiet": true,
			"--regex-search-auto": "y",
			"--update": true,
			"--usepkg": true,
			"--verbose": true
		}
	},
	"starttime": 0,
}
"""

_TWO_RESUME_LISTS_JSON = b"""{
	"info": {
		"/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": 1711787325,
		"/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": 1735158257,
		"/usr/share/info": 1650633847
	},
	"ldpath": {
		"/lib": 1748456830,
		"/lib64": 1750523381,
		"/usr/lib": 1750461195,
		"/usr/lib/llvm/13/lib64": 1747003135,
		"/usr/lib/rust/lib": 1750461173,
		"/usr/lib64": 1750881821,
	},
	"resume": {
		"favorites": [
			"@world"
		],
		"mergelist": [
			[
				"ebuild",
				"/",
				"some-cat/some-package-1.2.3-r4",
				"merge"
			],
			[
				"ebuild",
				"/",
				"another-cat/another-package-4.3.2-r1",
				"merge"
			]
		],
		"myopts": {
			"--buildpkg": true,
			"--deep": true,
			"--getbinpkg": true,
			"--keep-going": true,
			"--newuse": true,
			"--quiet": true,
			"--regex-search-auto": "y",
			"--update": true,
			"--usepkg": true,
			"--verbose": true
		}
	},
	"resume_backup": {
		"favorites": [
			"my-cat/cool"
		],
		"mergelist": [
			[
				"ebuild",
				"/",
				"sys-apps/cool-dep-2.2.2",
				"merge"
			],
			[
				"binary",
				"/",
				"my-cat/cool-1.5.2",
				"merge"
			]
		],
		"myopts": {
			"--getbinpkg": true,
			"--regex-search-auto": "y",
			"--usepkg": true,
		}
	},
	"starttime": 0,
	"updates": {
		"/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791,
		"/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209,
		"/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797
	},
	"version": "3.0.30"
}
"""


class MtimeDBTestCase(TestCase):
    text = b"Unit tests for MtimeDB"

    def test_instances_created_with_only_expected_keys(self):
        all_fixtures = (
            _ONE_RESUME_LIST_JSON,
            _EMPTY_FILE,
            _PARTIAL_FILE_JSON,
            _TWO_RESUME_LISTS_JSON,
        )
        for contents in all_fixtures:
            with patch(
                "portage.util.mtimedb.open", mock_open(read_data=contents)
            ):
                mtimedb = MtimeDB("/path/to/mtimedb")
            self.assertLessEqual(set(mtimedb.keys()), _MTIMEDBKEYS)

    def test_default_values(self):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_EMPTY_FILE)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        self.assertEqual(mtimedb["starttime"], 0)
        self.assertEqual(mtimedb["version"], "")
        self.assertEqual(mtimedb["info"], {})
        self.assertEqual(mtimedb["ldpath"], {})
        self.assertEqual(mtimedb["updates"], {})

    def test_instances_keep_a_deepcopy_of_clean_data(self):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_ONE_RESUME_LIST_JSON)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        self.assertEqual(dict(mtimedb), dict(mtimedb._clean_data))
        self.assertIsNot(mtimedb, mtimedb._clean_data)

    def test_load_data_called_at_instance_creation_time(self):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_ONE_RESUME_LIST_JSON)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        self.assertEqual(
            mtimedb["info"],
            {
		"/usr/share/binutils-data/x86_64-pc-linux-gnu/2.37/info": (
                    1711787325),
		"/usr/share/gcc-data/x86_64-pc-linux-gnu/11.2.0/info": (
                    1735158257),
		"/usr/share/info": 1650633847
	    }
        )
        self.assertEqual(
            mtimedb["ldpath"],
            {
		"/lib": 1748456830,
		"/lib64": 1750523381,
		"/usr/lib": 1750461195,
		"/usr/lib/llvm/13/lib64": 1747003135,
		"/usr/lib/rust/lib": 1750461173,
		"/usr/lib64": 1750881821,
		"/usr/local/lib": 1711784303,
		"/usr/local/lib64": 1711784303
	    }
        )
        self.assertEqual(
            mtimedb["resume"],
            {
		"favorites": [
			"@world"
		],
		"mergelist": [
			[
				"ebuild",
				"/",
				"some-cat/some-package-1.2.3-r4",
				"merge"
			],
			[
				"ebuild",
				"/",
				"another-cat/another-package-4.3.2-r1",
				"merge"
			]
		],
		"myopts": {
			"--buildpkg": True,
			"--deep": True,
			"--getbinpkg": True,
			"--keep-going": True,
			"--newuse": True,
			"--quiet": True,
			"--regex-search-auto": "y",
			"--update": True,
			"--usepkg": True,
			"--verbose": True
		}
	    }
        )
        self.assertEqual(mtimedb["starttime"], 0)
        self.assertEqual(
            mtimedb["updates"],
            {
		"/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791,
		"/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209,
		"/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797
	    }
        )
        self.assertEqual(mtimedb["version"], "3.0.30")

    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
    def test_commit_writes_to_disk_if_needed_and_possible(self, pwrite2disk):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_EMPTY_FILE)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        mtimedb.commit()
        pwrite2disk.assert_not_called()
        mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
        d = {}
        d.update(mtimedb)
        mtimedb.commit()
        pwrite2disk.assert_called_once_with(d)

    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
    def test_commit_does_not_write_to_disk_if_no_file(self, pwrite2disk):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_EMPTY_FILE)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        mtimedb["updates"]["/long/path/1Q-2021"] = 1739992409
        mtimedb.filename = None
        mtimedb.commit()
        pwrite2disk.assert_not_called()

    @patch("portage.util.mtimedb.MtimeDB._MtimeDB__write_to_disk")
    def test_commit_does_not_write_to_disk_if_no_changes(self, pwrite2disk):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_EMPTY_FILE)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        mtimedb.commit()
        pwrite2disk.assert_not_called()

    def test_is_readonly_attribute(self):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_ONE_RESUME_LIST_JSON)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        self.assertFalse(mtimedb.is_readonly)

        mtimedb.filename = None
        self.assertTrue(mtimedb.is_readonly)

        mtimedb.filename = "/what/ever/mtimedb"
        self.assertFalse(mtimedb.is_readonly)

    def test_make_readonly(self):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_ONE_RESUME_LIST_JSON)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        mtimedb.make_readonly()
        self.assertTrue(mtimedb.is_readonly)
        self.assertIs(mtimedb.filename, None)

    @patch("portage.util.mtimedb.apply_secpass_permissions")
    @patch("portage.util.mtimedb.atomic_ofstream")
    def test_write_to_disk(self, matomic_ofstream, mapply_perms):
        with patch("portage.util.mtimedb.open",
                   mock_open(read_data=_ONE_RESUME_LIST_JSON)):
            mtimedb = MtimeDB("/some/path/mtimedb")
        d = {"z": "zome", "a": "AAA"}
        encoding = portage._encodings["repo.content"]
        # I'm interested here in unit testing, but the method is private
        # and shouldn't be called directly from outside, obviously:
        mtimedb._MtimeDB__write_to_disk(d)
        self.assertEqual(d["version"], str(portage.VERSION))
        matomic_ofstream.return_value.write.assert_called_once_with(
            json.dumps(d, **mtimedb._json_write_opts).encode(encoding)
        )
        mapply_perms.assert_called_once_with(
            mtimedb.filename, uid=uid, gid=portage_gid, mode=0o644
        )
        self.assertEqual(d, mtimedb._clean_data)
        self.assertIsNot(d, mtimedb._clean_data)