@@ -10,13 +10,13 @@ import (
10
10
11
11
"github.com/coder/quartz"
12
12
"github.com/google/uuid"
13
- "github.com/stretchr/testify/assert"
14
13
"github.com/stretchr/testify/require"
15
14
16
15
"github.com/coder/coder/v2/coderd/coderdtest"
17
16
"github.com/coder/coder/v2/coderd/database"
18
17
"github.com/coder/coder/v2/coderd/database/dbauthz"
19
18
"github.com/coder/coder/v2/coderd/database/dbtestutil"
19
+ agplprebuilds "github.com/coder/coder/v2/coderd/prebuilds"
20
20
"github.com/coder/coder/v2/coderd/rbac"
21
21
"github.com/coder/coder/v2/codersdk"
22
22
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
@@ -78,40 +78,20 @@ func TestClaimPrebuild(t *testing.T) {
78
78
)
79
79
80
80
cases := map [string ]struct {
81
- entitlementEnabled bool
82
- experimentEnabled bool
83
- attemptPrebuildClaim bool
84
81
expectPrebuildClaimed bool
85
82
markPrebuildsClaimable bool
86
- expectedPrebuildsCount int
87
83
}{
88
- "without the experiment enabled, prebuilds will not provisioned" : {
89
- experimentEnabled : false ,
90
- entitlementEnabled : true ,
91
- attemptPrebuildClaim : false ,
92
- expectedPrebuildsCount : 0 ,
93
- },
94
- "without the entitlement, prebuilds will not provisioned" : {
95
- experimentEnabled : true ,
96
- entitlementEnabled : false ,
97
- attemptPrebuildClaim : false ,
98
- expectedPrebuildsCount : 0 ,
99
- },
100
- "with everything enabled, but no eligible prebuilds to claim" : {
101
- entitlementEnabled : true ,
102
- experimentEnabled : true ,
103
- attemptPrebuildClaim : true ,
84
+ "no eligible prebuilds to claim" : {
104
85
expectPrebuildClaimed : false ,
105
86
markPrebuildsClaimable : false ,
106
- expectedPrebuildsCount : desiredInstances * presetCount ,
107
87
},
108
- "with everything enabled, claiming an eligible prebuild should succeed" : {
109
- entitlementEnabled : true ,
110
- experimentEnabled : true ,
111
- attemptPrebuildClaim : true ,
88
+ "claiming an eligible prebuild should succeed" : {
89
+ expectPrebuildClaimed : true ,
90
+ markPrebuildsClaimable : true ,
91
+ },
92
+ "claiming an eligible prebuild results in error" : {
112
93
expectPrebuildClaimed : true ,
113
94
markPrebuildsClaimable : true ,
114
- expectedPrebuildsCount : desiredInstances * presetCount ,
115
95
},
116
96
}
117
97
@@ -121,49 +101,26 @@ func TestClaimPrebuild(t *testing.T) {
121
101
t .Run (name , func (t * testing.T ) {
122
102
t .Parallel ()
123
103
124
- // Setup. // TODO: abstract?
125
-
104
+ // Setup.
126
105
ctx := testutil .Context (t , testutil .WaitMedium )
127
106
db , pubsub := dbtestutil .NewDB (t )
128
107
spy := newStoreSpy (db )
129
-
130
- //var prebuildsEntitled int64
131
- //if tc.entitlementEnabled {
132
- // prebuildsEntitled = 1
133
- //}
108
+ expectedPrebuildsCount := desiredInstances * presetCount
134
109
135
110
logger := testutil .Logger (t )
136
- client , _ , _ , owner := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
111
+ client , _ , api , owner := coderdenttest .NewWithAPI (t , & coderdenttest.Options {
137
112
Options : & coderdtest.Options {
138
113
IncludeProvisionerDaemon : true ,
139
114
Database : spy ,
140
115
Pubsub : pubsub ,
141
- DeploymentValues : coderdtest .DeploymentValues (t , func (values * codersdk.DeploymentValues ) {
142
- //values.Prebuilds.ReconciliationInterval = serpent.Duration(time.Hour) // We will kick off a reconciliation manually.
143
- //
144
- //if tc.experimentEnabled {
145
- // values.Experiments = serpent.StringArray{string(codersdk.ExperimentWorkspacePrebuilds)}
146
- //}
147
- }),
148
116
},
149
117
150
118
EntitlementsUpdateInterval : time .Second ,
151
- //LicenseOptions: &coderdenttest.LicenseOptions{
152
- // Features: license.Features{
153
- // codersdk.FeatureWorkspacePrebuilds: prebuildsEntitled,
154
- // },
155
- //},
156
119
})
157
- reconciler := prebuilds .NewStoreReconciler (spy , pubsub , codersdk.PrebuildsConfig {}, logger , quartz .NewMock (t ))
158
-
159
- // The entitlements will need to refresh before the reconciler is set.
160
- require .Eventually (t , func () bool {
161
- if tc .entitlementEnabled && tc .experimentEnabled {
162
- assert .IsType (t , & prebuilds.StoreReconciler {}, reconciler )
163
- }
164
120
165
- return reconciler != nil
166
- }, testutil .WaitSuperLong , testutil .IntervalFast )
121
+ reconciler := prebuilds .NewStoreReconciler (spy , pubsub , codersdk.PrebuildsConfig {}, logger , quartz .NewMock (t ))
122
+ var claimer agplprebuilds.Claimer = & prebuilds.EnterpriseClaimer {}
123
+ api .AGPL .PrebuildsClaimer .Store (& claimer )
167
124
168
125
version := coderdtest .CreateTemplateVersion (t , client , owner .OrganizationID , templateWithAgentAndPresetsWithPrebuilds (desiredInstances ))
169
126
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
@@ -179,14 +136,8 @@ func TestClaimPrebuild(t *testing.T) {
179
136
// Given: the reconciliation state is snapshot.
180
137
state , err := reconciler .SnapshotState (ctx , spy )
181
138
require .NoError (t , err )
182
-
183
- // When: the experiment or entitlement is not preset, there should be nothing to reconcile.
184
- if ! tc .entitlementEnabled || ! tc .experimentEnabled {
185
- require .Len (t , state .Presets , 0 )
186
- return
187
- }
188
-
189
139
require .Len (t , state .Presets , presetCount )
140
+
190
141
// When: a reconciliation is setup for each preset.
191
142
for _ , preset := range presets {
192
143
ps , err := state .FilterByPreset (preset .ID )
@@ -215,6 +166,7 @@ func TestClaimPrebuild(t *testing.T) {
215
166
agents , err := db .GetWorkspaceAgentsInLatestBuildByWorkspaceID (ctx , row .ID )
216
167
require .NoError (t , err )
217
168
169
+ // Workspaces are eligible once its agent is marked "ready".
218
170
for _ , agent := range agents {
219
171
require .NoError (t , db .UpdateWorkspaceAgentLifecycleStateByID (ctx , database.UpdateWorkspaceAgentLifecycleStateByIDParams {
220
172
ID : agent .ID ,
@@ -225,9 +177,9 @@ func TestClaimPrebuild(t *testing.T) {
225
177
}
226
178
}
227
179
228
- t .Logf ("found %d running prebuilds so far, want %d" , len (runningPrebuilds ), tc . expectedPrebuildsCount )
180
+ t .Logf ("found %d running prebuilds so far, want %d" , len (runningPrebuilds ), expectedPrebuildsCount )
229
181
230
- return len (runningPrebuilds ) == tc . expectedPrebuildsCount
182
+ return len (runningPrebuilds ) == expectedPrebuildsCount
231
183
}, testutil .WaitSuperLong , testutil .IntervalSlow )
232
184
233
185
// When: a user creates a new workspace with a preset for which prebuilds are configured.
@@ -243,21 +195,10 @@ func TestClaimPrebuild(t *testing.T) {
243
195
TemplateVersionPresetID : presets [0 ].ID ,
244
196
ClaimPrebuildIfAvailable : true , // TODO: doesn't do anything yet; it probably should though.
245
197
})
198
+
246
199
require .NoError (t , err )
247
200
coderdtest .AwaitWorkspaceBuildJobCompleted (t , userClient , userWorkspace .LatestBuild .ID )
248
201
249
- // Then: if we're not expecting any prebuild claims to succeed, handle this specifically.
250
- if ! tc .attemptPrebuildClaim {
251
- require .EqualValues (t , spy .claims .Load (), 0 )
252
- require .Nil (t , spy .claimedWorkspace .Load ())
253
-
254
- currentPrebuilds , err := spy .GetRunningPrebuiltWorkspaces (ctx )
255
- require .NoError (t , err )
256
- // The number of prebuilds should NOT change.
257
- require .Equal (t , len (currentPrebuilds ), len (runningPrebuilds ))
258
- return
259
- }
260
-
261
202
// Then: a prebuild should have been claimed.
262
203
require .EqualValues (t , spy .claims .Load (), 1 )
263
204
require .NotNil (t , spy .claims .Load ())
@@ -315,9 +256,9 @@ func TestClaimPrebuild(t *testing.T) {
315
256
rows , err := spy .GetRunningPrebuiltWorkspaces (ctx )
316
257
require .NoError (t , err )
317
258
318
- t .Logf ("found %d running prebuilds so far, want %d" , len (rows ), tc . expectedPrebuildsCount )
259
+ t .Logf ("found %d running prebuilds so far, want %d" , len (rows ), expectedPrebuildsCount )
319
260
320
- return len (runningPrebuilds ) == tc . expectedPrebuildsCount
261
+ return len (runningPrebuilds ) == expectedPrebuildsCount
321
262
}, testutil .WaitSuperLong , testutil .IntervalSlow )
322
263
323
264
// Then: when restarting the created workspace (which claimed a prebuild), it should not try and claim a new prebuild.
0 commit comments