From f68e89737d82f09e4d27ae2ecf84606c3d019069 Mon Sep 17 00:00:00 2001 From: Bernhard Kaindl Date: Tue, 2 Jul 2024 12:00:00 +0200 Subject: [PATCH] CP-49928: Add code coverage for static-vdis:attach() to fix warnings Signed-off-by: Bernhard Kaindl --- python3/tests/test_static_vdis.py | 59 ++++++++++++++++++++++++++++--- scripts/static-vdis | 9 +++-- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/python3/tests/test_static_vdis.py b/python3/tests/test_static_vdis.py index ee424c157a1..1b7efc0bcf0 100644 --- a/python3/tests/test_static_vdis.py +++ b/python3/tests/test_static_vdis.py @@ -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 @@ -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""" @@ -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()""" @@ -82,4 +131,4 @@ def test_sr_attach(static_vdis: ModuleType, mocker): "plugin_name", "SR.attach", ["--configuration", "key1", "value1", "--configuration", "key2", "value2"], - ) \ No newline at end of file + ) diff --git a/scripts/static-vdis b/scripts/static-vdis index ec24848e934..d94a5282ac1 100755 --- a/scripts/static-vdis +++ b/scripts/static-vdis @@ -375,8 +375,9 @@ def usage(): print(" %s attach -- attach the VDI immediately" % sys.argv[0]) print(" %s detach -- detach the VDI immediately" % sys.argv[0]) sys.exit(1) - -if __name__ == "__main__": + + +def main(): if len(sys.argv) < 2: usage() @@ -401,3 +402,7 @@ if __name__ == "__main__": detach(sys.argv[2]) else: usage() + + +if __name__ == "__main__": # pragma: no cover + main()