Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d142f5

Browse files
committed
ORANGE: coderd: fix a whole bunch of tests
1 parent6d3c3cf commit8d142f5

File tree

5 files changed

+163
-110
lines changed

5 files changed

+163
-110
lines changed

‎coderd/autobuild/executor/lifecycle_executor_test.go

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package executor_test
22

33
import (
44
"context"
5-
"fmt"
65
"os"
76
"strings"
87
"testing"
@@ -26,31 +25,25 @@ func TestExecutorAutostartOK(t *testing.T) {
2625
t.Parallel()
2726

2827
var (
29-
ctx=context.Background()
30-
errerror
28+
sched=mustSchedule(t,"CRON_TZ=UTC 0 * * * *")
3129
tickCh=make(chan time.Time)
3230
statsCh=make(chan executor.Stats)
3331
client=coderdtest.New(t,&coderdtest.Options{
3432
AutobuildTicker:tickCh,
3533
IncludeProvisionerD:true,
3634
AutobuildStats:statsCh,
3735
})
38-
// Given: we have a user with a workspace
39-
workspace=mustProvisionWorkspace(t,client)
36+
// Given: we have a user with a workspace that has autostart enabled
37+
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
38+
cwr.AutostartSchedule=ptr.Ref(sched.String())
39+
})
4040
)
4141
// Given: workspace is stopped
4242
workspace=mustTransitionWorkspace(t,client,workspace.ID,database.WorkspaceTransitionStart,database.WorkspaceTransitionStop)
4343

44-
// When: we enable workspace autostart
45-
sched,err:=schedule.Weekly("* * * * *")
46-
require.NoError(t,err)
47-
require.NoError(t,client.UpdateWorkspaceAutostart(ctx,workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
48-
Schedule:ptr.Ref(sched.String()),
49-
}))
50-
51-
// When: the autobuild executor ticks
44+
// When: the autobuild executor ticks after the scheduled time
5245
gofunc() {
53-
tickCh<-time.Now().UTC().Add(time.Minute)
46+
tickCh<-sched.Next(workspace.LatestBuild.CreatedAt)
5447
close(tickCh)
5548
}()
5649

@@ -66,6 +59,7 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
6659
t.Parallel()
6760

6861
var (
62+
sched=mustSchedule(t,"CRON_TZ=UTC 0 * * * *")
6963
ctx=context.Background()
7064
errerror
7165
tickCh=make(chan time.Time)
@@ -75,8 +69,10 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
7569
IncludeProvisionerD:true,
7670
AutobuildStats:statsCh,
7771
})
78-
// Given: we have a user with a workspace
79-
workspace=mustProvisionWorkspace(t,client)
72+
// Given: we have a user with a workspace that has autostart enabled
73+
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
74+
cwr.AutostartSchedule=ptr.Ref(sched.String())
75+
})
8076
)
8177
// Given: workspace is stopped
8278
workspace=mustTransitionWorkspace(t,client,workspace.ID,database.WorkspaceTransitionStart,database.WorkspaceTransitionStop)
@@ -92,16 +88,9 @@ func TestExecutorAutostartTemplateUpdated(t *testing.T) {
9288
ID:newVersion.ID,
9389
}))
9490

95-
// When: we enable workspace autostart
96-
sched,err:=schedule.Weekly("* * * * *")
97-
require.NoError(t,err)
98-
require.NoError(t,client.UpdateWorkspaceAutostart(ctx,workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
99-
Schedule:ptr.Ref(sched.String()),
100-
}))
101-
102-
// When: the autobuild executor ticks
91+
// When: the autobuild executor ticks after the scheduled time
10392
gofunc() {
104-
tickCh<-time.Now().UTC().Add(time.Minute)
93+
tickCh<-sched.Next(workspace.LatestBuild.CreatedAt)
10594
close(tickCh)
10695
}()
10796

@@ -119,32 +108,26 @@ func TestExecutorAutostartAlreadyRunning(t *testing.T) {
119108
t.Parallel()
120109

121110
var (
122-
ctx=context.Background()
123-
errerror
111+
sched=mustSchedule(t,"CRON_TZ=UTC 0 * * * *")
124112
tickCh=make(chan time.Time)
125113
statsCh=make(chan executor.Stats)
126114
client=coderdtest.New(t,&coderdtest.Options{
127115
AutobuildTicker:tickCh,
128116
IncludeProvisionerD:true,
129117
AutobuildStats:statsCh,
130118
})
131-
// Given: we have a user with a workspace
132-
workspace=mustProvisionWorkspace(t,client)
119+
// Given: we have a user with a workspace that has autostart enabled
120+
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
121+
cwr.AutostartSchedule=ptr.Ref(sched.String())
122+
})
133123
)
134124

135125
// Given: we ensure the workspace is running
136126
require.Equal(t,codersdk.WorkspaceTransitionStart,workspace.LatestBuild.Transition)
137127

138-
// When: we enable workspace autostart
139-
sched,err:=schedule.Weekly("* * * * *")
140-
require.NoError(t,err)
141-
require.NoError(t,client.UpdateWorkspaceAutostart(ctx,workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
142-
Schedule:ptr.Ref(sched.String()),
143-
}))
144-
145128
// When: the autobuild executor ticks
146129
gofunc() {
147-
tickCh<-time.Now().UTC().Add(time.Minute)
130+
tickCh<-sched.Next(workspace.LatestBuild.CreatedAt)
148131
close(tickCh)
149132
}()
150133

@@ -165,7 +148,7 @@ func TestExecutorAutostartNotEnabled(t *testing.T) {
165148
IncludeProvisionerD:true,
166149
AutobuildStats:statsCh,
167150
})
168-
// Given: we have a user with a workspace
151+
// Given: we have a user with a workspace that does not have autostart enabled
169152
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
170153
cwr.AutostartSchedule=nil
171154
})
@@ -177,9 +160,9 @@ func TestExecutorAutostartNotEnabled(t *testing.T) {
177160
// Given: workspace is stopped
178161
workspace=mustTransitionWorkspace(t,client,workspace.ID,database.WorkspaceTransitionStart,database.WorkspaceTransitionStop)
179162

180-
// When: the autobuild executor ticks
163+
// When: the autobuild executor ticks way into the future
181164
gofunc() {
182-
tickCh<-time.Now().UTC().Add(time.Minute)
165+
tickCh<-workspace.LatestBuild.CreatedAt.Add(24*time.Hour)
183166
close(tickCh)
184167
}()
185168

@@ -343,32 +326,26 @@ func TestExecutorWorkspaceDeleted(t *testing.T) {
343326
t.Parallel()
344327

345328
var (
346-
ctx=context.Background()
347-
errerror
329+
sched=mustSchedule(t,"CRON_TZ=UTC 0 * * * *")
348330
tickCh=make(chan time.Time)
349331
statsCh=make(chan executor.Stats)
350332
client=coderdtest.New(t,&coderdtest.Options{
351333
AutobuildTicker:tickCh,
352334
IncludeProvisionerD:true,
353335
AutobuildStats:statsCh,
354336
})
355-
// Given: we have a user with a workspace
356-
workspace=mustProvisionWorkspace(t,client)
337+
// Given: we have a user with a workspace that has autostart enabled
338+
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
339+
cwr.AutostartSchedule=ptr.Ref(sched.String())
340+
})
357341
)
358342

359-
// When: we enable workspace autostart
360-
sched,err:=schedule.Weekly("* * * * *")
361-
require.NoError(t,err)
362-
require.NoError(t,client.UpdateWorkspaceAutostart(ctx,workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
363-
Schedule:ptr.Ref(sched.String()),
364-
}))
365-
366343
// Given: workspace is deleted
367344
workspace=mustTransitionWorkspace(t,client,workspace.ID,database.WorkspaceTransitionStart,database.WorkspaceTransitionDelete)
368345

369346
// When: the autobuild executor ticks
370347
gofunc() {
371-
tickCh<-time.Now().UTC().Add(time.Minute)
348+
tickCh<-sched.Next(workspace.LatestBuild.CreatedAt)
372349
close(tickCh)
373350
}()
374351

@@ -382,33 +359,25 @@ func TestExecutorWorkspaceAutostartTooEarly(t *testing.T) {
382359
t.Parallel()
383360

384361
var (
385-
ctx=context.Background()
386-
errerror
362+
sched=mustSchedule(t,"CRON_TZ=UTC 0 * * * *")
387363
tickCh=make(chan time.Time)
388364
statsCh=make(chan executor.Stats)
389365
client=coderdtest.New(t,&coderdtest.Options{
390366
AutobuildTicker:tickCh,
391367
IncludeProvisionerD:true,
392368
AutobuildStats:statsCh,
393369
})
394-
futureTime=time.Now().Add(time.Hour)
395-
futureTimeCron=fmt.Sprintf("%d %d * * *",futureTime.Minute(),futureTime.Hour())
370+
//futureTime = time.Now().Add(time.Hour)
371+
//futureTimeCron = fmt.Sprintf("%d %d * * *", futureTime.Minute(), futureTime.Hour())
396372
// Given: we have a user with a workspace configured to autostart some time in the future
397373
workspace=mustProvisionWorkspace(t,client,func(cwr*codersdk.CreateWorkspaceRequest) {
398-
cwr.AutostartSchedule=&futureTimeCron
374+
cwr.AutostartSchedule=ptr.Ref(sched.String())
399375
})
400376
)
401377

402-
// When: we enable workspace autostart with some time in the future
403-
sched,err:=schedule.Weekly(futureTimeCron)
404-
require.NoError(t,err)
405-
require.NoError(t,client.UpdateWorkspaceAutostart(ctx,workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
406-
Schedule:ptr.Ref(sched.String()),
407-
}))
408-
409-
// When: the autobuild executor ticks
378+
// When: the autobuild executor ticks before the next scheduled time
410379
gofunc() {
411-
tickCh<-time.Now().UTC()
380+
tickCh<-sched.Next(workspace.LatestBuild.CreatedAt).Add(-time.Minute)
412381
close(tickCh)
413382
}()
414383

@@ -573,6 +542,13 @@ func mustWorkspace(t *testing.T, client *codersdk.Client, workspaceID uuid.UUID)
573542
returnws
574543
}
575544

545+
funcmustSchedule(t*testing.T,sstring)*schedule.Schedule {
546+
t.Helper()
547+
sched,err:=schedule.Weekly(s)
548+
require.NoError(t,err)
549+
returnsched
550+
}
551+
576552
funcTestMain(m*testing.M) {
577553
goleak.VerifyTestMain(m)
578554
}

‎coderd/database/databasefake/databasefake.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,14 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
12241224
q.mutex.Lock()
12251225
deferq.mutex.Unlock()
12261226

1227+
// default values
1228+
ifarg.MaxTtl==0 {
1229+
arg.MaxTtl=int64(168*time.Hour)
1230+
}
1231+
ifarg.MinAutostartInterval==0 {
1232+
arg.MinAutostartInterval=int64(time.Hour)
1233+
}
1234+
12271235
//nolint:gosimple
12281236
template:= database.Template{
12291237
ID:arg.ID,

‎coderd/templates.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"time"
89

910
"github.com/go-chi/chi/v5"
1011
"github.com/google/uuid"
@@ -14,9 +15,15 @@ import (
1415
"github.com/coder/coder/coderd/httpapi"
1516
"github.com/coder/coder/coderd/httpmw"
1617
"github.com/coder/coder/coderd/rbac"
18+
"github.com/coder/coder/coderd/util/ptr"
1719
"github.com/coder/coder/codersdk"
1820
)
1921

22+
var (
23+
maxTTLDefault=24*7*time.Hour
24+
minAutostartIntervalDefault=time.Hour
25+
)
26+
2027
// Returns a single template.
2128
func (api*API)template(rw http.ResponseWriter,r*http.Request) {
2229
template:=httpmw.TemplateParam(r)
@@ -135,18 +142,30 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
135142
return
136143
}
137144

145+
maxTTL:=maxTTLDefault
146+
if!ptr.NilOrZero(createTemplate.MaxTTLMillis) {
147+
maxTTL=time.Duration(*createTemplate.MaxTTLMillis)*time.Millisecond
148+
}
149+
150+
minAutostartInterval:=minAutostartIntervalDefault
151+
if!ptr.NilOrZero(createTemplate.MinAutostartIntervalMillis) {
152+
minAutostartInterval=time.Duration(*createTemplate.MinAutostartIntervalMillis)*time.Millisecond
153+
}
154+
138155
vartemplate codersdk.Template
139156
err=api.Database.InTx(func(db database.Store)error {
140157
now:=database.Now()
141158
dbTemplate,err:=db.InsertTemplate(r.Context(), database.InsertTemplateParams{
142-
ID:uuid.New(),
143-
CreatedAt:now,
144-
UpdatedAt:now,
145-
OrganizationID:organization.ID,
146-
Name:createTemplate.Name,
147-
Provisioner:importJob.Provisioner,
148-
ActiveVersionID:templateVersion.ID,
149-
Description:createTemplate.Description,
159+
ID:uuid.New(),
160+
CreatedAt:now,
161+
UpdatedAt:now,
162+
OrganizationID:organization.ID,
163+
Name:createTemplate.Name,
164+
Provisioner:importJob.Provisioner,
165+
ActiveVersionID:templateVersion.ID,
166+
Description:createTemplate.Description,
167+
MaxTtl:int64(maxTTL),
168+
MinAutostartInterval:int64(minAutostartInterval),
150169
})
151170
iferr!=nil {
152171
returnxerrors.Errorf("insert template: %s",err)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp