mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: update workspace TTL on template TTL change (#15761)
Relates to https://github.com/coder/coder/issues/15390 Currently when a user creates a workspace, their workspace's TTL is determined by the template's default TTL. If the Coder instance is AGPL, or if the template has disallowed the user from configuring autostop, then it is not possible to change the workspace's TTL after creation. Any changes to the template's default TTL only takes effect on _new_ workspaces. This PR modifies the behaviour slightly so that on AGPL Coder, or on enterprise when a template does not allow user's to configure their workspace's TTL, updating the template's default TTL will also update any workspace's TTL to match this value.
This commit is contained in:
@ -19,6 +19,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/notifications/notificationstest"
|
||||
agplschedule "github.com/coder/coder/v2/coderd/schedule"
|
||||
@ -708,6 +709,252 @@ func TestNotifications(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestTemplateTTL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
allowUserAutostop bool
|
||||
fromTTL time.Duration
|
||||
toTTL time.Duration
|
||||
expected sql.NullInt64
|
||||
}{
|
||||
{
|
||||
name: "AllowUserAutostopFalse/ModifyTTLDurationDown",
|
||||
allowUserAutostop: false,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 1 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(1 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopFalse/ModifyTTLDurationUp",
|
||||
allowUserAutostop: false,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 36 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(36 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopFalse/ModifyTTLDurationSame",
|
||||
allowUserAutostop: false,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 24 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopFalse/DisableTTL",
|
||||
allowUserAutostop: false,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 0,
|
||||
expected: sql.NullInt64{},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopTrue/ModifyTTLDurationDown",
|
||||
allowUserAutostop: true,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 1 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopTrue/ModifyTTLDurationUp",
|
||||
allowUserAutostop: true,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 36 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopTrue/ModifyTTLDurationSame",
|
||||
allowUserAutostop: true,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 24 * time.Hour,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
|
||||
},
|
||||
{
|
||||
name: "AllowUserAutostopTrue/DisableTTL",
|
||||
allowUserAutostop: true,
|
||||
fromTTL: 24 * time.Hour,
|
||||
toTTL: 0,
|
||||
expected: sql.NullInt64{Valid: true, Int64: int64(24 * time.Hour)},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
logger = slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
db, _ = dbtestutil.NewDB(t)
|
||||
ctx = testutil.Context(t, testutil.WaitLong)
|
||||
user = dbgen.User(t, db, database.User{})
|
||||
file = dbgen.File(t, db, database.File{CreatedBy: user.ID})
|
||||
// Create first template
|
||||
templateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
|
||||
FileID: file.ID,
|
||||
InitiatorID: user.ID,
|
||||
Tags: database.StringMap{"foo": "bar"},
|
||||
})
|
||||
templateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
|
||||
CreatedBy: user.ID,
|
||||
JobID: templateJob.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
})
|
||||
template = dbgen.Template(t, db, database.Template{
|
||||
ActiveVersionID: templateVersion.ID,
|
||||
CreatedBy: user.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
AllowUserAutostop: false,
|
||||
})
|
||||
// Create second template
|
||||
otherTTL = tt.fromTTL + 6*time.Hour
|
||||
otherTemplateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
|
||||
FileID: file.ID,
|
||||
InitiatorID: user.ID,
|
||||
Tags: database.StringMap{"foo": "bar"},
|
||||
})
|
||||
otherTemplateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
|
||||
CreatedBy: user.ID,
|
||||
JobID: otherTemplateJob.ID,
|
||||
OrganizationID: otherTemplateJob.OrganizationID,
|
||||
})
|
||||
otherTemplate = dbgen.Template(t, db, database.Template{
|
||||
ActiveVersionID: otherTemplateVersion.ID,
|
||||
CreatedBy: user.ID,
|
||||
OrganizationID: otherTemplateJob.OrganizationID,
|
||||
AllowUserAutostop: false,
|
||||
})
|
||||
)
|
||||
|
||||
// Setup the template schedule store
|
||||
notifyEnq := notifications.NewNoopEnqueuer()
|
||||
const userQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *" // midnight UTC
|
||||
userQuietHoursStore, err := schedule.NewEnterpriseUserQuietHoursScheduleStore(userQuietHoursSchedule, true)
|
||||
require.NoError(t, err)
|
||||
userQuietHoursStorePtr := &atomic.Pointer[agplschedule.UserQuietHoursScheduleStore]{}
|
||||
userQuietHoursStorePtr.Store(&userQuietHoursStore)
|
||||
templateScheduleStore := schedule.NewEnterpriseTemplateScheduleStore(userQuietHoursStorePtr, notifyEnq, logger, nil)
|
||||
|
||||
// Set both template's default TTL
|
||||
template, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
|
||||
DefaultTTL: tt.fromTTL,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
otherTemplate, err = templateScheduleStore.Set(ctx, db, otherTemplate, agplschedule.TemplateScheduleOptions{
|
||||
DefaultTTL: otherTTL,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// We create two workspaces here, one with the template we're modifying, the
|
||||
// other with a different template. We want to ensure we only modify one
|
||||
// of the workspaces.
|
||||
workspace := dbgen.Workspace(t, db, database.WorkspaceTable{
|
||||
OwnerID: user.ID,
|
||||
TemplateID: template.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
LastUsedAt: dbtime.Now(),
|
||||
Ttl: sql.NullInt64{Valid: true, Int64: int64(tt.fromTTL)},
|
||||
})
|
||||
otherWorkspace := dbgen.Workspace(t, db, database.WorkspaceTable{
|
||||
OwnerID: user.ID,
|
||||
TemplateID: otherTemplate.ID,
|
||||
OrganizationID: otherTemplateJob.OrganizationID,
|
||||
LastUsedAt: dbtime.Now(),
|
||||
Ttl: sql.NullInt64{Valid: true, Int64: int64(otherTTL)},
|
||||
})
|
||||
|
||||
// Ensure the workspace's start with the correct TTLs
|
||||
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(tt.fromTTL)}, workspace.Ttl)
|
||||
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(otherTTL)}, otherWorkspace.Ttl)
|
||||
|
||||
// Update _only_ the primary template's TTL
|
||||
_, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
|
||||
UserAutostopEnabled: tt.allowUserAutostop,
|
||||
DefaultTTL: tt.toTTL,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the primary workspace's TTL is what we expect
|
||||
ws, err := db.GetWorkspaceByID(ctx, workspace.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tt.expected, ws.Ttl)
|
||||
|
||||
// Verify we haven't changed the other workspace's TTL
|
||||
ws, err = db.GetWorkspaceByID(ctx, otherWorkspace.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(otherTTL)}, ws.Ttl)
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("WorkspaceTTLUpdatedWhenAllowUserAutostopGetsDisabled", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
logger = slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
|
||||
db, _ = dbtestutil.NewDB(t)
|
||||
ctx = testutil.Context(t, testutil.WaitLong)
|
||||
user = dbgen.User(t, db, database.User{})
|
||||
file = dbgen.File(t, db, database.File{CreatedBy: user.ID})
|
||||
// Create first template
|
||||
templateJob = dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
|
||||
FileID: file.ID,
|
||||
InitiatorID: user.ID,
|
||||
Tags: database.StringMap{"foo": "bar"},
|
||||
})
|
||||
templateVersion = dbgen.TemplateVersion(t, db, database.TemplateVersion{
|
||||
CreatedBy: user.ID,
|
||||
JobID: templateJob.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
})
|
||||
template = dbgen.Template(t, db, database.Template{
|
||||
ActiveVersionID: templateVersion.ID,
|
||||
CreatedBy: user.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
})
|
||||
)
|
||||
|
||||
// Setup the template schedule store
|
||||
notifyEnq := notifications.NewNoopEnqueuer()
|
||||
const userQuietHoursSchedule = "CRON_TZ=UTC 0 0 * * *" // midnight UTC
|
||||
userQuietHoursStore, err := schedule.NewEnterpriseUserQuietHoursScheduleStore(userQuietHoursSchedule, true)
|
||||
require.NoError(t, err)
|
||||
userQuietHoursStorePtr := &atomic.Pointer[agplschedule.UserQuietHoursScheduleStore]{}
|
||||
userQuietHoursStorePtr.Store(&userQuietHoursStore)
|
||||
templateScheduleStore := schedule.NewEnterpriseTemplateScheduleStore(userQuietHoursStorePtr, notifyEnq, logger, nil)
|
||||
|
||||
// Enable AllowUserAutostop
|
||||
template, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
|
||||
DefaultTTL: 24 * time.Hour,
|
||||
UserAutostopEnabled: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create a workspace with a TTL different than the template's default TTL
|
||||
workspace := dbgen.Workspace(t, db, database.WorkspaceTable{
|
||||
OwnerID: user.ID,
|
||||
TemplateID: template.ID,
|
||||
OrganizationID: templateJob.OrganizationID,
|
||||
LastUsedAt: dbtime.Now(),
|
||||
Ttl: sql.NullInt64{Valid: true, Int64: int64(48 * time.Hour)},
|
||||
})
|
||||
|
||||
// Ensure the workspace start with the correct TTLs
|
||||
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(48 * time.Hour)}, workspace.Ttl)
|
||||
|
||||
// Disable AllowUserAutostop
|
||||
template, err = templateScheduleStore.Set(ctx, db, template, agplschedule.TemplateScheduleOptions{
|
||||
DefaultTTL: 23 * time.Hour,
|
||||
UserAutostopEnabled: false,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Ensure the workspace ends with the correct TTLs
|
||||
ws, err := db.GetWorkspaceByID(ctx, workspace.ID)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sql.NullInt64{Valid: true, Int64: int64(23 * time.Hour)}, ws.Ttl)
|
||||
})
|
||||
}
|
||||
|
||||
func must[V any](v V, err error) V {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
Reference in New Issue
Block a user