Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backend/local: do not use backend operation variables #19175

Merged
merged 2 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ type Operation struct {
ModuleDepth int
Parallelism int
Targets []string
Variables map[string]interface{}

// Variables should only contain any variables passed as command
// arguments and not any variables read from the terraform.tfvars
// or *.auto.tfvars files.
Variables map[string]interface{}

// Input/output/control options.
UIIn terraform.UIInput
Expand Down
34 changes: 34 additions & 0 deletions backend/local/backend_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ func TestLocal_applyEmptyDirDestroy(t *testing.T) {
checkState(t, b.StateOutPath, `<no state>`)
}

func TestLocal_applyWithVariables(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
p := TestLocalProvider(t, b, "test")

p.ApplyReturn = &terraform.InstanceState{ID: "yes"}

mod, modCleanup := module.TestTree(t, "./test-fixtures/apply-vars")
defer modCleanup()

op := testOperationApply()
op.Module = mod
op.Variables = map[string]interface{}{"cli": "var"}

// Set some variables that would have been read from
// any terraform.tfvars or *.auto.tfvars files.
b.ContextOpts.Variables = map[string]interface{}{"foo": "bar"}

run, err := b.Operation(context.Background(), op)
if err != nil {
t.Fatalf("bad: %s", err)
}
<-run.Done()
if run.Err != nil {
t.Fatalf("err: %s", run.Err)
}

checkState(t, b.StateOutPath, `
test_instance.foo:
ID = yes
provider = provider.test
`)
}

func TestLocal_applyError(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
Expand Down
3 changes: 0 additions & 3 deletions backend/local/backend_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func (b *Local) context(op *backend.Operation) (*terraform.Context, state.State,
opts.Module = op.Module
opts.Targets = op.Targets
opts.UIInput = op.UIIn
if op.Variables != nil {
opts.Variables = op.Variables
}

// Load our state
// By the time we get here, the backend creation code in "command" took
Expand Down
31 changes: 31 additions & 0 deletions backend/local/backend_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ func TestLocal_planRefreshFalse(t *testing.T) {
}
}

func TestLocal_planWithVariables(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
TestLocalProvider(t, b, "test")

mod, modCleanup := module.TestTree(t, "./test-fixtures/plan-vars")
defer modCleanup()

op := testOperationPlan()
op.Module = mod
op.PlanRefresh = true
op.Variables = map[string]interface{}{"cli": "var"}

// Set some variables that would have been read from
// any terraform.tfvars or *.auto.tfvars files.
b.ContextOpts.Variables = map[string]interface{}{"foo": "bar"}

run, err := b.Operation(context.Background(), op)
if err != nil {
t.Fatalf("bad: %s", err)
}
<-run.Done()
if run.Err != nil {
t.Fatalf("err: %s", run.Err)
}

if run.PlanEmpty {
t.Fatal("plan should not be empty")
}
}

func TestLocal_planDestroy(t *testing.T) {
b, cleanup := TestLocal(t)
defer cleanup()
Expand Down
5 changes: 5 additions & 0 deletions backend/local/test-fixtures/apply-vars/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "foo" {}

resource "test_instance" "foo" {
foo = "${var.foo}"
}
11 changes: 11 additions & 0 deletions backend/local/test-fixtures/plan-vars/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
variable "foo" {}

resource "test_instance" "foo" {
foo = "${var.foo}"

# This is here because at some point it caused a test failure
network_interface {
device_index = 0
description = "Main network interface"
}
}