Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aea55aa

Browse files
committed
Add test to verify dormancy in lifecycle executor
1 parent cf9eaec commit aea55aa

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

coderd/autobuild/lifecycle_executor_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package autobuild_test
33
import (
44
"context"
55
"os"
6+
"sync/atomic"
67
"testing"
78
"time"
89

@@ -25,6 +26,8 @@ import (
2526
"github.com/coder/coder/v2/provisioner/echo"
2627
"github.com/coder/coder/v2/provisionersdk/proto"
2728
"github.com/coder/coder/v2/testutil"
29+
30+
enterpriseSchedule "github.com/coder/coder/v2/enterprise/coderd/schedule"
2831
)
2932

3033
func TestExecutorAutostartOK(t *testing.T) {
@@ -1062,6 +1065,52 @@ func TestExecutorInactiveWorkspace(t *testing.T) {
10621065
})
10631066
}
10641067

1068+
func TestNotifications(t *testing.T) {
1069+
t.Parallel()
1070+
1071+
t.Run("Dormancy", func(t *testing.T) {
1072+
t.Parallel()
1073+
1074+
// Setup template with dormancy and create a workspace with it
1075+
var (
1076+
ticker = make(chan time.Time)
1077+
statCh = make(chan autobuild.Stats)
1078+
logger = slogtest.Make(t, &slogtest.Options{})
1079+
notificationsEnqueuer = testutil.FakeNotificationsEnqueuer{}
1080+
client = coderdtest.New(t, &coderdtest.Options{
1081+
AutobuildTicker: ticker,
1082+
AutobuildStats: statCh,
1083+
IncludeProvisionerDaemon: true,
1084+
NotificationsEnqueuer: &notificationsEnqueuer,
1085+
TemplateScheduleStore: enterpriseSchedule.NewEnterpriseTemplateScheduleStore(userQuietHoursScheduleStore(), &notificationsEnqueuer, logger),
1086+
})
1087+
admin = coderdtest.CreateFirstUser(t, client)
1088+
version = coderdtest.CreateTemplateVersion(t, client, admin.OrganizationID, nil)
1089+
timeTilDormant = 1000
1090+
)
1091+
1092+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
1093+
template := coderdtest.CreateTemplate(t, client, admin.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
1094+
ctr.TimeTilDormantMillis = ptr.Ref(int64(timeTilDormant))
1095+
})
1096+
userClient, _ := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
1097+
workspace := coderdtest.CreateWorkspace(t, userClient, admin.OrganizationID, template.ID)
1098+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, workspace.LatestBuild.ID)
1099+
1100+
// Stop workspace
1101+
workspace = coderdtest.MustTransitionWorkspace(t, client, workspace.ID, database.WorkspaceTransitionStart, database.WorkspaceTransitionStop)
1102+
build := coderdtest.AwaitWorkspaceBuildJobCompleted(t, userClient, workspace.LatestBuild.ID)
1103+
1104+
// Wait for workspace to become dormant
1105+
ticker <- build.Job.CompletedAt.Add(time.Millisecond * time.Duration(timeTilDormant) * 2)
1106+
<-statCh
1107+
1108+
// Check that the workspace is dormant
1109+
workspace = coderdtest.MustWorkspace(t, client, workspace.ID)
1110+
require.NotNil(t, workspace.DormantAt)
1111+
})
1112+
}
1113+
10651114
func mustProvisionWorkspace(t *testing.T, client *codersdk.Client, mut ...func(*codersdk.CreateWorkspaceRequest)) codersdk.Workspace {
10661115
t.Helper()
10671116
user := coderdtest.CreateFirstUser(t, client)
@@ -1113,3 +1162,10 @@ func mustWorkspaceParameters(t *testing.T, client *codersdk.Client, workspaceID
11131162
func TestMain(m *testing.M) {
11141163
goleak.VerifyTestMain(m)
11151164
}
1165+
1166+
func userQuietHoursScheduleStore() *atomic.Pointer[schedule.UserQuietHoursScheduleStore] {
1167+
store := schedule.NewAGPLUserQuietHoursScheduleStore()
1168+
p := &atomic.Pointer[schedule.UserQuietHoursScheduleStore]{}
1169+
p.Store(&store)
1170+
return p
1171+
}

0 commit comments

Comments
 (0)