mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
migrate test_guestfs to pytest
This commit is contained in:
parent
36a4c99a24
commit
d7cf126114
2 changed files with 96 additions and 104 deletions
96
tests/pytests/unit/modules/test_guestfs.py
Normal file
96
tests/pytests/unit/modules/test_guestfs.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
"""
|
||||
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
||||
|
||||
Test cases for salt.modules.guestfs
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.guestfs as guestfs
|
||||
from tests.support.mock import MagicMock, call, patch
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def configure_loader_modules():
|
||||
return {guestfs: {}}
|
||||
|
||||
|
||||
# 'mount' function tests: 1
|
||||
def test_mount():
|
||||
"""
|
||||
Test if it mounts an image
|
||||
"""
|
||||
# Test case with non-existing mount folder
|
||||
run_mock = MagicMock(return_value="")
|
||||
with patch("os.path.join", MagicMock(return_value="/tmp/guest/fedora.qcow")), patch(
|
||||
"os.path.isdir", MagicMock(return_value=False)
|
||||
), patch("os.makedirs", MagicMock()) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(return_value=False)
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
assert guestfs.mount("/srv/images/fedora.qcow")
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw /tmp/guest/fedora.qcow",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_called_once()
|
||||
|
||||
# Test case with existing but empty mount folder
|
||||
run_mock.reset_mock()
|
||||
with patch("os.path.join", MagicMock(return_value="/tmp/guest/fedora.qcow")), patch(
|
||||
"os.path.isdir", MagicMock(return_value=True)
|
||||
), patch("os.makedirs", MagicMock()) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(return_value=False)
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
assert guestfs.mount("/srv/images/fedora.qcow")
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw /tmp/guest/fedora.qcow",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_not_called()
|
||||
|
||||
# Test case with existing but not empty mount folder
|
||||
run_mock.reset_mock()
|
||||
with patch(
|
||||
"os.path.join",
|
||||
MagicMock(side_effect=["/tmp/guest/fedora.qcow", "/tmp/guest/fedora.qcowabc"]),
|
||||
), patch("os.path.isdir", MagicMock(side_effect=[True, False])), patch(
|
||||
"os.makedirs", MagicMock()
|
||||
) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(side_effect=[True, False])
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
assert guestfs.mount("/srv/images/fedora.qcow")
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw"
|
||||
" /tmp/guest/fedora.qcowabc",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_umount():
|
||||
"""
|
||||
Test the guestfs.unmount function
|
||||
"""
|
||||
run_mock = MagicMock(side_effect=["", "lsof output line", ""])
|
||||
with patch.dict(guestfs.__salt__, {"cmd.run": run_mock}):
|
||||
guestfs.umount("/tmp/mnt/opensuse.qcow", disk="/path/to/opensuse.qcow")
|
||||
expected = [
|
||||
call("guestunmount -q /tmp/mnt/opensuse.qcow"),
|
||||
call("lsof /path/to/opensuse.qcow"),
|
||||
call("lsof /path/to/opensuse.qcow"),
|
||||
]
|
||||
assert expected == run_mock.call_args_list
|
||||
|
||||
# Test without the disk image path
|
||||
run_mock = MagicMock(side_effect=[""])
|
||||
with patch.dict(guestfs.__salt__, {"cmd.run": run_mock}):
|
||||
guestfs.umount("/tmp/mnt/opensuse.qcow")
|
||||
expected = [call("guestunmount -q /tmp/mnt/opensuse.qcow")]
|
||||
assert expected == run_mock.call_args_list
|
|
@ -1,104 +0,0 @@
|
|||
"""
|
||||
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.modules.guestfs as guestfs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock, call, patch
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
|
||||
class GuestfsTestCase(TestCase, LoaderModuleMockMixin):
|
||||
"""
|
||||
Test cases for salt.modules.guestfs
|
||||
"""
|
||||
|
||||
def setup_loader_modules(self):
|
||||
return {guestfs: {}}
|
||||
|
||||
# 'mount' function tests: 1
|
||||
def test_mount(self):
|
||||
"""
|
||||
Test if it mounts an image
|
||||
"""
|
||||
# Test case with non-existing mount folder
|
||||
run_mock = MagicMock(return_value="")
|
||||
with patch(
|
||||
"os.path.join", MagicMock(return_value="/tmp/guest/fedora.qcow")
|
||||
), patch("os.path.isdir", MagicMock(return_value=False)), patch(
|
||||
"os.makedirs", MagicMock()
|
||||
) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(return_value=False)
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
self.assertTrue(guestfs.mount("/srv/images/fedora.qcow"))
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw /tmp/guest/fedora.qcow",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_called_once()
|
||||
|
||||
# Test case with existing but empty mount folder
|
||||
run_mock.reset_mock()
|
||||
with patch(
|
||||
"os.path.join", MagicMock(return_value="/tmp/guest/fedora.qcow")
|
||||
), patch("os.path.isdir", MagicMock(return_value=True)), patch(
|
||||
"os.makedirs", MagicMock()
|
||||
) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(return_value=False)
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
self.assertTrue(guestfs.mount("/srv/images/fedora.qcow"))
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw /tmp/guest/fedora.qcow",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_not_called()
|
||||
|
||||
# Test case with existing but not empty mount folder
|
||||
run_mock.reset_mock()
|
||||
with patch(
|
||||
"os.path.join",
|
||||
MagicMock(
|
||||
side_effect=["/tmp/guest/fedora.qcow", "/tmp/guest/fedora.qcowabc"]
|
||||
),
|
||||
), patch("os.path.isdir", MagicMock(side_effect=[True, False])), patch(
|
||||
"os.makedirs", MagicMock()
|
||||
) as makedirs_mock, patch(
|
||||
"os.listdir", MagicMock(side_effect=[True, False])
|
||||
), patch.dict(
|
||||
guestfs.__salt__, {"cmd.run": run_mock}
|
||||
):
|
||||
self.assertTrue(guestfs.mount("/srv/images/fedora.qcow"))
|
||||
run_mock.assert_called_once_with(
|
||||
"guestmount -i -a /srv/images/fedora.qcow --rw"
|
||||
" /tmp/guest/fedora.qcowabc",
|
||||
python_shell=False,
|
||||
)
|
||||
makedirs_mock.assert_called_once()
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_umount(self):
|
||||
"""
|
||||
Test the guestfs.unmount function
|
||||
"""
|
||||
run_mock = MagicMock(side_effect=["", "lsof output line", ""])
|
||||
with patch.dict(guestfs.__salt__, {"cmd.run": run_mock}):
|
||||
guestfs.umount("/tmp/mnt/opensuse.qcow", disk="/path/to/opensuse.qcow")
|
||||
expected = [
|
||||
call("guestunmount -q /tmp/mnt/opensuse.qcow"),
|
||||
call("lsof /path/to/opensuse.qcow"),
|
||||
call("lsof /path/to/opensuse.qcow"),
|
||||
]
|
||||
self.assertEqual(expected, run_mock.call_args_list)
|
||||
|
||||
# Test without the disk image path
|
||||
run_mock = MagicMock(side_effect=[""])
|
||||
with patch.dict(guestfs.__salt__, {"cmd.run": run_mock}):
|
||||
guestfs.umount("/tmp/mnt/opensuse.qcow")
|
||||
expected = [call("guestunmount -q /tmp/mnt/opensuse.qcow")]
|
||||
self.assertEqual(expected, run_mock.call_args_list)
|
Loading…
Add table
Reference in a new issue