Skip to content

Commit 2aca671

Browse files
authored
SIMPLE-7745 - allow .tar.gz image file extension (#147)
* SIMPLE-7745 - allow .tar.gz image file extension * Path is supported in import_lab_from_path
1 parent 371e983 commit 2aca671

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

tests/test_image_upload.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import contextlib
2323
import pathlib
2424
from io import BufferedReader
25+
from typing import Iterator
2526
from unittest.mock import ANY, MagicMock
2627

2728
import pytest
@@ -51,7 +52,8 @@
5152
"file. qcow",
5253
"file.qcow2 2",
5354
"file.qcow ",
54-
"file.qcow.tar.gz",
55+
"file.qcow.gz",
56+
"file.tgz",
5557
]
5658
EXPECTED_PASS_LIST = [
5759
"file.qcow",
@@ -62,6 +64,7 @@
6264
"file.iol",
6365
"qcow.iol",
6466
"file.tar",
67+
"file.tar.gz",
6568
]
6669

6770

@@ -77,7 +80,7 @@ def create_test_files(change_test_dir):
7780

7881

7982
@contextlib.contextmanager
80-
def windows_path(path: str):
83+
def windows_path(path: str) -> Iterator[None]:
8184
if "\\" in path:
8285
orig = pathlib.Path
8386
pathlib.Path = pathlib.PureWindowsPath
@@ -86,7 +89,7 @@ def windows_path(path: str):
8689
finally:
8790
pathlib.Path = orig
8891
else:
89-
yield path
92+
yield
9093

9194

9295
@pytest.mark.parametrize(
@@ -98,10 +101,12 @@ def windows_path(path: str):
98101
"test_string",
99102
WRONG_FORMAT_LIST + NOT_SUPPORTED_LIST + EXPECTED_PASS_LIST,
100103
)
101-
def test_image_upload_file(rename: str, test_string: str, test_path: str):
104+
def test_image_upload_file(rename: str | None, test_string: str, test_path: str):
102105
session = MagicMock()
103106
nid = NodeImageDefinitions(session)
104107
filename = test_path + test_string
108+
if rename is not None:
109+
rename += test_string
105110

106111
if test_string in WRONG_FORMAT_LIST:
107112
with pytest.raises(InvalidImageFile, match="wrong format"):
@@ -123,6 +128,10 @@ def test_image_upload_file(rename: str, test_string: str, test_path: str):
123128
assert pathlib.Path(file.name).resolve() == pathlib.Path(filename).resolve()
124129
file.close()
125130
else:
131+
if rename is not None:
132+
with pytest.raises(InvalidImageFile, match="does not match source"):
133+
with windows_path(filename):
134+
nid.upload_image_file(filename, rename[:-3])
126135
with pytest.raises(FileNotFoundError):
127136
with windows_path(filename):
128137
nid.upload_image_file(filename, rename)

virl2_client/models/node_image_definition.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
import httpx
3434

3535

36+
TARGZ = ".tar.gz"
37+
EXTENSION_LIST = [".qcow", ".qcow2", ".iol", ".tar", TARGZ]
38+
39+
3640
class NodeImageDefinitions:
3741
_URL_TEMPLATES = {
3842
"node_defs": "node_definitions",
@@ -216,31 +220,37 @@ def upload_image_file(
216220
:param filename: The path of the image to upload.
217221
:param rename: Optional filename to rename to.
218222
"""
219-
extension_list = [".qcow", ".qcow2", ".iol", ".tar"]
220223
url = self._url_for("upload")
221224

222225
path = pathlib.Path(filename)
223-
extension = "".join(path.suffixes)
224-
last_ext = path.suffix
226+
227+
extension = TARGZ if filename.endswith(TARGZ) else path.suffix
225228
name = rename or path.name
226229

230+
if not name.endswith(extension):
231+
message = (
232+
f"Specified filename ({name}) does not match source file's "
233+
f"extension ({extension}), possibly using a different file format."
234+
)
235+
raise InvalidImageFile(message)
236+
227237
if extension == "" or name == "":
228238
message = (
229239
f"Specified filename ({name}) has wrong format "
230-
f"(correct format is filename.({'|'.join(extension_list)}) )."
240+
f"(correct format is filename.({'|'.join(EXTENSION_LIST)}) )."
231241
)
232242
raise InvalidImageFile(message)
233243

234-
if extension not in extension_list and last_ext not in extension_list:
244+
if extension not in EXTENSION_LIST:
235245
message = (
236246
f"Specified filename ({name}) has unsupported extension ({extension}) "
237-
f"(supported extensions are {', '.join(extension_list)})."
247+
f"(supported extensions are {', '.join(EXTENSION_LIST)})."
238248
)
239249
raise InvalidImageFile(message)
240250

241-
if not os.path.exists(filename):
251+
if not os.path.isfile(filename):
242252
raise FileNotFoundError(filename)
243-
253+
# TODO: a library should not be printing to stdout unless interactive
244254
print(f"Uploading {name}")
245255
headers = {"X-Original-File-Name": name}
246256

virl2_client/virl2_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ def _create_imported_lab(
585585
)
586586

587587
@locked
588-
def import_lab_from_path(self, path: str, title: str | None = None) -> Lab:
588+
def import_lab_from_path(self, path: Path | str, title: str | None = None) -> Lab:
589589
"""
590590
Import an existing topology from a file or path.
591591

0 commit comments

Comments
 (0)