1""" 2 :codeauthor: Jayesh Kariya <jayeshk@saltstack.com> 3""" 4 5import pytest 6import salt.states.splunk_search as splunk_search 7from tests.support.mock import MagicMock, patch 8 9 10@pytest.fixture 11def configure_loader_modules(): 12 return {splunk_search: {}} 13 14 15def test_present(): 16 """ 17 Test to ensure a search is present. 18 """ 19 name = "API Error Search" 20 21 ret = {"name": name, "changes": {}, "result": None, "comment": ""} 22 23 mock = MagicMock(side_effect=[True, False, False, True]) 24 with patch.dict( 25 splunk_search.__salt__, 26 {"splunk_search.get": mock, "splunk_search.create": mock}, 27 ): 28 with patch.dict(splunk_search.__opts__, {"test": True}): 29 comt = "Would update {}".format(name) 30 ret.update({"comment": comt}) 31 assert splunk_search.present(name) == ret 32 33 comt = "Would create {}".format(name) 34 ret.update({"comment": comt}) 35 assert splunk_search.present(name) == ret 36 37 with patch.dict(splunk_search.__opts__, {"test": False}): 38 ret.update( 39 {"comment": "", "result": True, "changes": {"new": {}, "old": False}} 40 ) 41 assert splunk_search.present(name) == ret 42 43 44def test_absent(): 45 """ 46 Test to ensure a search is absent. 47 """ 48 name = "API Error Search" 49 50 ret = {"name": name, "result": None, "comment": ""} 51 52 mock = MagicMock(side_effect=[True, False]) 53 with patch.dict(splunk_search.__salt__, {"splunk_search.get": mock}): 54 with patch.dict(splunk_search.__opts__, {"test": True}): 55 comt = "Would delete {}".format(name) 56 ret.update({"comment": comt}) 57 assert splunk_search.absent(name) == ret 58 59 comt = "{} is absent.".format(name) 60 ret.update({"comment": comt, "result": True, "changes": {}}) 61 assert splunk_search.absent(name) == ret 62