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

Commitd4a7dba

Browse files
committed
test: add prebuilds tests
1 parent095dd80 commitd4a7dba

File tree

4 files changed

+474
-18
lines changed

4 files changed

+474
-18
lines changed

‎coderd/prebuilds/global_snapshot.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ func (s GlobalSnapshot) FilterByPreset(presetID uuid.UUID) (*PresetSnapshot, err
8585
returnprebuild.PresetID.UUID==preset.ID
8686
})
8787

88-
// Includes pending prebuilds only for non-active template versions
89-
pending:=slice.Filter(s.PendingPrebuilds,func(prebuild database.CountPendingNonActivePrebuildsRow)bool {
88+
// Includes count of pending prebuilds only for non-active template versions
89+
pendingCount:=0
90+
iffound,ok:=slice.Find(s.PendingPrebuilds,func(prebuild database.CountPendingNonActivePrebuildsRow)bool {
9091
returnprebuild.PresetID.UUID==preset.ID
91-
})
92+
});ok {
93+
pendingCount=int(found.Count)
94+
}
9295

9396
varbackoffPtr*database.GetPresetsBackoffRow
9497
backoff,found:=slice.Find(s.Backoffs,func(row database.GetPresetsBackoffRow)bool {
@@ -106,7 +109,7 @@ func (s GlobalSnapshot) FilterByPreset(presetID uuid.UUID) (*PresetSnapshot, err
106109
nonExpired,
107110
expired,
108111
inProgress,
109-
pending,
112+
pendingCount,
110113
backoffPtr,
111114
isHardLimited,
112115
s.clock,

‎coderd/prebuilds/preset_snapshot.go‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type PresetSnapshot struct {
5252
Running []database.GetRunningPrebuiltWorkspacesRow
5353
Expired []database.GetRunningPrebuiltWorkspacesRow
5454
InProgress []database.CountInProgressPrebuildsRow
55-
Pending []database.CountPendingNonActivePrebuildsRow
55+
PendingCountint
5656
Backoff*database.GetPresetsBackoffRow
5757
IsHardLimitedbool
5858
clock quartz.Clock
@@ -65,7 +65,7 @@ func NewPresetSnapshot(
6565
running []database.GetRunningPrebuiltWorkspacesRow,
6666
expired []database.GetRunningPrebuiltWorkspacesRow,
6767
inProgress []database.CountInProgressPrebuildsRow,
68-
pending []database.CountPendingNonActivePrebuildsRow,
68+
pendingCountint,
6969
backoff*database.GetPresetsBackoffRow,
7070
isHardLimitedbool,
7171
clock quartz.Clock,
@@ -77,7 +77,7 @@ func NewPresetSnapshot(
7777
Running:running,
7878
Expired:expired,
7979
InProgress:inProgress,
80-
Pending:pending,
80+
PendingCount:pendingCount,
8181
Backoff:backoff,
8282
IsHardLimited:isHardLimited,
8383
clock:clock,
@@ -358,7 +358,7 @@ func (p PresetSnapshot) handleActiveTemplateVersion() (actions []*Reconciliation
358358
// create a delete reconciliation action to remove all running prebuilt workspaces.
359359
func (pPresetSnapshot)handleInactiveTemplateVersion() (actions []*ReconciliationActions,errerror) {
360360
// Cancel pending initial prebuild jobs from inactive version
361-
iflen(p.Pending)>0 {
361+
ifp.PendingCount>0 {
362362
actions=append(actions,
363363
&ReconciliationActions{
364364
ActionType:ActionTypeCancelPending,
@@ -367,11 +367,13 @@ func (p PresetSnapshot) handleInactiveTemplateVersion() (actions []*Reconciliati
367367

368368
// Delete prebuilds running in inactive version
369369
deleteIDs:=p.getOldestPrebuildIDs(len(p.Running))
370-
actions=append(actions,
371-
&ReconciliationActions{
372-
ActionType:ActionTypeDelete,
373-
DeleteIDs:deleteIDs,
374-
})
370+
iflen(deleteIDs)>0 {
371+
actions=append(actions,
372+
&ReconciliationActions{
373+
ActionType:ActionTypeDelete,
374+
DeleteIDs:deleteIDs,
375+
})
376+
}
375377
returnactions,nil
376378
}
377379

‎coderd/prebuilds/preset_snapshot_test.go‎

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/coder/coder/v2/testutil"
10-
119
"github.com/google/uuid"
1210
"github.com/stretchr/testify/assert"
1311
"github.com/stretchr/testify/require"
1412

15-
"github.com/coder/quartz"
16-
1713
"github.com/coder/coder/v2/coderd/database"
1814
"github.com/coder/coder/v2/coderd/prebuilds"
15+
"github.com/coder/coder/v2/testutil"
16+
"github.com/coder/quartz"
1917
)
2018

2119
typeoptionsstruct {
@@ -238,6 +236,74 @@ func TestDeleteOutdatedPrebuilds(t *testing.T) {
238236
},actions)
239237
}
240238

239+
funcTestCancelPendingPrebuilds(t*testing.T) {
240+
t.Parallel()
241+
242+
// Setup
243+
current:=opts[optionSet3]
244+
clock:=quartz.NewMock(t)
245+
246+
t.Run("CancelPendingPrebuildsNonActiveVersion",func(t*testing.T) {
247+
t.Parallel()
248+
249+
// Given: a preset from a non-active version
250+
defaultPreset:=preset(false,0,current)
251+
presets:= []database.GetTemplatePresetsWithPrebuildsRow{
252+
defaultPreset,
253+
}
254+
255+
// Given: 2 pending prebuilt workspaces for the preset
256+
pending:= []database.CountPendingNonActivePrebuildsRow{{
257+
PresetID: uuid.NullUUID{
258+
UUID:defaultPreset.ID,
259+
Valid:true,
260+
},
261+
Count:2,
262+
}}
263+
264+
// When: calculating the current preset's state
265+
snapshot:=prebuilds.NewGlobalSnapshot(presets,nil,nil,nil,pending,nil,nil,clock,testutil.Logger(t))
266+
ps,err:=snapshot.FilterByPreset(current.presetID)
267+
require.NoError(t,err)
268+
269+
// Then: it should create a cancel reconciliation action
270+
actions,err:=ps.CalculateActions(backoffInterval)
271+
require.NoError(t,err)
272+
expectedAction:= []*prebuilds.ReconciliationActions{{ActionType:prebuilds.ActionTypeCancelPending}}
273+
require.Equal(t,expectedAction,actions)
274+
})
275+
276+
t.Run("NotCancelPendingPrebuildsActiveVersion",func(t*testing.T) {
277+
t.Parallel()
278+
279+
// Given: a preset from an active version
280+
defaultPreset:=preset(true,0,current)
281+
presets:= []database.GetTemplatePresetsWithPrebuildsRow{
282+
defaultPreset,
283+
}
284+
285+
// Given: 2 pending prebuilt workspaces for the preset
286+
pending:= []database.CountPendingNonActivePrebuildsRow{{
287+
PresetID: uuid.NullUUID{
288+
UUID:defaultPreset.ID,
289+
Valid:true,
290+
},
291+
Count:2,
292+
}}
293+
294+
// When: calculating the current preset's state
295+
snapshot:=prebuilds.NewGlobalSnapshot(presets,nil,nil,nil,pending,nil,nil,clock,testutil.Logger(t))
296+
ps,err:=snapshot.FilterByPreset(current.presetID)
297+
require.NoError(t,err)
298+
299+
// Then: it should not create a cancel reconciliation action
300+
actions,err:=ps.CalculateActions(backoffInterval)
301+
require.NoError(t,err)
302+
varexpectedAction []*prebuilds.ReconciliationActions
303+
require.Equal(t,expectedAction,actions)
304+
})
305+
}
306+
241307
// A new template version is created with a preset with prebuilds configured; while a prebuild is provisioning up or down,
242308
// the calculated actions should indicate the state correctly.
243309
funcTestInProgressActions(t*testing.T) {
@@ -1093,7 +1159,7 @@ func TestCalculateDesiredInstances(t *testing.T) {
10931159
nil,
10941160
nil,
10951161
nil,
1096-
nil,
1162+
0,
10971163
nil,
10981164
false,
10991165
quartz.NewMock(t),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp