github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/backend/local/backend_apply_test.go (about) 1 package local 2 3 import ( 4 "context" 5 "fmt" 6 "sync" 7 "testing" 8 9 "github.com/hashicorp/terraform/backend" 10 "github.com/hashicorp/terraform/config/module" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestLocal_applyBasic(t *testing.T) { 15 b := TestLocal(t) 16 p := TestLocalProvider(t, b, "test") 17 18 p.ApplyReturn = &terraform.InstanceState{ID: "yes"} 19 20 mod, modCleanup := module.TestTree(t, "./test-fixtures/apply") 21 defer modCleanup() 22 23 op := testOperationApply() 24 op.Module = mod 25 26 run, err := b.Operation(context.Background(), op) 27 if err != nil { 28 t.Fatalf("bad: %s", err) 29 } 30 <-run.Done() 31 if run.Err != nil { 32 t.Fatalf("err: %s", err) 33 } 34 35 if p.RefreshCalled { 36 t.Fatal("refresh should not be called") 37 } 38 39 if !p.DiffCalled { 40 t.Fatal("diff should be called") 41 } 42 43 if !p.ApplyCalled { 44 t.Fatal("apply should be called") 45 } 46 47 checkState(t, b.StateOutPath, ` 48 test_instance.foo: 49 ID = yes 50 `) 51 } 52 53 func TestLocal_applyError(t *testing.T) { 54 b := TestLocal(t) 55 p := TestLocalProvider(t, b, "test") 56 57 var lock sync.Mutex 58 errored := false 59 p.ApplyFn = func( 60 info *terraform.InstanceInfo, 61 s *terraform.InstanceState, 62 d *terraform.InstanceDiff) (*terraform.InstanceState, error) { 63 lock.Lock() 64 defer lock.Unlock() 65 66 if !errored && info.Id == "test_instance.bar" { 67 errored = true 68 return nil, fmt.Errorf("error") 69 } 70 71 return &terraform.InstanceState{ID: "foo"}, nil 72 } 73 p.DiffFn = func( 74 *terraform.InstanceInfo, 75 *terraform.InstanceState, 76 *terraform.ResourceConfig) (*terraform.InstanceDiff, error) { 77 return &terraform.InstanceDiff{ 78 Attributes: map[string]*terraform.ResourceAttrDiff{ 79 "ami": &terraform.ResourceAttrDiff{ 80 New: "bar", 81 }, 82 }, 83 }, nil 84 } 85 86 mod, modCleanup := module.TestTree(t, "./test-fixtures/apply-error") 87 defer modCleanup() 88 89 op := testOperationApply() 90 op.Module = mod 91 92 run, err := b.Operation(context.Background(), op) 93 if err != nil { 94 t.Fatalf("bad: %s", err) 95 } 96 <-run.Done() 97 if run.Err == nil { 98 t.Fatal("should error") 99 } 100 101 checkState(t, b.StateOutPath, ` 102 test_instance.foo: 103 ID = foo 104 `) 105 } 106 107 func testOperationApply() *backend.Operation { 108 return &backend.Operation{ 109 Type: backend.OperationTypeApply, 110 } 111 } 112 113 // testApplyState is just a common state that we use for testing refresh. 114 func testApplyState() *terraform.State { 115 return &terraform.State{ 116 Version: 2, 117 Modules: []*terraform.ModuleState{ 118 &terraform.ModuleState{ 119 Path: []string{"root"}, 120 Resources: map[string]*terraform.ResourceState{ 121 "test_instance.foo": &terraform.ResourceState{ 122 Type: "test_instance", 123 Primary: &terraform.InstanceState{ 124 ID: "bar", 125 }, 126 }, 127 }, 128 }, 129 }, 130 } 131 }