github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/states/statefile/roundtrip_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package statefile 5 6 import ( 7 "bytes" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "sort" 12 "strings" 13 "testing" 14 15 "github.com/go-test/deep" 16 ) 17 18 func TestRoundtrip(t *testing.T) { 19 const dir = "testdata/roundtrip" 20 entries, err := ioutil.ReadDir(dir) 21 if err != nil { 22 t.Fatal(err) 23 } 24 25 for _, info := range entries { 26 const inSuffix = ".in.tfstate" 27 const outSuffix = ".out.tfstate" 28 29 if info.IsDir() { 30 continue 31 } 32 inName := info.Name() 33 if !strings.HasSuffix(inName, inSuffix) { 34 continue 35 } 36 name := inName[:len(inName)-len(inSuffix)] 37 outName := name + outSuffix 38 39 t.Run(name, func(t *testing.T) { 40 oSrcWant, err := ioutil.ReadFile(filepath.Join(dir, outName)) 41 if err != nil { 42 t.Fatal(err) 43 } 44 oWant, diags := readStateV4(oSrcWant) 45 if diags.HasErrors() { 46 t.Fatal(diags.Err()) 47 } 48 49 ir, err := os.Open(filepath.Join(dir, inName)) 50 if err != nil { 51 t.Fatal(err) 52 } 53 defer ir.Close() 54 55 f, err := Read(ir) 56 if err != nil { 57 t.Fatalf("unexpected error: %s", err) 58 } 59 60 var buf bytes.Buffer 61 err = Write(f, &buf) 62 if err != nil { 63 t.Fatal(err) 64 } 65 oSrcWritten := buf.Bytes() 66 67 oGot, diags := readStateV4(oSrcWritten) 68 if diags.HasErrors() { 69 t.Fatal(diags.Err()) 70 } 71 72 problems := deep.Equal(oGot, oWant) 73 sort.Strings(problems) 74 for _, problem := range problems { 75 t.Error(problem) 76 } 77 }) 78 } 79 }