salt/tests/pytests/unit/states/test_openvswitch_db.py
Sebastian Marsching 02a1ee476e Extend OpenVSwitch modules (closes #58986).
This adds the new openvswitch_db state module. It also  adds the new
functions bridge_to_parent, bridge_to_vlan, db_get, and db_set to the
openvswitch execution module.

Besides, it adds two new optional parameters parent and vlan to the
openvswitch_bridge.present state module function and the
openvswitch.bridge_create execution module function.
2022-12-09 19:43:03 +01:00

80 lines
2.6 KiB
Python

"""
Test cases for salt.states.openvswitch_db.
"""
import pytest
import salt.states.openvswitch_db as openvswitch_db
from tests.support.mock import MagicMock, patch
@pytest.fixture
def configure_loader_modules():
return {openvswitch_db: {"__opts__": {"test": False}}}
def test_managed_different_entry_present():
"""
Test managed function.
This tests the case where there already is an entry, but it does not match.
"""
get_mock = MagicMock(return_value="01:02:03:04:05:06")
set_mock = MagicMock(return_value=None)
with patch.dict(
openvswitch_db.__salt__,
{"openvswitch.db_get": get_mock, "openvswitch.db_set": set_mock},
):
ret = openvswitch_db.managed(
name="br0", table="Interface", data={"mac": "01:02:03:04:05:07"}
)
get_mock.assert_called_with("Interface", "br0", "mac", True)
set_mock.assert_called_with("Interface", "br0", "mac", "01:02:03:04:05:07")
assert ret["result"] is True
assert ret["changes"] == {
"mac": {"old": "01:02:03:04:05:06", "new": "01:02:03:04:05:07"}
}
def test_managed_matching_entry_present():
"""
Test managed function.
This tests the case where there already is a matching entry.
"""
get_mock = MagicMock(return_value="01:02:03:04:05:06")
set_mock = MagicMock(return_value=None)
with patch.dict(
openvswitch_db.__salt__,
{"openvswitch.db_get": get_mock, "openvswitch.db_set": set_mock},
):
ret = openvswitch_db.managed(
name="br0", table="Interface", data={"mac": "01:02:03:04:05:06"}
)
get_mock.assert_called_with("Interface", "br0", "mac", True)
set_mock.assert_not_called()
assert ret["result"] is True
assert "changes" not in ret or not ret["changes"]
def test_managed_no_entry_present():
"""
Test managed function.
This tests the case where there is no entry yet.
"""
get_mock = MagicMock(return_value="01:02:03:04:05:06")
set_mock = MagicMock(return_value=None)
with patch.dict(
openvswitch_db.__salt__,
{"openvswitch.db_get": get_mock, "openvswitch.db_set": set_mock},
):
ret = openvswitch_db.managed(
name="br0", table="Interface", data={"mac": "01:02:03:04:05:07"}
)
get_mock.assert_called_with("Interface", "br0", "mac", True)
set_mock.assert_called_with("Interface", "br0", "mac", "01:02:03:04:05:07")
assert ret["result"] is True
assert ret["changes"] == {
"mac": {"old": "01:02:03:04:05:06", "new": "01:02:03:04:05:07"}
}