From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id B7C03158091 for ; Tue, 7 Jun 2022 23:48:22 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id CF86AE085E; Tue, 7 Jun 2022 23:48:20 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6EDD4E085E for ; Tue, 7 Jun 2022 23:48:20 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 501E0335D31 for ; Tue, 7 Jun 2022 23:48:18 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 9BB624FA for ; Tue, 7 Jun 2022 23:48:15 +0000 (UTC) From: "Mike Gilbert" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Gilbert" Message-ID: <1654645675.8a0a42ceb530b277e17220614f8c34bc8d0e503c.floppym@gentoo> Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/util/, lib/portage/tests/util/ X-VCS-Repository: proj/portage X-VCS-Files: lib/portage/tests/util/test_mtimedb.py lib/portage/util/mtimedb.py X-VCS-Directories: lib/portage/util/ lib/portage/tests/util/ X-VCS-Committer: floppym X-VCS-Committer-Name: Mike Gilbert X-VCS-Revision: 8a0a42ceb530b277e17220614f8c34bc8d0e503c X-VCS-Branch: master Date: Tue, 7 Jun 2022 23:48:15 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 48676462-9f18-4596-bf46-8478be17189e X-Archives-Hash: 067ffdf8c38a37ad4585ab2da037da20 commit: 8a0a42ceb530b277e17220614f8c34bc8d0e503c Author: David Palao gmail com> AuthorDate: Tue May 31 15:30:48 2022 +0000 Commit: Mike Gilbert gentoo org> CommitDate: Tue Jun 7 23:47:55 2022 +0000 URL: https://gitweb.gentoo.org/proj/portage.git/commit/?id=8a0a42ce test(MtimeDB): added some unit tests for __write_to_disk and for _load Signed-off-by: David Palao gmail.com> Signed-off-by: Mike Gilbert gentoo.org> lib/portage/tests/util/test_mtimedb.py | 122 +++++++++++++++++++++++++++------ lib/portage/util/mtimedb.py | 1 + 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/lib/portage/tests/util/test_mtimedb.py b/lib/portage/tests/util/test_mtimedb.py index ecc0e7135..09d72049f 100644 --- a/lib/portage/tests/util/test_mtimedb.py +++ b/lib/portage/tests/util/test_mtimedb.py @@ -2,31 +2,28 @@ # 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 random data for the fixtures: +# Some data for the fixtures: _ONE_RESUME_LIST_JSON = b"""{ "info": { - "/tmp/stage1root/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711785090, - "/tmp/stage1root/usr/share/gcc-data/x86_64-pc-linux-gnu/9.3.0/info": 1711785090, - "/tmp/stage1root/usr/share/info": 1611785090, - "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711787325, + "/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/gcc-data/x86_64-pc-linux-gnu/9.3.0/info": 1711787325, "/usr/share/info": 1650633847 }, "ldpath": { "/lib": 1748456830, "/lib64": 1750523381, "/usr/lib": 1750461195, - "/usr/lib/llvm/11/lib64": 1723048948, - "/usr/lib/llvm/12/lib64": 1730499781, "/usr/lib/llvm/13/lib64": 1747003135, "/usr/lib/rust/lib": 1750461173, "/usr/lib64": 1750881821, @@ -66,12 +63,8 @@ _ONE_RESUME_LIST_JSON = b"""{ }, "starttime": 0, "updates": { - "/var/db/repos/gentoo/profiles/updates/1Q-2021": 1739992409, "/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791, - "/var/db/repos/gentoo/profiles/updates/2Q-2021": 1724404379, "/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209, - "/var/db/repos/gentoo/profiles/updates/3Q-2021": 1741119203, - "/var/db/repos/gentoo/profiles/updates/4Q-2020": 1709167362, "/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797 }, "version": "3.0.30" @@ -85,8 +78,6 @@ _PARTIAL_FILE_JSON = b"""{ "/lib": 1748456830, "/lib64": 1750523381, "/usr/lib": 1750461195, - "/usr/lib/llvm/11/lib64": 1723048948, - "/usr/lib/llvm/12/lib64": 1730499781, "/usr/lib/llvm/13/lib64": 1747003135, "/usr/lib/rust/lib": 1750461173, "/usr/lib64": 1750881821, @@ -130,7 +121,7 @@ _PARTIAL_FILE_JSON = b"""{ _TWO_RESUME_LISTS_JSON = b"""{ "info": { - "/usr/share/binutils-data/x86_64-pc-linux-gnu/2.34/info": 1711787325, + "/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 }, @@ -199,12 +190,8 @@ _TWO_RESUME_LISTS_JSON = b"""{ }, "starttime": 0, "updates": { - "/var/db/repos/gentoo/profiles/updates/1Q-2021": 1739992409, "/var/db/repos/gentoo/profiles/updates/1Q-2022": 1747854791, - "/var/db/repos/gentoo/profiles/updates/2Q-2021": 1724404379, "/var/db/repos/gentoo/profiles/updates/2Q-2022": 1752846209, - "/var/db/repos/gentoo/profiles/updates/3Q-2021": 1741119203, - "/var/db/repos/gentoo/profiles/updates/4Q-2020": 1709167362, "/var/db/repos/gentoo/profiles/updates/4Q-2021": 1742787797 }, "version": "3.0.30" @@ -215,7 +202,7 @@ _TWO_RESUME_LISTS_JSON = b"""{ class MtimeDBTestCase(TestCase): text = b"Unit tests for MtimeDB" - def test_instances_are_created_with_only_expected_keys(self): + def test_instances_created_with_only_expected_keys(self): all_fixtures = ( _ONE_RESUME_LIST_JSON, _EMPTY_FILE, @@ -229,7 +216,7 @@ class MtimeDBTestCase(TestCase): mtimedb = MtimeDB("/path/to/mtimedb") self.assertLessEqual(set(mtimedb.keys()), _MTIMEDBKEYS) - def test_instances_have_default_values(self): + def test_default_values(self): with patch("portage.util.mtimedb.open", mock_open(read_data=_EMPTY_FILE)): mtimedb = MtimeDB("/some/path/mtimedb") @@ -246,6 +233,78 @@ class MtimeDBTestCase(TestCase): 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", @@ -296,3 +355,24 @@ class MtimeDBTestCase(TestCase): 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) diff --git a/lib/portage/util/mtimedb.py b/lib/portage/util/mtimedb.py index 95da2235f..9884746f3 100644 --- a/lib/portage/util/mtimedb.py +++ b/lib/portage/util/mtimedb.py @@ -133,6 +133,7 @@ class MtimeDB(dict): self.__write_to_disk(d) def __write_to_disk(self, d): + """Private method used by the ``commit`` method.""" d["version"] = str(portage.VERSION) try: f = atomic_ofstream(self.filename, mode="wb")