|
| 1 | +from http import HTTPStatus |
| 2 | +from pathlib import Path |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from pquisby.lib.post_processing import QuisbyProcessing |
| 6 | +import pytest |
| 7 | +import requests |
| 8 | + |
| 9 | +from pbench.server import JSON |
| 10 | +from pbench.server.cache_manager import CacheManager, TarballUnpackError |
| 11 | +from pbench.server.database.models.datasets import Dataset, DatasetNotFound, Metadata |
| 12 | +from pbench.server.database.models.users import User |
| 13 | + |
| 14 | + |
| 15 | +def mock_get_value(dataset: Dataset, key: str, user: Optional[User] = None) -> str: |
| 16 | + if dataset.name == "uperf_3" or dataset.name == "uperf_4": |
| 17 | + return "hammerDB" |
| 18 | + return "uperf" |
| 19 | + |
| 20 | + |
| 21 | +class TestCompareDatasets: |
| 22 | + @pytest.fixture() |
| 23 | + def query_get_as(self, client, server_config, more_datasets, get_token_func): |
| 24 | + """ |
| 25 | + Helper fixture to perform the API query and validate an expected |
| 26 | + return status. |
| 27 | +
|
| 28 | + Args: |
| 29 | + client: Flask test API client fixture |
| 30 | + server_config: Pbench config fixture |
| 31 | + more_datasets: Dataset construction fixture |
| 32 | + get_token_func: Pbench token fixture |
| 33 | + """ |
| 34 | + |
| 35 | + def query_api( |
| 36 | + datasets: list, user: str, expected_status: HTTPStatus |
| 37 | + ) -> requests.Response: |
| 38 | + ds_list = [] |
| 39 | + for dataset in datasets: |
| 40 | + try: |
| 41 | + dataset_id = Dataset.query(name=dataset).resource_id |
| 42 | + ds_list.append(dataset_id) |
| 43 | + except DatasetNotFound: |
| 44 | + ds_list.append(dataset) # Allow passing deliberately bad value |
| 45 | + headers = None |
| 46 | + if user: |
| 47 | + headers = {"authorization": f"bearer {get_token_func(user)}"} |
| 48 | + response = client.get( |
| 49 | + f"{server_config.rest_uri}/compare", |
| 50 | + query_string={"datasets": ds_list}, |
| 51 | + headers=headers, |
| 52 | + ) |
| 53 | + assert response.status_code == expected_status |
| 54 | + return response |
| 55 | + |
| 56 | + return query_api |
| 57 | + |
| 58 | + class MockTarball: |
| 59 | + tarball_path = Path("/dataset/tarball.tar.xz") |
| 60 | + name = "tarball" |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def extract(_tarball_path: Path, _path: str) -> str: |
| 64 | + return "CSV_file_as_a_string" |
| 65 | + |
| 66 | + def mock_find_dataset(self, dataset) -> MockTarball: |
| 67 | + # Validate the resource_id |
| 68 | + Dataset.query(resource_id=dataset) |
| 69 | + return self.MockTarball() |
| 70 | + |
| 71 | + def test_dataset_not_present(self, query_get_as, monkeypatch): |
| 72 | + monkeypatch.setattr(Metadata, "getvalue", mock_get_value) |
| 73 | + |
| 74 | + query_get_as(["fio_2"], "drb", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 75 | + |
| 76 | + def test_unsuccessful_get_with_incorrect_data(self, query_get_as, monkeypatch): |
| 77 | + @staticmethod |
| 78 | + def mock_extract(_tarball_path: Path, _path: str) -> str: |
| 79 | + return "IncorrectData" |
| 80 | + |
| 81 | + def mock_compare_csv_to_json( |
| 82 | + self, benchmark_name, input_type, data_stream |
| 83 | + ) -> JSON: |
| 84 | + return {"status": "failed", "exception": "Unsupported Media Type"} |
| 85 | + |
| 86 | + monkeypatch.setattr(CacheManager, "find_dataset", self.mock_find_dataset) |
| 87 | + monkeypatch.setattr(self.MockTarball, "extract", mock_extract) |
| 88 | + monkeypatch.setattr(Metadata, "getvalue", mock_get_value) |
| 89 | + monkeypatch.setattr( |
| 90 | + QuisbyProcessing, "compare_csv_to_json", mock_compare_csv_to_json |
| 91 | + ) |
| 92 | + query_get_as(["uperf_1", "uperf_2"], "test", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 93 | + |
| 94 | + def test_tarball_unpack_exception(self, query_get_as, monkeypatch): |
| 95 | + @staticmethod |
| 96 | + def mock_extract(_tarball_path: Path, _path: str): |
| 97 | + raise TarballUnpackError( |
| 98 | + _tarball_path, f"Testing unpack exception for path {_path}" |
| 99 | + ) |
| 100 | + |
| 101 | + monkeypatch.setattr(CacheManager, "find_dataset", self.mock_find_dataset) |
| 102 | + monkeypatch.setattr(self.MockTarball, "extract", mock_extract) |
| 103 | + monkeypatch.setattr(Metadata, "getvalue", mock_get_value) |
| 104 | + query_get_as(["uperf_1", "uperf_2"], "test", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 105 | + |
| 106 | + @pytest.mark.parametrize( |
| 107 | + "user,datasets,exp_status,exp_message", |
| 108 | + ( |
| 109 | + ( |
| 110 | + "drb", |
| 111 | + ["uperf_1", "nonexistent-dataset"], |
| 112 | + HTTPStatus.BAD_REQUEST, |
| 113 | + "Unrecognized list value ['nonexistent-dataset'] given for parameter datasets; expected Dataset", |
| 114 | + ), |
| 115 | + ( |
| 116 | + "drb", |
| 117 | + ["uperf_1", "uperf_2"], |
| 118 | + HTTPStatus.FORBIDDEN, |
| 119 | + "User drb is not authorized to READ a resource owned by test with private access", |
| 120 | + ), |
| 121 | + ( |
| 122 | + "test", |
| 123 | + ["uperf_1", "uperf_2"], |
| 124 | + HTTPStatus.OK, |
| 125 | + None, |
| 126 | + ), |
| 127 | + ( |
| 128 | + None, |
| 129 | + ["fio_1", "fio_2"], |
| 130 | + HTTPStatus.OK, |
| 131 | + None, |
| 132 | + ), |
| 133 | + ( |
| 134 | + "test", |
| 135 | + ["fio_1", "uperf_3"], |
| 136 | + HTTPStatus.BAD_REQUEST, |
| 137 | + "Selected dataset benchmarks must match: uperf and hammerDB cannot be compared.", |
| 138 | + ), |
| 139 | + ( |
| 140 | + "test", |
| 141 | + ["uperf_3", "uperf_4"], |
| 142 | + HTTPStatus.UNSUPPORTED_MEDIA_TYPE, |
| 143 | + "Unsupported Benchmark: hammerDB", |
| 144 | + ), |
| 145 | + ), |
| 146 | + ) |
| 147 | + def test_datasets_with_different_benchmark( |
| 148 | + self, user, datasets, exp_status, exp_message, query_get_as, monkeypatch |
| 149 | + ): |
| 150 | + def mock_compare_csv_to_json( |
| 151 | + self, benchmark_name, input_type, data_stream |
| 152 | + ) -> JSON: |
| 153 | + return {"status": "success", "json_data": "quisby_data"} |
| 154 | + |
| 155 | + monkeypatch.setattr(CacheManager, "find_dataset", self.mock_find_dataset) |
| 156 | + monkeypatch.setattr(Metadata, "getvalue", mock_get_value) |
| 157 | + monkeypatch.setattr( |
| 158 | + QuisbyProcessing, "compare_csv_to_json", mock_compare_csv_to_json |
| 159 | + ) |
| 160 | + |
| 161 | + response = query_get_as(datasets, user, exp_status) |
| 162 | + if exp_status == HTTPStatus.OK: |
| 163 | + assert response.json["status"] == "success" |
| 164 | + assert response.json["json_data"] == "quisby_data" |
| 165 | + else: |
| 166 | + assert response.json["message"] == exp_message |
0 commit comments