Skip to content

Commit

Permalink
add mocks for datasource_by_name
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-s-webb committed Oct 9, 2024
1 parent 539d502 commit fd004e1
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions tests/unit/modules/grafana/grafana_silence/test_grafana_silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def get_version_resp():
return {"major": 10, "minor": 0, "rev": 0}


def get_datasource_resp():
server_response = json.dumps({"uid": "AB981B38A76F"})
return (MockedReponse(server_response), {"status": 200})


class GrafanaSilenceTest(TestCase):
def setUp(self):
self.authorization = basic_auth_header("admin", "changeme")
Expand Down Expand Up @@ -171,11 +176,18 @@ def test_create_silence_new_silence(
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_version"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.datasource_by_name"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.fetch_url"
)
def test_create_silence_new_silence_with_datasource(
self, mock_fetch_url, mock_get_version, mock_get_silence
self,
mock_fetch_url,
mock_get_version,
mock_get_silence,
mock_datasource_by_name,
):
set_module_args(
{
Expand All @@ -200,6 +212,7 @@ def test_create_silence_new_silence_with_datasource(
)
module = grafana_silence.setup_module_object()
mock_get_version.return_value = get_version_resp()
mock_datasource_by_name.return_value = get_datasource_resp()
mock_fetch_url.return_value = silence_created_resp()
mock_get_silence.return_value = silence_get_resp()

Expand All @@ -220,7 +233,7 @@ def test_create_silence_new_silence_with_datasource(
)
mock_fetch_url.assert_called_with(
module,
"https://grafana.example.com/api/alertmanager/testds/api/v2/silences",
"https://grafana.example.com/api/alertmanager/AB981B38A76F/api/v2/silences",
data=json.dumps(
{
"comment": "a testcomment",
Expand All @@ -246,7 +259,6 @@ def test_create_silence_new_silence_with_datasource(
)
self.assertEquals(result, {"silenceID": "470b7116-8f06-4bb6-9e6c-6258aa92218e"})


@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_version"
)
Expand Down Expand Up @@ -296,10 +308,15 @@ def test_delete_silence(self, mock_fetch_url, mock_get_version):
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_version"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.datasource_by_name"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.fetch_url"
)
def test_delete_silence_with_datasource(self, mock_fetch_url, mock_get_version):
def test_delete_silence_with_datasource(
self, mock_fetch_url, mock_get_version, mock_datasource_by_name
):
set_module_args(
{
"url": "https://grafana.example.com",
Expand All @@ -324,13 +341,14 @@ def test_delete_silence_with_datasource(self, mock_fetch_url, mock_get_version):
module = grafana_silence.setup_module_object()
mock_fetch_url.return_value = silence_deleted_resp()
mock_get_version.return_value = get_version_resp()
mock_datasource_by_name.return_value = get_datasource_resp()

grafana_iface = grafana_silence.GrafanaSilenceInterface(module)
silence_id = "470b7116-8f06-4bb6-9e6c-6258aa92218e"
result = grafana_iface.delete_silence(silence_id)
mock_fetch_url.assert_called_with(
module,
"https://grafana.example.com/api/alertmanager/testds/api/v2/silence/470b7116-8f06-4bb6-9e6c-6258aa92218e",
"https://grafana.example.com/api/alertmanager/AB981B38A76F/api/v2/silence/470b7116-8f06-4bb6-9e6c-6258aa92218e",
data=None,
headers={
"Content-Type": "application/json",
Expand Down

0 comments on commit fd004e1

Please sign in to comment.