@@ -22,11 +22,11 @@ import (
2222)
2323
2424func TestAccTemplateResource (t * testing.T ) {
25- if os .Getenv ("TF_ACC" )== "" {
26- t .Skip ("Acceptance tests are disabled." )
27- }
25+ // if os.Getenv("TF_ACC") == "" {
26+ // t.Skip("Acceptance tests are disabled.")
27+ // }
2828ctx := context .Background ()
29- client := integration .StartCoder (ctx ,t ,"template_acc" ,true )
29+ client := integration .StartCoder (ctx ,t ,"template_acc" ,"" )
3030firstUser ,err := client .User (ctx ,codersdk .Me )
3131require .NoError (t ,err )
3232
@@ -58,31 +58,15 @@ func TestAccTemplateResource(t *testing.T) {
5858},
5959},
6060ACL :testAccTemplateACLConfig {
61- GroupACL : []testAccTemplateKeyValueConfig {
62- {
63- Key :PtrTo (firstUser .OrganizationIDs [0 ].String ()),
64- Value :PtrTo ("use" ),
65- },
66- },
61+ null :true ,
6762},
6863}
6964
7065cfg2 := cfg1
7166cfg2 .Versions = slices .Clone (cfg2 .Versions )
7267cfg2 .Name = PtrTo ("example-template-new" )
73- cfg2 .AllowUserAutostart = PtrTo (false )
7468cfg2 .Versions [0 ].Directory = & exTemplateTwo
7569cfg2 .Versions [0 ].Name = PtrTo ("new" )
76- cfg2 .ACL .UserACL = []testAccTemplateKeyValueConfig {
77- {
78- Key :PtrTo (firstUser .ID .String ()),
79- Value :PtrTo ("admin" ),
80- },
81- }
82- cfg2 .AutostopRequirement = testAccAutostopRequirementConfig {
83- DaysOfWeek :PtrTo ([]string {"monday" ,"tuesday" }),
84- Weeks :PtrTo (int64 (2 )),
85- }
8670
8771cfg3 := cfg2
8872cfg3 .Versions = slices .Clone (cfg3 .Versions )
@@ -110,9 +94,6 @@ func TestAccTemplateResource(t *testing.T) {
11094cfg6 := cfg4
11195cfg6 .Versions = slices .Clone (cfg6 .Versions [1 :])
11296
113- cfg7 := cfg6
114- cfg7 .ACL .null = true
115-
11697resource .Test (t , resource.TestCase {
11798PreCheck :func () {testAccPreCheck (t ) },
11899IsUnitTest :true ,
@@ -187,9 +168,6 @@ func TestAccTemplateResource(t *testing.T) {
187168Check :resource .ComposeAggregateTestCheckFunc (
188169resource .TestCheckResourceAttrSet ("coderd_template.test" ,"id" ),
189170resource .TestCheckResourceAttr ("coderd_template.test" ,"name" ,"example-template-new" ),
190- resource .TestCheckResourceAttr ("coderd_template.test" ,"allow_user_auto_start" ,"false" ),
191- resource .TestCheckResourceAttr ("coderd_template.test" ,"auto_stop_requirement.days_of_week.#" ,"2" ),
192- resource .TestCheckResourceAttr ("coderd_template.test" ,"auto_stop_requirement.weeks" ,"2" ),
193171resource .TestMatchTypeSetElemNestedAttrs ("coderd_template.test" ,"versions.*" ,map [string ]* regexp.Regexp {
194172"name" :regexp .MustCompile ("new" ),
195173}),
@@ -251,14 +229,6 @@ func TestAccTemplateResource(t *testing.T) {
251229}),
252230),
253231},
254- // Unmanaged ACL
255- {
256- Config :cfg7 .String (t ),
257- Check :resource .ComposeAggregateTestCheckFunc (
258- resource .TestCheckNoResourceAttr ("coderd_template.test" ,"acl" ),
259- testAccCheckNumTemplateVersions (ctx ,client ,5 ),
260- ),
261- },
262232// Resource deleted
263233},
264234})
@@ -292,6 +262,9 @@ func TestAccTemplateResource(t *testing.T) {
292262},
293263},
294264},
265+ ACL :testAccTemplateACLConfig {
266+ null :true ,
267+ },
295268}
296269
297270cfg2 := cfg1
@@ -374,12 +347,132 @@ func TestAccTemplateResource(t *testing.T) {
374347})
375348}
376349
350+ func TestAccTemplateResourceEnterprise (t * testing.T ) {
351+ if os .Getenv ("TF_ACC" )== "" {
352+ t .Skip ("Acceptance tests are disabled." )
353+ }
354+ license := os .Getenv ("CODER_ENTERPRISE_LICENSE" )
355+ if license == "" {
356+ t .Skip ("Some template tests require an enterprise Coder deployment" )
357+ }
358+ ctx := context .Background ()
359+ client := integration .StartCoder (ctx ,t ,"template_acc" ,license )
360+ firstUser ,err := client .User (ctx ,codersdk .Me )
361+ require .NoError (t ,err )
362+
363+ cfg1 := testAccTemplateResourceConfig {
364+ URL :client .URL .String (),
365+ Token :client .SessionToken (),
366+ Name :PtrTo ("example-template" ),
367+ Versions : []testAccTemplateVersionConfig {
368+ {
369+ // Auto-generated version name
370+ Directory :PtrTo ("../../integration/template-test/example-template" ),
371+ Active :PtrTo (true ),
372+ // TODO(ethanndickson): Remove this when we add in `*.tfvars` parsing
373+ TerraformVariables : []testAccTemplateKeyValueConfig {
374+ {
375+ Key :PtrTo ("name" ),
376+ Value :PtrTo ("world" ),
377+ },
378+ },
379+ },
380+ },
381+ ACL :testAccTemplateACLConfig {
382+ GroupACL : []testAccTemplateKeyValueConfig {
383+ {
384+ Key :PtrTo (firstUser .OrganizationIDs [0 ].String ()),
385+ Value :PtrTo ("use" ),
386+ },
387+ },
388+ UserACL : []testAccTemplateKeyValueConfig {
389+ {
390+ Key :PtrTo (firstUser .ID .String ()),
391+ Value :PtrTo ("admin" ),
392+ },
393+ },
394+ },
395+ }
396+
397+ cfg2 := cfg1
398+ cfg2 .ACL .null = true
399+
400+ cfg3 := cfg2
401+ cfg3 .AllowUserAutostart = PtrTo (false )
402+ cfg3 .AutostopRequirement = testAccAutostopRequirementConfig {
403+ DaysOfWeek :PtrTo ([]string {"monday" ,"tuesday" }),
404+ Weeks :PtrTo (int64 (2 )),
405+ }
406+
407+ resource .Test (t , resource.TestCase {
408+ PreCheck :func () {testAccPreCheck (t ) },
409+ IsUnitTest :true ,
410+ ProtoV6ProviderFactories :testAccProtoV6ProviderFactories ,
411+ Steps : []resource.TestStep {
412+ {
413+ Config :cfg1 .String (t ),
414+ Check :resource .ComposeAggregateTestCheckFunc (
415+ resource .TestCheckResourceAttr ("coderd_template.test" ,"acl.groups.#" ,"1" ),
416+ resource .TestMatchTypeSetElemNestedAttrs ("coderd_template.test" ,"acl.groups.*" ,map [string ]* regexp.Regexp {
417+ "id" :regexp .MustCompile (".+" ),
418+ "role" :regexp .MustCompile ("^use$" ),
419+ }),
420+ resource .TestMatchTypeSetElemNestedAttrs ("coderd_template.test" ,"acl.users.*" ,map [string ]* regexp.Regexp {
421+ "id" :regexp .MustCompile (".+" ),
422+ "role" :regexp .MustCompile ("^admin$" ),
423+ }),
424+ ),
425+ },
426+ {
427+ Config :cfg2 .String (t ),
428+ Check :resource .ComposeAggregateTestCheckFunc (
429+ resource .TestCheckNoResourceAttr ("coderd_template.test" ,"acl" ),
430+ func (s * terraform.State )error {
431+ templates ,err := client .Templates (ctx , codersdk.TemplateFilter {})
432+ if err != nil {
433+ return err
434+ }
435+ if len (templates )!= 1 {
436+ return fmt .Errorf ("expected 1 template, got %d" ,len (templates ))
437+ }
438+ acl ,err := client .TemplateACL (ctx ,templates [0 ].ID )
439+ if err != nil {
440+ return err
441+ }
442+ if len (acl .Groups )!= 1 {
443+ return fmt .Errorf ("expected 1 group ACL, got %d" ,len (acl .Groups ))
444+ }
445+ if acl .Groups [0 ].Role != "use" && acl .Groups [0 ].ID != firstUser .OrganizationIDs [0 ] {
446+ return fmt .Errorf ("expected group ACL to be 'use' for %s, got %s" ,firstUser .OrganizationIDs [0 ].String (),acl .Groups [0 ].Role )
447+ }
448+ if len (acl .Users )!= 1 {
449+ return fmt .Errorf ("expected 1 user ACL, got %d" ,len (acl .Users ))
450+ }
451+ if acl .Users [0 ].Role != "admin" && acl .Users [0 ].ID != firstUser .ID {
452+ return fmt .Errorf ("expected user ACL to be 'admin' for %s, got %s" ,firstUser .ID .String (),acl .Users [0 ].Role )
453+ }
454+ return nil
455+ },
456+ ),
457+ },
458+ {
459+ Config :cfg3 .String (t ),
460+ Check :resource .ComposeAggregateTestCheckFunc (
461+ resource .TestCheckResourceAttr ("coderd_template.test" ,"allow_user_auto_start" ,"false" ),
462+ resource .TestCheckResourceAttr ("coderd_template.test" ,"auto_stop_requirement.days_of_week.#" ,"2" ),
463+ resource .TestCheckResourceAttr ("coderd_template.test" ,"auto_stop_requirement.weeks" ,"2" ),
464+ ),
465+ },
466+ },
467+ })
468+ }
469+
377470func TestAccTemplateResourceAGPL (t * testing.T ) {
378471if os .Getenv ("TF_ACC" )== "" {
379472t .Skip ("Acceptance tests are disabled." )
380473}
381474ctx := context .Background ()
382- client := integration .StartCoder (ctx ,t ,"template_acc" ,false )
475+ client := integration .StartCoder (ctx ,t ,"template_acc" ,"" )
383476firstUser ,err := client .User (ctx ,codersdk .Me )
384477require .NoError (t ,err )
385478