github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/plans/planfile/config_snapshot_test.go (about)

     1  package planfile
     2  
     3  import (
     4  	"archive/zip"
     5  	"bytes"
     6  	"path/filepath"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/davecgh/go-spew/spew"
    11  
    12  	"github.com/hashicorp/terraform-plugin-sdk/internal/configs/configload"
    13  )
    14  
    15  func TestConfigSnapshotRoundtrip(t *testing.T) {
    16  	fixtureDir := filepath.Join("testdata", "test-config")
    17  	loader, err := configload.NewLoader(&configload.Config{
    18  		ModulesDir: filepath.Join(fixtureDir, ".terraform", "modules"),
    19  	})
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	_, snapIn, diags := loader.LoadConfigWithSnapshot(fixtureDir)
    25  	if diags.HasErrors() {
    26  		t.Fatal(diags.Error())
    27  	}
    28  
    29  	var buf bytes.Buffer
    30  	zw := zip.NewWriter(&buf)
    31  	err = writeConfigSnapshot(snapIn, zw)
    32  	if err != nil {
    33  		t.Fatalf("failed to write snapshot: %s", err)
    34  	}
    35  	zw.Close()
    36  
    37  	raw := buf.Bytes()
    38  	r := bytes.NewReader(raw)
    39  	zr, err := zip.NewReader(r, int64(len(raw)))
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	snapOut, err := readConfigSnapshot(zr)
    45  	if err != nil {
    46  		t.Fatalf("failed to read snapshot: %s", err)
    47  	}
    48  
    49  	if !reflect.DeepEqual(snapIn, snapOut) {
    50  		t.Errorf("result does not match input\nresult: %sinput: %s", spew.Sdump(snapOut), spew.Sdump(snapIn))
    51  	}
    52  }