Skip to content

Commit 81ed967

Browse files
authored
SIMPLE-7783 Use test_data_dir fixture, allow Path for image upload (#148)
1 parent 9dffe45 commit 81ed967

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

conftest.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,3 @@ def pytest_addoption(
4444
# pytest-asycnio
4545
if pluginmanager.get_plugin("asyncio") is None:
4646
parser.addini("asyncio_mode", "suppress the warning")
47-
48-
49-
@pytest.fixture
50-
def test_dir(request: pytest.FixtureRequest) -> Path:
51-
return request.path.parent

tests/conftest.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# limitations under the License.
1919
#
2020

21+
from functools import partial
2122
from pathlib import Path
2223
from unittest.mock import patch
2324

@@ -60,7 +61,12 @@ def mocked_session():
6061
yield session
6162

6263

63-
def resp_body_from_file(request: httpx.Request) -> httpx.Response:
64+
@pytest.fixture(scope="session")
65+
def test_data_dir() -> Path:
66+
return Path(__file__).parent / "test_data"
67+
68+
69+
def resp_body_from_file(test_data_dir: Path, request: httpx.Request) -> httpx.Response:
6470
"""
6571
A callback that returns the contents of a file based on the request.
6672
@@ -75,13 +81,12 @@ def resp_body_from_file(request: httpx.Request) -> httpx.Response:
7581
elif endpoint_parts[0] == "labs":
7682
lab_id = endpoint_parts[1]
7783
filename = "_".join(endpoint_parts[2:]) + "-" + lab_id + ".json"
78-
test_dir = Path(__file__).parent.resolve()
79-
file_path = test_dir / "test_data" / filename
84+
file_path = test_data_dir / filename
8085
return httpx.Response(200, text=file_path.read_text())
8186

8287

8388
@pytest.fixture
84-
def respx_mock_with_labs(respx_mock):
89+
def respx_mock_with_labs(respx_mock, test_data_dir: Path):
8590
"""
8691
A test fixture that provides basic lab data with respx_mock so that unit tests can
8792
call ``client.all_labs`` or ``client.join_existing_lab``. The sample data includes
@@ -143,8 +148,9 @@ def respx_mock_with_labs(respx_mock):
143148
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/simulation_stats",
144149
"labs/863799a0-3d09-4af4-be26-cad997b6ab27/layer3_addresses",
145150
)
151+
side_effect = partial(resp_body_from_file, test_data_dir)
146152
for api in resp_from_files:
147-
respx_mock.get(FAKE_HOST_API + api).mock(side_effect=resp_body_from_file)
153+
respx_mock.get(FAKE_HOST_API + api).mock(side_effect=side_effect)
148154

149155

150156
@pytest.fixture

tests/test_client_library.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,12 @@ def test_different_version_strings():
690690

691691

692692
def test_import_lab_offline_deprecated(
693-
client_library_server_current, mocked_session, tmp_path: Path, test_dir
693+
client_library_server_current, mocked_session, tmp_path: Path, test_data_dir: Path
694694
):
695695
client_library = ClientLibrary(
696696
url="https://0.0.0.0/fake_url/", username="test", password="pa$$"
697697
)
698-
topology_file_path = test_dir / "test_data/sample_topology.json"
698+
topology_file_path = test_data_dir / "sample_topology.json"
699699
with open(topology_file_path) as fh:
700700
topology_file = fh.read()
701701
with pytest.deprecated_call():

tests/test_image_upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
# then locally run test_image_upload_file, and this will generate all the files
7474
# in the expected_pass_list into test_data.
7575
@pytest.fixture
76-
def create_test_files(change_test_dir):
76+
def create_test_files(test_data_dir):
7777
for file_path in EXPECTED_PASS_LIST:
78-
path = pathlib.Path("test_data") / file_path
78+
path = test_data_dir / file_path
7979
path.write_text("test")
8080

8181

virl2_client/models/node_image_definition.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ def download_image_definition(self, definition_id: str) -> str:
210210
return self._session.get(url).text
211211

212212
def upload_image_file(
213-
self,
214-
filename: str,
215-
rename: str | None = None,
213+
self, filename: Path | str, rename: str | None = None,
216214
) -> None:
217215
"""
218216
Upload an image file.
@@ -224,8 +222,8 @@ def upload_image_file(
224222

225223
path = pathlib.Path(filename)
226224

227-
extension = TARGZ if filename.endswith(TARGZ) else path.suffix
228225
name = rename or path.name
226+
extension = TARGZ if path.name.endswith(TARGZ) else path.suffix
229227

230228
if not name.endswith(extension):
231229
message = (
@@ -248,6 +246,7 @@ def upload_image_file(
248246
)
249247
raise InvalidImageFile(message)
250248

249+
# path may be a PureWindowsPath, cannot use path.is_file
251250
if not os.path.isfile(filename):
252251
raise FileNotFoundError(filename)
253252
# TODO: a library should not be printing to stdout unless interactive
@@ -270,10 +269,13 @@ def callback_read(__n):
270269
return callback_read
271270

272271
_file = open(filename, "rb")
273-
_file.read = callback_read_factory(_file, print_progress_bar)
274-
files = {"field0": (name, _file)}
272+
try:
273+
_file.read = callback_read_factory(_file, print_progress_bar)
274+
files = {"field0": (name, _file)}
275275

276-
self._session.post(url, files=files, headers=headers)
276+
self._session.post(url, files=files, headers=headers)
277+
finally:
278+
_file.close()
277279
print("Upload completed")
278280

279281
def download_image_file_list(self) -> list[str]:

0 commit comments

Comments
 (0)