2014-06-19 06:36:44 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-10-01 17:43:00 +02:00
|
|
|
"bytes"
|
2014-07-08 06:20:48 +02:00
|
|
|
"fmt"
|
2014-07-12 06:30:40 +02:00
|
|
|
"io/ioutil"
|
2014-09-30 00:55:28 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2014-06-19 06:36:44 +02:00
|
|
|
"os"
|
2014-07-12 06:30:40 +02:00
|
|
|
"path/filepath"
|
2014-09-18 19:40:23 +02:00
|
|
|
"strings"
|
2014-07-08 06:20:48 +02:00
|
|
|
"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
|
|
|
|
2018-10-13 01:31:18 +02:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2019-06-04 16:32:12 +02:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2018-05-23 04:33:45 +02:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-07-05 19:33:29 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
2018-09-29 18:35:19 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2014-06-19 06:36:44 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestApply(t *testing.T) {
|
2014-06-27 23:43:23 +02:00
|
|
|
statePath := testTempFile(t)
|
2014-06-19 06:36:44 +02:00
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2018-05-23 04:33:45 +02:00
|
|
|
|
2014-06-19 06:36:44 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:30:40 +02:00
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-06-19 06:36:44 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-06-19 06:36:44 +02:00
|
|
|
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) {
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
unlock, err := testLockState(testDataDir, statePath)
|
2017-02-03 21:32:40 +01:00
|
|
|
if err != nil {
|
2017-02-03 20:55:13 +01:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-02-03 21:32:40 +01:00
|
|
|
defer unlock()
|
2017-02-03 20:55:13 +01:00
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2017-02-03 20:55:13 +01:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-02-03 20:55:13 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2017-02-03 20:55:13 +01:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
2017-02-03 21:32:40 +01:00
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.ErrorWriter.String()
|
2017-02-15 20:01:18 +01:00
|
|
|
if !strings.Contains(output, "lock") {
|
2017-02-03 21:32:40 +01:00
|
|
|
t.Fatal("command output does not look like a lock error:", output)
|
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) {
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
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()
|
|
|
|
}()
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2017-04-03 17:33:38 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
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",
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2017-04-03 17:33:38 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2018-10-12 02:58:46 +02:00
|
|
|
t.Fatalf("lock should have succeeded in less than 3s: %s", ui.ErrorWriter)
|
2017-04-03 17:33:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:26:16 +02:00
|
|
|
// high water mark counter
|
|
|
|
type hwm struct {
|
|
|
|
sync.Mutex
|
|
|
|
val int
|
|
|
|
max int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *hwm) Inc() {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
t.val++
|
|
|
|
if t.val > t.max {
|
|
|
|
t.max = t.val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *hwm) Dec() {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
t.val--
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *hwm) Max() int {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
return t.max
|
|
|
|
}
|
|
|
|
|
2015-10-14 19:43:51 +02:00
|
|
|
func TestApply_parallelism(t *testing.T) {
|
2015-10-30 00:06:30 +01:00
|
|
|
statePath := testTempFile(t)
|
2015-05-08 09:01:21 +02:00
|
|
|
|
2016-10-18 00:26:16 +02:00
|
|
|
par := 4
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
// This blocks all the apply functions. We close it when we exit so
|
2015-10-14 19:43:51 +02:00
|
|
|
// they end quickly after this test finishes.
|
|
|
|
block := make(chan struct{})
|
2016-10-18 00:26:16 +02:00
|
|
|
// signal how many goroutines have started
|
|
|
|
started := make(chan int, 100)
|
|
|
|
|
|
|
|
runCount := &hwm{}
|
2015-05-08 09:01:21 +02:00
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
// 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.
|
2019-12-04 17:30:20 +01:00
|
|
|
providerFactories := map[addrs.Provider]providers.Factory{}
|
2018-10-12 02:58:46 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
name := fmt.Sprintf("test%d", i)
|
|
|
|
provider := &terraform.MockProvider{}
|
|
|
|
provider.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
2018-10-14 16:59:15 +02:00
|
|
|
name + "_instance": {},
|
2018-10-12 02:58:46 +02:00
|
|
|
},
|
|
|
|
}
|
2018-10-14 16:59:15 +02:00
|
|
|
provider.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2018-10-12 02:58:46 +02:00
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 16:59:15 +02:00
|
|
|
provider.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
2018-10-12 02:58:46 +02:00
|
|
|
// Increment so we're counting parallelism
|
|
|
|
started <- 1
|
|
|
|
runCount.Inc()
|
|
|
|
defer runCount.Dec()
|
|
|
|
// Block here to stage up our max number of parallel instances
|
|
|
|
<-block
|
|
|
|
|
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: cty.EmptyObjectVal,
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 21:55:25 +02:00
|
|
|
providerFactories[addrs.NewDefaultProvider(name)] = providers.FactoryFixed(provider)
|
2018-10-12 02:58:46 +02:00
|
|
|
}
|
|
|
|
testingOverrides := &testingOverrides{
|
2020-03-31 00:30:56 +02:00
|
|
|
Providers: providerFactories,
|
2015-05-08 09:01:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-14 19:43:51 +02:00
|
|
|
ui := new(cli.MockUi)
|
2015-05-08 09:01:21 +02:00
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2018-10-12 02:58:46 +02:00
|
|
|
testingOverrides: testingOverrides,
|
2017-04-14 03:05:58 +02:00
|
|
|
Ui: ui,
|
2015-05-08 09:01:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2015-10-30 00:06:30 +01:00
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2015-10-14 19:43:51 +02:00
|
|
|
fmt.Sprintf("-parallelism=%d", par),
|
2015-05-08 09:01:21 +02:00
|
|
|
testFixturePath("parallelism"),
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:26:16 +02:00
|
|
|
// Run in a goroutine. We can get any errors from the ui.OutputWriter
|
|
|
|
doneCh := make(chan int, 1)
|
2015-10-14 19:43:51 +02:00
|
|
|
go func() {
|
2016-10-18 00:26:16 +02:00
|
|
|
doneCh <- c.Run(args)
|
2015-10-14 19:43:51 +02:00
|
|
|
}()
|
2016-10-18 00:26:16 +02:00
|
|
|
|
|
|
|
timeout := time.After(5 * time.Second)
|
|
|
|
|
|
|
|
// ensure things are running
|
|
|
|
for i := 0; i < par; i++ {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout waiting for all goroutines to start")
|
|
|
|
case <-started:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// a little extra sleep, since we can't ensure all goroutines from the walk have
|
|
|
|
// really started
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
close(block)
|
|
|
|
|
2015-10-14 19:43:51 +02:00
|
|
|
select {
|
2016-10-18 00:26:16 +02:00
|
|
|
case res := <-doneCh:
|
|
|
|
if res != 0 {
|
|
|
|
t.Fatal(ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("timeout waiting from Run()")
|
2015-05-08 09:01:21 +02:00
|
|
|
}
|
|
|
|
|
2015-10-14 19:43:51 +02:00
|
|
|
// The total in flight should equal the parallelism
|
2016-10-18 00:26:16 +02:00
|
|
|
if runCount.Max() != par {
|
|
|
|
t.Fatalf("Expected parallelism: %d, got: %d", par, runCount.Max())
|
2015-05-08 09:01:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-03 06:16:36 +02:00
|
|
|
func TestApply_configInvalid(t *testing.T) {
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-03 06:16:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:30:40 +02:00
|
|
|
"-state", testTempFile(t),
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-03 06:16:36 +02:00
|
|
|
testFixturePath("apply-config-invalid"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:30:40 +02:00
|
|
|
func TestApply_defaultState(t *testing.T) {
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
2014-07-12 06:30:40 +02:00
|
|
|
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)
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-07-12 06:30:40 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:30:40 +02:00
|
|
|
}
|
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
// create an existing state file
|
2020-08-11 17:43:01 +02:00
|
|
|
localState := statemgr.NewFilesystem(statePath)
|
|
|
|
if err := localState.WriteState(states.NewState()); err != nil {
|
2017-07-06 00:17:07 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:30:40 +02:00
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-12 06:30:40 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-07-12 06:30:40 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 06:20:48 +02:00
|
|
|
func TestApply_error(t *testing.T) {
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
2018-10-13 03:04:26 +02:00
|
|
|
ui := cli.NewMockUi()
|
2014-07-08 06:20:48 +02:00
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var lock sync.Mutex
|
|
|
|
errored := false
|
2020-10-08 19:42:06 +02:00
|
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
|
2014-07-08 06:20:48 +02:00
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
if !errored {
|
|
|
|
errored = true
|
2020-10-08 19:42:06 +02:00
|
|
|
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("error"))
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 19:42:06 +02:00
|
|
|
s := req.PlannedState.AsValueMap()
|
|
|
|
s["id"] = cty.StringVal("foo")
|
|
|
|
|
|
|
|
resp.NewState = cty.ObjectVal(s)
|
|
|
|
return
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
2020-10-08 19:42:06 +02:00
|
|
|
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
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
2018-10-13 03:04:26 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
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},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2014-07-08 06:20:48 +02:00
|
|
|
|
|
|
|
args := []string{
|
2014-07-12 06:30:40 +02:00
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-08 06:20:48 +02:00
|
|
|
testFixturePath("apply-error"),
|
|
|
|
}
|
2018-10-13 03:04:26 +02:00
|
|
|
if ui.ErrorWriter != nil {
|
|
|
|
t.Logf("stdout:\n%s", ui.OutputWriter.String())
|
|
|
|
t.Logf("stderr:\n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
2014-07-08 06:20:48 +02:00
|
|
|
if code := c.Run(args); code != 1 {
|
2018-10-13 03:04:26 +02:00
|
|
|
t.Fatalf("wrong exit code %d; want 1", code)
|
2014-07-08 06:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-07-08 06:20:48 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
2014-09-17 20:15:07 +02:00
|
|
|
if len(state.RootModule().Resources) == 0 {
|
2014-07-08 06:20:48 +02:00
|
|
|
t.Fatal("no resources in state")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:46:38 +01:00
|
|
|
func TestApply_input(t *testing.T) {
|
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
2019-10-08 21:08:27 +02:00
|
|
|
// 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.
|
2015-01-16 19:46:38 +01:00
|
|
|
defaultInputReader = bytes.NewBufferString("foo\n")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-01-16 19:46:38 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2015-01-16 19:46:38 +01:00
|
|
|
testFixturePath("apply-input"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:35:19 +02:00
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
<no state>
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
result = foo
|
|
|
|
`)
|
|
|
|
testStateOutput(t, statePath, expected)
|
2015-01-16 19:46:38 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 03:16:43 +01:00
|
|
|
// 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) {
|
|
|
|
// 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()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-11-02 03:16:43 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2016-11-02 03:16:43 +01:00
|
|
|
"-var", "foo=foovalue",
|
|
|
|
testFixturePath("apply-input-partial"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
<no state>
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
bar = one
|
|
|
|
foo = foovalue
|
|
|
|
`)
|
|
|
|
testStateOutput(t, statePath, expected)
|
|
|
|
}
|
|
|
|
|
2014-07-12 06:32:34 +02:00
|
|
|
func TestApply_noArgs(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-10-13 01:31:18 +02:00
|
|
|
if err := os.Chdir(testFixturePath("apply")); err != nil {
|
2014-07-12 06:32:34 +02:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-10-13 01:31:18 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-07-12 06:32:34 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-12 06:32:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-12 06:32:34 +02:00
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-07-12 06:32:34 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
func TestApply_plan(t *testing.T) {
|
2014-10-01 17:43:00 +02:00
|
|
|
// 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)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath := applyFixturePlanFile(t)
|
2014-06-27 23:43:23 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-06-19 06:36:44 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-01-19 05:50:45 +01:00
|
|
|
"-state-out", statePath,
|
2014-06-27 23:43:23 +02:00
|
|
|
planPath,
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
2014-06-27 23:43:23 +02:00
|
|
|
if code := c.Run(args); code != 0 {
|
2014-08-25 06:40:58 +02:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-08-25 06:40:58 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 02:51:05 +02:00
|
|
|
func TestApply_plan_backup(t *testing.T) {
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath := applyFixturePlanFile(t)
|
2016-10-29 02:51:05 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
backupPath := testTempFile(t)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
2016-10-29 02:51:05 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-10-29 02:51:05 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:07:34 +01:00
|
|
|
// create a state file that needs to be backed up
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
err := statemgr.NewFilesystem(statePath).WriteState(states.NewState())
|
2017-02-03 19:07:34 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-10-17 02:51:47 +02:00
|
|
|
|
2017-03-14 00:41:33 +01:00
|
|
|
args := []string{
|
2018-10-17 02:51:47 +02:00
|
|
|
"-state", statePath,
|
2016-10-29 02:51:05 +02:00
|
|
|
"-backup", backupPath,
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-07-06 00:17:07 +02:00
|
|
|
// Should have a backup file
|
|
|
|
testStateRead(t, backupPath)
|
2016-10-29 02:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply_plan_noBackup(t *testing.T) {
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath := applyFixturePlanFile(t)
|
2016-10-29 02:51:05 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
2016-10-29 02:51:05 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-10-29 02:51:05 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-01-19 05:50:45 +01:00
|
|
|
"-state-out", statePath,
|
2016-10-29 02:51:05 +02:00
|
|
|
"-backup", "-",
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 22:08:32 +01:00
|
|
|
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)
|
2015-01-07 22:08:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set some default reader/writers for the inputs
|
|
|
|
defaultInputReader = new(bytes.Buffer)
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Create a remote state
|
|
|
|
state := testState()
|
2018-11-09 00:22:41 +01:00
|
|
|
_, srv := testRemoteState(t, state, 200)
|
2015-01-07 22:08:32 +01:00
|
|
|
defer srv.Close()
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
_, snap := testModuleWithSnapshot(t, "apply")
|
2018-10-13 03:04:26 +02:00
|
|
|
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),
|
2019-06-05 22:12:07 +02:00
|
|
|
"retry_max": cty.NullVal(cty.String),
|
|
|
|
"retry_wait_min": cty.NullVal(cty.String),
|
|
|
|
"retry_wait_max": cty.NullVal(cty.String),
|
2018-10-13 03:04:26 +02:00
|
|
|
})
|
|
|
|
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(),
|
|
|
|
})
|
2015-01-07 22:08:32 +01:00
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-01-07 22:08:32 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
2015-01-07 22:08:32 +01:00
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Check that there is no remote state config
|
2018-11-09 00:22:41 +01:00
|
|
|
if src, err := ioutil.ReadFile(remoteStatePath); err == nil {
|
|
|
|
t.Fatalf("has %s file; should not\n%s", remoteStatePath, src)
|
2015-01-07 22:08:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-25 06:40:58 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath := applyFixturePlanFile(t)
|
2014-08-25 06:40:58 +02:00
|
|
|
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)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-08-25 06:40:58 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-08-25 06:40:58 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-01-19 05:50:45 +01:00
|
|
|
"-state-out", statePath,
|
2014-08-25 06:40:58 +02:00
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
2014-06-27 23:43:23 +02:00
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-06-27 23:43:23 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-26 23:32:09 +02:00
|
|
|
func TestApply_planVars(t *testing.T) {
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath := applyFixturePlanFile(t)
|
2014-07-26 23:32:09 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-07-26 23:32:09 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-26 23:32:09 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-var", "foo=bar",
|
|
|
|
planPath,
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code == 0 {
|
|
|
|
t.Fatal("should've failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:39:18 +02:00
|
|
|
// 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
|
2018-03-28 19:08:38 +02:00
|
|
|
td := testTempDir(t)
|
2017-03-28 00:39:18 +02:00
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
p := applyFixtureProvider()
|
|
|
|
planPath := applyFixturePlanFile(t)
|
2017-03-28 00:39:18 +02:00
|
|
|
|
|
|
|
apply := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: new(cli.MockUi),
|
2017-03-28 00:39:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{
|
2018-10-13 03:04:26 +02:00
|
|
|
planPath,
|
2017-03-28 00:39:18 +02:00
|
|
|
}
|
|
|
|
apply.Run(args)
|
2018-10-19 15:29:14 +02:00
|
|
|
if p.PrepareProviderConfigCalled {
|
|
|
|
t.Fatal("Prepare provider config should not be called with a plan")
|
2017-03-28 00:39:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 02:50:13 +02:00
|
|
|
func TestApply_refresh(t *testing.T) {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
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-13 01:31:18 +02:00
|
|
|
AttrsJSON: []byte(`{"ami":"bar"}`),
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
Status: states.ObjectReady,
|
2014-07-27 02:50:13 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2014-07-27 02:50:13 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-07-27 02:50:13 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-27 02:50:13 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-27 02:50:13 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:35:19 +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)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
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
|
2017-07-05 23:59:42 +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-07-27 02:50:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 02:01:02 +02:00
|
|
|
func TestApply_shutdown(t *testing.T) {
|
2018-02-10 01:13:34 +01:00
|
|
|
cancelled := make(chan struct{})
|
|
|
|
shutdownCh := make(chan struct{})
|
2014-07-03 02:01:02 +02:00
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
p := testProvider()
|
2017-12-01 17:27:32 +01:00
|
|
|
|
2014-07-03 02:01:02 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-12-01 17:03:41 +01:00
|
|
|
ShutdownCh: shutdownCh,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
|
|
|
|
2017-12-03 04:36:43 +01:00
|
|
|
p.StopFn = func() error {
|
2018-02-10 01:13:34 +01:00
|
|
|
close(cancelled)
|
2017-12-03 04:36:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-08 19:42:06 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
return
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
2018-02-10 01:13:34 +01:00
|
|
|
|
|
|
|
var once sync.Once
|
2020-10-08 19:42:06 +02:00
|
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
|
2017-12-03 04:36:43 +01:00
|
|
|
// only cancel once
|
2018-02-10 01:13:34 +01:00
|
|
|
once.Do(func() {
|
2017-12-01 17:27:32 +01:00
|
|
|
shutdownCh <- struct{}{}
|
2018-02-10 01:13:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2020-10-08 19:42:06 +02:00
|
|
|
resp.NewState = req.PlannedState
|
|
|
|
return
|
2014-07-03 02:01:02 +02:00
|
|
|
}
|
|
|
|
|
2018-10-13 01:31:18 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-07-03 02:01:02 +02:00
|
|
|
args := []string{
|
2014-07-12 06:30:40 +02:00
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-03 02:01:02 +02:00
|
|
|
testFixturePath("apply-shutdown"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-02-10 01:13:34 +01:00
|
|
|
select {
|
|
|
|
case <-cancelled:
|
|
|
|
default:
|
2017-12-01 17:27:32 +01:00
|
|
|
t.Fatal("command not cancelled")
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-07-03 02:01:02 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 23:43:23 +02:00
|
|
|
func TestApply_state(t *testing.T) {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
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"}`),
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
Status: states.ObjectReady,
|
2014-06-19 21:12:24 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2014-06-27 23:43:23 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
2014-06-19 21:12:24 +02:00
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2018-09-29 18:35:19 +02:00
|
|
|
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
2014-07-01 05:49:49 +02:00
|
|
|
}
|
2018-10-13 01:31:18 +02:00
|
|
|
p.ApplyResourceChangeResponse = providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
}
|
2014-07-01 05:49:49 +02:00
|
|
|
|
2014-06-19 21:12:24 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 21:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2014-07-12 06:30:40 +02:00
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-06-19 21:12:24 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the provider was called with the existing state
|
2018-09-29 18:35:19 +02:00
|
|
|
actual := p.PlanResourceChangeRequest.PriorState
|
|
|
|
expected := cty.ObjectVal(map[string]cty.Value{
|
2018-10-13 01:31:18 +02:00
|
|
|
"id": cty.NullVal(cty.String),
|
2018-10-17 02:51:47 +02:00
|
|
|
"ami": cty.StringVal("foo"),
|
2018-09-29 18:35:19 +02:00
|
|
|
})
|
|
|
|
if !expected.RawEquals(actual) {
|
|
|
|
t.Fatalf("wrong prior state during plan\ngot: %#v\nwant: %#v", actual, expected)
|
2014-06-19 21:13:47 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:35:19 +02:00
|
|
|
actual = p.ApplyResourceChangeRequest.PriorState
|
|
|
|
expected = cty.ObjectVal(map[string]cty.Value{
|
2018-10-13 01:31:18 +02:00
|
|
|
"id": cty.NullVal(cty.String),
|
2018-10-17 02:51:47 +02:00
|
|
|
"ami": cty.StringVal("foo"),
|
2018-09-29 18:35:19 +02:00
|
|
|
})
|
2018-10-17 02:51:47 +02:00
|
|
|
if !expected.RawEquals(actual) {
|
2018-09-29 18:35:19 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
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
|
|
|
|
2017-07-05 23:59:42 +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) {
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2014-06-19 06:36:44 +02:00
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
2014-07-13 05:37:30 +02:00
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-13 05:37:30 +02:00
|
|
|
},
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2014-06-27 23:43:23 +02:00
|
|
|
"idontexist.tfstate",
|
2014-06-19 06:36:44 +02:00
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 20:37:27 +02:00
|
|
|
|
2016-05-09 21:46:07 +02:00
|
|
|
func TestApply_sensitiveOutput(t *testing.T) {
|
2016-05-10 20:56:26 +02:00
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2016-05-10 20:56:26 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-05-09 21:46:07 +02:00
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
2016-03-11 20:07:54 +01:00
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2016-03-11 20:07:54 +01:00
|
|
|
testFixturePath("apply-sensitive-output"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
output := ui.OutputWriter.String()
|
2020-09-09 20:13:53 +02:00
|
|
|
if !strings.Contains(output, "notsensitive = \"Hello world\"") {
|
2016-03-11 20:07:54 +01:00
|
|
|
t.Fatalf("bad: output should contain 'notsensitive' output\n%s", output)
|
|
|
|
}
|
2016-05-10 20:56:26 +02:00
|
|
|
if !strings.Contains(output, "sensitive = <sensitive>") {
|
2016-03-11 20:07:54 +01:00
|
|
|
t.Fatalf("bad: output should contain 'sensitive' output\n%s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 20:37:27 +02:00
|
|
|
func TestApply_vars(t *testing.T) {
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 20:37:27 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:46:55 +01:00
|
|
|
actual := ""
|
2018-10-13 01:31:18 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
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 {
|
2018-10-13 01:31:18 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: req.PlannedState,
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 02:46:55 +01:00
|
|
|
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{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-18 20:37:27 +02:00
|
|
|
"-var", "foo=bar",
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
2014-07-18 23:00:40 +02:00
|
|
|
|
|
|
|
func TestApply_varFile(t *testing.T) {
|
|
|
|
varFilePath := testTempFile(t)
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFile), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-18 23:00:40 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:46:55 +01:00
|
|
|
actual := ""
|
2018-10-13 01:31:18 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
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 {
|
2018-10-13 01:31:18 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: req.PlannedState,
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 02:46:55 +01:00
|
|
|
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{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-18 23:00:40 +02:00
|
|
|
"-var-file", varFilePath,
|
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
func TestApply_varFileDefault(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-08-05 18:32:01 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:46:55 +01:00
|
|
|
actual := ""
|
2018-10-13 01:31:18 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
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 {
|
2018-10-13 01:31:18 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: req.PlannedState,
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 02:46:55 +01:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
actual = req.ProposedNewState.GetAttr("value").AsString()
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
2014-08-05 18:32:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-08-05 18:32:01 +02:00
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:21:45 +01:00
|
|
|
func TestApply_varFileDefaultJSON(t *testing.T) {
|
|
|
|
varFileDir := testTempDir(t)
|
|
|
|
varFilePath := filepath.Join(varFileDir, "terraform.tfvars.json")
|
|
|
|
if err := ioutil.WriteFile(varFilePath, []byte(applyVarFileJSON), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2015-03-02 18:21:45 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-12 02:46:55 +01:00
|
|
|
actual := ""
|
2018-10-13 01:31:18 +02:00
|
|
|
p.GetSchemaReturn = &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
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 {
|
2018-10-13 01:31:18 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
|
|
|
NewState: req.PlannedState,
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 02:46:55 +01:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
|
|
actual = req.ProposedNewState.GetAttr("value").AsString()
|
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
2015-03-02 18:21:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2015-03-02 18:21:45 +01:00
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("apply-vars"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != "bar" {
|
|
|
|
t.Fatal("didn't work")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-28 05:38:41 +02:00
|
|
|
func TestApply_backup(t *testing.T) {
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
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-13 01:31:18 +02:00
|
|
|
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
Status: states.ObjectReady,
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
)
|
|
|
|
})
|
2014-07-28 05:38:41 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
backupPath := testTempFile(t)
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2018-09-29 18:35:19 +02:00
|
|
|
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-28 05:38:41 +02:00
|
|
|
"-state", statePath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify a new state exists
|
|
|
|
if _, err := os.Stat(statePath); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
state := testStateRead(t, statePath)
|
2014-07-28 05:38:41 +02:00
|
|
|
if state == nil {
|
|
|
|
t.Fatal("state should not be nil")
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
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"]
|
2019-06-04 16:32:12 +02:00
|
|
|
if !cmp.Equal(actual, expected, cmpopts.EquateEmpty()) {
|
2018-10-13 01:31:18 +02:00
|
|
|
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 {
|
2018-10-13 01:31:18 +02:00
|
|
|
return string(b)
|
|
|
|
})),
|
|
|
|
)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApply_disableBackup(t *testing.T) {
|
2014-09-18 19:40:23 +02:00
|
|
|
originalState := testState()
|
2014-07-28 05:38:41 +02:00
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
p := applyFixtureProvider()
|
2018-09-29 18:35:19 +02:00
|
|
|
p.PlanResourceChangeResponse = providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"ami": cty.StringVal("bar"),
|
|
|
|
}),
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2014-07-28 05:38:41 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the apply command pointing to our existing state
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2014-07-28 05:38:41 +02:00
|
|
|
"-state", statePath,
|
|
|
|
"-backup", "-",
|
|
|
|
testFixturePath("apply"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the provider was called with the existing state
|
2018-09-29 18:35:19 +02:00
|
|
|
actual := p.PlanResourceChangeRequest.PriorState
|
|
|
|
expected := cty.ObjectVal(map[string]cty.Value{
|
2018-10-13 01:31:18 +02:00
|
|
|
"id": cty.StringVal("bar"),
|
|
|
|
"ami": cty.NullVal(cty.String),
|
2018-09-29 18:35:19 +02:00
|
|
|
})
|
2018-10-13 01:31:18 +02:00
|
|
|
if !expected.RawEquals(actual) {
|
2018-09-29 18:35:19 +02:00
|
|
|
t.Fatalf("wrong prior state during plan\ngot: %#v\nwant: %#v", actual, expected)
|
2014-07-28 05:38:41 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:35:19 +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"),
|
2018-10-13 01:31:18 +02:00
|
|
|
"ami": cty.NullVal(cty.String),
|
2018-09-29 18:35:19 +02:00
|
|
|
})
|
2018-10-13 01:31:18 +02:00
|
|
|
if !expected.RawEquals(actual) {
|
2018-09-29 18:35:19 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-07-05 23:59:42 +02:00
|
|
|
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
|
2017-07-05 23:59:42 +02:00
|
|
|
_, 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")
|
|
|
|
}
|
2015-03-02 18:11:49 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-03-14 00:25:27 +01:00
|
|
|
// Test that the Terraform env is passed through
|
|
|
|
func TestApply_terraformEnv(t *testing.T) {
|
|
|
|
statePath := testTempFile(t)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-14 00:25:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2017-03-14 00:25:27 +01:00
|
|
|
"-state", statePath,
|
|
|
|
testFixturePath("apply-terraform-env"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
os.MkdirAll(td, 0755)
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
// Create new env
|
|
|
|
{
|
|
|
|
ui := new(cli.MockUi)
|
2017-05-31 00:06:13 +02:00
|
|
|
newCmd := &WorkspaceNewCommand{}
|
2017-03-14 00:25:27 +01:00
|
|
|
newCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := newCmd.Run([]string{"test"}); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch to it
|
|
|
|
{
|
|
|
|
args := []string{"test"}
|
|
|
|
ui := new(cli.MockUi)
|
2017-05-31 00:06:13 +02:00
|
|
|
selCmd := &WorkspaceSelectCommand{}
|
2017-03-14 00:25:27 +01:00
|
|
|
selCmd.Meta = Meta{Ui: ui}
|
|
|
|
if code := selCmd.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &ApplyCommand{
|
|
|
|
Meta: Meta{
|
2017-04-14 03:05:58 +02:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
2017-03-14 00:25:27 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
2017-10-30 21:33:27 +01:00
|
|
|
"-auto-approve",
|
2017-03-14 00:25:27 +01:00
|
|
|
testFixturePath("apply-terraform-env"),
|
|
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
statePath := filepath.Join("terraform.tfstate.d", "test", "terraform.tfstate")
|
|
|
|
expected := strings.TrimSpace(`
|
|
|
|
<no state>
|
|
|
|
Outputs:
|
|
|
|
|
|
|
|
output = test
|
|
|
|
`)
|
|
|
|
testStateOutput(t, statePath, expected)
|
|
|
|
}
|
|
|
|
|
2014-09-30 00:55:28 +02:00
|
|
|
func testHttpServer(t *testing.T) net.Listener {
|
2015-01-28 06:22:35 +01:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
2014-09-30 00:55:28 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/header", testHttpHandlerHeader)
|
|
|
|
|
|
|
|
var server http.Server
|
|
|
|
server.Handler = mux
|
|
|
|
go server.Serve(ln)
|
|
|
|
|
|
|
|
return ln
|
|
|
|
}
|
|
|
|
|
|
|
|
func testHttpHandlerHeader(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var url url.URL
|
|
|
|
url.Scheme = "file"
|
2015-01-28 06:39:16 +01:00
|
|
|
url.Path = filepath.ToSlash(testFixturePath("init"))
|
2014-09-30 00:55:28 +02:00
|
|
|
|
|
|
|
w.Header().Add("X-Terraform-Get", url.String())
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}
|
|
|
|
|
2018-05-23 04:33:45 +02:00
|
|
|
// applyFixtureSchema returns a schema suitable for processing the
|
2019-06-30 09:38:36 +02:00
|
|
|
// configuration in testdata/apply . This schema should be
|
2018-05-23 04:33:45 +02:00
|
|
|
// assigned to a mock provider named "test".
|
|
|
|
func applyFixtureSchema() *terraform.ProviderSchema {
|
|
|
|
return &terraform.ProviderSchema{
|
|
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
|
|
"test_instance": {
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
2018-10-14 16:59:15 +02:00
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
2018-05-23 04:33:45 +02:00
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 02:58:46 +02:00
|
|
|
// applyFixtureProvider returns a mock provider that is configured for basic
|
2019-06-30 09:38:36 +02:00
|
|
|
// operation with the configuration in testdata/apply. This mock has
|
2018-10-12 02:58:46 +02:00
|
|
|
// GetSchemaReturn, 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.GetSchemaReturn = applyFixtureSchema()
|
2018-10-14 16:59:15 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
2018-10-12 02:58:46 +02:00
|
|
|
return providers.PlanResourceChangeResponse{
|
|
|
|
PlannedState: req.ProposedNewState,
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 16:59:15 +02:00
|
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
2018-10-12 02:58:46 +02:00
|
|
|
return providers.ApplyResourceChangeResponse{
|
2018-10-13 01:31:18 +02:00
|
|
|
NewState: cty.UnknownAsNull(req.PlannedState),
|
2018-10-12 02:58:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2018-10-13 03:04:26 +02:00
|
|
|
// 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),
|
2020-02-13 21:32:58 +01:00
|
|
|
ProviderAddr: addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
2018-10-13 03:04:26 +02:00
|
|
|
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
|
|
|
|
2015-03-02 18:21:45 +01:00
|
|
|
const applyVarFileJSON = `
|
|
|
|
{ "foo": "bar" }
|
|
|
|
`
|
|
|
|
|
2014-09-18 19:40:23 +02:00
|
|
|
const testApplyDisableBackupStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testApplyDisableBackupStateStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testApplyStateStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const testApplyStateDiffStr = `
|
|
|
|
ID = bar
|
2016-04-21 21:59:10 +02:00
|
|
|
Tainted = false
|
2014-09-18 19:40:23 +02:00
|
|
|
`
|