2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-06-27 13:09:01 -05:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-11-27 09:10:42 -06:00
|
|
|
"bytes"
|
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 16:24:45 -05:00
|
|
|
"fmt"
|
2014-06-27 13:09:01 -05:00
|
|
|
"os"
|
2014-07-11 23:56:43 -05:00
|
|
|
"path/filepath"
|
2018-10-14 18:07:42 -05:00
|
|
|
"reflect"
|
2014-09-18 12:40:23 -05:00
|
|
|
"strings"
|
2014-09-21 11:42:48 -05:00
|
|
|
"testing"
|
2014-06-27 13:09:01 -05:00
|
|
|
|
2018-10-14 18:07:42 -05:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2019-06-04 09:32:12 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
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 16:24:45 -05:00
|
|
|
"github.com/mitchellh/cli"
|
2018-09-30 10:01:59 -05:00
|
|
|
"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 16:24:45 -05:00
|
|
|
|
2023-09-20 06:35:35 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configschema"
|
|
|
|
"github.com/opentofu/opentofu/internal/providers"
|
|
|
|
"github.com/opentofu/opentofu/internal/states"
|
|
|
|
"github.com/opentofu/opentofu/internal/states/statefile"
|
|
|
|
"github.com/opentofu/opentofu/internal/states/statemgr"
|
|
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
2014-06-27 13:09:01 -05:00
|
|
|
)
|
|
|
|
|
2019-10-23 21:32:44 -05:00
|
|
|
var equateEmpty = cmpopts.EquateEmpty()
|
|
|
|
|
2014-06-27 13:09:01 -05:00
|
|
|
func TestRefresh(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
state := testState()
|
2014-07-07 23:12:12 -05:00
|
|
|
statePath := testStateFile(t, state)
|
2014-06-27 13:09:01 -05:00
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2014-06-27 13:09:01 -05:00
|
|
|
c := &RefreshCommand{
|
2014-07-12 22:37:30 -05:00
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2014-07-12 22:37:30 -05:00
|
|
|
},
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-09-30 10:01:59 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-09-30 10:01:59 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2014-06-27 13:09:01 -05:00
|
|
|
|
|
|
|
args := []string{
|
2014-07-11 23:56:43 -05:00
|
|
|
"-state", statePath,
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:59 -05:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:33:55 -05:00
|
|
|
newStateFile, err := statefile.Read(f)
|
2014-06-27 13:09:01 -05:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:33:55 -05:00
|
|
|
actual := strings.TrimSpace(newStateFile.State.String())
|
2014-09-18 12:40:23 -05:00
|
|
|
expected := strings.TrimSpace(testRefreshStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-06-27 13:09:01 -05:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 13:12:45 -05:00
|
|
|
|
2017-03-16 14:05:11 -05:00
|
|
|
func TestRefresh_empty(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2020-10-07 11:48:25 -05:00
|
|
|
testCopyDir(t, testFixturePath("refresh-empty"), td)
|
2017-03-16 14:05:11 -05:00
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2017-03-16 14:05:11 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2017-03-16 14:05:11 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:59 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-09-30 10:01:59 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2017-03-16 14:05:11 -05:00
|
|
|
|
2021-02-02 09:35:45 -06:00
|
|
|
args := []string{}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2017-03-16 14:05:11 -05:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:59 -05:00
|
|
|
if p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should not have been called")
|
2017-03-16 14:05:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:32:40 -06:00
|
|
|
func TestRefresh_lockedState(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2017-02-03 14:32:40 -06:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
2022-04-08 11:34:16 -05:00
|
|
|
unlock, err := testLockState(t, testDataDir, statePath)
|
2017-02-03 14:32:40 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2017-02-03 14:32:40 -06:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2017-02-03 14:32:40 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-09-30 10:01:59 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-09-30 10:01:59 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2017-02-03 14:32:40 -06:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code == 0 {
|
2017-02-03 14:32:40 -06:00
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
got := output.Stderr()
|
|
|
|
if !strings.Contains(got, "lock") {
|
|
|
|
t.Fatal("command output does not look like a lock error:", got)
|
2017-02-03 14:32:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 23:56:43 -05:00
|
|
|
func TestRefresh_cwd(t *testing.T) {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(testFixturePath("refresh")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Chdir(cwd)
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
state := testState()
|
2014-07-11 23:56:43 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2014-07-11 23:56:43 -05:00
|
|
|
c := &RefreshCommand{
|
2014-07-12 22:37:30 -05:00
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2014-07-12 22:37:30 -05:00
|
|
|
},
|
2014-07-11 23:56:43 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-09-30 10:01:59 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-09-30 10:01:59 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
2014-07-11 23:56:43 -05:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-11 23:56:43 -05:00
|
|
|
}
|
|
|
|
|
2018-09-30 10:01:59 -05:00
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
2014-07-11 23:56:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:33:55 -05:00
|
|
|
newStateFile, err := statefile.Read(f)
|
2014-07-11 23:56:43 -05:00
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-10-14 17:33:55 -05:00
|
|
|
actual := strings.TrimSpace(newStateFile.State.String())
|
2014-09-18 12:40:23 -05:00
|
|
|
expected := strings.TrimSpace(testRefreshCwdStr)
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad:\n\n%s", actual)
|
2014-07-11 23:56:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRefresh_defaultState(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-14 18:07:42 -05:00
|
|
|
originalState := testState()
|
|
|
|
|
|
|
|
// Write the state file in a temporary directory with the
|
|
|
|
// default filename.
|
|
|
|
statePath := testStateFile(t, originalState)
|
|
|
|
|
|
|
|
localState := statemgr.NewFilesystem(statePath)
|
2023-11-08 15:09:14 -06:00
|
|
|
if err := localState.RefreshState(); err != nil {
|
2018-10-14 18:07:42 -05:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
s := localState.State()
|
|
|
|
if s == nil {
|
|
|
|
t.Fatal("empty test state")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change to that 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 := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2018-10-14 18:07:42 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2018-10-14 18:07:42 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-10-14 18:07:42 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-10-14 18:07:42 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !p.ReadResourceCalled {
|
|
|
|
t.Fatal("ReadResource should have been called")
|
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
2020-03-23 14:26:18 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
backupState := testStateRead(t, statePath+DefaultBackupExtension)
|
|
|
|
|
|
|
|
actual = backupState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected = originalState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
2016-03-11 13:07:54 -06:00
|
|
|
}
|
|
|
|
|
2014-06-27 13:12:45 -05:00
|
|
|
func TestRefresh_outPath(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-14 18:07:42 -05:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
2023-09-07 11:53:12 -05:00
|
|
|
outf, err := os.CreateTemp(td, "tf")
|
2018-10-14 18:07:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2018-10-14 18:07:42 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2018-10-14 18:07:42 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-10-14 18:07:42 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-10-14 18:07:42 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
|
|
|
if !reflect.DeepEqual(newState, state) {
|
|
|
|
t.Fatalf("bad: %#v", newState)
|
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
2020-03-23 14:26:18 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
2018-11-16 20:37:26 -06:00
|
|
|
if _, err := os.Stat(outPath + DefaultBackupExtension); !os.IsNotExist(err) {
|
2018-11-16 18:09:25 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to test for backup file: %s", err)
|
|
|
|
}
|
|
|
|
t.Fatalf("backup file exists, but it should not because output file did not initially exist")
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
2014-06-27 13:12:45 -05:00
|
|
|
}
|
2014-07-18 13:37:27 -05:00
|
|
|
|
|
|
|
func TestRefresh_var(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-var"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
state := testState()
|
2014-07-18 13:37:27 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2014-07-18 13:37:27 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2014-07-18 13:37:27 -05:00
|
|
|
},
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshVarFixtureSchema()
|
2014-07-18 13:37:27 -05:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var", "foo=bar",
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-18 13:37:27 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
if !p.ConfigureProviderCalled {
|
2014-07-18 13:37:27 -05:00
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
if got, want := p.ConfigureProviderRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
2018-09-30 10:01:59 -05:00
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-07-18 13:37:27 -05:00
|
|
|
}
|
|
|
|
}
|
2014-07-18 16:00:40 -05:00
|
|
|
|
|
|
|
func TestRefresh_varFile(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-var"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
state := testState()
|
2014-07-18 16:00:40 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2014-07-18 16:00:40 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2014-07-18 16:00:40 -05:00
|
|
|
},
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshVarFixtureSchema()
|
2014-07-18 16:00:40 -05:00
|
|
|
|
|
|
|
varFilePath := testTempFile(t)
|
2023-09-07 11:53:12 -05:00
|
|
|
if err := os.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
|
2014-07-18 16:00:40 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-var-file", varFilePath,
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-07-18 16:00:40 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
if !p.ConfigureProviderCalled {
|
2014-07-18 16:00:40 -05:00
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
if got, want := p.ConfigureProviderRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
2018-09-30 10:01:59 -05:00
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-07-18 16:00:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 11:32:01 -05:00
|
|
|
func TestRefresh_varFileDefault(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-var"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2014-09-17 13:15:07 -05:00
|
|
|
state := testState()
|
2014-08-05 11:32:01 -05:00
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2014-08-05 11:32:01 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2014-08-05 11:32:01 -05:00
|
|
|
},
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshVarFixtureSchema()
|
2014-08-05 11:32:01 -05:00
|
|
|
|
2021-02-02 09:35:45 -06:00
|
|
|
varFilePath := filepath.Join(td, "terraform.tfvars")
|
2023-09-07 11:53:12 -05:00
|
|
|
if err := os.WriteFile(varFilePath, []byte(refreshVarFile), 0644); err != nil {
|
2014-08-05 11:32:01 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2014-08-05 11:32:01 -05:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
if !p.ConfigureProviderCalled {
|
2014-08-05 11:32:01 -05:00
|
|
|
t.Fatal("configure should be called")
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
if got, want := p.ConfigureProviderRequest.Config.GetAttr("value"), cty.StringVal("bar"); !want.RawEquals(got) {
|
2018-09-30 10:01:59 -05:00
|
|
|
t.Fatalf("wrong provider configuration\ngot: %#v\nwant: %#v", got, want)
|
2014-08-05 11:32:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-23 02:32:46 -06:00
|
|
|
func TestRefresh_varsUnset(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-unset-var"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2015-11-23 02:32:46 -06:00
|
|
|
// Disable test mode so input would be asked
|
|
|
|
test = false
|
|
|
|
defer func() { test = true }()
|
|
|
|
|
|
|
|
defaultInputReader = bytes.NewBufferString("bar\n")
|
|
|
|
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
ui := new(cli.MockUi)
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2015-11-23 02:32:46 -06:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
Ui: ui,
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2015-11-23 02:32:46 -06:00
|
|
|
},
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 15:13:10 -06:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:33:55 -05:00
|
|
|
"test_instance": {
|
2021-01-12 15:13:10 -06:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 17:33:55 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-11-23 02:32:46 -06:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2015-11-23 02:32:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 21:58:12 -05:00
|
|
|
func TestRefresh_backup(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-14 18:07:42 -05:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
2023-09-07 11:53:12 -05:00
|
|
|
outf, err := os.CreateTemp(td, "tf")
|
2018-10-14 18:07:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
2018-11-16 18:37:02 -06:00
|
|
|
defer outf.Close()
|
|
|
|
|
|
|
|
// Need to put some state content in the output file so that there's
|
|
|
|
// something to back up.
|
|
|
|
err = statefile.Write(statefile.New(state, "baz", 0), outf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error writing initial output state file %s", err)
|
|
|
|
}
|
2018-10-14 18:07:42 -05:00
|
|
|
|
|
|
|
// Backup path
|
2023-09-07 11:53:12 -05:00
|
|
|
backupf, err := os.CreateTemp(td, "tf")
|
2018-10-14 18:07:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
backupPath := backupf.Name()
|
|
|
|
backupf.Close()
|
|
|
|
os.Remove(backupPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2018-10-14 18:07:42 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2018-10-14 18:07:42 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-10-14 18:07:42 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-10-14 18:07:42 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("changed"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", backupPath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
2019-06-04 09:32:12 -05:00
|
|
|
if !cmp.Equal(newState, state, cmpopts.EquateEmpty()) {
|
|
|
|
t.Fatalf("got:\n%s\nexpected:\n%s\n", newState, state)
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"changed\"\n }"),
|
2020-03-23 14:26:18 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
2018-11-08 17:05:49 -06:00
|
|
|
backupState := testStateRead(t, backupPath)
|
2018-10-14 18:07:42 -05:00
|
|
|
actualStr := strings.TrimSpace(backupState.String())
|
|
|
|
expectedStr := strings.TrimSpace(state.String())
|
|
|
|
if actualStr != expectedStr {
|
|
|
|
t.Fatalf("bad:\n\n%s\n\n%s", actualStr, expectedStr)
|
|
|
|
}
|
2014-07-27 21:58:12 -05:00
|
|
|
}
|
|
|
|
|
2014-07-27 22:38:41 -05:00
|
|
|
func TestRefresh_disableBackup(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2018-10-14 18:07:42 -05:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
// Output path
|
2023-09-07 11:53:12 -05:00
|
|
|
outf, err := os.CreateTemp(td, "tf")
|
2018-10-14 18:07:42 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
outPath := outf.Name()
|
|
|
|
outf.Close()
|
|
|
|
os.Remove(outPath)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2018-10-14 18:07:42 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2018-10-14 18:07:42 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
2018-10-14 18:07:42 -05:00
|
|
|
p.ReadResourceFn = nil
|
2021-01-12 15:13:10 -06:00
|
|
|
p.ReadResourceResponse = &providers.ReadResourceResponse{
|
2018-10-14 18:07:42 -05:00
|
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"id": cty.StringVal("yes"),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
"-state-out", outPath,
|
|
|
|
"-backup", "-",
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newState := testStateRead(t, statePath)
|
2019-10-23 21:32:44 -05:00
|
|
|
if !cmp.Equal(state, newState, equateEmpty) {
|
|
|
|
spew.Config.DisableMethods = true
|
|
|
|
fmt.Println(cmp.Diff(state, newState, equateEmpty))
|
|
|
|
t.Fatalf("bad: %s", newState)
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
newState = testStateRead(t, outPath)
|
|
|
|
actual := newState.RootModule().Resources["test_instance.foo"].Instances[addrs.NoKey].Current
|
|
|
|
expected := &states.ResourceInstanceObjectSrc{
|
|
|
|
Status: states.ObjectReady,
|
|
|
|
AttrsJSON: []byte("{\n \"ami\": null,\n \"id\": \"yes\"\n }"),
|
2020-03-23 14:26:18 -05:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2018-10-14 18:07:42 -05:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Fatalf("wrong new object\ngot: %swant: %s", spew.Sdump(actual), spew.Sdump(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure there is no backup
|
|
|
|
_, err = os.Stat(outPath + DefaultBackupExtension)
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
|
|
|
_, err = os.Stat("-")
|
|
|
|
if err == nil || !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("backup should not exist")
|
|
|
|
}
|
2014-07-27 22:38:41 -05:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:40:56 -05:00
|
|
|
func TestRefresh_displaysOutputs(t *testing.T) {
|
2021-02-02 09:35:45 -06:00
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-02 09:35:45 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-output"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
2015-06-15 10:40:56 -05:00
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
view, done := testView(t)
|
2015-06-15 10:40:56 -05:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
2017-04-13 20:05:58 -05:00
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2015-06-15 10:40:56 -05:00
|
|
|
},
|
|
|
|
}
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-01-12 15:13:10 -06:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:33:55 -05:00
|
|
|
"test_instance": {
|
2021-01-12 15:13:10 -06:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 17:33:55 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-06-15 10:40:56 -05:00
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2015-06-15 10:40:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that outputs were displayed
|
|
|
|
outputValue := "foo.example.com"
|
2021-02-22 10:38:39 -06:00
|
|
|
actual := output.Stdout()
|
2015-06-15 10:40:56 -05:00
|
|
|
if !strings.Contains(actual, outputValue) {
|
|
|
|
t.Fatalf("Expected:\n%s\n\nTo include: %q", actual, outputValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 12:29:42 -06:00
|
|
|
// Config with multiple resources, targeting refresh of a subset
|
|
|
|
func TestRefresh_targeted(t *testing.T) {
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-02-08 12:29:42 -06:00
|
|
|
testCopyDir(t, testFixturePath("refresh-targeted"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
state := testState()
|
|
|
|
statePath := testStateFile(t, state)
|
|
|
|
|
|
|
|
p := testProvider()
|
2021-02-18 09:13:43 -06:00
|
|
|
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
|
2021-02-08 12:29:42 -06:00
|
|
|
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-11 19:52:10 -06:00
|
|
|
view, done := testView(t)
|
2021-02-08 12:29:42 -06:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2021-02-08 12:29:42 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-target", "test_instance.foo",
|
|
|
|
"-state", statePath,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2021-02-08 12:29:42 -06:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
got := output.Stdout()
|
2021-02-08 12:29:42 -06:00
|
|
|
if want := "test_instance.foo: Refreshing"; !strings.Contains(got, want) {
|
|
|
|
t.Fatalf("expected output to contain %q, got:\n%s", want, got)
|
|
|
|
}
|
|
|
|
if doNotWant := "test_instance.bar: Refreshing"; strings.Contains(got, doNotWant) {
|
|
|
|
t.Fatalf("expected output not to contain %q, got:\n%s", doNotWant, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Diagnostics for invalid -target flags
|
|
|
|
func TestRefresh_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-22 10:38:39 -06:00
|
|
|
view, done := testView(t)
|
2021-02-08 12:29:42 -06:00
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 14:51:40 -06:00
|
|
|
View: view,
|
2021-02-08 12:29:42 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-target", target,
|
|
|
|
}
|
2021-02-22 10:38:39 -06:00
|
|
|
code := c.Run(args)
|
|
|
|
output := done(t)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
2021-02-08 12:29:42 -06:00
|
|
|
}
|
|
|
|
|
2021-02-22 10:38:39 -06:00
|
|
|
got := output.Stderr()
|
2021-02-08 12:29:42 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:13:20 -05:00
|
|
|
func TestRefresh_warnings(t *testing.T) {
|
|
|
|
// Create a temporary working directory that is empty
|
2022-04-08 11:34:16 -05:00
|
|
|
td := t.TempDir()
|
2021-05-05 13:13:20 -05:00
|
|
|
testCopyDir(t, testFixturePath("apply"), td)
|
|
|
|
defer testChdir(t, td)()
|
|
|
|
|
|
|
|
p := testProvider()
|
|
|
|
p.GetProviderSchemaResponse = refreshFixtureSchema()
|
|
|
|
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"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("full warnings", func(t *testing.T) {
|
|
|
|
view, done := testView(t)
|
|
|
|
c := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run([]string{})
|
|
|
|
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 := &RefreshCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
View: view,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run([]string{"-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",
|
2023-09-21 07:38:46 -05:00
|
|
|
"To see the full warning notes, run OpenTofu without -compact-warnings.",
|
2021-05-05 13:13:20 -05:00
|
|
|
}
|
|
|
|
for _, want := range wantWarnings {
|
|
|
|
if !strings.Contains(output.Stdout(), want) {
|
|
|
|
t.Errorf("missing warning %s", want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-06-30 02:38:36 -05:00
|
|
|
// configuration in testdata/refresh . This schema should be
|
2018-10-14 17:33:55 -05:00
|
|
|
// assigned to a mock provider named "test".
|
2021-02-18 09:13:43 -06:00
|
|
|
func refreshFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
2021-01-12 15:13:10 -06:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:33:55 -05:00
|
|
|
"test_instance": {
|
2021-01-12 15:13:10 -06:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
"ami": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 17:33:55 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// refreshVarFixtureSchema returns a schema suitable for processing the
|
2019-06-30 02:38:36 -05:00
|
|
|
// configuration in testdata/refresh-var . This schema should be
|
2018-10-14 17:33:55 -05:00
|
|
|
// assigned to a mock provider named "test".
|
2021-02-18 09:13:43 -06:00
|
|
|
func refreshVarFixtureSchema() *providers.GetProviderSchemaResponse {
|
|
|
|
return &providers.GetProviderSchemaResponse{
|
2021-01-12 15:13:10 -06:00
|
|
|
Provider: providers.Schema{
|
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"value": {Type: cty.String, Optional: true},
|
|
|
|
},
|
2018-10-14 17:33:55 -05:00
|
|
|
},
|
|
|
|
},
|
2021-01-12 15:13:10 -06:00
|
|
|
ResourceTypes: map[string]providers.Schema{
|
2018-10-14 17:33:55 -05:00
|
|
|
"test_instance": {
|
2021-01-12 15:13:10 -06:00
|
|
|
Block: &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
|
|
},
|
2018-10-14 17:33:55 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-18 16:00:40 -05:00
|
|
|
const refreshVarFile = `
|
|
|
|
foo = "bar"
|
|
|
|
`
|
2014-09-18 12:40:23 -05:00
|
|
|
|
|
|
|
const testRefreshStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2023-12-11 14:10:03 -06:00
|
|
|
provider = provider["registry.opentofu.org/hashicorp/test"]
|
2014-09-18 12:40:23 -05:00
|
|
|
`
|
|
|
|
const testRefreshCwdStr = `
|
|
|
|
test_instance.foo:
|
|
|
|
ID = yes
|
2023-12-11 14:10:03 -06:00
|
|
|
provider = provider["registry.opentofu.org/hashicorp/test"]
|
2014-09-18 12:40:23 -05:00
|
|
|
`
|