github.com/sl1pm4t/terraform@v0.6.4-0.20170725213156-870617d22df3/command/e2etest/primary_test.go (about)

     1  package e2etest
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/davecgh/go-spew/spew"
    10  )
    11  
    12  // The tests in this file are for the "primary workflow", which includes
    13  // variants of the following sequence, with different details:
    14  // terraform init
    15  // terraform plan
    16  // terraform apply
    17  // terraform destroy
    18  
    19  func TestPrimarySeparatePlan(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	// This test reaches out to releases.hashicorp.com to download the
    23  	// template and null providers, so it can only run if network access is
    24  	// allowed.
    25  	skipIfCannotAccessNetwork(t)
    26  
    27  	tf := newTerraform("full-workflow-null")
    28  	defer tf.Close()
    29  
    30  	//// INIT
    31  	stdout, stderr, err := tf.Run("init")
    32  	if err != nil {
    33  		t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr)
    34  	}
    35  
    36  	// Make sure we actually downloaded the plugins, rather than picking up
    37  	// copies that might be already installed globally on the system.
    38  	if !strings.Contains(stdout, "- Downloading plugin for provider \"template\"") {
    39  		t.Errorf("template provider download message is missing from init output:\n%s", stdout)
    40  		t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
    41  	}
    42  	if !strings.Contains(stdout, "- Downloading plugin for provider \"null\"") {
    43  		t.Errorf("null provider download message is missing from init output:\n%s", stdout)
    44  		t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
    45  	}
    46  
    47  	//// PLAN
    48  	stdout, stderr, err = tf.Run("plan", "-out=tfplan")
    49  	if err != nil {
    50  		t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr)
    51  	}
    52  
    53  	if !strings.Contains(stdout, "1 to add, 0 to change, 0 to destroy") {
    54  		t.Errorf("incorrect plan tally; want 1 to add:\n%s", stdout)
    55  	}
    56  
    57  	plan, err := tf.Plan("tfplan")
    58  	if err != nil {
    59  		t.Fatalf("failed to read plan file: %s", err)
    60  	}
    61  
    62  	stateResources := plan.State.RootModule().Resources
    63  	diffResources := plan.Diff.RootModule().Resources
    64  
    65  	if len(stateResources) != 1 || stateResources["data.template_file.test"] == nil {
    66  		t.Errorf("incorrect state in plan; want just data.template_file.test to have been rendered, but have:\n%s", spew.Sdump(stateResources))
    67  	}
    68  	if len(diffResources) != 1 || diffResources["null_resource.test"] == nil {
    69  		t.Errorf("incorrect diff in plan; want just null_resource.test to have been rendered, but have:\n%s", spew.Sdump(diffResources))
    70  	}
    71  
    72  	//// APPLY
    73  	stdout, stderr, err = tf.Run("apply", "tfplan")
    74  	if err != nil {
    75  		t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
    76  	}
    77  
    78  	if !strings.Contains(stdout, "Resources: 1 added, 0 changed, 0 destroyed") {
    79  		t.Errorf("incorrect apply tally; want 1 added:\n%s", stdout)
    80  	}
    81  
    82  	state, err := tf.LocalState()
    83  	if err != nil {
    84  		t.Fatalf("failed to read state file: %s", err)
    85  	}
    86  
    87  	stateResources = state.RootModule().Resources
    88  	var gotResources []string
    89  	for n := range stateResources {
    90  		gotResources = append(gotResources, n)
    91  	}
    92  	sort.Strings(gotResources)
    93  
    94  	wantResources := []string{
    95  		"data.template_file.test",
    96  		"null_resource.test",
    97  	}
    98  
    99  	if !reflect.DeepEqual(gotResources, wantResources) {
   100  		t.Errorf("wrong resources in state\ngot: %#v\nwant: %#v", gotResources, wantResources)
   101  	}
   102  
   103  	//// DESTROY
   104  	stdout, stderr, err = tf.Run("destroy", "-force")
   105  	if err != nil {
   106  		t.Fatalf("unexpected destroy error: %s\nstderr:\n%s", err, stderr)
   107  	}
   108  
   109  	if !strings.Contains(stdout, "Resources: 2 destroyed") {
   110  		t.Errorf("incorrect destroy tally; want 2 destroyed:\n%s", stdout)
   111  	}
   112  
   113  	state, err = tf.LocalState()
   114  	if err != nil {
   115  		t.Fatalf("failed to read state file after destroy: %s", err)
   116  	}
   117  
   118  	stateResources = state.RootModule().Resources
   119  	if len(stateResources) != 0 {
   120  		t.Errorf("wrong resources in state after destroy; want none, but still have:%s", spew.Sdump(stateResources))
   121  	}
   122  
   123  }