mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: generate golden files for notification templates (#14537)
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/parser"
|
||||
@@ -12,8 +13,11 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -46,6 +50,9 @@ import (
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
// updateGoldenFiles is a flag that can be set to update golden files.
|
||||
var updateGoldenFiles = flag.Bool("update", false, "Update golden files")
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
goleak.VerifyTestMain(m)
|
||||
}
|
||||
@@ -693,7 +700,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceDeleted",
|
||||
id: notifications.TemplateWorkspaceDeleted,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"reason": "autodeleted due to dormancy",
|
||||
@@ -705,7 +712,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceAutobuildFailed",
|
||||
id: notifications.TemplateWorkspaceAutobuildFailed,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"reason": "autostart",
|
||||
@@ -716,7 +723,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceDormant",
|
||||
id: notifications.TemplateWorkspaceDormant,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"reason": "breached the template's threshold for inactivity",
|
||||
@@ -730,7 +737,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceAutoUpdated",
|
||||
id: notifications.TemplateWorkspaceAutoUpdated,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"template_version_name": "1.0",
|
||||
@@ -742,7 +749,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceMarkedForDeletion",
|
||||
id: notifications.TemplateWorkspaceMarkedForDeletion,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"reason": "template updated to new dormancy policy",
|
||||
@@ -755,7 +762,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateUserAccountCreated",
|
||||
id: notifications.TemplateUserAccountCreated,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"created_account_name": "bobby",
|
||||
},
|
||||
@@ -765,7 +772,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateUserAccountDeleted",
|
||||
id: notifications.TemplateUserAccountDeleted,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"deleted_account_name": "bobby",
|
||||
},
|
||||
@@ -775,7 +782,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateUserAccountSuspended",
|
||||
id: notifications.TemplateUserAccountSuspended,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"suspended_account_name": "bobby",
|
||||
},
|
||||
@@ -785,7 +792,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateUserAccountActivated",
|
||||
id: notifications.TemplateUserAccountActivated,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"activated_account_name": "bobby",
|
||||
},
|
||||
@@ -795,7 +802,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateYourAccountSuspended",
|
||||
id: notifications.TemplateYourAccountSuspended,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"suspended_account_name": "bobby",
|
||||
},
|
||||
@@ -805,7 +812,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateYourAccountActivated",
|
||||
id: notifications.TemplateYourAccountActivated,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"activated_account_name": "bobby",
|
||||
},
|
||||
@@ -815,7 +822,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateTemplateDeleted",
|
||||
id: notifications.TemplateTemplateDeleted,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-template",
|
||||
"initiator": "rob",
|
||||
@@ -826,7 +833,7 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
name: "TemplateWorkspaceManualBuildFailed",
|
||||
id: notifications.TemplateWorkspaceManualBuildFailed,
|
||||
payload: types.MessagePayload{
|
||||
UserName: "bobby",
|
||||
UserName: "Bobby",
|
||||
Labels: map[string]string{
|
||||
"name": "bobby-workspace",
|
||||
"template_name": "bobby-template",
|
||||
@@ -869,14 +876,36 @@ func TestNotificationTemplatesCanRender(t *testing.T) {
|
||||
Scan(&titleTmpl, &bodyTmpl)
|
||||
require.NoError(t, err, "failed to query body template for template:", tc.id)
|
||||
|
||||
title, err := render.GoTemplate(titleTmpl, tc.payload, nil)
|
||||
title, err := render.GoTemplate(titleTmpl, tc.payload, defaultHelpers())
|
||||
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")
|
||||
|
||||
body, err := render.GoTemplate(bodyTmpl, tc.payload, nil)
|
||||
body, err := render.GoTemplate(bodyTmpl, tc.payload, defaultHelpers())
|
||||
require.NoError(t, err, "failed to render notification body template")
|
||||
require.NotEmpty(t, body, "body should not be empty")
|
||||
|
||||
partialName := strings.Split(t.Name(), "/")[1]
|
||||
bodyGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-body.md.golden")
|
||||
titleGoldenFile := filepath.Join("testdata", "rendered-templates", partialName+"-title.md.golden")
|
||||
|
||||
if *updateGoldenFiles {
|
||||
err = os.MkdirAll(filepath.Dir(bodyGoldenFile), 0o755)
|
||||
require.NoError(t, err, "want no error creating golden file directory")
|
||||
err = os.WriteFile(bodyGoldenFile, []byte(body), 0o600)
|
||||
require.NoError(t, err, "want no error writing body golden file")
|
||||
err = os.WriteFile(titleGoldenFile, []byte(title), 0o600)
|
||||
require.NoError(t, err, "want no error writing title golden file")
|
||||
return
|
||||
}
|
||||
|
||||
wantBody, err := os.ReadFile(bodyGoldenFile)
|
||||
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")
|
||||
wantTitle, err := os.ReadFile(titleGoldenFile)
|
||||
require.NoError(t, err, "open golden file, run \"DB=ci make update-golden-files\" and commit the changes")
|
||||
|
||||
require.Equal(t, string(wantBody), body, "body should be equal")
|
||||
require.Equal(t, string(wantTitle), title, "title should be equal")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Hi Bobby
|
||||
|
||||
The template **bobby-template** was deleted by **rob**.
|
||||
+1
@@ -0,0 +1 @@
|
||||
Template "bobby-template" deleted
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
Hi Bobby,
|
||||
User account **bobby** has been activated.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
User account "bobby" activated
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Hi Bobby,
|
||||
|
||||
New user account **bobby** has been created.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
User account "bobby" created
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
Hi Bobby,
|
||||
|
||||
User account **bobby** has been deleted.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
User account "bobby" deleted
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
Hi Bobby,
|
||||
User account **bobby** has been suspended.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
User account "bobby" suspended
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
Hi Bobby
|
||||
Your workspace **bobby-workspace** has been updated automatically to the latest template version (1.0).
|
||||
Reason for update: **template now includes catnip**
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" updated automatically
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
Hi Bobby
|
||||
Automatic build of your workspace **bobby-workspace** failed.
|
||||
The specified reason was "**autostart**".
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" autobuild failed
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
Hi Bobby
|
||||
|
||||
Your workspace **bobby-workspace** was deleted.
|
||||
The specified reason was "**autodeleted due to dormancy (autobuild)**".
|
||||
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" deleted
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
Hi Bobby
|
||||
|
||||
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.
|
||||
Dormant workspaces are [automatically deleted](https://coder.com/docs/templates/schedule#dormancy-auto-deletion-enterprise) after 24 hours of inactivity.
|
||||
To prevent deletion, use your workspace with the link below.
|
||||
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" marked as dormant
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
Hi Bobby,
|
||||
|
||||
A manual build of the workspace **bobby-workspace** using the template **bobby-template** failed (version: **bobby-template-version**).
|
||||
The workspace build was initiated by **joe**.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" manual build failed
|
||||
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
Hi Bobby
|
||||
|
||||
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.
|
||||
To prevent deletion, use your workspace with the link below.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Workspace "bobby-workspace" marked for deletion
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
Hi Bobby,
|
||||
Your account **bobby** has been activated.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Your account "bobby" has been activated
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
Hi Bobby,
|
||||
Your account **bobby** has been suspended.
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
Your account "bobby" has been suspended
|
||||
Reference in New Issue
Block a user