feat: allow templates to specify max_ttl or autostop_requirement (#10920)

This commit is contained in:
Dean Sheather
2023-12-15 00:27:56 -08:00
committed by GitHub
parent 30f032d282
commit b36071c6bb
46 changed files with 699 additions and 495 deletions

View File

@ -268,6 +268,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
AllowUserAutostart: options.UserAutostartEnabled,
AllowUserAutostop: options.UserAutostopEnabled,
DefaultTTL: int64(options.DefaultTTL),
UseMaxTtl: options.UseMaxTTL,
MaxTTL: int64(options.MaxTTL),
AutostopRequirementDaysOfWeek: int16(options.AutostopRequirement.DaysOfWeek),
AutostopRequirementWeeks: options.AutostopRequirement.Weeks,
@ -296,6 +297,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
})
require.NoError(t, err)
require.False(t, got.UseMaxTTL) // default
require.EqualValues(t, 1, atomic.LoadInt64(&setCalled))
require.Empty(t, got.AutostopRequirement.DaysOfWeek)
require.EqualValues(t, 1, got.AutostopRequirement.Weeks)
@ -318,6 +320,7 @@ func TestPostTemplateByOrganization(t *testing.T) {
AllowUserAutostart: options.UserAutostartEnabled,
AllowUserAutostop: options.UserAutostopEnabled,
DefaultTTL: int64(options.DefaultTTL),
UseMaxTtl: options.UseMaxTTL,
MaxTTL: int64(options.MaxTTL),
AutostopRequirementDaysOfWeek: int16(options.AutostopRequirement.DaysOfWeek),
AutostopRequirementWeeks: options.AutostopRequirement.Weeks,
@ -351,11 +354,13 @@ func TestPostTemplateByOrganization(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, 1, atomic.LoadInt64(&setCalled))
require.False(t, got.UseMaxTTL)
require.Equal(t, []string{"friday", "saturday"}, got.AutostopRequirement.DaysOfWeek)
require.EqualValues(t, 2, got.AutostopRequirement.Weeks)
got, err = client.Template(ctx, got.ID)
require.NoError(t, err)
require.False(t, got.UseMaxTTL)
require.Equal(t, []string{"friday", "saturday"}, got.AutostopRequirement.DaysOfWeek)
require.EqualValues(t, 2, got.AutostopRequirement.Weeks)
})
@ -380,10 +385,36 @@ func TestPostTemplateByOrganization(t *testing.T) {
})
require.NoError(t, err)
// ignored and use AGPL defaults
require.False(t, got.UseMaxTTL)
require.Empty(t, got.AutostopRequirement.DaysOfWeek)
require.EqualValues(t, 1, got.AutostopRequirement.Weeks)
})
})
t.Run("BothMaxTTLAndAutostopRequirement", func(t *testing.T) {
t.Parallel()
// Fake template schedule store is unneeded for this test since the
// route fails before it is called.
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
_, err := client.CreateTemplate(ctx, user.OrganizationID, codersdk.CreateTemplateRequest{
Name: "testing",
VersionID: version.ID,
MaxTTLMillis: ptr.Ref(24 * time.Hour.Milliseconds()),
AutostopRequirement: &codersdk.TemplateAutostopRequirement{
DaysOfWeek: []string{"friday", "saturday"},
Weeks: 2,
},
})
require.Error(t, err)
require.ErrorContains(t, err, "max_ttl_ms")
})
}
func TestTemplatesByOrganization(t *testing.T) {
@ -677,6 +708,7 @@ func TestPatchTemplateMeta(t *testing.T) {
AllowUserAutostop: options.UserAutostopEnabled,
DefaultTTL: int64(options.DefaultTTL),
MaxTTL: int64(options.MaxTTL),
UseMaxTtl: options.UseMaxTTL,
AutostopRequirementDaysOfWeek: int16(options.AutostopRequirement.DaysOfWeek),
AutostopRequirementWeeks: options.AutostopRequirement.Weeks,
FailureTTL: int64(options.FailureTTL),
@ -1073,6 +1105,7 @@ func TestPatchTemplateMeta(t *testing.T) {
AllowUserAutostart: options.UserAutostartEnabled,
AllowUserAutostop: options.UserAutostopEnabled,
DefaultTTL: int64(options.DefaultTTL),
UseMaxTtl: options.UseMaxTTL,
MaxTTL: int64(options.MaxTTL),
AutostopRequirementDaysOfWeek: int16(options.AutostopRequirement.DaysOfWeek),
AutostopRequirementWeeks: options.AutostopRequirement.Weeks,
@ -1144,6 +1177,7 @@ func TestPatchTemplateMeta(t *testing.T) {
AllowUserAutostart: options.UserAutostartEnabled,
AllowUserAutostop: options.UserAutostopEnabled,
DefaultTTL: int64(options.DefaultTTL),
UseMaxTtl: options.UseMaxTTL,
MaxTTL: int64(options.MaxTTL),
AutostopRequirementDaysOfWeek: int16(options.AutostopRequirement.DaysOfWeek),
AutostopRequirementWeeks: options.AutostopRequirement.Weeks,
@ -1238,6 +1272,38 @@ func TestPatchTemplateMeta(t *testing.T) {
require.False(t, template.Deprecated)
})
})
t.Run("BothMaxTTLAndAutostopRequirement", func(t *testing.T) {
t.Parallel()
// Fake template schedule store is unneeded for this test since the
// route fails before it is called.
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
req := codersdk.UpdateTemplateMeta{
Name: template.Name,
DisplayName: template.DisplayName,
Description: template.Description,
Icon: template.Icon,
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
DefaultTTLMillis: time.Hour.Milliseconds(),
MaxTTLMillis: time.Hour.Milliseconds(),
AutostopRequirement: &codersdk.TemplateAutostopRequirement{
DaysOfWeek: []string{"monday"},
Weeks: 2,
},
}
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()
_, err := client.UpdateTemplateMeta(ctx, template.ID, req)
require.Error(t, err)
require.ErrorContains(t, err, "max_ttl_ms")
})
}
func TestDeleteTemplate(t *testing.T) {