Skip to content

Commit

Permalink
add tests for targeting alertmanager datasource for silences
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-s-webb committed Oct 4, 2024
1 parent 4c7340c commit a13d474
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions tests/unit/modules/grafana/grafana_silence/test_grafana_silence.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,89 @@ def test_create_silence_new_silence(
)
self.assertEquals(result, {"silenceID": "470b7116-8f06-4bb6-9e6c-6258aa92218e"})

# create a new silence with alertmanager datasource defined
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_silence"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_version"
)
@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
):
set_module_args(
{
"url": "https://grafana.example.com",
"url_username": "admin",
"url_password": "changeme",
"comment": "a testcomment",
"created_by": "me",
"starts_at": "2029-07-29T08:45:45.000Z",
"ends_at": "2029-07-29T08:55:45.000Z",
"alertmanager_datasource": "testds",
"matchers": [
{
"isEqual": True,
"isRegex": True,
"name": "environment",
"value": "test",
}
],
"state": "present",
}
)
module = grafana_silence.setup_module_object()
mock_get_version.return_value = get_version_resp()
mock_fetch_url.return_value = silence_created_resp()
mock_get_silence.return_value = silence_get_resp()

grafana_iface = grafana_silence.GrafanaSilenceInterface(module)
result = grafana_iface.create_silence(
"a testcomment",
"me",
"2029-07-29T08:45:45.000Z",
"2029-07-29T08:55:45.000Z",
[
{
"isEqual": True,
"isRegex": True,
"name": "environment",
"value": "test",
}
],
)
mock_fetch_url.assert_called_with(
module,
"https://grafana.example.com/api/alertmanager/testds/api/v2/silences",
data=json.dumps(
{
"comment": "a testcomment",
"createdBy": "me",
"startsAt": "2029-07-29T08:45:45.000Z",
"endsAt": "2029-07-29T08:55:45.000Z",
"matchers": [
{
"isEqual": True,
"isRegex": True,
"name": "environment",
"value": "test",
}
],
},
sort_keys=True,
),
headers={
"Content-Type": "application/json",
"Authorization": self.authorization,
},
method="POST",
)
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 @@ -209,3 +292,50 @@ def test_delete_silence(self, mock_fetch_url, mock_get_version):
method="DELETE",
)
self.assertEquals(result, {"message": "silence deleted"})

@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.GrafanaSilenceInterface.get_version"
)
@patch(
"ansible_collections.community.grafana.plugins.modules.grafana_silence.fetch_url"
)
def test_delete_silence_with_datasource(self, mock_fetch_url, mock_get_version):
set_module_args(
{
"url": "https://grafana.example.com",
"url_username": "admin",
"url_password": "changeme",
"comment": "a testcomment",
"created_by": "me",
"ends_at": "2029-07-29T08:55:45.000Z",
"alertmanager_datasource": "testds",
"matchers": [
{
"isEqual": True,
"isRegex": True,
"name": "environment",
"value": "test",
}
],
"starts_at": "2029-07-29T08:45:45.000Z",
"state": "present",
}
)
module = grafana_silence.setup_module_object()
mock_fetch_url.return_value = silence_deleted_resp()
mock_get_version.return_value = get_version_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",
data=None,
headers={
"Content-Type": "application/json",
"Authorization": self.authorization,
},
method="DELETE",
)
self.assertEquals(result, {"message": "silence deleted"})

0 comments on commit a13d474

Please sign in to comment.