terraform/command/apply_test.go

2326 lines
57 KiB
Go
Raw Normal View History

2014-06-19 06:36:44 +02:00
package command
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
2014-06-19 06:36:44 +02:00
"os"
"path"
"path/filepath"
2021-02-18 23:23:34 +01:00
"reflect"
2014-09-18 19:40:23 +02:00
"strings"
"sync"
2014-06-19 06:36:44 +02:00
"testing"
2014-07-03 02:08:58 +02:00
"time"
2014-06-19 06:36:44 +02:00
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/mitchellh/cli"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/command/views"
"github.com/hashicorp/terraform/configs/configschema"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/hashicorp/terraform/plans"
"github.com/hashicorp/terraform/states"
"github.com/hashicorp/terraform/states/statemgr"
2014-06-19 06:36:44 +02:00
"github.com/hashicorp/terraform/terraform"
tfversion "github.com/hashicorp/terraform/version"
2014-06-19 06:36:44 +02:00
)
func TestApply(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
2014-06-19 06:36:44 +02:00
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-06-19 06:36:44 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 06:36:44 +02:00
}
args := []string{
"-state", statePath,
"-auto-approve",
2014-06-19 06:36:44 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-19 06:36:44 +02:00
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
2014-06-19 06:36:44 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_path(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-auto-approve",
testFixturePath("apply"),
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
2021-02-18 23:23:34 +01:00
if !strings.Contains(output.Stderr(), "-chdir") {
t.Fatal("expected command output to refer to -chdir flag, but got:", output.Stderr())
}
}
func TestApply_approveNo(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
2021-02-18 23:23:34 +01:00
defer testInputMap(t, map[string]string{
"approve": "no",
})()
2021-02-18 23:23:34 +01:00
// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
ui := new(cli.MockUi)
p := applyFixtureProvider()
2021-02-17 19:01:30 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}
args := []string{
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
2021-02-18 23:23:34 +01:00
if got, want := output.Stdout(), "Apply cancelled"; !strings.Contains(got, want) {
t.Fatalf("expected output to include %q, but was:\n%s", want, got)
}
if _, err := os.Stat(statePath); err == nil || !os.IsNotExist(err) {
t.Fatalf("state file should not exist")
}
}
func TestApply_approveYes(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
defer testInputMap(t, map[string]string{
"approve": "yes",
})()
2021-02-18 23:23:34 +01:00
// Do not use the NewMockUi initializer here, as we want to delay
// the call to init until after setting up the input mocks
ui := new(cli.MockUi)
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: ui,
View: view,
},
}
args := []string{
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
}
2017-02-03 20:55:13 +01:00
// test apply with locked state
func TestApply_lockedState(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2017-02-03 20:55:13 +01:00
statePath := testTempFile(t)
unlock, err := testLockState(testDataDir, statePath)
if err != nil {
2017-02-03 20:55:13 +01:00
t.Fatal(err)
}
defer unlock()
2017-02-03 20:55:13 +01:00
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2017-02-03 20:55:13 +01:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2017-02-03 20:55:13 +01:00
},
}
args := []string{
"-state", statePath,
"-auto-approve",
2017-02-03 20:55:13 +01:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code == 0 {
t.Fatal("expected error")
}
2021-02-18 23:23:34 +01:00
if !strings.Contains(output.Stderr(), "lock") {
t.Fatal("command output does not look like a lock error:", output.Stderr())
2017-02-03 20:55:13 +01:00
}
}
2017-04-03 17:33:38 +02:00
// test apply with locked state, waiting for unlock
func TestApply_lockedStateWait(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2017-04-03 17:33:38 +02:00
statePath := testTempFile(t)
unlock, err := testLockState(testDataDir, statePath)
2017-04-03 17:33:38 +02:00
if err != nil {
t.Fatal(err)
}
// unlock during apply
go func() {
time.Sleep(500 * time.Millisecond)
unlock()
}()
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2017-04-03 17:33:38 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2017-04-03 17:33:38 +02:00
},
}
// wait 4s just in case the lock process doesn't release in under a second,
// and we want our context to be alive for a second retry at the 3s mark.
args := []string{
"-state", statePath,
"-lock-timeout", "4s",
"-auto-approve",
2017-04-03 17:33:38 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("lock should have succeeded in less than 3s: %s", output.Stderr())
2017-04-03 17:33:38 +02:00
}
}
// Verify that the parallelism flag allows no more than the desired number of
// concurrent calls to ApplyResourceChange.
func TestApply_parallelism(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("parallelism"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
par := 4
// started is a semaphore that we use to ensure that we never have more
// than "par" apply operations happening concurrently
started := make(chan struct{}, par)
// beginCtx is used as a starting gate to hold back ApplyResourceChange
// calls until we reach the desired concurrency. The cancel func "begin" is
// called once we reach the desired concurrency, allowing all apply calls
// to proceed in unison.
beginCtx, begin := context.WithCancel(context.Background())
// Since our mock provider has its own mutex preventing concurrent calls
// to ApplyResourceChange, we need to use a number of separate providers
// here. They will all have the same mock implementation function assigned
// but crucially they will each have their own mutex.
providerFactories := map[addrs.Provider]providers.Factory{}
for i := 0; i < 10; i++ {
name := fmt.Sprintf("test%d", i)
provider := &terraform.MockProvider{}
provider.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
name + "_instance": {Block: &configschema.Block{}},
},
}
2018-10-14 16:59:15 +02:00
provider.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
2018-10-14 16:59:15 +02:00
provider.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
// If we ever have more than our intended parallelism number of
// apply operations running concurrently, the semaphore will fail.
select {
case started <- struct{}{}:
defer func() {
<-started
}()
default:
t.Fatal("too many concurrent apply operations")
}
// If we never reach our intended parallelism, the context will
// never be canceled and the test will time out.
if len(started) >= par {
begin()
}
<-beginCtx.Done()
// do some "work"
// Not required for correctness, but makes it easier to spot a
// failure when there is more overlap.
time.Sleep(10 * time.Millisecond)
return providers.ApplyResourceChangeResponse{
NewState: cty.EmptyObjectVal,
}
}
providerFactories[addrs.NewDefaultProvider(name)] = providers.FactoryFixed(provider)
}
testingOverrides := &testingOverrides{
Providers: providerFactories,
}
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: testingOverrides,
View: view,
},
}
args := []string{
"-state", statePath,
"-auto-approve",
fmt.Sprintf("-parallelism=%d", par),
}
res := c.Run(args)
2021-02-18 23:23:34 +01:00
output := done(t)
if res != 0 {
2021-02-18 23:23:34 +01:00
t.Fatal(output.Stdout())
}
}
func TestApply_configInvalid(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-config-invalid"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", testTempFile(t),
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: \n%s", output.Stdout())
}
}
func TestApply_defaultState(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := filepath.Join(td, DefaultStateFilename)
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
// create an existing state file
localState := statemgr.NewFilesystem(statePath)
if err := localState.WriteState(states.NewState()); err != nil {
t.Fatal(err)
}
args := []string{
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_error(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-error"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
var lock sync.Mutex
errored := false
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
lock.Lock()
defer lock.Unlock()
if !errored {
errored = true
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("error"))
}
s := req.PlannedState.AsValueMap()
s["id"] = cty.StringVal("foo")
resp.NewState = cty.ObjectVal(s)
return
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
s := req.ProposedNewState.AsValueMap()
s["id"] = cty.UnknownVal(cty.String)
resp.PlannedState = cty.ObjectVal(s)
return
}
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
"error": {Type: cty.Bool, Optional: true},
},
},
},
},
}
args := []string{
"-state", statePath,
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("wrong exit code %d; want 1\n%s", code, output.Stdout())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
2014-09-17 20:15:07 +02:00
if len(state.RootModule().Resources) == 0 {
t.Fatal("no resources in state")
}
}
func TestApply_input(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-input"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
// The configuration for this test includes a declaration of variable
// "foo" with no default, and we don't set it on the command line below,
// so the apply command will produce an interactive prompt for the
// value of var.foo. We'll answer "foo" here, and we expect the output
// value "result" to echo that back to us below.
defaultInputReader = bytes.NewBufferString("foo\n")
defaultInputWriter = new(bytes.Buffer)
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", statePath,
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
expected := strings.TrimSpace(`
<no state>
Outputs:
result = foo
`)
testStateOutput(t, statePath, expected)
}
// When only a partial set of the variables are set, Terraform
// should still ask for the unset ones by default (with -input=true)
func TestApply_inputPartial(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-input-partial"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
// Set some default reader/writers for the inputs
defaultInputReader = bytes.NewBufferString("one\ntwo\n")
defaultInputWriter = new(bytes.Buffer)
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", statePath,
"-auto-approve",
"-var", "foo=foovalue",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
expected := strings.TrimSpace(`
<no state>
Outputs:
bar = one
foo = foovalue
`)
testStateOutput(t, statePath, expected)
}
func TestApply_noArgs(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", statePath,
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_plan(t *testing.T) {
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
// Set some default reader/writers for the inputs
defaultInputReader = new(bytes.Buffer)
defaultInputWriter = new(bytes.Buffer)
planPath := applyFixturePlanFile(t)
statePath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-06-19 06:36:44 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 06:36:44 +02:00
}
args := []string{
2017-01-19 05:50:45 +01:00
"-state-out", statePath,
planPath,
2014-06-19 06:36:44 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_plan_backup(t *testing.T) {
planPath := applyFixturePlanFile(t)
statePath := testTempFile(t)
backupPath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
// create a state file that needs to be backed up
err := statemgr.NewFilesystem(statePath).WriteState(states.NewState())
if err != nil {
t.Fatal(err)
}
2018-10-17 02:51:47 +02:00
args := []string{
2018-10-17 02:51:47 +02:00
"-state", statePath,
"-backup", backupPath,
planPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
// Should have a backup file
testStateRead(t, backupPath)
}
func TestApply_plan_noBackup(t *testing.T) {
planPath := applyFixturePlanFile(t)
statePath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
2017-01-19 05:50:45 +01:00
"-state-out", statePath,
"-backup", "-",
planPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
// Ensure there is no backup
_, err := os.Stat(statePath + DefaultBackupExtension)
if err == nil || !os.IsNotExist(err) {
t.Fatalf("backup should not exist")
}
// Ensure there is no literal "-"
_, err = os.Stat("-")
if err == nil || !os.IsNotExist(err) {
t.Fatalf("backup should not exist")
}
}
func TestApply_plan_remoteState(t *testing.T) {
// Disable test mode so input would be asked
test = false
defer func() { test = true }()
tmp, cwd := testCwd(t)
defer testFixCwd(t, tmp, cwd)
2015-02-22 20:13:21 +01:00
remoteStatePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
if err := os.MkdirAll(filepath.Dir(remoteStatePath), 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Set some default reader/writers for the inputs
defaultInputReader = new(bytes.Buffer)
defaultInputWriter = new(bytes.Buffer)
// Create a remote state
state := testState()
_, srv := testRemoteState(t, state, 200)
defer srv.Close()
_, snap := testModuleWithSnapshot(t, "apply")
backendConfig := cty.ObjectVal(map[string]cty.Value{
"address": cty.StringVal(srv.URL),
"update_method": cty.NullVal(cty.String),
"lock_address": cty.NullVal(cty.String),
"unlock_address": cty.NullVal(cty.String),
"lock_method": cty.NullVal(cty.String),
"unlock_method": cty.NullVal(cty.String),
"username": cty.NullVal(cty.String),
"password": cty.NullVal(cty.String),
"skip_cert_verification": cty.NullVal(cty.Bool),
"retry_max": cty.NullVal(cty.String),
"retry_wait_min": cty.NullVal(cty.String),
"retry_wait_max": cty.NullVal(cty.String),
})
backendConfigRaw, err := plans.NewDynamicValue(backendConfig, backendConfig.Type())
if err != nil {
t.Fatal(err)
}
planPath := testPlanFile(t, snap, state, &plans.Plan{
Backend: plans.Backend{
Type: "http",
Config: backendConfigRaw,
},
Changes: plans.NewChanges(),
})
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
planPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
// State file should be not be installed
2015-02-22 20:13:21 +01:00
if _, err := os.Stat(filepath.Join(tmp, DefaultStateFilename)); err == nil {
2017-01-19 05:50:45 +01:00
data, _ := ioutil.ReadFile(DefaultStateFilename)
t.Fatalf("State path should not exist: %s", string(data))
}
2017-01-19 05:50:45 +01:00
// Check that there is no remote state config
if src, err := ioutil.ReadFile(remoteStatePath); err == nil {
t.Fatalf("has %s file; should not\n%s", remoteStatePath, src)
}
}
func TestApply_planWithVarFile(t *testing.T) {
varFileDir := testTempDir(t)
varFilePath := filepath.Join(varFileDir, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
planPath := applyFixturePlanFile(t)
statePath := testTempFile(t)
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(varFileDir); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
2017-01-19 05:50:45 +01:00
"-state-out", statePath,
planPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-19 06:36:44 +02:00
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_planVars(t *testing.T) {
planPath := applyFixturePlanFile(t)
statePath := testTempFile(t)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", statePath,
"-var", "foo=bar",
planPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code == 0 {
t.Fatal("should've failed: ", output.Stdout())
}
}
// we should be able to apply a plan file with no other file dependencies
func TestApply_planNoModuleFiles(t *testing.T) {
2017-04-26 16:10:04 +02:00
// temporary data directory which we can remove between commands
td := testTempDir(t)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := applyFixtureProvider()
planPath := applyFixturePlanFile(t)
2021-02-18 23:23:34 +01:00
view, done := testView(t)
apply := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
Ui: new(cli.MockUi),
View: view,
},
}
args := []string{
planPath,
}
apply.Run(args)
2021-02-18 23:23:34 +01:00
done(t)
}
2014-07-27 02:50:13 +02:00
func TestApply_refresh(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"ami":"bar"}`),
Status: states.ObjectReady,
2014-07-27 02:50:13 +02:00
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
2014-07-27 02:50:13 +02:00
statePath := testStateFile(t, originalState)
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-27 02:50:13 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-27 02:50:13 +02:00
},
}
args := []string{
"-state", statePath,
"-auto-approve",
2014-07-27 02:50:13 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-27 02:50:13 +02:00
}
if !p.ReadResourceCalled {
t.Fatal("should call ReadResource")
2014-07-27 02:50:13 +02:00
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
2014-07-27 02:50:13 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
2014-07-28 05:38:41 +02:00
// Should have a backup file
backupState := testStateRead(t, statePath+DefaultBackupExtension)
2014-07-28 05:38:41 +02:00
2014-09-19 02:16:09 +02:00
actualStr := strings.TrimSpace(backupState.String())
expectedStr := strings.TrimSpace(originalState.String())
if actualStr != expectedStr {
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
2014-07-28 05:38:41 +02:00
}
2014-07-27 02:50:13 +02:00
}
func TestApply_refreshFalse(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"ami":"bar"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, originalState)
p := applyFixtureProvider()
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-state", statePath,
"-auto-approve",
"-refresh=false",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if p.ReadResourceCalled {
t.Fatal("should not call ReadResource when refresh=false")
}
}
2014-07-03 02:01:02 +02:00
func TestApply_shutdown(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-shutdown"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
cancelled := make(chan struct{})
shutdownCh := make(chan struct{})
2014-07-03 02:01:02 +02:00
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-03 02:01:02 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
ShutdownCh: shutdownCh,
},
2014-07-03 02:01:02 +02:00
}
p.StopFn = func() error {
close(cancelled)
return nil
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
resp.PlannedState = req.ProposedNewState
return
2014-07-03 02:01:02 +02:00
}
var once sync.Once
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
// only cancel once
once.Do(func() {
shutdownCh <- struct{}{}
})
// Because of the internal lock in the MockProvider, we can't
// coordiante directly with the calling of Stop, and making the
// MockProvider concurrent is disruptive to a lot of existing tests.
// Wait here a moment to help make sure the main goroutine gets to the
// Stop call before we exit, or the plan may finish before it can be
// canceled.
time.Sleep(200 * time.Millisecond)
resp.NewState = req.PlannedState
return
2014-07-03 02:01:02 +02:00
}
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
2014-07-03 02:01:02 +02:00
args := []string{
"-state", statePath,
"-auto-approve",
2014-07-03 02:01:02 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-03 02:01:02 +02:00
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
select {
case <-cancelled:
default:
t.Fatal("command not cancelled")
}
state := testStateRead(t, statePath)
2014-07-03 02:01:02 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
}
func TestApply_state(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
2018-10-17 02:51:47 +02:00
AttrsJSON: []byte(`{"ami":"foo"}`),
Status: states.ObjectReady,
2014-06-19 21:12:24 +02:00
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, originalState)
2014-06-19 21:12:24 +02:00
p := applyFixtureProvider()
2021-01-12 22:13:10 +01:00
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
2014-07-01 05:49:49 +02:00
}
2021-01-12 22:13:10 +01:00
p.ApplyResourceChangeResponse = &providers.ApplyResourceChangeResponse{
NewState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
}
2014-07-01 05:49:49 +02:00
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-06-19 21:12:24 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 21:12:24 +02:00
}
// Run the apply command pointing to our existing state
args := []string{
"-state", statePath,
"-auto-approve",
2014-06-19 21:12:24 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-06-19 21:12:24 +02:00
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
"id": cty.NullVal(cty.String),
2018-10-17 02:51:47 +02:00
"ami": cty.StringVal("foo"),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state during plan\ngot: %#v\nwant: %#v", actual, expected)
}
actual = p.ApplyResourceChangeRequest.PriorState
expected = cty.ObjectVal(map[string]cty.Value{
"id": cty.NullVal(cty.String),
2018-10-17 02:51:47 +02:00
"ami": cty.StringVal("foo"),
})
2018-10-17 02:51:47 +02:00
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state during apply\ngot: %#v\nwant: %#v", actual, expected)
2014-06-19 21:12:24 +02:00
}
// Verify a new state exists
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
2014-06-19 21:12:24 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
2014-07-28 05:38:41 +02:00
backupState := testStateRead(t, statePath+DefaultBackupExtension)
2014-07-28 05:38:41 +02:00
2014-09-19 02:16:09 +02:00
actualStr := strings.TrimSpace(backupState.String())
expectedStr := strings.TrimSpace(originalState.String())
if actualStr != expectedStr {
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
2014-07-28 05:38:41 +02:00
}
2014-06-19 21:12:24 +02:00
}
2014-06-19 06:36:44 +02:00
func TestApply_stateNoExist(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := applyFixtureProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-06-19 06:36:44 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
2014-06-19 06:36:44 +02:00
}
args := []string{
"idontexist.tfstate",
2014-06-19 06:36:44 +02:00
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: \n%s", output.Stdout())
2014-06-19 06:36:44 +02:00
}
}
2014-07-18 20:37:27 +02:00
func TestApply_sensitiveOutput(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-sensitive-output"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
statePath := testTempFile(t)
args := []string{
"-state", statePath,
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: \n%s", output.Stdout())
}
2021-02-18 23:23:34 +01:00
stdout := output.Stdout()
if !strings.Contains(stdout, "notsensitive = \"Hello world\"") {
t.Fatalf("bad: output should contain 'notsensitive' output\n%s", stdout)
}
2021-02-18 23:23:34 +01:00
if !strings.Contains(stdout, "sensitive = <sensitive>") {
t.Fatalf("bad: output should contain 'sensitive' output\n%s", stdout)
}
}
2014-07-18 20:37:27 +02:00
func TestApply_vars(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-vars"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2014-07-18 20:37:27 +02:00
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-18 20:37:27 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-18 20:37:27 +02:00
},
}
actual := ""
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
2018-10-14 16:59:15 +02:00
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: req.PlannedState,
}
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual = req.ProposedNewState.GetAttr("value").AsString()
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
2014-07-18 20:37:27 +02:00
}
}
args := []string{
"-auto-approve",
2014-07-18 20:37:27 +02:00
"-var", "foo=bar",
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-18 20:37:27 +02:00
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
2014-07-18 23:00:40 +02:00
func TestApply_varFile(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-vars"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2014-07-18 23:00:40 +02:00
varFilePath := testTempFile(t)
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-18 23:00:40 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-18 23:00:40 +02:00
},
}
actual := ""
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
2018-10-14 16:59:15 +02:00
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: req.PlannedState,
}
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual = req.ProposedNewState.GetAttr("value").AsString()
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
2014-07-18 23:00:40 +02:00
}
}
args := []string{
"-auto-approve",
2014-07-18 23:00:40 +02:00
"-var-file", varFilePath,
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-18 23:00:40 +02:00
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestApply_varFileDefault(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-vars"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
varFilePath := filepath.Join(td, "terraform.tfvars")
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
t.Fatalf("err: %s", err)
}
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
actual := ""
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
2018-10-14 16:59:15 +02:00
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: req.PlannedState,
}
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual = req.ProposedNewState.GetAttr("value").AsString()
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
args := []string{
"-auto-approve",
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
func TestApply_varFileDefaultJSON(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-vars"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
varFilePath := filepath.Join(td, "terraform.tfvars.json")
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFileJSON), 0644); err != nil {
t.Fatalf("err: %s", err)
}
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
actual := ""
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"value": {Type: cty.String, Optional: true},
},
},
},
},
}
2018-10-14 16:59:15 +02:00
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: req.PlannedState,
}
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
actual = req.ProposedNewState.GetAttr("value").AsString()
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
args := []string{
"-auto-approve",
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if actual != "bar" {
t.Fatal("didn't work")
}
}
2014-07-28 05:38:41 +02:00
func TestApply_backup(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
Status: states.ObjectReady,
2014-07-28 05:38:41 +02:00
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
2014-07-28 05:38:41 +02:00
statePath := testStateFile(t, originalState)
backupPath := testTempFile(t)
p := applyFixtureProvider()
2021-01-12 22:13:10 +01:00
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
2014-07-28 05:38:41 +02:00
}
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-28 05:38:41 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-28 05:38:41 +02:00
},
}
// Run the apply command pointing to our existing state
args := []string{
"-auto-approve",
2014-07-28 05:38:41 +02:00
"-state", statePath,
"-backup", backupPath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-28 05:38:41 +02:00
}
// Verify a new state exists
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
2014-07-28 05:38:41 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
backupState := testStateRead(t, backupPath)
2014-07-28 05:38:41 +02:00
2014-09-17 20:15:07 +02:00
actual := backupState.RootModule().Resources["test_instance.foo"]
expected := originalState.RootModule().Resources["test_instance.foo"]
if !cmp.Equal(actual, expected, cmpopts.EquateEmpty()) {
t.Fatalf(
"wrong aws_instance.foo state\n%s",
2018-10-14 16:59:15 +02:00
cmp.Diff(expected, actual, cmp.Transformer("bytesAsString", func(b []byte) string {
return string(b)
})),
)
2014-07-28 05:38:41 +02:00
}
}
func TestApply_disableBackup(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2014-09-18 19:40:23 +02:00
originalState := testState()
2014-07-28 05:38:41 +02:00
statePath := testStateFile(t, originalState)
p := applyFixtureProvider()
2021-01-12 22:13:10 +01:00
p.PlanResourceChangeResponse = &providers.PlanResourceChangeResponse{
PlannedState: cty.ObjectVal(map[string]cty.Value{
"ami": cty.StringVal("bar"),
}),
2014-07-28 05:38:41 +02:00
}
2021-02-18 23:23:34 +01:00
view, done := testView(t)
2014-07-28 05:38:41 +02:00
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
2014-07-28 05:38:41 +02:00
},
}
// Run the apply command pointing to our existing state
args := []string{
"-auto-approve",
2014-07-28 05:38:41 +02:00
"-state", statePath,
"-backup", "-",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
2014-07-28 05:38:41 +02:00
}
// Verify that the provider was called with the existing state
actual := p.PlanResourceChangeRequest.PriorState
expected := cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state during plan\ngot: %#v\nwant: %#v", actual, expected)
2014-07-28 05:38:41 +02:00
}
actual = p.ApplyResourceChangeRequest.PriorState
expected = cty.ObjectVal(map[string]cty.Value{
2018-10-14 16:59:15 +02:00
"id": cty.StringVal("bar"),
"ami": cty.NullVal(cty.String),
})
if !expected.RawEquals(actual) {
t.Fatalf("wrong prior state during apply\ngot: %#v\nwant: %#v", actual, expected)
2014-07-28 05:38:41 +02:00
}
// Verify a new state exists
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
2014-07-28 05:38:41 +02:00
if state == nil {
t.Fatal("state should not be nil")
}
// Ensure there is no backup
_, err := os.Stat(statePath + DefaultBackupExtension)
2014-07-28 05:38:41 +02:00
if err == nil || !os.IsNotExist(err) {
t.Fatalf("backup should not exist")
}
// Ensure there is no literal "-"
_, err = os.Stat("-")
if err == nil || !os.IsNotExist(err) {
t.Fatalf("backup should not exist")
}
2014-07-28 05:38:41 +02:00
}
// Test that the Terraform env is passed through
func TestApply_terraformEnv(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-terraform-env"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-auto-approve",
"-state", statePath,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
expected := strings.TrimSpace(`
<no state>
Outputs:
output = default
`)
testStateOutput(t, statePath, expected)
}
// Test that the Terraform env is passed through
func TestApply_terraformEnvNonDefault(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-terraform-env"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
// Create new env
{
ui := new(cli.MockUi)
2021-02-18 23:23:34 +01:00
newCmd := &WorkspaceNewCommand{
Meta: Meta{
Ui: ui,
},
}
if code := newCmd.Run([]string{"test"}); code != 0 {
}
}
// Switch to it
{
args := []string{"test"}
ui := new(cli.MockUi)
2021-02-18 23:23:34 +01:00
selCmd := &WorkspaceSelectCommand{
Meta: Meta{
Ui: ui,
},
}
if code := selCmd.Run(args); code != 0 {
}
}
p := testProvider()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-auto-approve",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
statePath := filepath.Join("terraform.tfstate.d", "test", "terraform.tfstate")
expected := strings.TrimSpace(`
<no state>
Outputs:
output = test
`)
testStateOutput(t, statePath, expected)
}
// Config with multiple resources, targeting apply of a subset
func TestApply_targeted(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-targeted"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-auto-approve",
"-target", "test_instance.foo",
"-target", "test_instance.baz",
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
2021-02-18 23:23:34 +01:00
if got, want := output.Stdout(), "3 added, 0 changed, 0 destroyed"; !strings.Contains(got, want) {
t.Fatalf("bad change summary, want %q, got:\n%s", want, got)
}
}
// Diagnostics for invalid -target flags
func TestApply_targetFlagsDiags(t *testing.T) {
testCases := map[string]string{
"test_instance.": "Dot must be followed by attribute name.",
"test_instance": "Resource specification must include a resource type and name.",
}
for target, wantDiag := range testCases {
t.Run(target, func(t *testing.T) {
td := testTempDir(t)
defer os.RemoveAll(td)
defer testChdir(t, td)()
2021-02-18 23:23:34 +01:00
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
View: view,
},
}
args := []string{
"-auto-approve",
"-target", target,
}
2021-02-18 23:23:34 +01:00
code := c.Run(args)
output := done(t)
if code != 1 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
2021-02-18 23:23:34 +01:00
got := output.Stderr()
if !strings.Contains(got, target) {
t.Fatalf("bad error output, want %q, got:\n%s", target, got)
}
if !strings.Contains(got, wantDiag) {
t.Fatalf("bad error output, want %q, got:\n%s", wantDiag, got)
}
})
}
}
func TestApply_replace(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("apply-replace"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
originalState := states.BuildState(func(s *states.SyncState) {
s.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "a",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
&states.ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{"id":"hello"}`),
Status: states.ObjectReady,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
})
statePath := testStateFile(t, originalState)
p := testProvider()
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
},
},
},
},
}
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
createCount := 0
deleteCount := 0
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
if req.PriorState.IsNull() {
createCount++
}
if req.PlannedState.IsNull() {
deleteCount++
}
return providers.ApplyResourceChangeResponse{
NewState: req.PlannedState,
}
}
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-auto-approve",
"-state", statePath,
"-replace", "test_instance.a",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("wrong exit code %d\n\n%s", code, output.Stderr())
}
if got, want := output.Stdout(), "1 added, 0 changed, 1 destroyed"; !strings.Contains(got, want) {
t.Errorf("wrong change summary\ngot output:\n%s\n\nwant substring: %s", got, want)
}
if got, want := createCount, 1; got != want {
t.Errorf("wrong create count %d; want %d", got, want)
}
if got, want := deleteCount, 1; got != want {
t.Errorf("wrong create count %d; want %d", got, want)
}
}
2021-02-18 23:23:34 +01:00
func TestApply_pluginPath(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := applyFixtureProvider()
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
pluginPath := []string{"a", "b", "c"}
if err := c.Meta.storePluginPath(pluginPath); err != nil {
t.Fatal(err)
}
c.Meta.pluginPath = nil
args := []string{
"-state", statePath,
"-auto-approve",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if !reflect.DeepEqual(pluginPath, c.Meta.pluginPath) {
t.Fatalf("expected plugin path %#v, got %#v", pluginPath, c.Meta.pluginPath)
}
}
func TestApply_jsonGoldenReference(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
statePath := testTempFile(t)
p := applyFixtureProvider()
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{
"-json",
"-state", statePath,
"-auto-approve",
}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
if _, err := os.Stat(statePath); err != nil {
t.Fatalf("err: %s", err)
}
state := testStateRead(t, statePath)
if state == nil {
t.Fatal("state should not be nil")
}
// Load the golden reference fixture
wantFile, err := os.Open(path.Join(testFixturePath("apply"), "output.jsonlog"))
if err != nil {
t.Fatalf("failed to open output file: %s", err)
}
defer wantFile.Close()
wantBytes, err := ioutil.ReadAll(wantFile)
if err != nil {
t.Fatalf("failed to read output file: %s", err)
}
want := string(wantBytes)
got := output.Stdout()
// Split the output and the reference into lines so that we can compare
// messages
got = strings.TrimSuffix(got, "\n")
gotLines := strings.Split(got, "\n")
want = strings.TrimSuffix(want, "\n")
wantLines := strings.Split(want, "\n")
if len(gotLines) != len(wantLines) {
t.Errorf("unexpected number of log lines: got %d, want %d", len(gotLines), len(wantLines))
}
// Verify that the log starts with a version message
type versionMessage struct {
Level string `json:"@level"`
Message string `json:"@message"`
Type string `json:"type"`
Terraform string `json:"terraform"`
UI string `json:"ui"`
}
var gotVersion versionMessage
if err := json.Unmarshal([]byte(gotLines[0]), &gotVersion); err != nil {
t.Errorf("failed to unmarshal version line: %s\n%s", err, gotLines[0])
}
wantVersion := versionMessage{
"info",
fmt.Sprintf("Terraform %s", tfversion.String()),
"version",
tfversion.String(),
views.JSON_UI_VERSION,
}
if !cmp.Equal(wantVersion, gotVersion) {
t.Errorf("unexpected first message:\n%s", cmp.Diff(wantVersion, gotVersion))
}
// Compare the rest of the lines against the golden reference
var gotLineMaps []map[string]interface{}
for i, line := range gotLines[1:] {
index := i + 1
var gotMap map[string]interface{}
if err := json.Unmarshal([]byte(line), &gotMap); err != nil {
t.Errorf("failed to unmarshal got line %d: %s\n%s", index, err, gotLines[index])
}
if _, ok := gotMap["@timestamp"]; !ok {
t.Errorf("missing @timestamp field in log: %s", gotLines[index])
}
delete(gotMap, "@timestamp")
gotLineMaps = append(gotLineMaps, gotMap)
}
var wantLineMaps []map[string]interface{}
for i, line := range wantLines[1:] {
index := i + 1
var wantMap map[string]interface{}
if err := json.Unmarshal([]byte(line), &wantMap); err != nil {
t.Errorf("failed to unmarshal want line %d: %s\n%s", index, err, gotLines[index])
}
wantLineMaps = append(wantLineMaps, wantMap)
}
if diff := cmp.Diff(wantLineMaps, gotLineMaps); diff != "" {
t.Errorf("wrong output lines\n%s", diff)
}
}
func TestApply_warnings(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
testCopyDir(t, testFixturePath("apply"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()
p := testProvider()
p.GetProviderSchemaResponse = applyFixtureSchema()
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
Diagnostics: tfdiags.Diagnostics{
tfdiags.SimpleWarning("warning 1"),
tfdiags.SimpleWarning("warning 2"),
},
}
}
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: cty.UnknownAsNull(req.PlannedState),
}
}
t.Run("full warnings", func(t *testing.T) {
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
args := []string{"-auto-approve"}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
wantWarnings := []string{
"warning 1",
"warning 2",
}
for _, want := range wantWarnings {
if !strings.Contains(output.Stdout(), want) {
t.Errorf("missing warning %s", want)
}
}
})
t.Run("compact warnings", func(t *testing.T) {
view, done := testView(t)
c := &ApplyCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}
code := c.Run([]string{"-auto-approve", "-compact-warnings"})
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}
// the output should contain 2 warnings and a message about -compact-warnings
wantWarnings := []string{
"warning 1",
"warning 2",
"To see the full warning notes, run Terraform without -compact-warnings.",
}
for _, want := range wantWarnings {
if !strings.Contains(output.Stdout(), want) {
t.Errorf("missing warning %s", want)
}
}
})
}
// applyFixtureSchema returns a schema suitable for processing the
// configuration in testdata/apply . This schema should be
// assigned to a mock provider named "test".
func applyFixtureSchema() *providers.GetProviderSchemaResponse {
return &providers.GetProviderSchemaResponse{
2021-01-12 22:13:10 +01:00
ResourceTypes: map[string]providers.Schema{
"test_instance": {
2021-01-12 22:13:10 +01:00
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
"ami": {Type: cty.String, Optional: true},
},
},
},
},
}
}
// applyFixtureProvider returns a mock provider that is configured for basic
// operation with the configuration in testdata/apply. This mock has
2021-01-12 22:13:10 +01:00
// GetSchemaResponse, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
// with the plan/apply steps just passing through the data determined by
// Terraform Core.
func applyFixtureProvider() *terraform.MockProvider {
p := testProvider()
p.GetProviderSchemaResponse = applyFixtureSchema()
2018-10-14 16:59:15 +02:00
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
return providers.PlanResourceChangeResponse{
PlannedState: req.ProposedNewState,
}
}
2018-10-14 16:59:15 +02:00
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
return providers.ApplyResourceChangeResponse{
NewState: cty.UnknownAsNull(req.PlannedState),
}
}
return p
}
// applyFixturePlanFile creates a plan file at a temporary location containing
// a single change to create the test_instance.foo that is included in the
// "apply" test fixture, returning the location of that plan file.
func applyFixturePlanFile(t *testing.T) string {
_, snap := testModuleWithSnapshot(t, "apply")
plannedVal := cty.ObjectVal(map[string]cty.Value{
"id": cty.UnknownVal(cty.String),
"ami": cty.StringVal("bar"),
})
priorValRaw, err := plans.NewDynamicValue(cty.NullVal(plannedVal.Type()), plannedVal.Type())
if err != nil {
t.Fatal(err)
}
plannedValRaw, err := plans.NewDynamicValue(plannedVal, plannedVal.Type())
if err != nil {
t.Fatal(err)
}
plan := testPlan(t)
plan.Changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "foo",
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
ProviderAddr: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
ChangeSrc: plans.ChangeSrc{
Action: plans.Create,
Before: priorValRaw,
After: plannedValRaw,
},
})
return testPlanFile(
t,
snap,
states.NewState(),
plan,
)
}
2014-07-18 23:00:40 +02:00
const applyVarFile = `
foo = "bar"
`
2014-09-18 19:40:23 +02:00
const applyVarFileJSON = `
{ "foo": "bar" }
`