github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/command/format_plan_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  	"github.com/mitchellh/colorstring"
     9  )
    10  
    11  // Test that a root level data source gets a special plan output on create
    12  func TestFormatPlan_rootDataSource(t *testing.T) {
    13  	plan := &terraform.Plan{
    14  		Diff: &terraform.Diff{
    15  			Modules: []*terraform.ModuleDiff{
    16  				&terraform.ModuleDiff{
    17  					Path: []string{"root"},
    18  					Resources: map[string]*terraform.InstanceDiff{
    19  						"data.type.name": &terraform.InstanceDiff{
    20  							Attributes: map[string]*terraform.ResourceAttrDiff{
    21  								"A": &terraform.ResourceAttrDiff{
    22  									New:         "B",
    23  									RequiresNew: true,
    24  								},
    25  							},
    26  						},
    27  					},
    28  				},
    29  			},
    30  		},
    31  	}
    32  	opts := &FormatPlanOpts{
    33  		Plan: plan,
    34  		Color: &colorstring.Colorize{
    35  			Colors:  colorstring.DefaultColors,
    36  			Disable: true,
    37  		},
    38  		ModuleDepth: 1,
    39  	}
    40  
    41  	actual := FormatPlan(opts)
    42  
    43  	expected := strings.TrimSpace(`
    44   <= data.type.name
    45      A: "B"
    46  	`)
    47  	if actual != expected {
    48  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
    49  	}
    50  }
    51  
    52  // Test that data sources nested in modules get the same plan output
    53  func TestFormatPlan_nestedDataSource(t *testing.T) {
    54  	plan := &terraform.Plan{
    55  		Diff: &terraform.Diff{
    56  			Modules: []*terraform.ModuleDiff{
    57  				&terraform.ModuleDiff{
    58  					Path: []string{"root", "nested"},
    59  					Resources: map[string]*terraform.InstanceDiff{
    60  						"data.type.name": &terraform.InstanceDiff{
    61  							Attributes: map[string]*terraform.ResourceAttrDiff{
    62  								"A": &terraform.ResourceAttrDiff{
    63  									New:         "B",
    64  									RequiresNew: true,
    65  								},
    66  							},
    67  						},
    68  					},
    69  				},
    70  			},
    71  		},
    72  	}
    73  	opts := &FormatPlanOpts{
    74  		Plan: plan,
    75  		Color: &colorstring.Colorize{
    76  			Colors:  colorstring.DefaultColors,
    77  			Disable: true,
    78  		},
    79  		ModuleDepth: 2,
    80  	}
    81  
    82  	actual := FormatPlan(opts)
    83  
    84  	expected := strings.TrimSpace(`
    85   <= module.nested.data.type.name
    86      A: "B"
    87  	`)
    88  	if actual != expected {
    89  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
    90  	}
    91  }