-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(logger): avoid mutating passed params (#184)
this fixes an old bug where the logger, when combined with other services would add the param `message` to other services' `Send()` (which they usually don't like) also adds tests for this and for normal operation
- Loading branch information
Showing
2 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package logger_test | ||
|
||
import ( | ||
unit "github.com/containrrr/shoutrrr/pkg/services/logger" | ||
"github.com/containrrr/shoutrrr/pkg/types" | ||
|
||
"github.com/containrrr/shoutrrr/pkg/util" | ||
"github.com/onsi/gomega/gbytes" | ||
|
||
"log" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLogger(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Logger Suite") | ||
} | ||
|
||
var _ = Describe("the logger service", func() { | ||
|
||
When("sending a notification", func() { | ||
|
||
It("should output the message to the log", func() { | ||
logbuf := gbytes.NewBuffer() | ||
service := &unit.Service{} | ||
_ = service.Initialize(util.URLMust(`logger://`), log.New(logbuf, "", 0)) | ||
|
||
err := service.Send(`Failed - Requires Toaster Repair Level 10`, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Eventually(logbuf).Should(gbytes.Say("Failed - Requires Toaster Repair Level 10")) | ||
}) | ||
|
||
It("should not mutate the passed params", func() { | ||
service := &unit.Service{} | ||
_ = service.Initialize(util.URLMust(`logger://`), nil) | ||
params := types.Params{} | ||
err := service.Send(`Failed - Requires Toaster Repair Level 10`, ¶ms) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(params).To(BeEmpty()) | ||
}) | ||
|
||
When("when a template has been added", func() { | ||
It("should render template with params", func() { | ||
logbuf := gbytes.NewBuffer() | ||
service := &unit.Service{} | ||
_ = service.Initialize(util.URLMust(`logger://`), log.New(logbuf, "", 0)) | ||
err := service.SetTemplateString(`message`, `{{.level}}: {{.message}}`) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
params := types.Params{ | ||
"level": "warning", | ||
} | ||
err = service.Send(`Requires Toaster Repair Level 10`, ¶ms) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Eventually(logbuf).Should(gbytes.Say("warning: Requires Toaster Repair Level 10")) | ||
}) | ||
}) | ||
}) | ||
}) |