Skip to content

Commit fd6dbb9

Browse files
authored
tgc-revival: add TGCTest struct (#15471)
1 parent 3768aee commit fd6dbb9

File tree

6 files changed

+34
-37
lines changed

6 files changed

+34
-37
lines changed

mmv1/api/resource.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ type TGCResource struct {
393393
// Generally, it shouldn't be set when the identity can be decided.
394394
// Otherswise, it should be set.
395395
CaiIdentity string `yaml:"cai_identity,omitempty"`
396+
397+
// Tests for TGC, will automatically be filled with resource's examples
398+
// and handwritten tests. Can be specified in order to skip specific tests.
399+
TGCTests []resource.TGCTest `yaml:"tgc_tests,omitempty"`
396400
}
397401

398402
func (r *Resource) UnmarshalYAML(unmarshal func(any) error) error {
@@ -2281,7 +2285,7 @@ func (r Resource) MarkdownHeader(templatePath string) string {
22812285
// TGC Methods
22822286
// ====================
22832287
// Lists fields that test.BidirectionalConversion should ignore
2284-
func (r Resource) TGCTestIgnorePropertiesToStrings(e resource.Examples) []string {
2288+
func (r Resource) TGCTestIgnorePropertiesToStrings() []string {
22852289
props := []string{
22862290
"depends_on",
22872291
"count",
@@ -2301,7 +2305,6 @@ func (r Resource) TGCTestIgnorePropertiesToStrings(e resource.Examples) []string
23012305
props = append(props, tp.MetadataLineage())
23022306
}
23032307
}
2304-
props = append(props, e.TGCTestIgnoreExtra...)
23052308

23062309
slices.Sort(props)
23072310
return props

mmv1/api/resource/examples.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,6 @@ type Examples struct {
179179
// ====================
180180
// TGC
181181
// ====================
182-
// Extra properties to ignore test.
183-
// These properties are present in Terraform resources schema, but not in CAI assets.
184-
// Virtual Fields and url parameters are already ignored by default and do not need to be duplicated here.
185-
TGCTestIgnoreExtra []string `yaml:"tgc_test_ignore_extra,omitempty"`
186-
// The properties ignored in CAI assets. It is rarely used.
187-
TGCTestIgnoreInAsset []string `yaml:"tgc_test_ignore_in_asset,omitempty"`
188-
// The reason to skip a test. For example, a link to a ticket explaining the issue that needs to be resolved before
189-
// unskipping the test. If this is not empty, the test will be skipped.
190182
TGCSkipTest string `yaml:"tgc_skip_test,omitempty"`
191183
}
192184

mmv1/provider/terraform_tgc_next.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api"
3131
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/product"
32+
"github.com/GoogleCloudPlatform/magic-modules/mmv1/api/resource"
3233
"github.com/GoogleCloudPlatform/magic-modules/mmv1/google"
3334
"github.com/otiai10/copy"
3435
)
@@ -103,6 +104,7 @@ func (tgc TerraformGoogleConversionNext) GenerateObject(object api.Resource, out
103104

104105
if !object.IsExcluded() {
105106
tgc.GenerateResource(object, *templateData, outputFolder, generateCode, generateDocs)
107+
tgc.addTestsFromExamples(&object)
106108
tgc.GenerateResourceTests(object, *templateData, outputFolder)
107109
}
108110
}
@@ -130,16 +132,7 @@ func (tgc TerraformGoogleConversionNext) GenerateCaiToHclObjects(outputFolder, r
130132
}
131133

132134
func (tgc *TerraformGoogleConversionNext) GenerateResourceTests(object api.Resource, templateData TemplateData, outputFolder string) {
133-
eligibleExample := false
134-
for _, example := range object.Examples {
135-
if !example.ExcludeTest {
136-
if object.ProductMetadata.VersionObjOrClosest(tgc.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(example.MinVersion)) >= 0 {
137-
eligibleExample = true
138-
break
139-
}
140-
}
141-
}
142-
if !eligibleExample {
135+
if len(object.TGCTests) == 0 {
143136
return
144137
}
145138

@@ -314,6 +307,21 @@ func (tgc TerraformGoogleConversionNext) replaceImportPath(outputFolder, target
314307
}
315308
}
316309

310+
func (tgc TerraformGoogleConversionNext) addTestsFromExamples(object *api.Resource) {
311+
for _, example := range object.Examples {
312+
if example.ExcludeTest {
313+
continue
314+
}
315+
if object.ProductMetadata.VersionObjOrClosest(tgc.Version.Name).CompareTo(object.ProductMetadata.VersionObjOrClosest(example.MinVersion)) < 0 {
316+
continue
317+
}
318+
object.TGCTests = append(object.TGCTests, resource.TGCTest{
319+
Name: "TestAcc" + example.TestSlug(object.ProductMetadata.Name, object.Name),
320+
Skip: example.TGCSkipTest,
321+
})
322+
}
323+
}
324+
317325
// Generates the list of resources, and gets the count of resources.
318326
// The resource object has the format
319327
//

mmv1/templates/tgc_next/test/test_file.go.tmpl

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ import (
2020
"github.com/GoogleCloudPlatform/terraform-google-conversion/v7/test"
2121
)
2222

23-
{{ range $e := $.TestExamples }}
24-
func TestAcc{{ $e.TestSlug $.ProductMetadata.Name $.Name }}(t *testing.T) {
25-
{{- if $e.TGCSkipTest }}
26-
t.Skip("{{$e.TGCSkipTest}}")
23+
{{ range $t := $.TGCTests }}
24+
func {{ $t.Name }}(t *testing.T) {
25+
{{- if $t.Skip }}
26+
t.Skip("{{$t.Skip}}")
2727
{{- end }}
2828
t.Parallel()
2929

3030
test.BidirectionalConversion(
3131
t,
3232
[]string{
33-
{{- range $field := $.TGCTestIgnorePropertiesToStrings $e }}
34-
"{{ $field }}",
35-
{{- end }}
36-
},
37-
[]string{
38-
{{- range $field := $e.TGCTestIgnoreInAsset }}
33+
{{- range $field := $.TGCTestIgnorePropertiesToStrings }}
3934
"{{ $field }}",
4035
{{- end }}
4136
},

mmv1/third_party/tgc_next/test/assert_test_files.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var (
3434
tmpDir = os.TempDir()
3535
)
3636

37-
func BidirectionalConversion(t *testing.T, ignoredFields []string, ignoredAssetFields []string) {
37+
func BidirectionalConversion(t *testing.T, ignoredFields []string) {
3838
testName := t.Name()
3939
stepNumbers, err := getStepNumbers(testName)
4040
if err != nil {
@@ -77,13 +77,13 @@ func BidirectionalConversion(t *testing.T, ignoredFields []string, ignoredAssetF
7777
tName := fmt.Sprintf("%s_%s", testName, subtestName)
7878
if primaryResource != "" {
7979
t.Logf("Test for the primary resource %s begins.", primaryResource)
80-
err = testSingleResource(t, tName, resourceTestData[primaryResource], tfDir, ignoredFields, ignoredAssetFields, logger, true)
80+
err = testSingleResource(t, tName, resourceTestData[primaryResource], tfDir, ignoredFields, logger, true)
8181
if err != nil {
8282
return err
8383
}
8484
} else {
8585
for _, testData := range resourceTestData {
86-
err = testSingleResource(t, tName, testData, tfDir, ignoredFields, ignoredAssetFields, logger, false)
86+
err = testSingleResource(t, tName, testData, tfDir, ignoredFields, logger, false)
8787
if err != nil {
8888
return err
8989
}
@@ -110,7 +110,7 @@ func BidirectionalConversion(t *testing.T, ignoredFields []string, ignoredAssetF
110110
}
111111

112112
// Tests a single resource
113-
func testSingleResource(t *testing.T, testName string, testData ResourceTestData, tfDir string, ignoredFields []string, ignoredAssetFields []string, logger *zap.Logger, primaryResource bool) error {
113+
func testSingleResource(t *testing.T, testName string, testData ResourceTestData, tfDir string, ignoredFields []string, logger *zap.Logger, primaryResource bool) error {
114114
resourceType := testData.ResourceType
115115
var tfplan2caiSupported, cai2hclSupported bool
116116
if _, tfplan2caiSupported = tfplan2caiconverters.ConverterMap[resourceType]; !tfplan2caiSupported {
@@ -213,7 +213,7 @@ func testSingleResource(t *testing.T, testName string, testData ResourceTestData
213213

214214
// Convert the export config to roundtrip assets and then convert the roundtrip assets back to roundtrip config
215215
ancestryCache, defaultProject := getAncestryCache(assets)
216-
roundtripAssets, roundtripConfigData, err := getRoundtripConfig(t, testName, tfDir, ancestryCache, defaultProject, logger, ignoredAssetFields)
216+
roundtripAssets, roundtripConfigData, err := getRoundtripConfig(t, testName, tfDir, ancestryCache, defaultProject, logger)
217217
if err != nil {
218218
return fmt.Errorf("error when converting the round-trip config: %#v", err)
219219
}
@@ -404,7 +404,7 @@ func isIgnored(key string, ignoredFields map[string]any) bool {
404404
}
405405

406406
// Converts a tfplan to CAI asset, and then converts the CAI asset into HCL
407-
func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCache map[string]string, defaultProject string, logger *zap.Logger, ignoredAssetFields []string) ([]caiasset.Asset, []byte, error) {
407+
func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCache map[string]string, defaultProject string, logger *zap.Logger) ([]caiasset.Asset, []byte, error) {
408408
fileName := fmt.Sprintf("%s_export", testName)
409409
roundtripAssetFile := fmt.Sprintf("%s_roundtrip.json", testName)
410410

@@ -421,7 +421,6 @@ func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCac
421421
return nil, nil, err
422422
}
423423

424-
deleteFieldsFromAssets(roundtripAssetsCopy, ignoredAssetFields)
425424
roundtripConfig, err := cai2hcl.Convert(roundtripAssetsCopy, &cai2hcl.Options{
426425
ErrorLogger: logger,
427426
})

0 commit comments

Comments
 (0)