github.com/ojiry/terraform@v0.8.2-0.20161218223921-e50cec712c4a/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_destroyDeposed(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  						"aws_instance.foo": &terraform.InstanceDiff{
    20  							DestroyDeposed: true,
    21  						},
    22  					},
    23  				},
    24  			},
    25  		},
    26  	}
    27  	opts := &FormatPlanOpts{
    28  		Plan: plan,
    29  		Color: &colorstring.Colorize{
    30  			Colors:  colorstring.DefaultColors,
    31  			Disable: true,
    32  		},
    33  		ModuleDepth: 1,
    34  	}
    35  
    36  	actual := FormatPlan(opts)
    37  
    38  	expected := strings.TrimSpace(`
    39  - aws_instance.foo (deposed)
    40  	`)
    41  	if actual != expected {
    42  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
    43  	}
    44  }
    45  
    46  // Test that computed fields with an interpolation string get displayed
    47  func TestFormatPlan_displayInterpolations(t *testing.T) {
    48  	plan := &terraform.Plan{
    49  		Diff: &terraform.Diff{
    50  			Modules: []*terraform.ModuleDiff{
    51  				&terraform.ModuleDiff{
    52  					Path: []string{"root"},
    53  					Resources: map[string]*terraform.InstanceDiff{
    54  						"aws_instance.foo": &terraform.InstanceDiff{
    55  							Attributes: map[string]*terraform.ResourceAttrDiff{
    56  								"computed_field": &terraform.ResourceAttrDiff{
    57  									New:         "${aws_instance.other.id}",
    58  									NewComputed: true,
    59  								},
    60  							},
    61  						},
    62  					},
    63  				},
    64  			},
    65  		},
    66  	}
    67  	opts := &FormatPlanOpts{
    68  		Plan: plan,
    69  		Color: &colorstring.Colorize{
    70  			Colors:  colorstring.DefaultColors,
    71  			Disable: true,
    72  		},
    73  		ModuleDepth: 1,
    74  	}
    75  
    76  	out := FormatPlan(opts)
    77  	lines := strings.Split(out, "\n")
    78  	if len(lines) != 2 {
    79  		t.Fatal("expected 2 lines of output, got:\n", out)
    80  	}
    81  
    82  	actual := strings.TrimSpace(lines[1])
    83  	expected := `computed_field: "" => "${aws_instance.other.id}"`
    84  
    85  	if actual != expected {
    86  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
    87  	}
    88  }
    89  
    90  // Test that a root level data source gets a special plan output on create
    91  func TestFormatPlan_rootDataSource(t *testing.T) {
    92  	plan := &terraform.Plan{
    93  		Diff: &terraform.Diff{
    94  			Modules: []*terraform.ModuleDiff{
    95  				&terraform.ModuleDiff{
    96  					Path: []string{"root"},
    97  					Resources: map[string]*terraform.InstanceDiff{
    98  						"data.type.name": &terraform.InstanceDiff{
    99  							Attributes: map[string]*terraform.ResourceAttrDiff{
   100  								"A": &terraform.ResourceAttrDiff{
   101  									New:         "B",
   102  									RequiresNew: true,
   103  								},
   104  							},
   105  						},
   106  					},
   107  				},
   108  			},
   109  		},
   110  	}
   111  	opts := &FormatPlanOpts{
   112  		Plan: plan,
   113  		Color: &colorstring.Colorize{
   114  			Colors:  colorstring.DefaultColors,
   115  			Disable: true,
   116  		},
   117  		ModuleDepth: 1,
   118  	}
   119  
   120  	actual := FormatPlan(opts)
   121  
   122  	expected := strings.TrimSpace(`
   123   <= data.type.name
   124      A: "B"
   125  	`)
   126  	if actual != expected {
   127  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
   128  	}
   129  }
   130  
   131  // Test that data sources nested in modules get the same plan output
   132  func TestFormatPlan_nestedDataSource(t *testing.T) {
   133  	plan := &terraform.Plan{
   134  		Diff: &terraform.Diff{
   135  			Modules: []*terraform.ModuleDiff{
   136  				&terraform.ModuleDiff{
   137  					Path: []string{"root", "nested"},
   138  					Resources: map[string]*terraform.InstanceDiff{
   139  						"data.type.name": &terraform.InstanceDiff{
   140  							Attributes: map[string]*terraform.ResourceAttrDiff{
   141  								"A": &terraform.ResourceAttrDiff{
   142  									New:         "B",
   143  									RequiresNew: true,
   144  								},
   145  							},
   146  						},
   147  					},
   148  				},
   149  			},
   150  		},
   151  	}
   152  	opts := &FormatPlanOpts{
   153  		Plan: plan,
   154  		Color: &colorstring.Colorize{
   155  			Colors:  colorstring.DefaultColors,
   156  			Disable: true,
   157  		},
   158  		ModuleDepth: 2,
   159  	}
   160  
   161  	actual := FormatPlan(opts)
   162  
   163  	expected := strings.TrimSpace(`
   164   <= module.nested.data.type.name
   165      A: "B"
   166  	`)
   167  	if actual != expected {
   168  		t.Fatalf("expected:\n\n%s\n\ngot:\n\n%s", expected, actual)
   169  	}
   170  }