- Notifications
You must be signed in to change notification settings - Fork927
chore: improve notification template tests' resilience#14196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 6 additions & 4 deletionscoderd/autobuild/lifecycle_executor.go
This file contains hidden or 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
4 changes: 4 additions & 0 deletionscoderd/database/migrations/000240_notification_workspace_updated_version_message.down.sql
This file contains hidden or 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,4 @@ | ||
UPDATE notification_templates | ||
SET body_template = E'Hi {{.UserName}}\n' || | ||
E'Your workspace **{{.Labels.name}}** has been updated automatically to the latest template version ({{.Labels.template_version_name}}).' | ||
WHERE id = 'c34a0c09-0704-4cac-bd1c-0c0146811c2b'; | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. |
6 changes: 6 additions & 0 deletionscoderd/database/migrations/000240_notification_workspace_updated_version_message.up.sql
This file contains hidden or 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,6 @@ | ||
UPDATE notification_templates | ||
SET name = 'Workspace Updated Automatically', -- drive-by fix for capitalization to match other templates | ||
body_template = E'Hi {{.UserName}}\n' || | ||
E'Your workspace **{{.Labels.name}}** has been updated automatically to the latest template version ({{.Labels.template_version_name}}).\n' || | ||
E'Reason for update: **{{.Labels.template_version_message}}**' -- include template version message | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
WHERE id = 'c34a0c09-0704-4cac-bd1c-0c0146811c2b'; |
2 changes: 1 addition & 1 deletioncoderd/notifications/events.go
This file contains hidden or 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
97 changes: 87 additions & 10 deletionscoderd/notifications/notifications_test.go
This file contains hidden or 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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package notifications_test | ||
import ( | ||
"bytes" | ||
"context" | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
@@ -606,7 +611,42 @@ func TestNotifierPaused(t *testing.T) { | ||
}, testutil.WaitShort, testutil.IntervalFast) | ||
} | ||
//go:embed events.go | ||
var events []byte | ||
// enumerateAllTemplates gets all the template names from the coderd/notifications/events.go file. | ||
// TODO(dannyk): use code-generation to create a list of all templates: https://github.com/coder/team-coconut/issues/36 | ||
func enumerateAllTemplates(t *testing.T) ([]string, error) { | ||
t.Helper() | ||
fset := token.NewFileSet() | ||
node, err := parser.ParseFile(fset, "", bytes.NewBuffer(events), parser.AllErrors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var out []string | ||
// Traverse the AST and extract variable names. | ||
ast.Inspect(node, func(n ast.Node) bool { | ||
// Check if the node is a declaration statement. | ||
if decl, ok := n.(*ast.GenDecl); ok && decl.Tok == token.VAR { | ||
for _, spec := range decl.Specs { | ||
// Type assert the spec to a ValueSpec. | ||
if valueSpec, ok := spec.(*ast.ValueSpec); ok { | ||
for _, name := range valueSpec.Names { | ||
out = append(out, name.String()) | ||
} | ||
} | ||
} | ||
} | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return true | ||
}) | ||
return out, nil | ||
} | ||
func TestNotificationTemplatesCanRender(t *testing.T) { | ||
t.Parallel() | ||
if !dbtestutil.WillUsePostgres() { | ||
@@ -647,10 +687,11 @@ func TestNotificationTemplatesBody(t *testing.T) { | ||
payload: types.MessagePayload{ | ||
UserName: "bobby", | ||
Labels: map[string]string{ | ||
"name": "bobby-workspace", | ||
"reason": "breached the template's threshold for inactivity", | ||
"initiator": "autobuild", | ||
"dormancyHours": "24", | ||
"timeTilDormant": "24h", | ||
}, | ||
}, | ||
}, | ||
@@ -660,8 +701,9 @@ func TestNotificationTemplatesBody(t *testing.T) { | ||
payload: types.MessagePayload{ | ||
UserName: "bobby", | ||
Labels: map[string]string{ | ||
"name": "bobby-workspace", | ||
"template_version_name": "1.0", | ||
"template_version_message": "template now includes catnip", | ||
}, | ||
}, | ||
}, | ||
@@ -671,12 +713,46 @@ func TestNotificationTemplatesBody(t *testing.T) { | ||
payload: types.MessagePayload{ | ||
UserName: "bobby", | ||
Labels: map[string]string{ | ||
"name": "bobby-workspace", | ||
"reason": "template updated to new dormancy policy", | ||
"dormancyHours": "24", | ||
"timeTilDormant": "24h", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TemplateUserAccountCreated", | ||
id: notifications.TemplateUserAccountCreated, | ||
payload: types.MessagePayload{ | ||
UserName: "bobby", | ||
Labels: map[string]string{ | ||
"created_account_name": "bobby", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "TemplateUserAccountDeleted", | ||
id: notifications.TemplateUserAccountDeleted, | ||
payload: types.MessagePayload{ | ||
UserName: "bobby", | ||
Labels: map[string]string{ | ||
"deleted_account_name": "bobby", | ||
}, | ||
}, | ||
}, | ||
} | ||
allTemplates, err := enumerateAllTemplates(t) | ||
require.NoError(t, err) | ||
for _, name := range allTemplates { | ||
var found bool | ||
for _, tc := range tests { | ||
if tc.name == name { | ||
found = true | ||
} | ||
} | ||
require.Truef(t, found, "could not find test case for %q", name) | ||
} | ||
for _, tc := range tests { | ||
@@ -697,6 +773,7 @@ func TestNotificationTemplatesBody(t *testing.T) { | ||
require.NoError(t, err, "failed to query body template for template:", tc.id) | ||
title, err := render.GoTemplate(titleTmpl, tc.payload, nil) | ||
require.NotContainsf(t, title, render.NoValue, "template %q is missing a label value", tc.name) | ||
require.NoError(t, err, "failed to render notification title template") | ||
require.NotEmpty(t, title, "title should not be empty") | ||
11 changes: 10 additions & 1 deletioncoderd/notifications/render/gotmpl.go
This file contains hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.