2017-01-19 05:47:56 +01:00
|
|
|
package local
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-24 16:54:40 +02:00
|
|
|
"reflect"
|
2017-01-30 04:51:54 +01:00
|
|
|
"strings"
|
2017-01-19 05:47:56 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
2018-03-21 02:43:02 +01:00
|
|
|
"github.com/hashicorp/terraform/configs/configload"
|
2017-01-19 05:47:56 +01:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-09-09 02:14:37 +02:00
|
|
|
"github.com/mitchellh/cli"
|
2017-01-19 05:47:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLocal_planBasic(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")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan")
|
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
op.PlanRefresh = 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("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !p.DiffCalled {
|
|
|
|
t.Fatal("diff should be called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 02:14:37 +02:00
|
|
|
func TestLocal_planInAutomation(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-09-09 02:14:37 +02:00
|
|
|
TestLocalProvider(t, b, "test")
|
|
|
|
|
|
|
|
const msg = `You didn't specify an "-out" parameter`
|
|
|
|
|
|
|
|
// When we're "in automation" we omit certain text from the
|
|
|
|
// plan output. However, testing for the absense of text is
|
|
|
|
// unreliable in the face of future copy changes, so we'll
|
|
|
|
// mitigate that by running both with and without the flag
|
|
|
|
// set so we can ensure that the expected messages _are_
|
|
|
|
// included the first time.
|
|
|
|
b.RunningInAutomation = false
|
|
|
|
b.CLI = cli.NewMockUi()
|
|
|
|
{
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan")
|
|
|
|
defer configCleanup()
|
2017-09-09 02:14:37 +02:00
|
|
|
op.PlanRefresh = true
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("plan operation failed")
|
2017-09-09 02:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output := b.CLI.(*cli.MockUi).OutputWriter.String()
|
|
|
|
if !strings.Contains(output, msg) {
|
|
|
|
t.Fatalf("missing next-steps message when not in automation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// On the second run, we expect the next-steps messaging to be absent
|
|
|
|
// since we're now "running in automation".
|
|
|
|
b.RunningInAutomation = true
|
|
|
|
b.CLI = cli.NewMockUi()
|
|
|
|
{
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan")
|
|
|
|
defer configCleanup()
|
2017-09-09 02:14:37 +02:00
|
|
|
op.PlanRefresh = true
|
|
|
|
|
|
|
|
run, err := b.Operation(context.Background(), op)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
<-run.Done()
|
2018-03-21 02:43:02 +01:00
|
|
|
if run.Result != backend.OperationSuccess {
|
|
|
|
t.Fatalf("plan operation failed")
|
2017-09-09 02:14:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output := b.CLI.(*cli.MockUi).OutputWriter.String()
|
|
|
|
if strings.Contains(output, msg) {
|
|
|
|
t.Fatalf("next-steps message present when in automation")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-01-30 04:51:54 +01:00
|
|
|
func TestLocal_planNoConfig(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-01-30 04:51:54 +01:00
|
|
|
TestLocalProvider(t, b, "test")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b.CLI = cli.NewMockUi()
|
|
|
|
|
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/empty")
|
|
|
|
defer configCleanup()
|
2017-01-30 04:51:54 +01:00
|
|
|
op.PlanRefresh = 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.Fatal("plan operation succeeded; want failure")
|
2017-01-30 04:51:54 +01:00
|
|
|
}
|
2018-03-21 02:43:02 +01:00
|
|
|
output := b.CLI.(*cli.MockUi).ErrorWriter.String()
|
|
|
|
if !strings.Contains(output, "configuration") {
|
2017-01-30 04:51:54 +01:00
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:47:56 +01:00
|
|
|
func TestLocal_planRefreshFalse(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")
|
|
|
|
terraform.TestStateFile(t, b.StatePath, testPlanState())
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/empty")
|
|
|
|
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.Fatalf("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.RefreshCalled {
|
|
|
|
t.Fatal("refresh should not be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !run.PlanEmpty {
|
|
|
|
t.Fatal("plan should be empty")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocal_planDestroy(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")
|
|
|
|
terraform.TestStateFile(t, b.StatePath, testPlanState())
|
|
|
|
|
|
|
|
outDir := testTempDir(t)
|
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan")
|
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
op.Destroy = true
|
|
|
|
op.PlanRefresh = true
|
|
|
|
op.PlanOutPath = planPath
|
|
|
|
|
|
|
|
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("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !p.RefreshCalled {
|
|
|
|
t.Fatal("refresh should be called")
|
|
|
|
}
|
|
|
|
|
|
|
|
if run.PlanEmpty {
|
|
|
|
t.Fatal("plan should not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, planPath)
|
|
|
|
for _, m := range plan.Diff.Modules {
|
|
|
|
for _, r := range m.Resources {
|
|
|
|
if !r.Destroy {
|
|
|
|
t.Fatalf("bad: %#v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocal_planOutPathNoChange(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
|
|
|
TestLocalProvider(t, b, "test")
|
|
|
|
terraform.TestStateFile(t, b.StatePath, testPlanState())
|
|
|
|
|
|
|
|
outDir := testTempDir(t)
|
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
planPath := filepath.Join(outDir, "plan.tfplan")
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan")
|
|
|
|
defer configCleanup()
|
2017-01-19 05:47:56 +01:00
|
|
|
op.PlanOutPath = planPath
|
|
|
|
|
|
|
|
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("plan operation failed")
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
plan := testReadPlan(t, planPath)
|
|
|
|
if !plan.Diff.Empty() {
|
|
|
|
t.Fatalf("expected empty plan to be written")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-24 16:54:40 +02:00
|
|
|
// TestLocal_planScaleOutNoDupeCount tests a Refresh/Plan sequence when a
|
|
|
|
// resource count is scaled out. The scaled out node needs to exist in the
|
|
|
|
// graph and run through a plan-style sequence during the refresh phase, but
|
|
|
|
// can conflate the count if its post-diff count hooks are not skipped. This
|
|
|
|
// checks to make sure the correct resource count is ultimately given to the
|
|
|
|
// UI.
|
|
|
|
func TestLocal_planScaleOutNoDupeCount(t *testing.T) {
|
2018-03-28 16:54:08 +02:00
|
|
|
b, cleanup := TestLocal(t)
|
|
|
|
defer cleanup()
|
2017-06-24 16:54:40 +02:00
|
|
|
TestLocalProvider(t, b, "test")
|
|
|
|
state := &terraform.State{
|
|
|
|
Version: 2,
|
|
|
|
Modules: []*terraform.ModuleState{
|
|
|
|
&terraform.ModuleState{
|
|
|
|
Path: []string{"root"},
|
|
|
|
Resources: map[string]*terraform.ResourceState{
|
|
|
|
"test_instance.foo.0": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"test_instance.foo.1": &terraform.ResourceState{
|
|
|
|
Type: "test_instance",
|
|
|
|
Primary: &terraform.InstanceState{
|
|
|
|
ID: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
terraform.TestStateFile(t, b.StatePath, state)
|
|
|
|
|
|
|
|
actual := new(CountHook)
|
|
|
|
b.ContextOpts.Hooks = append(b.ContextOpts.Hooks, actual)
|
|
|
|
|
|
|
|
outDir := testTempDir(t)
|
|
|
|
defer os.RemoveAll(outDir)
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
op, configCleanup := testOperationPlan(t, "./test-fixtures/plan-scaleout")
|
|
|
|
defer configCleanup()
|
2017-06-24 16:54:40 +02:00
|
|
|
op.PlanRefresh = 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("plan operation failed")
|
2017-06-24 16:54:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := new(CountHook)
|
|
|
|
expected.ToAdd = 1
|
|
|
|
expected.ToChange = 0
|
|
|
|
expected.ToRemoveAndAdd = 0
|
|
|
|
expected.ToRemove = 0
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, actual) {
|
|
|
|
t.Fatalf("Expected %#v, got %#v instead.",
|
|
|
|
expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
func testOperationPlan(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.OperationTypePlan,
|
|
|
|
ConfigDir: configDir,
|
|
|
|
ConfigLoader: configLoader,
|
|
|
|
}, configCleanup
|
2017-01-19 05:47:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// testPlanState is just a common state that we use for testing refresh.
|
|
|
|
func testPlanState() *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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testReadPlan(t *testing.T, path string) *terraform.Plan {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
p, err := terraform.ReadPlan(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|