Skip to content

Commit a05eb6c

Browse files
committed
added BE tests
1 parent 8536b75 commit a05eb6c

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

coderd/apidoc/docs.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/templates_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,92 @@ func TestPatchTemplateMeta(t *testing.T) {
604604
})
605605
})
606606

607+
t.Run("CleanupTTLs", func(t *testing.T) {
608+
t.Parallel()
609+
610+
const (
611+
failureTTL = 7 * 24 * time.Hour
612+
inactivityTTL = 180 * 24 * time.Hour
613+
)
614+
615+
t.Run("OK", func(t *testing.T) {
616+
t.Parallel()
617+
618+
var setCalled int64
619+
client := coderdtest.New(t, &coderdtest.Options{
620+
TemplateScheduleStore: schedule.MockTemplateScheduleStore{
621+
SetFn: func(ctx context.Context, db database.Store, template database.Template, options schedule.TemplateScheduleOptions) (database.Template, error) {
622+
if atomic.AddInt64(&setCalled, 1) == 2 {
623+
require.Equal(t, failureTTL, options.FailureTTL)
624+
require.Equal(t, inactivityTTL, options.InactivityTTL)
625+
626+
}
627+
template.FailureTTL = int64(options.FailureTTL)
628+
template.InactivityTTL = int64(options.InactivityTTL)
629+
return template, nil
630+
},
631+
},
632+
})
633+
user := coderdtest.CreateFirstUser(t, client)
634+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
635+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
636+
ctr.FailureTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
637+
ctr.InactivityTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
638+
})
639+
640+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
641+
defer cancel()
642+
643+
got, err := client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
644+
Name: template.Name,
645+
DisplayName: template.DisplayName,
646+
Description: template.Description,
647+
Icon: template.Icon,
648+
DefaultTTLMillis: 0,
649+
MaxTTLMillis: 0,
650+
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
651+
FailureTTLMillis: failureTTL.Milliseconds(),
652+
InactivityTTLMillis: inactivityTTL.Milliseconds(),
653+
})
654+
require.NoError(t, err)
655+
656+
require.EqualValues(t, 2, atomic.LoadInt64(&setCalled))
657+
require.EqualValues(t, 0, got.DefaultTTLMillis)
658+
require.Equal(t, failureTTL.Milliseconds(), got.FailureTTLMillis)
659+
require.Equal(t, inactivityTTL.Milliseconds(), got.InactivityTTLMillis)
660+
})
661+
662+
t.Run("IgnoredUnlicensed", func(t *testing.T) {
663+
t.Parallel()
664+
665+
client := coderdtest.New(t, nil)
666+
user := coderdtest.CreateFirstUser(t, client)
667+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
668+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
669+
ctr.FailureTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
670+
ctr.InactivityTTLMillis = ptr.Ref(0 * time.Hour.Milliseconds())
671+
})
672+
673+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
674+
defer cancel()
675+
676+
got, err := client.UpdateTemplateMeta(ctx, template.ID, codersdk.UpdateTemplateMeta{
677+
Name: template.Name,
678+
DisplayName: template.DisplayName,
679+
Description: template.Description,
680+
Icon: template.Icon,
681+
DefaultTTLMillis: template.DefaultTTLMillis,
682+
MaxTTLMillis: template.MaxTTLMillis,
683+
AllowUserCancelWorkspaceJobs: template.AllowUserCancelWorkspaceJobs,
684+
FailureTTLMillis: failureTTL.Milliseconds(),
685+
InactivityTTLMillis: inactivityTTL.Milliseconds(),
686+
})
687+
require.NoError(t, err)
688+
require.Zero(t, got.FailureTTLMillis)
689+
require.Zero(t, got.InactivityTTLMillis)
690+
})
691+
})
692+
607693
t.Run("AllowUserScheduling", func(t *testing.T) {
608694
t.Parallel()
609695

codersdk/organizations.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ type CreateTemplateRequest struct {
106106
// false, the DefaultTTL will always be used. This can only be disabled when
107107
// using an enterprise license.
108108
AllowUserAutostop *bool `json:"allow_user_autostop"`
109+
110+
// FailureTTLMillis allows optionally specifying the max lifetime before Coder
111+
// stops all resources for failed workspaces created from this template.
112+
FailureTTLMillis *int64 `json:"failure_ttl_ms,omitempty"`
113+
// InactivityTTLMillis allows optionally specifying the max lifetime before Coder
114+
// deletes inactive workspaces created from this template.
115+
InactivityTTLMillis *int64 `json:"inactivity_ttl_ms,omitempty"`
109116
}
110117

111118
// CreateWorkspaceRequest provides options for creating a new workspace.

docs/api/schemas.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,9 @@ CreateParameterRequest is a structure used to create a new parameter value for a
13331333
"default_ttl_ms": 0,
13341334
"description": "string",
13351335
"display_name": "string",
1336+
"failure_ttl_ms": 0,
13361337
"icon": "string",
1338+
"inactivity_ttl_ms": 0,
13371339
"max_ttl_ms": 0,
13381340
"name": "string",
13391341
"parameter_values": [
@@ -1359,7 +1361,9 @@ CreateParameterRequest is a structure used to create a new parameter value for a
13591361
| `default_ttl_ms` | integer | false | | Default ttl ms allows optionally specifying the default TTL for all workspaces created from this template. |
13601362
| `description` | string | false | | Description is a description of what the template contains. It must be less than 128 bytes. |
13611363
| `display_name` | string | false | | Display name is the displayed name of the template. |
1364+
| `failure_ttl_ms` | integer | false | | Failure ttl ms allows optionally specifying the max lifetime before Coder stops all resources for failed workspaces created from this template. |
13621365
| `icon` | string | false | | Icon is a relative path or external URL that specifies an icon to be displayed in the dashboard. |
1366+
| `inactivity_ttl_ms` | integer | false | | Inactivity ttl ms allows optionally specifying the max lifetime before Coder deletes inactive workspaces created from this template. |
13631367
| `max_ttl_ms` | integer | false | | Max ttl ms allows optionally specifying the max lifetime for workspaces created from this template. |
13641368
| `name` | string | true | | Name is the name of the template. |
13651369
| `parameter_values` | array of [codersdk.CreateParameterRequest](#codersdkcreateparameterrequest) | false | | Parameter values is a structure used to create a new parameter value for a scope.] |

docs/api/templates.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ curl -X POST http://coder-server:8080/api/v2/organizations/{organization}/templa
130130
"default_ttl_ms": 0,
131131
"description": "string",
132132
"display_name": "string",
133+
"failure_ttl_ms": 0,
133134
"icon": "string",
135+
"inactivity_ttl_ms": 0,
134136
"max_ttl_ms": 0,
135137
"name": "string",
136138
"parameter_values": [

site/src/api/typesGenerated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ export interface CreateTemplateRequest {
194194
readonly allow_user_cancel_workspace_jobs?: boolean
195195
readonly allow_user_autostart?: boolean
196196
readonly allow_user_autostop?: boolean
197+
readonly failure_ttl_ms?: number
198+
readonly inactivity_ttl_ms?: number
197199
}
198200

199201
// From codersdk/templateversions.go

0 commit comments

Comments
 (0)