From 173357a07c3f3720a0953613121efbe753b5fcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?nils=20m=C3=A5s=C3=A9n?= Date: Sun, 29 Jan 2023 16:16:10 +0100 Subject: [PATCH] add tests for json template --- pkg/notifications/json.go | 6 +- pkg/notifications/json_test.go | 118 +++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 pkg/notifications/json_test.go diff --git a/pkg/notifications/json.go b/pkg/notifications/json.go index a750862..a6e3821 100644 --- a/pkg/notifications/json.go +++ b/pkg/notifications/json.go @@ -44,10 +44,10 @@ func marshalReports(reports []t.ContainerReport) []JSONMap { jsonReports := make([]JSONMap, len(reports)) for i, report := range reports { jsonReports[i] = JSONMap{ - `id`: report.ID(), + `id`: report.ID().ShortID(), `name`: report.Name(), - `currentImageId`: report.CurrentImageID(), - `latestImageId`: report.LatestImageID(), + `currentImageId`: report.CurrentImageID().ShortID(), + `latestImageId`: report.LatestImageID().ShortID(), `imageName`: report.ImageName(), `state`: report.State(), } diff --git a/pkg/notifications/json_test.go b/pkg/notifications/json_test.go new file mode 100644 index 0000000..ef30c59 --- /dev/null +++ b/pkg/notifications/json_test.go @@ -0,0 +1,118 @@ +package notifications + +import ( + s "github.com/containrrr/watchtower/pkg/session" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("JSON template", func() { + When("using report templates", func() { + When("JSON template is used", func() { + It("should format the messages to the expected format", func() { + expected := `{ + "entries": [ + { + "data": null, + "level": "info", + "message": "foo Bar", + "time": "0001-01-01T00:00:00Z" + } + ], + "host": "Mock", + "report": { + "failed": [ + { + "currentImageId": "01d210000000", + "error": "accidentally the whole container", + "id": "c79210000000", + "imageName": "mock/fail1:latest", + "latestImageId": "d0a210000000", + "name": "fail1", + "state": "Failed" + } + ], + "fresh": [ + { + "currentImageId": "01d310000000", + "id": "c79310000000", + "imageName": "mock/frsh1:latest", + "latestImageId": "01d310000000", + "name": "frsh1", + "state": "Fresh" + } + ], + "scanned": [ + { + "currentImageId": "01d110000000", + "id": "c79110000000", + "imageName": "mock/updt1:latest", + "latestImageId": "d0a110000000", + "name": "updt1", + "state": "Updated" + }, + { + "currentImageId": "01d120000000", + "id": "c79120000000", + "imageName": "mock/updt2:latest", + "latestImageId": "d0a120000000", + "name": "updt2", + "state": "Updated" + }, + { + "currentImageId": "01d210000000", + "error": "accidentally the whole container", + "id": "c79210000000", + "imageName": "mock/fail1:latest", + "latestImageId": "d0a210000000", + "name": "fail1", + "state": "Failed" + }, + { + "currentImageId": "01d310000000", + "id": "c79310000000", + "imageName": "mock/frsh1:latest", + "latestImageId": "01d310000000", + "name": "frsh1", + "state": "Fresh" + } + ], + "skipped": [ + { + "currentImageId": "01d410000000", + "error": "unpossible", + "id": "c79410000000", + "imageName": "mock/skip1:latest", + "latestImageId": "01d410000000", + "name": "skip1", + "state": "Skipped" + } + ], + "stale": [], + "updated": [ + { + "currentImageId": "01d110000000", + "id": "c79110000000", + "imageName": "mock/updt1:latest", + "latestImageId": "d0a110000000", + "name": "updt1", + "state": "Updated" + }, + { + "currentImageId": "01d120000000", + "id": "c79120000000", + "imageName": "mock/updt2:latest", + "latestImageId": "d0a120000000", + "name": "updt2", + "state": "Updated" + } + ] + }, + "title": "Watchtower updates on Mock" +}` + data := mockDataFromStates(s.UpdatedState, s.FreshState, s.FailedState, s.SkippedState, s.UpdatedState) + Expect(getTemplatedResult(`json.v1`, false, data)).To(MatchJSON(expected)) + }) + }) + }) +})