mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-14 06:06:38 +01:00
Session report collection and report templates (#981)
* wip: notification stats * make report notifications optional * linting/documentation fixes * linting/documentation fixes * merge types.Container and container.Interface * smaller naming/format fixes * use typed image/container IDs * simplify notifier and update tests * add missed doc comments * lint fixes * remove unused constructors * rename old/new current/latest
This commit is contained in:
parent
d0ecc23d72
commit
e3dd8d688a
32 changed files with 853 additions and 598 deletions
|
|
@ -2,169 +2,226 @@ package notifications
|
|||
|
||||
import (
|
||||
"github.com/containrrr/shoutrrr/pkg/types"
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/containrrr/watchtower/internal/actions/mocks"
|
||||
"github.com/containrrr/watchtower/internal/flags"
|
||||
log "github.com/sirupsen/logrus"
|
||||
s "github.com/containrrr/watchtower/pkg/session"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/gbytes"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestShoutrrrDefaultTemplate(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: getShoutrrrTemplate(cmd),
|
||||
}
|
||||
|
||||
entries := []*log.Entry{
|
||||
var legacyMockData = Data{
|
||||
Entries: []*logrus.Entry{
|
||||
{
|
||||
Message: "foo bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(entries)
|
||||
|
||||
require.Equal(t, "foo bar\n", s)
|
||||
}
|
||||
|
||||
func TestShoutrrrTemplate(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
flags.RegisterNotificationFlags(cmd)
|
||||
err := cmd.ParseFlags([]string{"--notification-template={{range .}}{{.Level}}: {{.Message}}{{println}}{{end}}"})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: getShoutrrrTemplate(cmd),
|
||||
}
|
||||
|
||||
entries := []*log.Entry{
|
||||
{
|
||||
Level: log.InfoLevel,
|
||||
Message: "foo bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(entries)
|
||||
|
||||
require.Equal(t, "info: foo bar\n", s)
|
||||
}
|
||||
|
||||
func TestShoutrrrStringFunctions(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
flags.RegisterNotificationFlags(cmd)
|
||||
err := cmd.ParseFlags([]string{"--notification-template={{range .}}{{.Level | printf \"%v\" | ToUpper }}: {{.Message | ToLower }} {{.Message | Title }}{{println}}{{end}}"})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: getShoutrrrTemplate(cmd),
|
||||
}
|
||||
|
||||
entries := []*log.Entry{
|
||||
{
|
||||
Level: log.InfoLevel,
|
||||
Level: logrus.InfoLevel,
|
||||
Message: "foo Bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(entries)
|
||||
|
||||
require.Equal(t, "INFO: foo bar Foo Bar\n", s)
|
||||
},
|
||||
}
|
||||
|
||||
func TestShoutrrrInvalidTemplateUsesTemplate(t *testing.T) {
|
||||
cmd := new(cobra.Command)
|
||||
|
||||
flags.RegisterNotificationFlags(cmd)
|
||||
err := cmd.ParseFlags([]string{"--notification-template={{"})
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: getShoutrrrTemplate(cmd),
|
||||
func mockDataFromStates(states ...s.State) Data {
|
||||
return Data{
|
||||
Entries: legacyMockData.Entries,
|
||||
Report: mocks.CreateMockProgressReport(states...),
|
||||
}
|
||||
|
||||
shoutrrrDefault := &shoutrrrTypeNotifier{
|
||||
template: template.Must(template.New("").Parse(shoutrrrDefaultTemplate)),
|
||||
}
|
||||
|
||||
entries := []*log.Entry{
|
||||
{
|
||||
Message: "foo bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(entries)
|
||||
sd := shoutrrrDefault.buildMessage(entries)
|
||||
|
||||
require.Equal(t, sd, s)
|
||||
}
|
||||
|
||||
var _ = Describe("Shoutrrr", func() {
|
||||
var logBuffer *gbytes.Buffer
|
||||
|
||||
BeforeEach(func() {
|
||||
logBuffer = gbytes.NewBuffer()
|
||||
logrus.SetOutput(logBuffer)
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
DisableColors: true,
|
||||
DisableTimestamp: true,
|
||||
})
|
||||
})
|
||||
|
||||
When("using legacy templates", func() {
|
||||
|
||||
When("no custom template is provided", func() {
|
||||
It("should format the messages using the default template", func() {
|
||||
cmd := new(cobra.Command)
|
||||
flags.RegisterNotificationFlags(cmd)
|
||||
|
||||
shoutrrr := createNotifier([]string{}, logrus.AllLevels, "", true)
|
||||
|
||||
entries := []*logrus.Entry{
|
||||
{
|
||||
Message: "foo bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(Data{Entries: entries})
|
||||
|
||||
Expect(s).To(Equal("foo bar\n"))
|
||||
})
|
||||
})
|
||||
When("given a valid custom template", func() {
|
||||
It("should format the messages using the custom template", func() {
|
||||
|
||||
tplString := `{{range .}}{{.Level}}: {{.Message}}{{println}}{{end}}`
|
||||
tpl, err := getShoutrrrTemplate(tplString, true)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: tpl,
|
||||
legacyTemplate: true,
|
||||
}
|
||||
|
||||
entries := []*logrus.Entry{
|
||||
{
|
||||
Level: logrus.InfoLevel,
|
||||
Message: "foo bar",
|
||||
},
|
||||
}
|
||||
|
||||
s := shoutrrr.buildMessage(Data{Entries: entries})
|
||||
|
||||
Expect(s).To(Equal("info: foo bar\n"))
|
||||
})
|
||||
})
|
||||
|
||||
When("given an invalid custom template", func() {
|
||||
It("should format the messages using the default template", func() {
|
||||
invNotif, err := createNotifierWithTemplate(`{{ intentionalSyntaxError`, true)
|
||||
Expect(err).To(HaveOccurred())
|
||||
|
||||
defNotif, err := createNotifierWithTemplate(``, true)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
|
||||
Expect(invNotif.buildMessage(legacyMockData)).To(Equal(defNotif.buildMessage(legacyMockData)))
|
||||
})
|
||||
})
|
||||
|
||||
When("given a template that is using ToUpper function", func() {
|
||||
It("should return the text in UPPER CASE", func() {
|
||||
tplString := `{{range .}}{{ .Message | ToUpper }}{{end}}`
|
||||
Expect(getTemplatedResult(tplString, true, legacyMockData)).To(Equal("FOO BAR"))
|
||||
})
|
||||
})
|
||||
|
||||
When("given a template that is using ToLower function", func() {
|
||||
It("should return the text in lower case", func() {
|
||||
tplString := `{{range .}}{{ .Message | ToLower }}{{end}}`
|
||||
Expect(getTemplatedResult(tplString, true, legacyMockData)).To(Equal("foo bar"))
|
||||
})
|
||||
})
|
||||
|
||||
When("given a template that is using Title function", func() {
|
||||
It("should return the text in Title Case", func() {
|
||||
tplString := `{{range .}}{{ .Message | Title }}{{end}}`
|
||||
Expect(getTemplatedResult(tplString, true, legacyMockData)).To(Equal("Foo Bar"))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
When("using report templates", func() {
|
||||
|
||||
When("no custom template is provided", func() {
|
||||
It("should format the messages using the default template", func() {
|
||||
expected := `4 Scanned, 2 Updated, 1 Failed
|
||||
- updt1 (mock/updt1:latest): 01d110000000 updated to d0a110000000
|
||||
- updt2 (mock/updt2:latest): 01d120000000 updated to d0a120000000
|
||||
- frsh1 (mock/frsh1:latest): Fresh
|
||||
- skip1 (mock/skip1:latest): Skipped: unpossible
|
||||
- fail1 (mock/fail1:latest): Failed: accidentally the whole container
|
||||
`
|
||||
data := mockDataFromStates(s.UpdatedState, s.FreshState, s.FailedState, s.SkippedState, s.UpdatedState)
|
||||
Expect(getTemplatedResult(``, false, data)).To(Equal(expected))
|
||||
})
|
||||
|
||||
It("should format the messages using the default template", func() {
|
||||
expected := `1 Scanned, 0 Updated, 0 Failed
|
||||
- frsh1 (mock/frsh1:latest): Fresh
|
||||
`
|
||||
data := mockDataFromStates(s.FreshState)
|
||||
Expect(getTemplatedResult(``, false, data)).To(Equal(expected))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
When("sending notifications", func() {
|
||||
|
||||
It("SlowNotificationNotSent", func() {
|
||||
_, blockingRouter := sendNotificationsWithBlockingRouter(true)
|
||||
|
||||
Eventually(blockingRouter.sent).Should(Not(Receive()))
|
||||
|
||||
})
|
||||
|
||||
It("SlowNotificationSent", func() {
|
||||
shoutrrr, blockingRouter := sendNotificationsWithBlockingRouter(true)
|
||||
|
||||
blockingRouter.unlock <- true
|
||||
shoutrrr.Close()
|
||||
|
||||
Eventually(blockingRouter.sent).Should(Receive(BeTrue()))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
type blockingRouter struct {
|
||||
unlock chan bool
|
||||
sent chan bool
|
||||
}
|
||||
|
||||
func (b blockingRouter) Send(message string, params *types.Params) []error {
|
||||
func (b blockingRouter) Send(_ string, _ *types.Params) []error {
|
||||
_ = <-b.unlock
|
||||
b.sent <- true
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestSlowNotificationNotSent(t *testing.T) {
|
||||
_, blockingRouter := sendNotificationsWithBlockingRouter()
|
||||
|
||||
notifSent := false
|
||||
select {
|
||||
case notifSent = <-blockingRouter.sent:
|
||||
default:
|
||||
}
|
||||
|
||||
require.Equal(t, false, notifSent)
|
||||
}
|
||||
|
||||
func TestSlowNotificationSent(t *testing.T) {
|
||||
shoutrrr, blockingRouter := sendNotificationsWithBlockingRouter()
|
||||
|
||||
blockingRouter.unlock <- true
|
||||
shoutrrr.Close()
|
||||
|
||||
notifSent := false
|
||||
select {
|
||||
case notifSent = <-blockingRouter.sent:
|
||||
default:
|
||||
}
|
||||
require.Equal(t, true, notifSent)
|
||||
}
|
||||
|
||||
func sendNotificationsWithBlockingRouter() (*shoutrrrTypeNotifier, *blockingRouter) {
|
||||
cmd := new(cobra.Command)
|
||||
func sendNotificationsWithBlockingRouter(legacy bool) (*shoutrrrTypeNotifier, *blockingRouter) {
|
||||
|
||||
router := &blockingRouter{
|
||||
unlock: make(chan bool, 1),
|
||||
sent: make(chan bool, 1),
|
||||
}
|
||||
|
||||
tpl, err := getShoutrrrTemplate("", legacy)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
shoutrrr := &shoutrrrTypeNotifier{
|
||||
template: getShoutrrrTemplate(cmd),
|
||||
messages: make(chan string, 1),
|
||||
done: make(chan bool),
|
||||
Router: router,
|
||||
template: tpl,
|
||||
messages: make(chan string, 1),
|
||||
done: make(chan bool),
|
||||
Router: router,
|
||||
legacyTemplate: legacy,
|
||||
}
|
||||
|
||||
entry := &log.Entry{
|
||||
entry := &logrus.Entry{
|
||||
Message: "foo bar",
|
||||
}
|
||||
|
||||
go sendNotifications(shoutrrr)
|
||||
|
||||
shoutrrr.StartNotification()
|
||||
shoutrrr.Fire(entry)
|
||||
_ = shoutrrr.Fire(entry)
|
||||
|
||||
shoutrrr.SendNotification()
|
||||
shoutrrr.SendNotification(nil)
|
||||
|
||||
return shoutrrr, router
|
||||
}
|
||||
|
||||
func createNotifierWithTemplate(tplString string, legacy bool) (*shoutrrrTypeNotifier, error) {
|
||||
tpl, err := getShoutrrrTemplate(tplString, legacy)
|
||||
|
||||
return &shoutrrrTypeNotifier{
|
||||
template: tpl,
|
||||
legacyTemplate: legacy,
|
||||
}, err
|
||||
}
|
||||
|
||||
func getTemplatedResult(tplString string, legacy bool, data Data) (string, error) {
|
||||
notifier, err := createNotifierWithTemplate(tplString, legacy)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return notifier.buildMessage(data), err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue