migrate test_glusterfs to pytest

This commit is contained in:
Frode Gundersen 2023-01-31 16:36:09 +00:00 committed by Daniel Wozniak
parent 1426e9973e
commit 4f2981f819

View file

@ -1,14 +1,16 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
:codeauthor: Joe Julian <me@joejulian.name>
Test cases for salt.modules.glusterfs
"""
import pytest
import salt.modules.glusterfs as glusterfs
from salt.exceptions import SaltInvocationError
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
class GlusterResults:
@ -498,395 +500,399 @@ xml_set_op_version_success = """
"""
class GlusterfsTestCase(TestCase, LoaderModuleMockMixin):
@pytest.fixture
def configure_loader_modules():
return {glusterfs: {}}
maxDiff = None
# 'peer_status' function tests: 1
def test__get_version():
"""
Test cases for salt.modules.glusterfs
Test parsing of gluster --version.
"""
mock_version = MagicMock(return_value="foo")
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
assert glusterfs._get_version() == (3, 6), "default behaviour"
def setup_loader_modules(self):
return {glusterfs: {}}
mock_version = MagicMock(return_value=version_output_362)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
assert glusterfs._get_version() == (3, 6, 2)
maxDiff = None
mock_version = MagicMock(return_value=version_output_61)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
assert glusterfs._get_version() == (6, 1)
# 'peer_status' function tests: 1
def test__get_version(self):
"""
Test parsing of gluster --version.
"""
mock_version = MagicMock(return_value="foo")
more_versions = {
"6.0": (6, 0),
"4.1.10": (4, 1, 10),
"5.13": (5, 13),
"10.0": (10, 0),
}
for v in more_versions:
mock_version = MagicMock(return_value="glusterfs {}".format(v))
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
self.assertEqual(glusterfs._get_version(), (3, 6), msg="default behaviour")
assert glusterfs._get_version() == more_versions[v]
mock_version = MagicMock(return_value=version_output_362)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
self.assertEqual(glusterfs._get_version(), (3, 6, 2))
mock_version = MagicMock(return_value=version_output_61)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
self.assertEqual(glusterfs._get_version(), (6, 1))
more_versions = {
"6.0": (6, 0),
"4.1.10": (4, 1, 10),
"5.13": (5, 13),
"10.0": (10, 0),
def test_peer_status():
"""
Test gluster peer status
"""
mock_run = MagicMock(return_value=xml_peer_present)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.peer_status() == {
"uuid1": {"hostnames": ["node02", "node02.domain.dom", "10.0.0.2"]}
}
for v in more_versions:
mock_version = MagicMock(return_value="glusterfs {}".format(v))
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
self.assertEqual(glusterfs._get_version(), more_versions[v])
def test_peer_status(self):
"""
Test gluster peer status
"""
mock_run = MagicMock(return_value=xml_peer_present)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertDictEqual(
glusterfs.peer_status(),
{"uuid1": {"hostnames": ["node02", "node02.domain.dom", "10.0.0.2"]}},
)
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.peer_status() == {}
# 'peer' function tests: 1
def test_peer():
"""
Test if gluster peer call is successful.
"""
mock_run = MagicMock()
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
mock_run.return_value = xml_peer_probe_already_member
assert glusterfs.peer("salt")
mock_run.return_value = xml_peer_probe_localhost
assert glusterfs.peer("salt")
mock_run.return_value = xml_peer_probe_fail_cant_connect
assert not glusterfs.peer("salt")
# 'create_volume' function tests: 1
def test_create_volume():
"""
Test if it creates a glusterfs volume.
"""
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
pytest.raises(
SaltInvocationError, glusterfs.create_volume, "newvolume", "host1:brick"
)
pytest.raises(
SaltInvocationError, glusterfs.create_volume, "newvolume", "host1/brick"
)
assert not mock_run.called
mock_start_volume = MagicMock(return_value=True)
with patch.object(glusterfs, "start_volume", mock_start_volume):
# Create, do not start
assert glusterfs.create_volume("newvolume", "host1:/brick")
assert not mock_start_volume.called
# Create and start
assert glusterfs.create_volume("newvolume", "host1:/brick", start=True)
assert mock_start_volume.called
mock_start_volume.return_value = False
# Create and fail start
assert not glusterfs.create_volume("newvolume", "host1:/brick", start=True)
mock_run.return_value = xml_command_fail
assert not glusterfs.create_volume(
"newvolume", "host1:/brick", True, True, True, "tcp", True
)
# 'list_volumes' function tests: 1
def test_list_volumes():
"""
Test if it list configured volumes
"""
mock = MagicMock(return_value=xml_volume_absent)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
assert glusterfs.list_volumes() == []
mock = MagicMock(return_value=xml_volume_present)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
assert glusterfs.list_volumes() == ["Newvolume1", "Newvolume2"]
# 'status' function tests: 1
def test_status():
"""
Test if it check the status of a gluster volume.
"""
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.status("myvol1") is None
res = {
"bricks": {
"node01:/tmp/foo": {
"host": "node01",
"hostname": "node01",
"online": True,
"path": "/tmp/foo",
"peerid": "830700d7-0684-497c-a12c-c02e365fb90b",
"pid": "2470",
"port": "49155",
"ports": {"rdma": "N/A", "tcp": "49155"},
"status": "1",
}
},
"healers": {},
"nfs": {
"node01": {
"host": "NFS Server",
"hostname": "NFS Server",
"online": False,
"path": "localhost",
"peerid": "830700d7-0684-497c-a12c-c02e365fb90b",
"pid": "-1",
"port": "N/A",
"ports": {"rdma": "N/A", "tcp": "N/A"},
"status": "0",
}
},
}
mock = MagicMock(return_value=xml_volume_status)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
assert glusterfs.status("myvol1") == res
# 'start_volume' function tests: 1
def test_volume_info():
"""
Test if it returns the volume info.
"""
res = {
"myvol1": {
"brickCount": "1",
"bricks": {
"brick1": {
"hostUuid": "830700d7-0684-497c-a12c-c02e365fb90b",
"path": "node01:/tmp/foo",
"uuid": "830700d7-0684-497c-a12c-c02e365fb90b",
}
},
"disperseCount": "0",
"distCount": "1",
"id": "f03c2180-cf55-4f77-ae0b-3650f57c82a1",
"name": "myvol1",
"optCount": "1",
"options": {"performance.readdir-ahead": "on"},
"redundancyCount": "0",
"replicaCount": "1",
"status": "1",
"statusStr": "Started",
"stripeCount": "1",
"transport": "0",
"type": "0",
"typeStr": "Distribute",
}
}
mock = MagicMock(return_value=xml_volume_info_running)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
assert glusterfs.info("myvol1") == res
def test_start_volume():
"""
Test if it start a gluster volume.
"""
# Stopped volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "0"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertDictEqual(glusterfs.peer_status(), {})
# 'peer' function tests: 1
def test_peer(self):
"""
Test if gluster peer call is successful.
"""
mock_run = MagicMock()
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
mock_run.return_value = xml_peer_probe_already_member
self.assertTrue(glusterfs.peer("salt"))
mock_run.return_value = xml_peer_probe_localhost
self.assertTrue(glusterfs.peer("salt"))
mock_run.return_value = xml_peer_probe_fail_cant_connect
self.assertFalse(glusterfs.peer("salt"))
# 'create_volume' function tests: 1
def test_create_volume(self):
"""
Test if it creates a glusterfs volume.
"""
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertRaises(
SaltInvocationError, glusterfs.create_volume, "newvolume", "host1:brick"
)
self.assertRaises(
SaltInvocationError, glusterfs.create_volume, "newvolume", "host1/brick"
)
self.assertFalse(mock_run.called)
mock_start_volume = MagicMock(return_value=True)
with patch.object(glusterfs, "start_volume", mock_start_volume):
# Create, do not start
self.assertTrue(glusterfs.create_volume("newvolume", "host1:/brick"))
self.assertFalse(mock_start_volume.called)
# Create and start
self.assertTrue(
glusterfs.create_volume("newvolume", "host1:/brick", start=True)
)
self.assertTrue(mock_start_volume.called)
mock_start_volume.return_value = False
# Create and fail start
self.assertFalse(
glusterfs.create_volume("newvolume", "host1:/brick", start=True)
)
mock_run.return_value = xml_command_fail
self.assertFalse(
glusterfs.create_volume(
"newvolume", "host1:/brick", True, True, True, "tcp", True
)
)
# 'list_volumes' function tests: 1
def test_list_volumes(self):
"""
Test if it list configured volumes
"""
mock = MagicMock(return_value=xml_volume_absent)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
self.assertListEqual(glusterfs.list_volumes(), [])
mock = MagicMock(return_value=xml_volume_present)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
self.assertListEqual(glusterfs.list_volumes(), ["Newvolume1", "Newvolume2"])
# 'status' function tests: 1
def test_status(self):
"""
Test if it check the status of a gluster volume.
"""
assert glusterfs.start_volume("Newvolume1") is True
assert glusterfs.start_volume("nonExisting") is False
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertIsNone(glusterfs.status("myvol1"))
assert glusterfs.start_volume("Newvolume1") is False
res = {
"bricks": {
"node01:/tmp/foo": {
"host": "node01",
"hostname": "node01",
"online": True,
"path": "/tmp/foo",
"peerid": "830700d7-0684-497c-a12c-c02e365fb90b",
"pid": "2470",
"port": "49155",
"ports": {"rdma": "N/A", "tcp": "49155"},
"status": "1",
}
},
"healers": {},
"nfs": {
"node01": {
"host": "NFS Server",
"hostname": "NFS Server",
"online": False,
"path": "localhost",
"peerid": "830700d7-0684-497c-a12c-c02e365fb90b",
"pid": "-1",
"port": "N/A",
"ports": {"rdma": "N/A", "tcp": "N/A"},
"status": "0",
}
},
}
mock = MagicMock(return_value=xml_volume_status)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
self.assertDictEqual(glusterfs.status("myvol1"), res)
# Started volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.start_volume("Newvolume1", force=True) is True
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
# cmd.run should not be called for already running volume:
assert glusterfs.start_volume("Newvolume1") is True
# except when forcing:
assert glusterfs.start_volume("Newvolume1", force=True) is False
# 'start_volume' function tests: 1
def test_volume_info(self):
"""
Test if it returns the volume info.
"""
res = {
"myvol1": {
"brickCount": "1",
"bricks": {
"brick1": {
"hostUuid": "830700d7-0684-497c-a12c-c02e365fb90b",
"path": "node01:/tmp/foo",
"uuid": "830700d7-0684-497c-a12c-c02e365fb90b",
}
},
"disperseCount": "0",
"distCount": "1",
"id": "f03c2180-cf55-4f77-ae0b-3650f57c82a1",
"name": "myvol1",
"optCount": "1",
"options": {"performance.readdir-ahead": "on"},
"redundancyCount": "0",
"replicaCount": "1",
# 'stop_volume' function tests: 1
def test_stop_volume():
"""
Test if it stop a gluster volume.
"""
# Stopped volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "0"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.stop_volume("Newvolume1") is True
assert glusterfs.stop_volume("nonExisting") is False
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
# cmd.run should not be called for already stopped volume:
assert glusterfs.stop_volume("Newvolume1") is True
# Started volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.stop_volume("Newvolume1") is True
assert glusterfs.stop_volume("nonExisting") is False
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.stop_volume("Newvolume1") is False
# 'delete_volume' function tests: 1
def test_delete_volume():
"""
Test if it deletes a gluster volume.
"""
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
# volume doesn't exist
assert not glusterfs.delete_volume("Newvolume3")
mock_stop_volume = MagicMock(return_value=True)
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
with patch.object(glusterfs, "stop_volume", mock_stop_volume):
# volume exists, should not be stopped, and is started
assert not glusterfs.delete_volume("Newvolume1", False)
assert not mock_run.called
assert not mock_stop_volume.called
# volume exists, should be stopped, and is started
assert glusterfs.delete_volume("Newvolume1")
assert mock_run.called
assert mock_stop_volume.called
# volume exists and isn't started
mock_info = MagicMock(return_value={"Newvolume1": {"status": "2"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.delete_volume("Newvolume1")
mock_run.return_value = xml_command_fail
assert not glusterfs.delete_volume("Newvolume1")
# 'add_volume_bricks' function tests: 1
def test_add_volume_bricks():
"""
Test if it add brick(s) to an existing volume
"""
mock_info = MagicMock(
return_value={
"Newvolume1": {
"status": "1",
"statusStr": "Started",
"stripeCount": "1",
"transport": "0",
"type": "0",
"typeStr": "Distribute",
"bricks": {
"brick1": {"path": "host:/path1"},
"brick2": {"path": "host:/path2"},
},
}
}
mock = MagicMock(return_value=xml_volume_info_running)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock}):
self.assertDictEqual(glusterfs.info("myvol1"), res)
def test_start_volume(self):
"""
Test if it start a gluster volume.
"""
# Stopped volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "0"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.start_volume("Newvolume1"), True)
self.assertEqual(glusterfs.start_volume("nonExisting"), False)
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.start_volume("Newvolume1"), False)
# Started volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.start_volume("Newvolume1", force=True), True)
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
# cmd.run should not be called for already running volume:
self.assertEqual(glusterfs.start_volume("Newvolume1"), True)
# except when forcing:
self.assertEqual(
glusterfs.start_volume("Newvolume1", force=True), False
)
# 'stop_volume' function tests: 1
def test_stop_volume(self):
"""
Test if it stop a gluster volume.
"""
# Stopped volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "0"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.stop_volume("Newvolume1"), True)
self.assertEqual(glusterfs.stop_volume("nonExisting"), False)
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
# cmd.run should not be called for already stopped volume:
self.assertEqual(glusterfs.stop_volume("Newvolume1"), True)
# Started volume
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.stop_volume("Newvolume1"), True)
self.assertEqual(glusterfs.stop_volume("nonExisting"), False)
mock_run = MagicMock(return_value=xml_command_fail)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.stop_volume("Newvolume1"), False)
# 'delete_volume' function tests: 1
def test_delete_volume(self):
"""
Test if it deletes a gluster volume.
"""
mock_info = MagicMock(return_value={"Newvolume1": {"status": "1"}})
with patch.object(glusterfs, "info", mock_info):
# volume doesn't exist
self.assertFalse(glusterfs.delete_volume("Newvolume3"))
mock_stop_volume = MagicMock(return_value=True)
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
with patch.object(glusterfs, "stop_volume", mock_stop_volume):
# volume exists, should not be stopped, and is started
self.assertFalse(glusterfs.delete_volume("Newvolume1", False))
self.assertFalse(mock_run.called)
self.assertFalse(mock_stop_volume.called)
# volume exists, should be stopped, and is started
self.assertTrue(glusterfs.delete_volume("Newvolume1"))
self.assertTrue(mock_run.called)
self.assertTrue(mock_stop_volume.called)
# volume exists and isn't started
mock_info = MagicMock(return_value={"Newvolume1": {"status": "2"}})
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertTrue(glusterfs.delete_volume("Newvolume1"))
mock_run.return_value = xml_command_fail
self.assertFalse(glusterfs.delete_volume("Newvolume1"))
# 'add_volume_bricks' function tests: 1
def test_add_volume_bricks(self):
"""
Test if it add brick(s) to an existing volume
"""
mock_info = MagicMock(
return_value={
"Newvolume1": {
"status": "1",
"bricks": {
"brick1": {"path": "host:/path1"},
"brick2": {"path": "host:/path2"},
},
}
}
)
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
# Volume does not exist
self.assertFalse(glusterfs.add_volume_bricks("nonExisting", ["bricks"]))
# Brick already exists
self.assertTrue(
glusterfs.add_volume_bricks("Newvolume1", ["host:/path2"])
)
# Already existing brick as a string
self.assertTrue(
glusterfs.add_volume_bricks("Newvolume1", "host:/path2")
)
self.assertFalse(mock_run.called)
# A new brick:
self.assertTrue(
glusterfs.add_volume_bricks("Newvolume1", ["host:/new1"])
)
self.assertTrue(mock_run.called)
# Gluster call fails
mock_run.return_value = xml_command_fail
self.assertFalse(
glusterfs.add_volume_bricks("Newvolume1", ["new:/path"])
)
# 'get_op_version' function tests: 1
def test_get_op_version(self):
"""
Test retrieving the glusterfs op-version
"""
# Test with xml output structure from v3.7
mock_run = MagicMock(return_value=xml_op_version_37)
)
with patch.object(glusterfs, "info", mock_info):
mock_run = MagicMock(return_value=xml_command_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.get_op_version("test"), "30707")
# Volume does not exist
assert not glusterfs.add_volume_bricks("nonExisting", ["bricks"])
# Brick already exists
assert glusterfs.add_volume_bricks("Newvolume1", ["host:/path2"])
# Already existing brick as a string
assert glusterfs.add_volume_bricks("Newvolume1", "host:/path2")
assert not mock_run.called
# A new brick:
assert glusterfs.add_volume_bricks("Newvolume1", ["host:/new1"])
assert mock_run.called
# Test with xml output structure from v3.12
mock_run = MagicMock(return_value=xml_op_version_312)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
self.assertEqual(glusterfs.get_op_version("test"), "30707")
# Gluster call fails
mock_run.return_value = xml_command_fail
assert not glusterfs.add_volume_bricks("Newvolume1", ["new:/path"])
# 'get_max_op_version' function tests: 1
def test_get_max_op_version(self):
"""
Test retrieving the glusterfs max-op-version.
"""
# 'get_op_version' function tests: 1
mock_xml = MagicMock(return_value=xml_max_op_version)
mock_version = MagicMock(return_value="glusterfs 3.9.1")
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
self.assertFalse(glusterfs.get_max_op_version()[0])
def test_get_op_version():
"""
Test retrieving the glusterfs op-version
"""
with patch.object(glusterfs, "_get_version", return_value=(3, 12, 0)):
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_xml}):
self.assertEqual(glusterfs.get_max_op_version(), "31200")
# Test with xml output structure from v3.7
mock_run = MagicMock(return_value=xml_op_version_37)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.get_op_version("test") == "30707"
# 'set_op_version' function tests: 1
# Test with xml output structure from v3.12
mock_run = MagicMock(return_value=xml_op_version_312)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_run}):
assert glusterfs.get_op_version("test") == "30707"
def test_set_op_version(self):
"""
Test setting the glusterfs op-version
"""
mock_failure = MagicMock(return_value=xml_set_op_version_failure)
mock_success = MagicMock(return_value=xml_set_op_version_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_failure}):
self.assertFalse(glusterfs.set_op_version(30707)[0])
# 'get_max_op_version' function tests: 1
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_success}):
self.assertEqual(glusterfs.set_op_version(31200), "Set volume successful")
def test_get_max_op_version():
"""
Test retrieving the glusterfs max-op-version.
"""
mock_xml = MagicMock(return_value=xml_max_op_version)
mock_version = MagicMock(return_value="glusterfs 3.9.1")
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_version}):
assert not glusterfs.get_max_op_version()[0]
with patch.object(glusterfs, "_get_version", return_value=(3, 12, 0)):
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_xml}):
assert glusterfs.get_max_op_version() == "31200"
# 'set_op_version' function tests: 1
def test_set_op_version():
"""
Test setting the glusterfs op-version
"""
mock_failure = MagicMock(return_value=xml_set_op_version_failure)
mock_success = MagicMock(return_value=xml_set_op_version_success)
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_failure}):
assert not glusterfs.set_op_version(30707)[0]
with patch.dict(glusterfs.__salt__, {"cmd.run": mock_success}):
assert glusterfs.set_op_version(31200) == "Set volume successful"