diff options
author | David Palao <david.palao@gmail.com> | 2022-05-31 17:30:48 +0200 |
---|---|---|
committer | Mike Gilbert <floppym@gentoo.org> | 2022-06-07 19:47:55 -0400 |
commit | 8a0a42ceb530b277e17220614f8c34bc8d0e503c (patch) | |
tree | 6b78f3c85e9e12ff58fa09773549714042d5e78c | |
parent | improvement(mtimedb): class has better API (diff) | |
download | gentoo-portage-8a0a42ceb530b277e17220614f8c34bc8d0e503c.tar.xz gentoo-portage-8a0a42ceb530b277e17220614f8c34bc8d0e503c.zip |
test(MtimeDB): added some unit tests for __write_to_disk and for _load
Signed-off-by: David Palao <david.palao@gmail.com>
Signed-off-by: Mike Gilbert <floppym@gentoo.org>
-rw-r--r-- | lib/portage/tests/util/test_mtimedb.py | 122 | ||||
-rw-r--r-- | 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") |