From b06425a140eea7b9cf02dfcbf53b193c41734bbb Mon Sep 17 00:00:00 2001 From: nicholasmhughes Date: Mon, 5 Feb 2024 18:51:04 -0500 Subject: [PATCH] removed placeholder files created by creation template --- docs/ref/modules/index.rst | 1 - .../saltext.grafana.modules.grafana_mod.rst | 5 - docs/ref/modules/saltext.grafana.modules.rst | 6 - docs/ref/states/index.rst | 1 - .../saltext.grafana.states.grafana_mod.rst | 5 - docs/ref/states/saltext.grafana.states.rst | 6 - src/saltext/grafana/modules/grafana_mod.py | 27 ---- src/saltext/grafana/states/grafana_mod.py | 30 ---- tests/functional/modules/test_grafana.py | 16 -- tests/functional/states/test_grafana.py | 18 --- tests/integration/modules/test_grafana.py | 13 -- tests/pytests/functional/conftest.py | 140 ----------------- tests/pytests/integration/conftest.py | 108 -------------- tests/pytests/unit/__init__.py | 0 tests/pytests/unit/conftest.py | 77 ---------- tests/pytests/unit/states/__init__.py | 0 tests/pytests/unit/states/test_grafana.py | 122 --------------- tests/unit/modules/test_grafana.py | 18 --- tests/unit/states/test_grafana.py | 141 ++++++++++++++---- .../unit/states/test_grafana_datasource.py | 0 20 files changed, 116 insertions(+), 618 deletions(-) delete mode 100644 docs/ref/modules/saltext.grafana.modules.grafana_mod.rst delete mode 100644 docs/ref/modules/saltext.grafana.modules.rst delete mode 100644 docs/ref/states/saltext.grafana.states.grafana_mod.rst delete mode 100644 docs/ref/states/saltext.grafana.states.rst delete mode 100644 src/saltext/grafana/modules/grafana_mod.py delete mode 100644 src/saltext/grafana/states/grafana_mod.py delete mode 100644 tests/functional/modules/test_grafana.py delete mode 100644 tests/functional/states/test_grafana.py delete mode 100644 tests/integration/modules/test_grafana.py delete mode 100644 tests/pytests/functional/conftest.py delete mode 100644 tests/pytests/integration/conftest.py delete mode 100644 tests/pytests/unit/__init__.py delete mode 100644 tests/pytests/unit/conftest.py delete mode 100644 tests/pytests/unit/states/__init__.py delete mode 100644 tests/pytests/unit/states/test_grafana.py delete mode 100644 tests/unit/modules/test_grafana.py rename tests/{pytests => }/unit/states/test_grafana_datasource.py (100%) diff --git a/docs/ref/modules/index.rst b/docs/ref/modules/index.rst index 7b7a979..91d06ed 100644 --- a/docs/ref/modules/index.rst +++ b/docs/ref/modules/index.rst @@ -10,4 +10,3 @@ _________________ :toctree: grafana4 - grafana_mod diff --git a/docs/ref/modules/saltext.grafana.modules.grafana_mod.rst b/docs/ref/modules/saltext.grafana.modules.grafana_mod.rst deleted file mode 100644 index 35eb1c6..0000000 --- a/docs/ref/modules/saltext.grafana.modules.grafana_mod.rst +++ /dev/null @@ -1,5 +0,0 @@ -``grafana`` -=========== - -.. automodule:: saltext.grafana.modules.grafana_mod - :members: diff --git a/docs/ref/modules/saltext.grafana.modules.rst b/docs/ref/modules/saltext.grafana.modules.rst deleted file mode 100644 index 4ed4362..0000000 --- a/docs/ref/modules/saltext.grafana.modules.rst +++ /dev/null @@ -1,6 +0,0 @@ - -saltext.grafana.modules.grafana_mod -=================================== - -.. automodule:: saltext.grafana.modules.grafana_mod - :members: diff --git a/docs/ref/states/index.rst b/docs/ref/states/index.rst index 3d5db0b..c71d801 100644 --- a/docs/ref/states/index.rst +++ b/docs/ref/states/index.rst @@ -16,4 +16,3 @@ _____________ grafana4_user grafana_dashboard grafana_datasource - grafana_mod diff --git a/docs/ref/states/saltext.grafana.states.grafana_mod.rst b/docs/ref/states/saltext.grafana.states.grafana_mod.rst deleted file mode 100644 index a4d3bc9..0000000 --- a/docs/ref/states/saltext.grafana.states.grafana_mod.rst +++ /dev/null @@ -1,5 +0,0 @@ -``grafana`` -=========== - -.. automodule:: saltext.grafana.states.grafana_mod - :members: diff --git a/docs/ref/states/saltext.grafana.states.rst b/docs/ref/states/saltext.grafana.states.rst deleted file mode 100644 index d90110d..0000000 --- a/docs/ref/states/saltext.grafana.states.rst +++ /dev/null @@ -1,6 +0,0 @@ - -saltext.grafana.states.grafana_mod -================================== - -.. automodule:: saltext.grafana.states.grafana_mod - :members: diff --git a/src/saltext/grafana/modules/grafana_mod.py b/src/saltext/grafana/modules/grafana_mod.py deleted file mode 100644 index c123c1b..0000000 --- a/src/saltext/grafana/modules/grafana_mod.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Salt execution module -""" -import logging - -log = logging.getLogger(__name__) - -__virtualname__ = "grafana" - - -def __virtual__(): - # To force a module not to load return something like: - # return (False, "The grafana execution module is not implemented yet") - return __virtualname__ - - -def example_function(text): - """ - This example function should be replaced - - CLI Example: - - .. code-block:: bash - - salt '*' grafana.example_function text="foo bar" - """ - return __salt__["test.echo"](text) diff --git a/src/saltext/grafana/states/grafana_mod.py b/src/saltext/grafana/states/grafana_mod.py deleted file mode 100644 index a35f043..0000000 --- a/src/saltext/grafana/states/grafana_mod.py +++ /dev/null @@ -1,30 +0,0 @@ -""" -Salt state module -""" -import logging - -log = logging.getLogger(__name__) - -__virtualname__ = "grafana" - - -def __virtual__(): - # To force a module not to load return something like: - # return (False, "The grafana state module is not implemented yet") - - # Replace this with your own logic - if "grafana.example_function" not in __salt__: - return False, "The 'grafana' execution module is not available" - return __virtualname__ - - -def exampled(name): - """ - This example function should be replaced - """ - ret = {"name": name, "changes": {}, "result": False, "comment": ""} - value = __salt__["grafana.example_function"](name) - if value == name: - ret["result"] = True - ret["comment"] = f"The 'grafana.example_function' returned: '{value}'" - return ret diff --git a/tests/functional/modules/test_grafana.py b/tests/functional/modules/test_grafana.py deleted file mode 100644 index 214983a..0000000 --- a/tests/functional/modules/test_grafana.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest - -pytestmark = [ - pytest.mark.requires_salt_modules("grafana.example_function"), -] - - -@pytest.fixture -def grafana(modules): - return modules.grafana - - -def test_replace_this_this_with_something_meaningful(grafana): - echo_str = "Echoed!" - res = grafana.example_function(echo_str) - assert res == echo_str diff --git a/tests/functional/states/test_grafana.py b/tests/functional/states/test_grafana.py deleted file mode 100644 index b43357a..0000000 --- a/tests/functional/states/test_grafana.py +++ /dev/null @@ -1,18 +0,0 @@ -import pytest - -pytestmark = [ - pytest.mark.requires_salt_states("grafana.exampled"), -] - - -@pytest.fixture -def grafana(states): - return states.grafana - - -def test_replace_this_this_with_something_meaningful(grafana): - echo_str = "Echoed!" - ret = grafana.exampled(echo_str) - assert ret.result - assert not ret.changes - assert echo_str in ret.comment diff --git a/tests/integration/modules/test_grafana.py b/tests/integration/modules/test_grafana.py deleted file mode 100644 index 0be4184..0000000 --- a/tests/integration/modules/test_grafana.py +++ /dev/null @@ -1,13 +0,0 @@ -import pytest - -pytestmark = [ - pytest.mark.requires_salt_modules("grafana.example_function"), -] - - -def test_replace_this_this_with_something_meaningful(salt_call_cli): - echo_str = "Echoed!" - ret = salt_call_cli.run("grafana.example_function", echo_str) - assert ret.exitcode == 0 - assert ret.json - assert ret.json == echo_str diff --git a/tests/pytests/functional/conftest.py b/tests/pytests/functional/conftest.py deleted file mode 100644 index 2fb2246..0000000 --- a/tests/pytests/functional/conftest.py +++ /dev/null @@ -1,140 +0,0 @@ -import logging -import shutil - -import pytest -from saltfactories.utils.functional import Loaders - -log = logging.getLogger(__name__) - - -@pytest.fixture(scope="package") -def minion_id(): - return "func-tests-minion-opts" - - -@pytest.fixture(scope="module") -def state_tree(tmp_path_factory): - state_tree_path = tmp_path_factory.mktemp("state-tree-base") - try: - yield state_tree_path - finally: - shutil.rmtree(str(state_tree_path), ignore_errors=True) - - -@pytest.fixture(scope="module") -def state_tree_prod(tmp_path_factory): - state_tree_path = tmp_path_factory.mktemp("state-tree-prod") - try: - yield state_tree_path - finally: - shutil.rmtree(str(state_tree_path), ignore_errors=True) - - -@pytest.fixture(scope="module") -def minion_config_defaults(): - """ - Functional test modules can provide this fixture to tweak the default configuration dictionary - passed to the minion factory - """ - return {} - - -@pytest.fixture(scope="module") -def minion_config_overrides(): - """ - Functional test modules can provide this fixture to tweak the configuration - overrides dictionary passed to the minion factory - """ - return {} - - -@pytest.fixture(scope="module") -def minion_opts( - salt_factories, - minion_id, - state_tree, - state_tree_prod, - minion_config_defaults, - minion_config_overrides, -): - minion_config_overrides.update( - { - "file_client": "local", - "file_roots": { - "base": [ - str(state_tree), - ], - "prod": [ - str(state_tree_prod), - ], - }, - } - ) - factory = salt_factories.salt_minion_daemon( - minion_id, - defaults=minion_config_defaults or None, - overrides=minion_config_overrides, - ) - return factory.config.copy() - - -@pytest.fixture(scope="module") -def master_config_defaults(): - """ - Functional test modules can provide this fixture to tweak the default configuration dictionary - passed to the master factory - """ - return {} - - -@pytest.fixture(scope="module") -def master_config_overrides(): - """ - Functional test modules can provide this fixture to tweak the configuration - overrides dictionary passed to the master factory - """ - return {} - - -@pytest.fixture(scope="module") -def master_opts( - salt_factories, - state_tree, - state_tree_prod, - master_config_defaults, - master_config_overrides, -): - master_config_overrides.update( - { - "file_client": "local", - "file_roots": { - "base": [ - str(state_tree), - ], - "prod": [ - str(state_tree_prod), - ], - }, - } - ) - factory = salt_factories.salt_master_daemon( - "func-tests-master-opts", - defaults=master_config_defaults or None, - overrides=master_config_overrides, - ) - return factory.config.copy() - - -@pytest.fixture(scope="module") -def loaders(minion_opts): - return Loaders(minion_opts, loaded_base_name=f"{__name__}.loaded") - - -@pytest.fixture(autouse=True) -def reset_loaders_state(loaders): - try: - # Run the tests - yield - finally: - # Reset the loaders state - loaders.reset_state() diff --git a/tests/pytests/integration/conftest.py b/tests/pytests/integration/conftest.py deleted file mode 100644 index ed1c25a..0000000 --- a/tests/pytests/integration/conftest.py +++ /dev/null @@ -1,108 +0,0 @@ -""" - tests.pytests.integration.conftest - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - - PyTest fixtures -""" -import logging - -import pytest - -log = logging.getLogger(__name__) - - -@pytest.fixture(scope="package") -def salt_master(salt_master_factory): - """ - A running salt-master fixture - """ - with salt_master_factory.started(): - yield salt_master_factory - - -@pytest.fixture(scope="package") -def salt_minion(salt_master, salt_minion_factory): - """ - A running salt-minion fixture - """ - assert salt_master.is_running() - with salt_minion_factory.started(): - # Sync All - salt_call_cli = salt_minion_factory.salt_call_cli() - ret = salt_call_cli.run("saltutil.sync_all", _timeout=120) - assert ret.returncode == 0, ret - yield salt_minion_factory - - -@pytest.fixture(scope="module") -def salt_sub_minion(salt_master, salt_sub_minion_factory): - """ - A second running salt-minion fixture - """ - assert salt_master.is_running() - with salt_sub_minion_factory.started(): - # Sync All - salt_call_cli = salt_sub_minion_factory.salt_call_cli() - ret = salt_call_cli.run("saltutil.sync_all", _timeout=120) - assert ret.returncode == 0, ret - yield salt_sub_minion_factory - - -@pytest.fixture(scope="package") -def salt_cli(salt_master): - """ - The ``salt`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_call_cli(salt_minion): - """ - The ``salt-call`` CLI as a fixture against the running minion - """ - assert salt_minion.is_running() - return salt_minion.salt_call_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_cp_cli(salt_master): - """ - The ``salt-cp`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_cp_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_key_cli(salt_master): - """ - The ``salt-key`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_key_cli(timeout=30) - - -@pytest.fixture(scope="package") -def salt_run_cli(salt_master): - """ - The ``salt-run`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_run_cli(timeout=30) - - -@pytest.fixture(scope="module") -def salt_ssh_cli(salt_master, salt_ssh_roster_file, sshd_config_dir): - """ - The ``salt-ssh`` CLI as a fixture against the running master - """ - assert salt_master.is_running() - return salt_master.salt_ssh_cli( - timeout=180, - roster_file=salt_ssh_roster_file, - target_host="localhost", - client_key=str(sshd_config_dir / "client_key"), - base_script_args=["--ignore-host-keys"], - ) diff --git a/tests/pytests/unit/__init__.py b/tests/pytests/unit/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/pytests/unit/conftest.py b/tests/pytests/unit/conftest.py deleted file mode 100644 index 7f70e46..0000000 --- a/tests/pytests/unit/conftest.py +++ /dev/null @@ -1,77 +0,0 @@ -import asyncio -import os -from unittest.mock import MagicMock -from unittest.mock import patch - -import pytest -import salt.config -import salt.transport.tcp - - -@pytest.fixture -def minion_opts(tmp_path): - """ - Default minion configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "minion" - opts = salt.config.DEFAULT_MINION_OPTS.copy() - opts["__role"] = "minion" - opts["root_dir"] = str(root_dir) - opts["master_uri"] = "tcp://127.0.0.1:{port}".format( # pylint: disable=consider-using-f-string - port=opts["master_port"] - ) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/minion.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "minion") - return opts - - -@pytest.fixture -def master_opts(tmp_path): - """ - Default master configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "master" - opts = salt.config.master_config(None) - opts["__role"] = "master" - opts["root_dir"] = str(root_dir) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/master.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "master") - return opts - - -@pytest.fixture -def syndic_opts(tmp_path): - """ - Default master configuration with relative temporary paths to not require root permissions. - """ - root_dir = tmp_path / "syndic" - opts = salt.config.DEFAULT_MINION_OPTS.copy() - opts["syndic_master"] = "127.0.0.1" - opts["__role"] = "minion" - opts["root_dir"] = str(root_dir) - for name in ("cachedir", "pki_dir", "sock_dir", "conf_dir"): - dirpath = root_dir / name - dirpath.mkdir(parents=True) - opts[name] = str(dirpath) - opts["log_file"] = "logs/syndic.log" - opts["conf_file"] = os.path.join(opts["conf_dir"], "syndic") - return opts - - -@pytest.fixture -def mocked_tcp_pub_client(): - transport = MagicMock(spec=salt.transport.tcp.TCPPubClient) - transport.connect = MagicMock() - future = asyncio.Future() - transport.connect.return_value = future - future.set_result(True) - with patch("salt.transport.tcp.TCPPubClient", transport): - yield diff --git a/tests/pytests/unit/states/__init__.py b/tests/pytests/unit/states/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/pytests/unit/states/test_grafana.py b/tests/pytests/unit/states/test_grafana.py deleted file mode 100644 index 603b99b..0000000 --- a/tests/pytests/unit/states/test_grafana.py +++ /dev/null @@ -1,122 +0,0 @@ -""" - :codeauthor: Jayesh Kariya -""" -from unittest.mock import MagicMock -from unittest.mock import patch - -import pytest -import salt.states.grafana as grafana -import salt.utils.json -from salt.exceptions import SaltInvocationError - - -@pytest.fixture -def configure_loader_modules(): - return {grafana: {}} - - -def test_dashboard_present(): - """ - Test to ensure the grafana dashboard exists and is managed. - """ - name = "myservice" - rows = ["systemhealth", "requests", "title"] - row = [{"panels": [{"id": "a"}], "title": "systemhealth"}] - - ret = {"name": name, "result": None, "changes": {}, "comment": ""} - - comt1 = ( - "Dashboard myservice is set to be updated. The following rows " # pylint: disable=consider-using-f-string - "set to be updated: {}".format(["systemhealth"]) - ) - pytest.raises(SaltInvocationError, grafana.dashboard_present, name, profile=False) - - pytest.raises(SaltInvocationError, grafana.dashboard_present, name, True, True) - - mock = MagicMock( - side_effect=[ - {"hosts": True, "index": False}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - ] - ) - mock_f = MagicMock(side_effect=[False, False, True, True, True, True]) - mock_t = MagicMock(return_value="") - mock_i = MagicMock(return_value=False) - source = {"dashboard": '["rows", {"rows":["baz", null, 1.0, 2]}]'} - mock_dict = MagicMock(return_value={"_source": source}) - with patch.dict( - grafana.__salt__, - { - "config.option": mock, - "elasticsearch.exists": mock_f, - "pillar.get": mock_t, - "elasticsearch.get": mock_dict, - "elasticsearch.index": mock_i, - }, - ): - pytest.raises(SaltInvocationError, grafana.dashboard_present, name) - - with patch.dict(grafana.__opts__, {"test": True}): - pytest.raises(SaltInvocationError, grafana.dashboard_present, name) - - comt = f"Dashboard {name} is set to be created." - ret.update({"comment": comt}) - assert grafana.dashboard_present(name, True) == ret - - mock = MagicMock(return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}) - with patch.object(salt.utils.json, "loads", mock): - ret.update({"comment": comt1, "result": None}) - assert grafana.dashboard_present(name, True, rows=row) == ret - - with patch.object(salt.utils.json, "loads", MagicMock(return_value={"rows": {}})): - pytest.raises( - SaltInvocationError, - grafana.dashboard_present, - name, - rows_from_pillar=rows, - ) - - comt = "Dashboard myservice is up to date" - ret.update({"comment": comt, "result": True}) - assert grafana.dashboard_present(name, True) == ret - - mock = MagicMock(return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}) - with patch.dict(grafana.__opts__, {"test": False}): - with patch.object(salt.utils.json, "loads", mock): - comt = "Failed to update dashboard myservice." - ret.update({"comment": comt, "result": False}) - assert grafana.dashboard_present(name, True, rows=row) == ret - - -def test_dashboard_absent(): - """ - Test to ensure the named grafana dashboard is deleted. - """ - name = "myservice" - - ret = {"name": name, "result": None, "changes": {}, "comment": ""} - - mock = MagicMock( - side_effect=[ - {"hosts": True, "index": False}, - {"hosts": True, "index": True}, - {"hosts": True, "index": True}, - ] - ) - mock_f = MagicMock(side_effect=[True, False]) - with patch.dict(grafana.__salt__, {"config.option": mock, "elasticsearch.exists": mock_f}): - pytest.raises(SaltInvocationError, grafana.dashboard_absent, name) - - with patch.dict(grafana.__opts__, {"test": True}): - comt = "Dashboard myservice is set to be removed." - ret.update({"comment": comt, "result": None}) - assert grafana.dashboard_absent(name) == ret - - comt = "Dashboard myservice does not exist." - ret.update({"comment": comt, "result": True}) - assert grafana.dashboard_absent(name) == ret diff --git a/tests/unit/modules/test_grafana.py b/tests/unit/modules/test_grafana.py deleted file mode 100644 index 1d73094..0000000 --- a/tests/unit/modules/test_grafana.py +++ /dev/null @@ -1,18 +0,0 @@ -import pytest -import salt.modules.test as testmod -import saltext.grafana.modules.grafana_mod as grafana_module - - -@pytest.fixture -def configure_loader_modules(): - module_globals = { - "__salt__": {"test.echo": testmod.echo}, - } - return { - grafana_module: module_globals, - } - - -def test_replace_this_this_with_something_meaningful(): - echo_str = "Echoed!" - assert grafana_module.example_function(echo_str) == echo_str diff --git a/tests/unit/states/test_grafana.py b/tests/unit/states/test_grafana.py index 3063dcb..603b99b 100644 --- a/tests/unit/states/test_grafana.py +++ b/tests/unit/states/test_grafana.py @@ -1,31 +1,122 @@ +""" + :codeauthor: Jayesh Kariya +""" +from unittest.mock import MagicMock +from unittest.mock import patch + import pytest -import salt.modules.test as testmod -import saltext.grafana.modules.grafana_mod as grafana_module -import saltext.grafana.states.grafana_mod as grafana_state +import salt.states.grafana as grafana +import salt.utils.json +from salt.exceptions import SaltInvocationError @pytest.fixture def configure_loader_modules(): - return { - grafana_module: { - "__salt__": { - "test.echo": testmod.echo, - }, - }, - grafana_state: { - "__salt__": { - "grafana.example_function": grafana_module.example_function, - }, + return {grafana: {}} + + +def test_dashboard_present(): + """ + Test to ensure the grafana dashboard exists and is managed. + """ + name = "myservice" + rows = ["systemhealth", "requests", "title"] + row = [{"panels": [{"id": "a"}], "title": "systemhealth"}] + + ret = {"name": name, "result": None, "changes": {}, "comment": ""} + + comt1 = ( + "Dashboard myservice is set to be updated. The following rows " # pylint: disable=consider-using-f-string + "set to be updated: {}".format(["systemhealth"]) + ) + pytest.raises(SaltInvocationError, grafana.dashboard_present, name, profile=False) + + pytest.raises(SaltInvocationError, grafana.dashboard_present, name, True, True) + + mock = MagicMock( + side_effect=[ + {"hosts": True, "index": False}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + ] + ) + mock_f = MagicMock(side_effect=[False, False, True, True, True, True]) + mock_t = MagicMock(return_value="") + mock_i = MagicMock(return_value=False) + source = {"dashboard": '["rows", {"rows":["baz", null, 1.0, 2]}]'} + mock_dict = MagicMock(return_value={"_source": source}) + with patch.dict( + grafana.__salt__, + { + "config.option": mock, + "elasticsearch.exists": mock_f, + "pillar.get": mock_t, + "elasticsearch.get": mock_dict, + "elasticsearch.index": mock_i, }, - } - - -def test_replace_this_this_with_something_meaningful(): - echo_str = "Echoed!" - expected = { - "name": echo_str, - "changes": {}, - "result": True, - "comment": f"The 'grafana.example_function' returned: '{echo_str}'", - } - assert grafana_state.exampled(echo_str) == expected + ): + pytest.raises(SaltInvocationError, grafana.dashboard_present, name) + + with patch.dict(grafana.__opts__, {"test": True}): + pytest.raises(SaltInvocationError, grafana.dashboard_present, name) + + comt = f"Dashboard {name} is set to be created." + ret.update({"comment": comt}) + assert grafana.dashboard_present(name, True) == ret + + mock = MagicMock(return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}) + with patch.object(salt.utils.json, "loads", mock): + ret.update({"comment": comt1, "result": None}) + assert grafana.dashboard_present(name, True, rows=row) == ret + + with patch.object(salt.utils.json, "loads", MagicMock(return_value={"rows": {}})): + pytest.raises( + SaltInvocationError, + grafana.dashboard_present, + name, + rows_from_pillar=rows, + ) + + comt = "Dashboard myservice is up to date" + ret.update({"comment": comt, "result": True}) + assert grafana.dashboard_present(name, True) == ret + + mock = MagicMock(return_value={"rows": [{"panels": "b", "title": "systemhealth"}]}) + with patch.dict(grafana.__opts__, {"test": False}): + with patch.object(salt.utils.json, "loads", mock): + comt = "Failed to update dashboard myservice." + ret.update({"comment": comt, "result": False}) + assert grafana.dashboard_present(name, True, rows=row) == ret + + +def test_dashboard_absent(): + """ + Test to ensure the named grafana dashboard is deleted. + """ + name = "myservice" + + ret = {"name": name, "result": None, "changes": {}, "comment": ""} + + mock = MagicMock( + side_effect=[ + {"hosts": True, "index": False}, + {"hosts": True, "index": True}, + {"hosts": True, "index": True}, + ] + ) + mock_f = MagicMock(side_effect=[True, False]) + with patch.dict(grafana.__salt__, {"config.option": mock, "elasticsearch.exists": mock_f}): + pytest.raises(SaltInvocationError, grafana.dashboard_absent, name) + + with patch.dict(grafana.__opts__, {"test": True}): + comt = "Dashboard myservice is set to be removed." + ret.update({"comment": comt, "result": None}) + assert grafana.dashboard_absent(name) == ret + + comt = "Dashboard myservice does not exist." + ret.update({"comment": comt, "result": True}) + assert grafana.dashboard_absent(name) == ret diff --git a/tests/pytests/unit/states/test_grafana_datasource.py b/tests/unit/states/test_grafana_datasource.py similarity index 100% rename from tests/pytests/unit/states/test_grafana_datasource.py rename to tests/unit/states/test_grafana_datasource.py