Skip to content

Commit

Permalink
CP-49928: Add code coverage for static-vdis:attach() to fix warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Bernhard Kaindl <[email protected]>
  • Loading branch information
bernhardkaindl committed Jul 3, 2024
1 parent 2f83515 commit f68e897
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
59 changes: 54 additions & 5 deletions python3/tests/test_static_vdis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""python3/tests/test_static_vdis.py: Test the static-vdis script"""

import os
import sys
from pathlib import Path
from types import ModuleType

Expand All @@ -26,17 +27,66 @@ def static_vdis() -> ModuleType:
# ----------------------------- Test cases -----------------------------------


def test_whole_file(static_vdis: ModuleType):
def test_whole_file(static_vdis: ModuleType, tmp_path):
"""Test read_whole_file() and write_whole_file()"""

with open(__file__, encoding="utf-8") as data:
contents = data.read().strip()
assert static_vdis.read_whole_file(__file__) == contents
assert static_vdis.write_whole_file(__file__, contents) is None
with open(__file__, encoding="utf-8") as written_data:
assert static_vdis.write_whole_file(tmp_path / "temp_file", contents) is None
with open(tmp_path / "temp_file", encoding="utf-8") as written_data:
assert written_data.read().strip() == contents


def test_attach(static_vdis: ModuleType, tmpdir, mocker, capsys):
"""Test five common and SMAPIv1 code paths in the attach() function"""

# Path 1: When the VDI is not found, expect attach() to raise an exception:
static_vdis.list_vdis = lambda: [{"vdi-uuid": "existing-uuid"}]
with pytest.raises(Exception) as exc_info:
static_vdis.attach("nonexisting-uuid")
assert exc_info.value.args[0] == "Disk configuration not found"

# Path 2: When the VDI is already attached, expect main():attach to return None\n:
static_vdis.list_vdis = lambda: [{"vdi-uuid": "attached", "path": "/attached"}]
sys.argv = ["static-vdis", "attach", "attached"]
static_vdis.main()
with capsys.disabled():
assert capsys.readouterr().out == "None\n"

# Path 3: When the VDI is not attached, attach() to return "the-id/disk":
vdis: list[dict[str, str]] = [{"vdi-uuid": "attach-uuid", "id": "the-id"}]
static_vdis.list_vdis = lambda: vdis
static_vdis.call_backend_attach = lambda driver, config: "/mock-attached-path"
static_vdis.read_whole_file = lambda path: '{"json":true}'
disk = tmpdir.mkdir(vdis[0]["id"]).join("disk")
static_vdis.main_dir = str(tmpdir)
assert static_vdis.attach("attach-uuid") == disk
assert os.readlink(disk) == "/mock-attached-path"
os.unlink(disk)

# Path 4: Create the disk file expect it to be deleted and replaced by a symlink:
disk.write("mock-disk-contents-to-delete")
assert static_vdis.attach("attach-uuid") == disk
assert os.readlink(disk) == "/mock-attached-path"

# Path 5: When the backend call returns None, expect attach() to raise TypeError
static_vdis.call_backend_attach = lambda driver, config: None
with pytest.raises(TypeError) as exc_info:
static_vdis.attach("attach-uuid")

# Path 6: When the backend returns an empty str, attach() raises FileNotFoundError:
static_vdis.call_backend_attach = lambda driver, config: ""
with pytest.raises(FileNotFoundError) as exc_info:
static_vdis.attach("attach-uuid")

# Path 7: If the smapiv3_config exists, but not the volume plugin, attach() fails:
with pytest.raises(FileNotFoundError) as exc_info:
mocker.patch("os.path.exists", return_value=True)
static_vdis.MULTIPATH_FLAG = __file__
static_vdis.attach("attach-uuid")


def test_fresh_name(static_vdis: ModuleType, tmp_path: Path):
"""Test fresh_name() and list_vdis() - all code paths"""

Expand All @@ -59,7 +109,6 @@ def test_fresh_name(static_vdis: ModuleType, tmp_path: Path):
assert static_vdis.fresh_name() == "0"



def test_sr_attach(static_vdis: ModuleType, mocker):
"""Test sr_attach()"""

Expand All @@ -82,4 +131,4 @@ def test_sr_attach(static_vdis: ModuleType, mocker):
"plugin_name",
"SR.attach",
["--configuration", "key1", "value1", "--configuration", "key2", "value2"],
)
)
9 changes: 7 additions & 2 deletions scripts/static-vdis
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,9 @@ def usage():
print(" %s attach <uuid> -- attach the VDI immediately" % sys.argv[0])
print(" %s detach <uuid> -- detach the VDI immediately" % sys.argv[0])
sys.exit(1)

if __name__ == "__main__":


def main():
if len(sys.argv) < 2:
usage()

Expand All @@ -401,3 +402,7 @@ if __name__ == "__main__":
detach(sys.argv[2])
else:
usage()


if __name__ == "__main__": # pragma: no cover
main()

0 comments on commit f68e897

Please sign in to comment.