@@ -4,11 +4,15 @@ import (
4
4
"testing"
5
5
"time"
6
6
7
+ "github.com/stretchr/testify/assert"
7
8
"github.com/stretchr/testify/require"
8
9
9
10
"github.com/coder/coder/v2/cli/clitest"
10
11
"github.com/coder/coder/v2/coderd/coderdtest"
12
+ "github.com/coder/coder/v2/coderd/database"
13
+ "github.com/coder/coder/v2/coderd/database/dbfake"
11
14
"github.com/coder/coder/v2/coderd/rbac"
15
+ "github.com/coder/coder/v2/coderd/util/ptr"
12
16
"github.com/coder/coder/v2/codersdk"
13
17
"github.com/coder/coder/v2/enterprise/coderd/coderdenttest"
14
18
"github.com/coder/coder/v2/enterprise/coderd/license"
@@ -105,7 +109,6 @@ func TestTemplateEdit(t *testing.T) {
105
109
_ = coderdtest .AwaitTemplateVersionJobCompleted (t , templateAdmin , version .ID )
106
110
template := coderdtest .CreateTemplate (t , templateAdmin , owner .OrganizationID , version .ID )
107
111
require .False (t , template .RequireActiveVersion )
108
-
109
112
const (
110
113
expectedFailureTTL = time .Hour * 3
111
114
expectedDormancyThreshold = time .Hour * 4
@@ -150,4 +153,168 @@ func TestTemplateEdit(t *testing.T) {
150
153
require .Equal (t , expectedDormancyThreshold .Milliseconds (), template .TimeTilDormantMillis )
151
154
require .Equal (t , expectedDormancyAutoDeletion .Milliseconds (), template .TimeTilDormantAutoDeleteMillis )
152
155
})
156
+
157
+ // Test that omitting a flag does not update a template with the
158
+ // default for a flag.
159
+ t .Run ("DefaultsDontOverride" , func (t * testing.T ) {
160
+ t .Parallel ()
161
+
162
+ ctx := testutil .Context (t , testutil .WaitMedium )
163
+ ownerClient , db , owner := coderdenttest .NewWithDatabase (t , & coderdenttest.Options {
164
+ LicenseOptions : & coderdenttest.LicenseOptions {
165
+ Features : license.Features {
166
+ codersdk .FeatureAdvancedTemplateScheduling : 1 ,
167
+ codersdk .FeatureAccessControl : 1 ,
168
+ codersdk .FeatureTemplateRBAC : 1 ,
169
+ },
170
+ },
171
+ })
172
+
173
+ dbtemplate := dbfake .TemplateVersion (t , db ).Seed (database.TemplateVersion {
174
+ CreatedBy : owner .UserID ,
175
+ OrganizationID : owner .OrganizationID ,
176
+ }).Do ().Template
177
+
178
+ var (
179
+ expectedName = "template"
180
+ expectedDisplayName = "template_display"
181
+ expectedDescription = "My description"
182
+ expectedIcon = "icon.pjg"
183
+ expectedDefaultTTLMillis = time .Hour .Milliseconds ()
184
+ expectedMaxTTLMillis = (time .Hour * 24 ).Milliseconds ()
185
+ expectedAllowAutostart = false
186
+ expectedAllowAutostop = false
187
+ expectedFailureTTLMillis = time .Minute .Milliseconds ()
188
+ expectedDormancyMillis = 2 * time .Minute .Milliseconds ()
189
+ expectedAutoDeleteMillis = 3 * time .Minute .Milliseconds ()
190
+ expectedRequireActiveVersion = true
191
+ expectedAllowCancelJobs = false
192
+ deprecationMessage = "Deprecate me"
193
+ expectedDisableEveryone = true
194
+ expectedAutostartDaysOfWeek = []string {}
195
+ expectedAutoStopDaysOfWeek = []string {}
196
+ expectedAutoStopWeeks = 1
197
+ )
198
+
199
+ assertFieldsFn := func (t * testing.T , tpl codersdk.Template , acl codersdk.TemplateACL ) {
200
+ t .Helper ()
201
+
202
+ assert .Equal (t , expectedName , tpl .Name )
203
+ assert .Equal (t , expectedDisplayName , tpl .DisplayName )
204
+ assert .Equal (t , expectedDescription , tpl .Description )
205
+ assert .Equal (t , expectedIcon , tpl .Icon )
206
+ assert .Equal (t , expectedDefaultTTLMillis , tpl .DefaultTTLMillis )
207
+ assert .Equal (t , expectedMaxTTLMillis , tpl .MaxTTLMillis )
208
+ assert .Equal (t , expectedAllowAutostart , tpl .AllowUserAutostart )
209
+ assert .Equal (t , expectedAllowAutostop , tpl .AllowUserAutostop )
210
+ assert .Equal (t , expectedFailureTTLMillis , tpl .FailureTTLMillis )
211
+ assert .Equal (t , expectedDormancyMillis , tpl .TimeTilDormantMillis )
212
+ assert .Equal (t , expectedAutoDeleteMillis , tpl .TimeTilDormantAutoDeleteMillis )
213
+ assert .Equal (t , expectedRequireActiveVersion , tpl .RequireActiveVersion )
214
+ assert .Equal (t , deprecationMessage , tpl .DeprecationMessage )
215
+ assert .Equal (t , expectedAllowCancelJobs , tpl .AllowUserCancelWorkspaceJobs )
216
+ assert .Equal (t , len (acl .Groups ) == 0 , expectedDisableEveryone )
217
+ assert .Equal (t , expectedAutostartDaysOfWeek , tpl .AutostartRequirement .DaysOfWeek )
218
+ assert .Equal (t , expectedAutoStopDaysOfWeek , tpl .AutostopRequirement .DaysOfWeek )
219
+ assert .Equal (t , int64 (expectedAutoStopWeeks ), tpl .AutostopRequirement .Weeks )
220
+ }
221
+
222
+ template , err := ownerClient .UpdateTemplateMeta (ctx , dbtemplate .ID , codersdk.UpdateTemplateMeta {
223
+ Name : expectedName ,
224
+ DisplayName : expectedDisplayName ,
225
+ Description : expectedDescription ,
226
+ Icon : expectedIcon ,
227
+ DefaultTTLMillis : expectedDefaultTTLMillis ,
228
+ MaxTTLMillis : expectedMaxTTLMillis ,
229
+ AllowUserAutostop : expectedAllowAutostop ,
230
+ AllowUserAutostart : expectedAllowAutostart ,
231
+ FailureTTLMillis : expectedFailureTTLMillis ,
232
+ TimeTilDormantMillis : expectedDormancyMillis ,
233
+ TimeTilDormantAutoDeleteMillis : expectedAutoDeleteMillis ,
234
+ RequireActiveVersion : expectedRequireActiveVersion ,
235
+ DeprecationMessage : ptr .Ref (deprecationMessage ),
236
+ DisableEveryoneGroupAccess : expectedDisableEveryone ,
237
+ AllowUserCancelWorkspaceJobs : expectedAllowCancelJobs ,
238
+ AutostartRequirement : & codersdk.TemplateAutostartRequirement {
239
+ DaysOfWeek : expectedAutostartDaysOfWeek ,
240
+ },
241
+ })
242
+ require .NoError (t , err )
243
+
244
+ templateACL , err := ownerClient .TemplateACL (ctx , template .ID )
245
+ require .NoError (t , err )
246
+
247
+ assertFieldsFn (t , template , templateACL )
248
+
249
+ expectedName = "newName"
250
+ inv , conf := newCLI (t , "templates" ,
251
+ "edit" , template .Name ,
252
+ "--name=newName" ,
253
+ "-y" ,
254
+ )
255
+
256
+ clitest .SetupConfig (t , ownerClient , conf )
257
+
258
+ err = inv .Run ()
259
+ require .NoError (t , err )
260
+
261
+ template , err = ownerClient .Template (ctx , template .ID )
262
+ require .NoError (t , err )
263
+ templateACL , err = ownerClient .TemplateACL (ctx , template .ID )
264
+ require .NoError (t , err )
265
+ assertFieldsFn (t , template , templateACL )
266
+
267
+ expectedAutostartDaysOfWeek = []string {"monday" , "wednesday" , "friday" }
268
+ expectedAutoStopDaysOfWeek = []string {"tuesday" , "thursday" }
269
+ expectedAutoStopWeeks = 2
270
+ expectedMaxTTLMillis = 0
271
+
272
+ template , err = ownerClient .UpdateTemplateMeta (ctx , dbtemplate .ID , codersdk.UpdateTemplateMeta {
273
+ Name : expectedName ,
274
+ DisplayName : expectedDisplayName ,
275
+ Description : expectedDescription ,
276
+ Icon : expectedIcon ,
277
+ DefaultTTLMillis : expectedDefaultTTLMillis ,
278
+ AllowUserAutostop : expectedAllowAutostop ,
279
+ AllowUserAutostart : expectedAllowAutostart ,
280
+ FailureTTLMillis : expectedFailureTTLMillis ,
281
+ TimeTilDormantMillis : expectedDormancyMillis ,
282
+ TimeTilDormantAutoDeleteMillis : expectedAutoDeleteMillis ,
283
+ RequireActiveVersion : expectedRequireActiveVersion ,
284
+ DeprecationMessage : ptr .Ref (deprecationMessage ),
285
+ DisableEveryoneGroupAccess : expectedDisableEveryone ,
286
+ AllowUserCancelWorkspaceJobs : expectedAllowCancelJobs ,
287
+ AutostartRequirement : & codersdk.TemplateAutostartRequirement {
288
+ DaysOfWeek : expectedAutostartDaysOfWeek ,
289
+ },
290
+
291
+ AutostopRequirement : & codersdk.TemplateAutostopRequirement {
292
+ DaysOfWeek : expectedAutoStopDaysOfWeek ,
293
+ Weeks : int64 (expectedAutoStopWeeks ),
294
+ },
295
+ })
296
+ require .NoError (t , err )
297
+ assertFieldsFn (t , template , templateACL )
298
+
299
+ // Rerun the update so we can assert that autostop days aren't
300
+ // mucked with.
301
+ expectedName = "newName2"
302
+ inv , conf = newCLI (t , "templates" ,
303
+ "edit" , template .Name ,
304
+ "--name=newName2" ,
305
+ "-y" ,
306
+ )
307
+
308
+ clitest .SetupConfig (t , ownerClient , conf )
309
+
310
+ err = inv .Run ()
311
+ require .NoError (t , err )
312
+
313
+ template , err = ownerClient .Template (ctx , template .ID )
314
+ require .NoError (t , err )
315
+
316
+ templateACL , err = ownerClient .TemplateACL (ctx , template .ID )
317
+ require .NoError (t , err )
318
+ assertFieldsFn (t , template , templateACL )
319
+ })
153
320
}
0 commit comments