From 589ce75ae6395b3e94a5d034dcd19418c224e116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Fri, 7 Sep 2018 23:26:04 +0100 Subject: [PATCH] feat(tests): more backend tests --- internal/models/alertgroup_test.go | 7 ++ internal/models/api_test.go | 147 +++++++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 internal/models/api_test.go diff --git a/internal/models/alertgroup_test.go b/internal/models/alertgroup_test.go index 517914028..9ee327acb 100644 --- a/internal/models/alertgroup_test.go +++ b/internal/models/alertgroup_test.go @@ -179,3 +179,10 @@ func TestAlertGroupContentFingerprint(t *testing.T) { }) } } + +func TestFingerprint(t *testing.T) { + ag := models.AlertGroup{} + if ag.LabelsFingerprint() == ag.ContentFingerprint() { + t.Errorf("Expected LabelsFingerprint and ContentFingerprint to return different values") + } +} diff --git a/internal/models/api_test.go b/internal/models/api_test.go new file mode 100644 index 000000000..f4a0e6e11 --- /dev/null +++ b/internal/models/api_test.go @@ -0,0 +1,147 @@ +package models_test + +import ( + "encoding/json" + "testing" + + "github.com/prymitive/unsee/internal/models" +) + +func TestDedupSharedMaps(t *testing.T) { + ag := models.APIAlertGroup{ + AlertGroup: models.AlertGroup{ + Labels: map[string]string{ + "alertname": "FakeAlert", + }, + Alerts: models.AlertList{ + models.Alert{ + Annotations: models.Annotations{ + models.Annotation{ + Name: "summary", + Value: "this is summary", + }, + models.Annotation{ + Name: "foo", + Value: "bar", + }, + }, + Labels: map[string]string{ + "alertname": "FakeAlert", + "job": "node_exporter", + "instance": "1", + }, + }, + models.Alert{ + Annotations: models.Annotations{ + models.Annotation{ + Name: "summary", + Value: "this is summary", + }, + }, + Labels: map[string]string{ + "alertname": "FakeAlert", + "job": "node_exporter", + "instance": "2", + }, + }, + models.Alert{ + Annotations: models.Annotations{ + models.Annotation{ + Name: "summary", + Value: "this is summary", + }, + }, + Labels: map[string]string{ + "alertname": "FakeAlert", + "job": "blackbox", + "instance": "3", + }, + }, + }, + }, + } + ag.DedupSharedMaps() + + expectedJSON := `{ + "receiver": "", + "labels": { + "alertname": "FakeAlert" + }, + "alerts": [ + { + "annotations": [ + { + "name": "foo", + "value": "bar", + "visible": false, + "isLink": false + } + ], + "labels": { + "instance": "1", + "job": "node_exporter" + }, + "startsAt": "0001-01-01T00:00:00Z", + "endsAt": "0001-01-01T00:00:00Z", + "state": "", + "alertmanager": null, + "receiver": "" + }, + { + "annotations": [], + "labels": { + "instance": "2", + "job": "node_exporter" + }, + "startsAt": "0001-01-01T00:00:00Z", + "endsAt": "0001-01-01T00:00:00Z", + "state": "", + "alertmanager": null, + "receiver": "" + }, + { + "annotations": [], + "labels": { + "instance": "3", + "job": "blackbox" + }, + "startsAt": "0001-01-01T00:00:00Z", + "endsAt": "0001-01-01T00:00:00Z", + "state": "", + "alertmanager": null, + "receiver": "" + } + ], + "id": "", + "hash": "", + "alertmanagerCount": null, + "stateCount": null, + "shared": { + "annotations": [ + { + "name": "summary", + "value": "this is summary", + "visible": false, + "isLink": false + } + ], + "labels": {} + } +}` + + agJSON, _ := json.MarshalIndent(ag, "", " ") + if string(agJSON) != expectedJSON { + t.Errorf("Expected: %s\nGot: %s\n", expectedJSON, string(agJSON)) + } +} + +func TestDedupSharedMapsWithSingleAlert(t *testing.T) { + ag := models.APIAlertGroup{ + AlertGroup: models.AlertGroup{ + Alerts: models.AlertList{ + models.Alert{}, + }, + }, + } + ag.DedupSharedMaps() +}