2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-05-18 00:26:21 +02:00
|
|
|
"errors"
|
2017-01-19 05:47:56 +01:00
|
|
|
"fmt"
|
2017-02-16 01:00:59 +01:00
|
|
|
"os"
|
2017-05-18 00:26:21 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configload"
|
2017-05-18 00:26:21 +02:00
|
|
|
"github.com/hashicorp/terraform/state"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-05-18 00:26:21 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocal_applyBasic(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
p := TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
p.ApplyReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationApply(t, "./test-fixtures/apply")
|
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatal("operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.RefreshCalled {
|
|
|
|
t.Fatal("refresh should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.DiffCalled {
|
|
|
|
t.Fatal("diff should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ApplyCalled {
|
|
|
|
t.Fatal("apply should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2017-11-08 03:23:08 +01:00
|
|
|
provider = provider.test
|
2017-01-19 05:47:56 +01:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2017-02-16 01:00:59 +01:00
|
|
|
func TestLocal_applyEmptyDir(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-02-16 01:00:59 +01:00
|
|
|
p := TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
p.ApplyReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationApply(t, "./test-fixtures/empty")
|
|
|
|
defer configCleanup()
|
2017-02-16 01:00:59 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
|
|
|
t.Fatal("operation succeeded; want error")
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.ApplyCalled {
|
|
|
|
t.Fatal("apply should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(b.StateOutPath); err == nil {
|
|
|
|
t.Fatal("should not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocal_applyEmptyDirDestroy(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-02-16 01:00:59 +01:00
|
|
|
p := TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
p.ApplyReturn = nil
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationApply(t, "./test-fixtures/empty")
|
|
|
|
defer configCleanup()
|
2017-02-16 01:00:59 +01:00
|
|
|
op.Destroy = true
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("apply operation failed")
|
2017-02-16 01:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.ApplyCalled {
|
|
|
|
t.Fatal("apply should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `<no state>`)
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func TestLocal_applyError(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
p := TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
var lock sync.Mutex
|
|
|
|
errored := false
|
|
|
|
p.ApplyFn = func(
|
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
|
|
|
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
if !errored && info.Id == "test_instance.bar" {
|
|
|
|
errored = true
|
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &terraform.InstanceState{ID: "foo"}, nil
|
|
|
|
}
|
|
|
|
p.DiffFn = func(
|
|
|
|
*terraform.InstanceInfo,
|
|
|
|
*terraform.InstanceState,
|
|
|
|
*terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
|
|
|
return &terraform.InstanceDiff{
|
|
|
|
Attributes: map[string]*terraform.ResourceAttrDiff{
|
|
|
|
"ami": &terraform.ResourceAttrDiff{
|
|
|
|
New: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationApply(t, "./test-fixtures/apply-error")
|
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
|
|
|
t.Fatal("operation succeeded; want failure")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
checkState(t, b.StateOutPath, `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = foo
|
2017-11-08 03:23:08 +01:00
|
|
|
provider = provider.test
|
2017-01-19 05:47:56 +01:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
func TestLocal_applyBackendFail(t *testing.T) {
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationApply(t, "./test-fixtures/apply")
|
|
|
|
defer configCleanup()
|
2017-05-18 00:26:21 +02:00
|
|
|
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-05-18 00:26:21 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get current working directory")
|
|
|
|
}
|
|
|
|
err = os.Chdir(filepath.Dir(b.StatePath))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to set temporary working directory")
|
|
|
|
}
|
|
|
|
defer os.Chdir(wd)
|
|
|
|
|
|
|
|
b.Backend = &backendWithFailingState{}
|
|
|
|
b.CLI = new(cli.MockUi)
|
|
|
|
p := TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
p.ApplyReturn = &terraform.InstanceState{ID: "yes"}
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result == backend.OperationSuccess {
|
2017-05-18 00:26:21 +02:00
|
|
|
t.Fatalf("apply succeeded; want error")
|
|
|
|
}
|
|
|
|
|
|
|
|
msgStr := b.CLI.(*cli.MockUi).ErrorWriter.String()
|
|
|
|
if !strings.Contains(msgStr, "Failed to save state: fake failure") {
|
|
|
|
t.Fatalf("missing original error message in output:\n%s", msgStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fallback behavior should've created a file errored.tfstate in the
|
|
|
|
// current working directory.
|
|
|
|
checkState(t, "errored.tfstate", `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2017-11-08 03:23:08 +01:00
|
|
|
provider = provider.test
|
2017-05-18 00:26:21 +02:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
type backendWithFailingState struct {
|
|
|
|
Local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendWithFailingState) State(name string) (state.State, error) {
|
|
|
|
return &failingState{
|
|
|
|
&state.LocalState{
|
|
|
|
Path: "failing-state.tfstate",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type failingState struct {
|
|
|
|
*state.LocalState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s failingState) WriteState(state *terraform.State) error {
|
|
|
|
return errors.New("fake failure")
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func testOperationApply(t *testing.T, configDir string) (*backend.Operation, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
_, configLoader, configCleanup := configload.MustLoadConfigForTests(t, configDir)
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
return &backend.Operation{
|
2018-03-21 02:43:02 +01:00
|
|
|
Type: backend.OperationTypeApply,
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
}, configCleanup
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// testApplyState is just a common state that we use for testing refresh.
|
|
|
|
func testApplyState() *terraform.State {
|
|
|
|
return &terraform.State{
|
|
|
|
Version: 2,
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"test_instance.foo": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|