@@ -227,8 +227,18 @@ func testSingleResource(t *testing.T, testName string, testData ResourceTestData
227227 }
228228
229229 if diff := cmp .Diff (string (roundtripConfigData ), string (exportConfigData )); diff != "" {
230- log .Printf ("Roundtrip config is different from the export config.\n roundtrip config:\n %s\n export config:\n %s" , string (roundtripConfigData ), string (exportConfigData ))
231- return fmt .Errorf ("test %s got diff (-want +got): %s" , testName , diff )
230+ tfFileName := fmt .Sprintf ("%s_roundtrip" , testName )
231+ jsonFileName := fmt .Sprintf ("%s_reexport" , testName )
232+
233+ reexportAssets , err := tfplan2caiConvert (t , tfFileName , jsonFileName , tfDir , ancestryCache , defaultProject , logger )
234+ if err != nil {
235+ return fmt .Errorf ("error when converting the third round-trip config: %#v" , err )
236+ }
237+
238+ if assestsDiff := cmp .Diff (reexportAssets , roundtripAssets ); assestsDiff != "" {
239+ log .Printf ("Roundtrip config is different from the export config.\n roundtrip config:\n %s\n export config:\n %s" , string (roundtripConfigData ), string (exportConfigData ))
240+ return fmt .Errorf ("test %s got diff (-want +got): %s" , testName , diff )
241+ }
232242 }
233243 log .Printf ("Step 2 passes for resource %s. Roundtrip config and export config are identical" , testData .ResourceAddress )
234244
@@ -377,19 +387,46 @@ func isIgnored(key string, ignoredFields map[string]any) bool {
377387// Converts a tfplan to CAI asset, and then converts the CAI asset into HCL
378388func getRoundtripConfig (t * testing.T , testName string , tfDir string , ancestryCache map [string ]string , defaultProject string , logger * zap.Logger , ignoredAssetFields []string ) ([]caiasset.Asset , []byte , error ) {
379389 fileName := fmt .Sprintf ("%s_export" , testName )
390+ roundtripAssetFile := fmt .Sprintf ("%s_roundtrip.json" , testName )
391+
392+ roundtripAssets , err := tfplan2caiConvert (t , fileName , roundtripAssetFile , tfDir , ancestryCache , defaultProject , logger )
393+ if err != nil {
394+ return nil , nil , err
395+ }
396+
397+ var roundtripAssetsCopy []caiasset.Asset
398+ // Perform the deep copy in case the assets are transformed in cai2hcl
399+ err = DeepCopyMap (roundtripAssets , & roundtripAssetsCopy )
400+ if err != nil {
401+ fmt .Println ("Error during deep copy:" , err )
402+ return nil , nil , err
403+ }
404+
405+ deleteFieldsFromAssets (roundtripAssetsCopy , ignoredAssetFields )
406+ roundtripConfig , err := cai2hcl .Convert (roundtripAssetsCopy , & cai2hcl.Options {
407+ ErrorLogger : logger ,
408+ })
409+ if err != nil {
410+ return nil , nil , err
411+ }
412+
413+ return roundtripAssets , roundtripConfig , nil
414+ }
380415
416+ // Converts tf file to CAI assets
417+ func tfplan2caiConvert (t * testing.T , tfFileName , jsonFileName string , tfDir string , ancestryCache map [string ]string , defaultProject string , logger * zap.Logger ) ([]caiasset.Asset , error ) {
381418 // Run terraform init and terraform apply to generate tfplan.json files
382- terraformWorkflow (t , tfDir , fileName )
419+ terraformWorkflow (t , tfDir , tfFileName )
383420
384- planFile := fmt .Sprintf ("%s.tfplan.json" , fileName )
421+ planFile := fmt .Sprintf ("%s.tfplan.json" , tfFileName )
385422 planfilePath := filepath .Join (tfDir , planFile )
386423 jsonPlan , err := os .ReadFile (planfilePath )
387424 if err != nil {
388- return nil , nil , err
425+ return nil , err
389426 }
390427
391428 ctx := context .Background ()
392- roundtripAssets , err := tfplan2cai .Convert (ctx , jsonPlan , & tfplan2cai.Options {
429+ assets , err := tfplan2cai .Convert (ctx , jsonPlan , & tfplan2cai.Options {
393430 ErrorLogger : logger ,
394431 Offline : true ,
395432 DefaultProject : defaultProject ,
@@ -400,24 +437,14 @@ func getRoundtripConfig(t *testing.T, testName string, tfDir string, ancestryCac
400437 })
401438
402439 if err != nil {
403- return nil , nil , err
440+ return nil , err
404441 }
405442
406- deleteFieldsFromAssets (roundtripAssets , ignoredAssetFields )
407-
408443 if os .Getenv ("WRITE_FILES" ) != "" {
409- roundtripAssetFile := fmt .Sprintf ("%s_roundtrip.json" , testName )
410- writeJSONFile (roundtripAssetFile , roundtripAssets )
411- }
412-
413- roundtripConfig , err := cai2hcl .Convert (roundtripAssets , & cai2hcl.Options {
414- ErrorLogger : logger ,
415- })
416- if err != nil {
417- return nil , nil , err
444+ writeJSONFile (jsonFileName , assets )
418445 }
419446
420- return roundtripAssets , roundtripConfig , nil
447+ return assets , nil
421448}
422449
423450// Example:
0 commit comments