|
13 | 13 | from pbench.server.cache_manager import ( |
14 | 14 | BadDirpath, |
15 | 15 | BadFilename, |
| 16 | + CacheExtractBadPath, |
16 | 17 | CacheManager, |
17 | 18 | CacheType, |
18 | 19 | Controller, |
@@ -903,35 +904,117 @@ def test_filestream( |
903 | 904 | tmp_path, "dir_name" |
904 | 905 | ) |
905 | 906 | tb.cache_map(tar_dir) |
906 | | - file_info = tb.filestream(file_path) |
| 907 | + file_info = tb.get_inventory(file_path) |
907 | 908 | assert file_info["type"] == exp_file_type |
908 | 909 | assert file_info["stream"].stream == exp_stream |
909 | 910 |
|
910 | | - def test_filestream_tarfile_open(self, monkeypatch, tmp_path): |
911 | | - """Test to check non-existent file or tarfile unpack issue""" |
912 | | - tar = Path("/mock/dir_name.tar.xz") |
913 | | - cache = Path("/mock/.cache") |
| 911 | + def test_tarfile_open_fails(self, monkeypatch, tmp_path): |
| 912 | + """Test to check non-existent tarfile""" |
| 913 | + tar = Path("/mock/result.tar.xz") |
914 | 914 |
|
915 | 915 | def fake_tarfile_open(self, path): |
916 | 916 | raise tarfile.TarError("Invalid Tarfile") |
917 | 917 |
|
918 | 918 | with monkeypatch.context() as m: |
919 | | - m.setattr(Tarball, "__init__", TestCacheManager.MockTarball.__init__) |
920 | | - m.setattr(Controller, "__init__", TestCacheManager.MockController.__init__) |
921 | 919 | m.setattr(tarfile, "open", fake_tarfile_open) |
922 | | - tb = Tarball(tar, Controller(Path("/mock/archive"), cache, None)) |
923 | | - tar_dir = TestCacheManager.MockController.generate_test_result_tree( |
924 | | - tmp_path, "dir_name" |
925 | | - ) |
926 | | - tb.cache_map(tar_dir) |
927 | 920 |
|
928 | | - expected_error_msg = ( |
929 | | - f"The dataset tarball named '{tb.tarball_path}' is not found" |
930 | | - ) |
| 921 | + expected_error_msg = f"The dataset tarball named '{tar}' is not found" |
931 | 922 | with pytest.raises(TarballNotFound) as exc: |
932 | | - tb.filestream("subdir1/f11.txt") |
| 923 | + Tarball.extract(tar, Path("subdir1/f11.txt")) |
| 924 | + assert str(exc.value) == expected_error_msg |
| 925 | + |
| 926 | + def test_tarfile_extractfile_fails(self, monkeypatch, tmp_path): |
| 927 | + """Test to check non-existent path in tarfile""" |
| 928 | + tar = Path("/mock/result.tar.xz") |
| 929 | + path = Path("subdir/f11.txt") |
| 930 | + |
| 931 | + class MockTarFile: |
| 932 | + def extractfile(self, path): |
| 933 | + raise Exception("Mr Robot refuses trivial human command") |
| 934 | + |
| 935 | + def fake_tarfile_open(self, path): |
| 936 | + return MockTarFile() |
| 937 | + |
| 938 | + with monkeypatch.context() as m: |
| 939 | + m.setattr(tarfile, "open", fake_tarfile_open) |
| 940 | + expected_error_msg = f"Unable to extract {path} from {tar.name}" |
| 941 | + with pytest.raises(CacheExtractBadPath) as exc: |
| 942 | + Tarball.extract(tar, path) |
933 | 943 | assert str(exc.value) == expected_error_msg |
934 | 944 |
|
| 945 | + def test_tarfile_extractfile_notfile(self, monkeypatch, tmp_path): |
| 946 | + """Test to check target that's not a file""" |
| 947 | + tar = Path("/mock/result.tar.xz") |
| 948 | + path = Path("subdir/f11.txt") |
| 949 | + |
| 950 | + class MockTarFile: |
| 951 | + def extractfile(self, path): |
| 952 | + return None |
| 953 | + |
| 954 | + def fake_tarfile_open(self, path): |
| 955 | + return MockTarFile() |
| 956 | + |
| 957 | + with monkeypatch.context() as m: |
| 958 | + m.setattr(tarfile, "open", fake_tarfile_open) |
| 959 | + expected_error_msg = f"Unable to extract {path} from {tar.name}" |
| 960 | + with pytest.raises(CacheExtractBadPath) as exc: |
| 961 | + Tarball.extract(tar, path) |
| 962 | + assert str(exc.value) == expected_error_msg |
| 963 | + |
| 964 | + @pytest.mark.parametrize( |
| 965 | + "tarball,stream", (("hasmetalog.tar.xz", True), ("nometalog.tar.xz", False)) |
| 966 | + ) |
| 967 | + def test_get_metadata(self, monkeypatch, tarball, stream): |
| 968 | + """Verify access and processing of `metadata.log`""" |
| 969 | + |
| 970 | + @staticmethod |
| 971 | + def fake_extract(t: Path, f: Path): |
| 972 | + if str(t) == tarball: |
| 973 | + if str(f) == f"{Dataset.stem(t)}/metadata.log": |
| 974 | + if stream: |
| 975 | + return io.BytesIO(b"[test]\nfoo = bar\n") |
| 976 | + else: |
| 977 | + raise CacheExtractBadPath(t, f) |
| 978 | + raise Exception(f"Unexpected mock exception with stream:{stream}: {t}, {f}") |
| 979 | + |
| 980 | + with monkeypatch.context() as m: |
| 981 | + m.setattr(Tarball, "extract", fake_extract) |
| 982 | + metadata = Tarball._get_metadata(Path(tarball)) |
| 983 | + |
| 984 | + if stream: |
| 985 | + assert metadata == {"test": {"foo": "bar"}} |
| 986 | + else: |
| 987 | + assert metadata is None |
| 988 | + |
| 989 | + def test_inventory(self): |
| 990 | + closed = False |
| 991 | + |
| 992 | + class MockTarFile: |
| 993 | + def close(self): |
| 994 | + nonlocal closed |
| 995 | + closed = True |
| 996 | + |
| 997 | + def __repr__(self) -> str: |
| 998 | + return "<Mock tarfile>" |
| 999 | + |
| 1000 | + raw = b"abcde\nfghij\n" |
| 1001 | + stream = Inventory(io.BytesIO(raw), MockTarFile()) |
| 1002 | + assert re.match( |
| 1003 | + r"<Stream <_io.BytesIO object at 0x[a-z0-9]+> from <Mock tarfile>", |
| 1004 | + str(stream), |
| 1005 | + ) |
| 1006 | + |
| 1007 | + assert stream.getbuffer() == raw |
| 1008 | + assert stream.readable() |
| 1009 | + assert stream.read(5) == b"abcde" |
| 1010 | + assert stream.read() == b"\nfghij\n" |
| 1011 | + assert stream.seek(0) == 0 |
| 1012 | + assert [b for b in stream] == [b"abcde\n", b"fghij\n"] |
| 1013 | + stream.close() |
| 1014 | + assert closed |
| 1015 | + with pytest.raises(ValueError): |
| 1016 | + stream.read() |
| 1017 | + |
935 | 1018 | def test_find( |
936 | 1019 | self, selinux_enabled, server_config, make_logger, tarball, monkeypatch |
937 | 1020 | ): |
|
0 commit comments