github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/terraform/plan_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  
     7  	"testing"
     8  )
     9  
    10  func TestReadWritePlan(t *testing.T) {
    11  	plan := &Plan{
    12  		Module: testModule(t, "new-good"),
    13  		Diff: &Diff{
    14  			Modules: []*ModuleDiff{
    15  				&ModuleDiff{
    16  					Path: rootModulePath,
    17  					Resources: map[string]*InstanceDiff{
    18  						"nodeA": &InstanceDiff{
    19  							Attributes: map[string]*ResourceAttrDiff{
    20  								"foo": &ResourceAttrDiff{
    21  									Old: "foo",
    22  									New: "bar",
    23  								},
    24  								"bar": &ResourceAttrDiff{
    25  									Old:         "foo",
    26  									NewComputed: true,
    27  								},
    28  								"longfoo": &ResourceAttrDiff{
    29  									Old:         "foo",
    30  									New:         "bar",
    31  									RequiresNew: true,
    32  								},
    33  							},
    34  
    35  							Meta: map[string]interface{}{
    36  								"foo": []interface{}{1, 2, 3},
    37  							},
    38  						},
    39  					},
    40  				},
    41  			},
    42  		},
    43  		State: &State{
    44  			Modules: []*ModuleState{
    45  				&ModuleState{
    46  					Path: rootModulePath,
    47  					Resources: map[string]*ResourceState{
    48  						"foo": &ResourceState{
    49  							Primary: &InstanceState{
    50  								ID: "bar",
    51  							},
    52  						},
    53  					},
    54  				},
    55  			},
    56  		},
    57  		Vars: map[string]interface{}{
    58  			"foo": "bar",
    59  		},
    60  	}
    61  
    62  	buf := new(bytes.Buffer)
    63  	if err := WritePlan(plan, buf); err != nil {
    64  		t.Fatalf("err: %s", err)
    65  	}
    66  
    67  	actual, err := ReadPlan(buf)
    68  	if err != nil {
    69  		t.Fatalf("err: %s", err)
    70  	}
    71  
    72  	actualStr := strings.TrimSpace(actual.String())
    73  	expectedStr := strings.TrimSpace(plan.String())
    74  	if actualStr != expectedStr {
    75  		t.Fatalf("bad:\n\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
    76  	}
    77  }