Skip to content

Commit 5a0e96f

Browse files
authored
Merge pull request #45215 from hashicorp/td-test-gen-vcr-ready
test generation: Updates Resource Identity tests to use VCR-ready random string generation
2 parents e5f91e8 + 8770a04 commit 5a0e96f

File tree

176 files changed

+695
-878
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

176 files changed

+695
-878
lines changed

internal/generate/identitytests/main.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
//go:build generate
4+
//go:build ignore
55

66
package main
77

@@ -526,7 +526,6 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
526526
IdentityVersions: make(map[int64]*version.Version, 0),
527527
}
528528
skip := false
529-
generatorSeen := false
530529
tlsKey := false
531530
var tlsKeyCN string
532531

@@ -608,11 +607,6 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
608607
continue
609608
}
610609

611-
// This needs better handling
612-
if _, ok := args.Keyword["generator"]; ok {
613-
generatorSeen = true
614-
}
615-
616610
if attr, ok := args.Keyword["idAttrDuplicates"]; ok {
617611
d.idAttrDuplicates = attr
618612
d.GoImports = append(d.GoImports,
@@ -749,6 +743,10 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
749743

750744
if d.HasResourceIdentity() {
751745
if !skip {
746+
if err := tests.Configure(&d.CommonArgs); err != nil {
747+
v.errs = append(v.errs, fmt.Errorf("%s: %w", fmt.Sprintf("%s.%s", v.packageName, v.functionName), err))
748+
return
749+
}
752750
if d.idAttrDuplicates != "" {
753751
d.GoImports = append(d.GoImports,
754752
common.GoImport{
@@ -759,29 +757,13 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
759757
},
760758
)
761759
}
762-
if d.Name == "" {
763-
v.errs = append(v.errs, fmt.Errorf("no name parameter set: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
764-
return
765-
}
766760
if d.HasV6_0NullValuesError {
767761
d.PreIdentityVersion = v5_100_0
768762
}
769763
if !d.HasNoPreExistingResource && d.PreIdentityVersion == nil {
770764
v.errs = append(v.errs, fmt.Errorf("preIdentityVersion is required when hasNoPreExistingResource is false: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
771765
return
772766
}
773-
if !generatorSeen {
774-
d.Generator = "sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)"
775-
d.GoImports = append(d.GoImports,
776-
common.GoImport{
777-
Path: "github.com/hashicorp/terraform-plugin-testing/helper/acctest",
778-
Alias: "sdkacctest",
779-
},
780-
common.GoImport{
781-
Path: "github.com/hashicorp/terraform-provider-aws/internal/acctest",
782-
},
783-
)
784-
}
785767
if d.IsARNIdentity() {
786768
d.arnAttribute = d.IdentityAttributeName()
787769
}
@@ -795,6 +777,7 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
795777
if d.IsSingletonIdentity() {
796778
d.Serialize = true
797779
}
780+
798781
v.identityResources = append(v.identityResources, d)
799782
}
800783
}

internal/generate/tagstests/main.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: MPL-2.0
33

4-
//go:build generate
4+
//go:build ignore
55

66
package main
77

@@ -516,7 +516,6 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
516516
}
517517
tagged := false
518518
skip := false
519-
generatorSeen := false
520519
tlsKey := false
521520
var tlsKeyCN string
522521
hasIdentifierAttribute := false
@@ -582,11 +581,6 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
582581
continue
583582
}
584583

585-
// This needs better handling
586-
if _, ok := args.Keyword["generator"]; ok {
587-
generatorSeen = true
588-
}
589-
590584
if attr, ok := args.Keyword["tagsIdentifierAttribute"]; ok {
591585
d.overrideIdentifierAttribute = attr
592586
}
@@ -695,22 +689,14 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
695689

696690
if tagged {
697691
if !skip {
698-
if d.Name == "" {
699-
v.errs = append(v.errs, fmt.Errorf("no name parameter set: %s", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
692+
if err := tests.Configure(&d.CommonArgs); err != nil {
693+
v.errs = append(v.errs, fmt.Errorf("%s: %w", fmt.Sprintf("%s.%s", v.packageName, v.functionName), err))
700694
return
701695
}
702696
if !hasIdentifierAttribute && len(d.overrideIdentifierAttribute) == 0 {
703697
v.errs = append(v.errs, fmt.Errorf("@Tags specification for %s does not use identifierAttribute. Missing @Testing(tagsIdentifierAttribute) and possibly tagsResourceType", fmt.Sprintf("%s.%s", v.packageName, v.functionName)))
704698
return
705699
}
706-
if !generatorSeen {
707-
d.Generator = "acctest.RandomWithPrefix(t, acctest.ResourcePrefix)"
708-
d.GoImports = append(d.GoImports,
709-
common.GoImport{
710-
Path: "github.com/hashicorp/terraform-provider-aws/internal/acctest",
711-
},
712-
)
713-
}
714700
if d.HasInherentRegionIdentity() {
715701
if d.Implementation == common.ImplementationFramework {
716702
if !slices.Contains(d.IdentityDuplicateAttrNames, "id") {
@@ -721,6 +707,7 @@ func (v *visitor) processFuncDecl(funcDecl *ast.FuncDecl) {
721707
if d.IsSingletonIdentity() {
722708
d.Serialize = true
723709
}
710+
724711
v.taggedResources = append(v.taggedResources, d)
725712
}
726713
}

internal/generate/tests/annotations.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package tests
55

66
import (
7+
"errors"
78
"fmt"
89
"strings"
910

@@ -51,7 +52,8 @@ type CommonArgs struct {
5152
UseAlternateAccount bool
5253
AlternateRegionProvider bool
5354

54-
Generator string
55+
Generator string
56+
generatorSeen bool
5557

5658
RequiredEnvVars []string
5759

@@ -333,6 +335,7 @@ func ParseTestingAnnotations(args common.Args, stuff *CommonArgs) error {
333335

334336
// TF Variables
335337
if attr, ok := args.Keyword["generator"]; ok {
338+
stuff.generatorSeen = true
336339
if attr != "false" {
337340
if funcName, importSpec, err := common.ParseIdentifierSpec(attr); err != nil {
338341
return fmt.Errorf("%s: %w", attr, err)
@@ -488,3 +491,20 @@ func endpointsConstOrQuote(region string) string {
488491

489492
return buf.String()
490493
}
494+
495+
func Configure(d *CommonArgs) error {
496+
if d.Name == "" {
497+
return errors.New("no name parameter set")
498+
}
499+
500+
if !d.generatorSeen {
501+
d.Generator = "acctest.RandomWithPrefix(t, acctest.ResourcePrefix)"
502+
d.GoImports = append(d.GoImports,
503+
common.GoImport{
504+
Path: "github.com/hashicorp/terraform-provider-aws/internal/acctest",
505+
},
506+
)
507+
}
508+
509+
return nil
510+
}

internal/service/acmpca/certificate_identity_gen_test.go

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/service/amp/rule_group_namespace_identity_gen_test.go

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/service/appflow/connector_profile_identity_gen_test.go

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/service/appflow/flow_identity_gen_test.go

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)