github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/backend/local/backend_apply_test.go (about)

     1  package local
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/backend"
    11  	"github.com/hashicorp/terraform/config/module"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestLocal_applyBasic(t *testing.T) {
    16  	b := TestLocal(t)
    17  	p := TestLocalProvider(t, b, "test")
    18  
    19  	p.ApplyReturn = &terraform.InstanceState{ID: "yes"}
    20  
    21  	mod, modCleanup := module.TestTree(t, "./test-fixtures/apply")
    22  	defer modCleanup()
    23  
    24  	op := testOperationApply()
    25  	op.Module = mod
    26  
    27  	run, err := b.Operation(context.Background(), op)
    28  	if err != nil {
    29  		t.Fatalf("bad: %s", err)
    30  	}
    31  	<-run.Done()
    32  	if run.Err != nil {
    33  		t.Fatalf("err: %s", err)
    34  	}
    35  
    36  	if p.RefreshCalled {
    37  		t.Fatal("refresh should not be called")
    38  	}
    39  
    40  	if !p.DiffCalled {
    41  		t.Fatal("diff should be called")
    42  	}
    43  
    44  	if !p.ApplyCalled {
    45  		t.Fatal("apply should be called")
    46  	}
    47  
    48  	checkState(t, b.StateOutPath, `
    49  test_instance.foo:
    50    ID = yes
    51  	`)
    52  }
    53  
    54  func TestLocal_applyEmptyDir(t *testing.T) {
    55  	b := TestLocal(t)
    56  	p := TestLocalProvider(t, b, "test")
    57  
    58  	p.ApplyReturn = &terraform.InstanceState{ID: "yes"}
    59  
    60  	op := testOperationApply()
    61  	op.Module = nil
    62  
    63  	run, err := b.Operation(context.Background(), op)
    64  	if err != nil {
    65  		t.Fatalf("bad: %s", err)
    66  	}
    67  	<-run.Done()
    68  	if run.Err == nil {
    69  		t.Fatal("should error")
    70  	}
    71  
    72  	if p.ApplyCalled {
    73  		t.Fatal("apply should not be called")
    74  	}
    75  
    76  	if _, err := os.Stat(b.StateOutPath); err == nil {
    77  		t.Fatal("should not exist")
    78  	}
    79  }
    80  
    81  func TestLocal_applyEmptyDirDestroy(t *testing.T) {
    82  	b := TestLocal(t)
    83  	p := TestLocalProvider(t, b, "test")
    84  
    85  	p.ApplyReturn = nil
    86  
    87  	op := testOperationApply()
    88  	op.Module = nil
    89  	op.Destroy = true
    90  
    91  	run, err := b.Operation(context.Background(), op)
    92  	if err != nil {
    93  		t.Fatalf("bad: %s", err)
    94  	}
    95  	<-run.Done()
    96  	if run.Err != nil {
    97  		t.Fatalf("err: %s", err)
    98  	}
    99  
   100  	if p.ApplyCalled {
   101  		t.Fatal("apply should not be called")
   102  	}
   103  
   104  	checkState(t, b.StateOutPath, `<no state>`)
   105  }
   106  
   107  func TestLocal_applyError(t *testing.T) {
   108  	b := TestLocal(t)
   109  	p := TestLocalProvider(t, b, "test")
   110  
   111  	var lock sync.Mutex
   112  	errored := false
   113  	p.ApplyFn = func(
   114  		info *terraform.InstanceInfo,
   115  		s *terraform.InstanceState,
   116  		d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
   117  		lock.Lock()
   118  		defer lock.Unlock()
   119  
   120  		if !errored && info.Id == "test_instance.bar" {
   121  			errored = true
   122  			return nil, fmt.Errorf("error")
   123  		}
   124  
   125  		return &terraform.InstanceState{ID: "foo"}, nil
   126  	}
   127  	p.DiffFn = func(
   128  		*terraform.InstanceInfo,
   129  		*terraform.InstanceState,
   130  		*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
   131  		return &terraform.InstanceDiff{
   132  			Attributes: map[string]*terraform.ResourceAttrDiff{
   133  				"ami": &terraform.ResourceAttrDiff{
   134  					New: "bar",
   135  				},
   136  			},
   137  		}, nil
   138  	}
   139  
   140  	mod, modCleanup := module.TestTree(t, "./test-fixtures/apply-error")
   141  	defer modCleanup()
   142  
   143  	op := testOperationApply()
   144  	op.Module = mod
   145  
   146  	run, err := b.Operation(context.Background(), op)
   147  	if err != nil {
   148  		t.Fatalf("bad: %s", err)
   149  	}
   150  	<-run.Done()
   151  	if run.Err == nil {
   152  		t.Fatal("should error")
   153  	}
   154  
   155  	checkState(t, b.StateOutPath, `
   156  test_instance.foo:
   157    ID = foo
   158  	`)
   159  }
   160  
   161  func testOperationApply() *backend.Operation {
   162  	return &backend.Operation{
   163  		Type: backend.OperationTypeApply,
   164  	}
   165  }
   166  
   167  // testApplyState is just a common state that we use for testing refresh.
   168  func testApplyState() *terraform.State {
   169  	return &terraform.State{
   170  		Version: 2,
   171  		Modules: []*terraform.ModuleState{
   172  			&terraform.ModuleState{
   173  				Path: []string{"root"},
   174  				Resources: map[string]*terraform.ResourceState{
   175  					"test_instance.foo": &terraform.ResourceState{
   176  						Type: "test_instance",
   177  						Primary: &terraform.InstanceState{
   178  							ID: "bar",
   179  						},
   180  					},
   181  				},
   182  			},
   183  		},
   184  	}
   185  }