Skip to content

Commit e6d8f67

Browse files
authored
feat: generate golden files for notification templates (coder#14537)
1 parent bcf9bc3 commit e6d8f67

27 files changed

+97
-15
lines changed

coderd/notifications/notifications_test.go

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import (
55
"context"
66
_ "embed"
77
"encoding/json"
8+
"flag"
89
"fmt"
910
"go/ast"
1011
"go/parser"
1112
"go/token"
1213
"net/http"
1314
"net/http/httptest"
1415
"net/url"
16+
"os"
17+
"path/filepath"
1518
"slices"
1619
"sort"
20+
"strings"
1721
"sync"
1822
"sync/atomic"
1923
"testing"
@@ -46,6 +50,9 @@ import (
4650
"github.com/coder/coder/v2/testutil"
4751
)
4852

53+
// updateGoldenFiles is a flag that can be set to update golden files.
54+
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")
55+
4956
func TestMain(m *testing.M) {
5057
goleak.VerifyTestMain(m)
5158
}
@@ -693,7 +700,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
693700
name: "TemplateWorkspaceDeleted",
694701
id: notifications.TemplateWorkspaceDeleted,
695702
payload: types.MessagePayload{
696-
UserName: "bobby",
703+
UserName: "Bobby",
697704
Labels: map[string]string{
698705
"name": "bobby-workspace",
699706
"reason": "autodeleted due to dormancy",
@@ -705,7 +712,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
705712
name: "TemplateWorkspaceAutobuildFailed",
706713
id: notifications.TemplateWorkspaceAutobuildFailed,
707714
payload: types.MessagePayload{
708-
UserName: "bobby",
715+
UserName: "Bobby",
709716
Labels: map[string]string{
710717
"name": "bobby-workspace",
711718
"reason": "autostart",
@@ -716,7 +723,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
716723
name: "TemplateWorkspaceDormant",
717724
id: notifications.TemplateWorkspaceDormant,
718725
payload: types.MessagePayload{
719-
UserName: "bobby",
726+
UserName: "Bobby",
720727
Labels: map[string]string{
721728
"name": "bobby-workspace",
722729
"reason": "breached the template's threshold for inactivity",
@@ -730,7 +737,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
730737
name: "TemplateWorkspaceAutoUpdated",
731738
id: notifications.TemplateWorkspaceAutoUpdated,
732739
payload: types.MessagePayload{
733-
UserName: "bobby",
740+
UserName: "Bobby",
734741
Labels: map[string]string{
735742
"name": "bobby-workspace",
736743
"template_version_name": "1.0",
@@ -742,7 +749,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
742749
name: "TemplateWorkspaceMarkedForDeletion",
743750
id: notifications.TemplateWorkspaceMarkedForDeletion,
744751
payload: types.MessagePayload{
745-
UserName: "bobby",
752+
UserName: "Bobby",
746753
Labels: map[string]string{
747754
"name": "bobby-workspace",
748755
"reason": "template updated to new dormancy policy",
@@ -755,7 +762,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
755762
name: "TemplateUserAccountCreated",
756763
id: notifications.TemplateUserAccountCreated,
757764
payload: types.MessagePayload{
758-
UserName: "bobby",
765+
UserName: "Bobby",
759766
Labels: map[string]string{
760767
"created_account_name": "bobby",
761768
},
@@ -765,7 +772,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
765772
name: "TemplateUserAccountDeleted",
766773
id: notifications.TemplateUserAccountDeleted,
767774
payload: types.MessagePayload{
768-
UserName: "bobby",
775+
UserName: "Bobby",
769776
Labels: map[string]string{
770777
"deleted_account_name": "bobby",
771778
},
@@ -775,7 +782,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
775782
name: "TemplateUserAccountSuspended",
776783
id: notifications.TemplateUserAccountSuspended,
777784
payload: types.MessagePayload{
778-
UserName: "bobby",
785+
UserName: "Bobby",
779786
Labels: map[string]string{
780787
"suspended_account_name": "bobby",
781788
},
@@ -785,7 +792,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
785792
name: "TemplateUserAccountActivated",
786793
id: notifications.TemplateUserAccountActivated,
787794
payload: types.MessagePayload{
788-
UserName: "bobby",
795+
UserName: "Bobby",
789796
Labels: map[string]string{
790797
"activated_account_name": "bobby",
791798
},
@@ -795,7 +802,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
795802
name: "TemplateYourAccountSuspended",
796803
id: notifications.TemplateYourAccountSuspended,
797804
payload: types.MessagePayload{
798-
UserName: "bobby",
805+
UserName: "Bobby",
799806
Labels: map[string]string{
800807
"suspended_account_name": "bobby",
801808
},
@@ -805,7 +812,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
805812
name: "TemplateYourAccountActivated",
806813
id: notifications.TemplateYourAccountActivated,
807814
payload: types.MessagePayload{
808-
UserName: "bobby",
815+
UserName: "Bobby",
809816
Labels: map[string]string{
810817
"activated_account_name": "bobby",
811818
},
@@ -815,7 +822,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
815822
name: "TemplateTemplateDeleted",
816823
id: notifications.TemplateTemplateDeleted,
817824
payload: types.MessagePayload{
818-
UserName: "bobby",
825+
UserName: "Bobby",
819826
Labels: map[string]string{
820827
"name": "bobby-template",
821828
"initiator": "rob",
@@ -826,7 +833,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
826833
name: "TemplateWorkspaceManualBuildFailed",
827834
id: notifications.TemplateWorkspaceManualBuildFailed,
828835
payload: types.MessagePayload{
829-
UserName: "bobby",
836+
UserName: "Bobby",
830837
Labels: map[string]string{
831838
"name": "bobby-workspace",
832839
"template_name": "bobby-template",
@@ -869,14 +876,36 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
869876
Scan(&titleTmpl, &bodyTmpl)
870877
require.NoError(t, err, "failed to query body template for template:", tc.id)
871878

872-
title, err := render.GoTemplate(titleTmpl, tc.payload, nil)
879+
title, err := render.GoTemplate(titleTmpl, tc.payload, defaultHelpers())
873880
require.NotContainsf(t, title, render.NoValue, "template %q is missing a label value", tc.name)
874881
require.NoError(t, err, "failed to render notification title template")
875882
require.NotEmpty(t, title, "title should not be empty")
876883

877-
body, err := render.GoTemplate(bodyTmpl, tc.payload, nil)
884+
body, err := render.GoTemplate(bodyTmpl, tc.payload, defaultHelpers())
878885
require.NoError(t, err, "failed to render notification body template")
879886
require.NotEmpty(t, body, "body should not be empty")
887+
888+
partialName := strings.Split(t.Name(), "/")[1]
889+
bodyGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-body.md.golden")
890+
titleGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-title.md.golden")
891+
892+
if *updateGoldenFiles {
893+
err = os.MkdirAll(filepath.Dir(bodyGoldenFile), 0o755)
894+
require.NoError(t, err, "want no error creating golden file directory")
895+
err = os.WriteFile(bodyGoldenFile, []byte(body), 0o600)
896+
require.NoError(t, err, "want no error writing body golden file")
897+
err = os.WriteFile(titleGoldenFile, []byte(title), 0o600)
898+
require.NoError(t, err, "want no error writing title golden file")
899+
return
900+
}
901+
902+
wantBody, err := os.ReadFile(bodyGoldenFile)
903+
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")
904+
wantTitle, err := os.ReadFile(titleGoldenFile)
905+
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")
906+
907+
require.Equal(t, string(wantBody), body, "body should be equal")
908+
require.Equal(t, string(wantTitle), title, "title should be equal")
880909
})
881910
}
882911
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hi Bobby
2+
3+
The template **bobby-template** was deleted by **rob**.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Template "bobby-template" deleted
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi Bobby,
2+
User account **bobby** has been activated.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
User account "bobby" activated
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hi Bobby,
2+
3+
New user account **bobby** has been created.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
User account "bobby" created
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hi Bobby,
2+
3+
User account **bobby** has been deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
User account "bobby" deleted
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi Bobby,
2+
User account **bobby** has been suspended.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
User account "bobby" suspended
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hi Bobby
2+
Your workspace **bobby-workspace** has been updated automatically to the latest template version (1.0).
3+
Reason for update: **template now includes catnip**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" updated automatically
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Hi Bobby
2+
Automatic build of your workspace **bobby-workspace** failed.
3+
The specified reason was "**autostart**".
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" autobuild failed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hi Bobby
2+
3+
Your workspace **bobby-workspace** was deleted.
4+
The specified reason was "**autodeleted due to dormancy (autobuild)**".
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" deleted
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Hi Bobby
2+
3+
Your workspace **bobby-workspace** has been marked as [**dormant**](https://coder.com/docs/templates/schedule#dormancy-threshold-enterprise) because of breached the template's threshold for inactivity.
4+
Dormant workspaces are [automatically deleted](https://coder.com/docs/templates/schedule#dormancy-auto-deletion-enterprise) after 24 hours of inactivity.
5+
To prevent deletion, use your workspace with the link below.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" marked as dormant
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hi Bobby,
2+
3+
A manual build of the workspace **bobby-workspace** using the template **bobby-template** failed (version: **bobby-template-version**).
4+
The workspace build was initiated by **joe**.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" manual build failed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hi Bobby
2+
3+
Your workspace **bobby-workspace** has been marked for **deletion** after 24 hours of [dormancy](https://coder.com/docs/templates/schedule#dormancy-auto-deletion-enterprise) because of template updated to new dormancy policy.
4+
To prevent deletion, use your workspace with the link below.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Workspace "bobby-workspace" marked for deletion
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi Bobby,
2+
Your account **bobby** has been activated.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Your account "bobby" has been activated
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi Bobby,
2+
Your account **bobby** has been suspended.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Your account "bobby" has been suspended

0 commit comments

Comments
 (0)