github.com/gavinw2006/hashicorp-terraform@v0.11.12-beta1/terraform/plan_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/config/module"
    10  )
    11  
    12  func TestPlanContextOpts(t *testing.T) {
    13  	plan := &Plan{
    14  		Diff: &Diff{
    15  			Modules: []*ModuleDiff{
    16  				{
    17  					Path: []string{"test"},
    18  				},
    19  			},
    20  		},
    21  		Module: module.NewTree("test", nil),
    22  		State: &State{
    23  			TFVersion: "sigil",
    24  		},
    25  		Vars:    map[string]interface{}{"foo": "bar"},
    26  		Targets: []string{"baz"},
    27  
    28  		TerraformVersion: VersionString(),
    29  		ProviderSHA256s: map[string][]byte{
    30  			"test": []byte("placeholder"),
    31  		},
    32  	}
    33  
    34  	got, err := plan.contextOpts(&ContextOpts{})
    35  	if err != nil {
    36  		t.Fatalf("error creating context: %s", err)
    37  	}
    38  
    39  	want := &ContextOpts{
    40  		Diff:            plan.Diff,
    41  		Module:          plan.Module,
    42  		State:           plan.State,
    43  		Variables:       plan.Vars,
    44  		Targets:         plan.Targets,
    45  		ProviderSHA256s: plan.ProviderSHA256s,
    46  	}
    47  
    48  	if !reflect.DeepEqual(got, want) {
    49  		t.Errorf("wrong result\ngot:  %#v\nwant %#v", got, want)
    50  	}
    51  }
    52  
    53  func TestReadWritePlan(t *testing.T) {
    54  	plan := &Plan{
    55  		Module: testModule(t, "new-good"),
    56  		Diff: &Diff{
    57  			Modules: []*ModuleDiff{
    58  				&ModuleDiff{
    59  					Path: rootModulePath,
    60  					Resources: map[string]*InstanceDiff{
    61  						"nodeA": &InstanceDiff{
    62  							Attributes: map[string]*ResourceAttrDiff{
    63  								"foo": &ResourceAttrDiff{
    64  									Old: "foo",
    65  									New: "bar",
    66  								},
    67  								"bar": &ResourceAttrDiff{
    68  									Old:         "foo",
    69  									NewComputed: true,
    70  								},
    71  								"longfoo": &ResourceAttrDiff{
    72  									Old:         "foo",
    73  									New:         "bar",
    74  									RequiresNew: true,
    75  								},
    76  							},
    77  
    78  							Meta: map[string]interface{}{
    79  								"foo": []interface{}{1, 2, 3},
    80  							},
    81  						},
    82  					},
    83  				},
    84  			},
    85  		},
    86  		State: &State{
    87  			Modules: []*ModuleState{
    88  				&ModuleState{
    89  					Path: rootModulePath,
    90  					Resources: map[string]*ResourceState{
    91  						"foo": &ResourceState{
    92  							Primary: &InstanceState{
    93  								ID: "bar",
    94  							},
    95  						},
    96  					},
    97  				},
    98  			},
    99  		},
   100  		Vars: map[string]interface{}{
   101  			"foo": "bar",
   102  		},
   103  	}
   104  
   105  	buf := new(bytes.Buffer)
   106  	if err := WritePlan(plan, buf); err != nil {
   107  		t.Fatalf("err: %s", err)
   108  	}
   109  
   110  	actual, err := ReadPlan(buf)
   111  	if err != nil {
   112  		t.Fatalf("err: %s", err)
   113  	}
   114  
   115  	actualStr := strings.TrimSpace(actual.String())
   116  	expectedStr := strings.TrimSpace(plan.String())
   117  	if actualStr != expectedStr {
   118  		t.Fatalf("bad:\n\n%s\n\nexpected:\n\n%s", actualStr, expectedStr)
   119  	}
   120  }
   121  
   122  func TestPlanContextOptsOverrideStateGood(t *testing.T) {
   123  	plan := &Plan{
   124  		Diff: &Diff{
   125  			Modules: []*ModuleDiff{
   126  				{
   127  					Path: []string{"test"},
   128  				},
   129  			},
   130  		},
   131  		Module: module.NewTree("test", nil),
   132  		State: &State{
   133  			TFVersion: "sigil",
   134  			Serial:    1,
   135  		},
   136  		Vars:    map[string]interface{}{"foo": "bar"},
   137  		Targets: []string{"baz"},
   138  
   139  		TerraformVersion: VersionString(),
   140  		ProviderSHA256s: map[string][]byte{
   141  			"test": []byte("placeholder"),
   142  		},
   143  	}
   144  
   145  	base := &ContextOpts{
   146  		State: &State{
   147  			TFVersion: "sigil",
   148  			Serial:    2,
   149  		},
   150  	}
   151  
   152  	got, err := plan.contextOpts(base)
   153  	if err != nil {
   154  		t.Fatalf("error creating context: %s", err)
   155  	}
   156  
   157  	want := &ContextOpts{
   158  		Diff:            plan.Diff,
   159  		Module:          plan.Module,
   160  		State:           base.State,
   161  		Variables:       plan.Vars,
   162  		Targets:         plan.Targets,
   163  		ProviderSHA256s: plan.ProviderSHA256s,
   164  	}
   165  
   166  	if !reflect.DeepEqual(got, want) {
   167  		t.Errorf("wrong result\ngot:  %#v\nwant %#v", got, want)
   168  	}
   169  }