mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-27 17:31:30 -06:00
a2eb462f5d
The new format is radically different in than the old in physical structure, but still has the same logical parts: the plan itself, a snapshot of the input configuration, and a snapshot of the state as it existed when the plan was created. Rather than creating plan-specific serializations of state and config, the new format instead leans on the existing file formats implemented elsewhere, wrapping the result up in a zip archive with some internal file naming conventions. The plan portion of the file is serialized with protobuf, consistent with our general strategy of replacing all use of encoding/gob with protobuf moving forward.
53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package planfile
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/hashicorp/terraform/configs/configload"
|
|
)
|
|
|
|
func TestConfigSnapshotRoundtrip(t *testing.T) {
|
|
fixtureDir := filepath.Join("testdata", "test-config")
|
|
loader, err := configload.NewLoader(&configload.Config{
|
|
ModulesDir: filepath.Join(fixtureDir, ".terraform", "modules"),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
_, snapIn, diags := loader.LoadConfigWithSnapshot(fixtureDir)
|
|
if diags.HasErrors() {
|
|
t.Fatal(diags.Error())
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
err = writeConfigSnapshot(snapIn, zw)
|
|
if err != nil {
|
|
t.Fatalf("failed to write snapshot: %s", err)
|
|
}
|
|
zw.Close()
|
|
|
|
raw := buf.Bytes()
|
|
r := bytes.NewReader(raw)
|
|
zr, err := zip.NewReader(r, int64(len(raw)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
snapOut, err := readConfigSnapshot(zr)
|
|
if err != nil {
|
|
t.Fatalf("failed to read snapshot: %s", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(snapIn, snapOut) {
|
|
t.Errorf("result does not match input\nresult: %sinput: %s", spew.Sdump(snapOut), spew.Sdump(snapIn))
|
|
}
|
|
}
|