2024-02-08 04:48:59 -05:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 11:33:06 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2014-06-19 00:36:44 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
2025-09-06 09:47:17 -04:00
|
|
|
"bufio"
|
2017-01-18 23:50:45 -05:00
|
|
|
"bytes"
|
2021-11-01 16:09:16 -04:00
|
|
|
"context"
|
2017-01-18 23:50:45 -05:00
|
|
|
"crypto/md5"
|
|
|
|
|
"encoding/base64"
|
|
|
|
|
"encoding/json"
|
2017-02-03 15:32:40 -05:00
|
|
|
"fmt"
|
2016-11-30 14:36:54 -05:00
|
|
|
"io"
|
2023-09-07 12:53:12 -04:00
|
|
|
"io/fs"
|
2017-01-18 23:50:45 -05:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2014-06-27 17:43:23 -04:00
|
|
|
"os"
|
2017-02-03 15:32:40 -05:00
|
|
|
"os/exec"
|
2022-07-12 17:00:36 -04:00
|
|
|
"path"
|
2014-06-19 00:36:44 -04:00
|
|
|
"path/filepath"
|
2025-08-28 18:55:45 -04:00
|
|
|
"runtime"
|
2015-02-26 13:29:23 -05:00
|
|
|
"strings"
|
2014-06-27 17:43:23 -04:00
|
|
|
"testing"
|
2014-06-19 00:36:44 -04:00
|
|
|
|
2022-08-29 15:29:07 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2026-03-31 07:22:31 -04:00
|
|
|
"github.com/mitchellh/cli"
|
2026-03-02 11:58:11 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/command/arguments"
|
2025-05-14 20:19:22 -04:00
|
|
|
"github.com/opentofu/svchost"
|
|
|
|
|
"github.com/opentofu/svchost/disco"
|
2023-07-19 04:07:46 -04:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/addrs"
|
|
|
|
|
backendInit "github.com/opentofu/opentofu/internal/backend/init"
|
|
|
|
|
backendLocal "github.com/opentofu/opentofu/internal/backend/local"
|
2026-03-19 11:03:16 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/clistate"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/command/views"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/command/workdir"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configload"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/configs/configschema"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/copy"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/depsfile"
|
2024-03-04 09:00:29 -05:00
|
|
|
"github.com/opentofu/opentofu/internal/encryption"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/getproviders"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/initwd"
|
|
|
|
|
_ "github.com/opentofu/opentofu/internal/logging"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/plans"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/plans/planfile"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/providers"
|
|
|
|
|
"github.com/opentofu/opentofu/internal/registry"
|
|
|
|
|
"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/terminal"
|
2023-09-20 08:16:53 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/tofu"
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/version"
|
2014-06-19 00:36:44 -04:00
|
|
|
)
|
|
|
|
|
|
2018-11-20 03:58:59 -05:00
|
|
|
// These are the directories for our test data and fixtures.
|
|
|
|
|
var (
|
2019-06-30 03:38:36 -04:00
|
|
|
fixtureDir = "./testdata"
|
2018-11-20 03:58:59 -05:00
|
|
|
testDataDir = "./testdata"
|
|
|
|
|
)
|
2014-07-12 00:03:56 -04:00
|
|
|
|
|
|
|
|
func init() {
|
2014-09-29 14:24:16 -04:00
|
|
|
test = true
|
|
|
|
|
|
2018-10-31 11:45:03 -04:00
|
|
|
// Initialize the backends
|
|
|
|
|
backendInit.Init(nil)
|
|
|
|
|
|
2018-11-20 03:58:59 -05:00
|
|
|
// Expand the data and fixture dirs on init because
|
|
|
|
|
// we change the working directory in some tests.
|
2014-07-12 00:03:56 -04:00
|
|
|
var err error
|
|
|
|
|
fixtureDir, err = filepath.Abs(fixtureDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2018-03-28 13:08:38 -04:00
|
|
|
|
2018-11-20 03:58:59 -05:00
|
|
|
testDataDir, err = filepath.Abs(testDataDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2014-07-12 00:03:56 -04:00
|
|
|
}
|
2014-06-19 00:36:44 -04:00
|
|
|
|
2016-08-01 17:16:22 -04:00
|
|
|
func TestMain(m *testing.M) {
|
2018-10-16 22:48:28 -04:00
|
|
|
// Make sure backend init is initialized, since our tests tend to assume it.
|
2018-10-31 11:45:03 -04:00
|
|
|
backendInit.Init(nil)
|
2018-10-16 22:48:28 -04:00
|
|
|
|
2016-08-01 17:16:22 -04:00
|
|
|
os.Exit(m.Run())
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 20:01:44 -04:00
|
|
|
// tempWorkingDir constructs a workdir.Dir object referring to a newly-created
|
2022-04-08 12:34:16 -04:00
|
|
|
// temporary directory. The temporary directory is automatically removed when
|
2022-08-17 14:46:02 -04:00
|
|
|
// the test and all its subtests complete.
|
2021-09-01 20:01:44 -04:00
|
|
|
//
|
|
|
|
|
// Although workdir.Dir is built to support arbitrary base directories, the
|
|
|
|
|
// not-yet-migrated behaviors in command.Meta tend to expect the root module
|
|
|
|
|
// directory to be the real process working directory, and so if you intend
|
|
|
|
|
// to use the result inside a command.Meta object you must use a pattern
|
|
|
|
|
// similar to the following when initializing your test:
|
|
|
|
|
//
|
2022-08-17 14:46:02 -04:00
|
|
|
// wd := tempWorkingDir(t)
|
|
|
|
|
// defer testChdir(t, wd.RootModuleDir())()
|
2021-09-01 20:01:44 -04:00
|
|
|
//
|
|
|
|
|
// Note that testChdir modifies global state for the test process, and so a
|
|
|
|
|
// test using this pattern must never call t.Parallel().
|
2022-04-08 12:34:16 -04:00
|
|
|
func tempWorkingDir(t *testing.T) *workdir.Dir {
|
2021-09-01 20:01:44 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2022-04-08 12:34:16 -04:00
|
|
|
dirPath := t.TempDir()
|
2021-09-01 20:01:44 -04:00
|
|
|
t.Logf("temporary directory %s", dirPath)
|
|
|
|
|
|
2022-04-08 12:34:16 -04:00
|
|
|
return workdir.NewDir(dirPath)
|
2021-09-01 20:01:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// tempWorkingDirFixture is like tempWorkingDir but it also copies the content
|
|
|
|
|
// from a fixture directory into the temporary directory before returning it.
|
|
|
|
|
//
|
|
|
|
|
// The same caveats about working directory apply as for testWorkingDir. See
|
|
|
|
|
// the testWorkingDir commentary for an example of how to use this function
|
|
|
|
|
// along with testChdir to meet the expectations of command.Meta legacy
|
|
|
|
|
// functionality.
|
2021-09-13 13:22:36 -04:00
|
|
|
func tempWorkingDirFixture(t *testing.T, fixtureName string) *workdir.Dir {
|
2021-09-01 20:01:44 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2025-04-23 07:48:41 -04:00
|
|
|
dirPath := testTempDirRealpath(t)
|
2021-09-01 20:01:44 -04:00
|
|
|
t.Logf("temporary directory %s with fixture %q", dirPath, fixtureName)
|
|
|
|
|
|
|
|
|
|
fixturePath := testFixturePath(fixtureName)
|
|
|
|
|
testCopyDir(t, fixturePath, dirPath)
|
|
|
|
|
// NOTE: Unfortunately because testCopyDir immediately aborts the test
|
|
|
|
|
// on failure, a failure to copy will prevent us from cleaning up the
|
|
|
|
|
// temporary directory. Oh well. :(
|
|
|
|
|
|
2021-09-13 13:22:36 -04:00
|
|
|
return workdir.NewDir(dirPath)
|
2021-09-01 20:01:44 -04:00
|
|
|
}
|
|
|
|
|
|
2014-06-19 00:36:44 -04:00
|
|
|
func testFixturePath(name string) string {
|
2014-07-11 23:38:03 -04:00
|
|
|
return filepath.Join(fixtureDir, name)
|
2014-06-19 00:36:44 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-29 12:34:23 -04:00
|
|
|
func metaOverridesForProvider(p providers.Interface) *testingOverrides {
|
2017-04-13 21:05:58 -04:00
|
|
|
return &testingOverrides{
|
2020-03-30 18:30:56 -04:00
|
|
|
Providers: map[addrs.Provider]providers.Factory{
|
2023-12-11 15:10:03 -05:00
|
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(p),
|
|
|
|
|
addrs.NewProvider(addrs.DefaultProviderRegistryHost, "hashicorp2", "test"): providers.FactoryFixed(p),
|
2020-03-30 18:30:56 -04:00
|
|
|
},
|
2014-06-19 00:36:44 -04: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 17:24:45 -04:00
|
|
|
func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *configload.Snapshot) {
|
|
|
|
|
t.Helper()
|
2017-08-25 19:23:47 -04:00
|
|
|
|
2018-05-22 22:33:45 -04:00
|
|
|
dir := filepath.Join(fixtureDir, name)
|
2025-04-23 07:48:41 -04:00
|
|
|
loader := configload.NewLoaderForTests(t)
|
2018-05-22 22:33:45 -04:00
|
|
|
|
|
|
|
|
// Test modules usually do not refer to remote sources, and for local
|
|
|
|
|
// sources only this ultimately just records all of the module paths
|
|
|
|
|
// in a JSON file so that we can load them below.
|
2025-05-09 05:16:38 -04:00
|
|
|
inst := initwd.NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(t.Context(), nil, nil), nil)
|
2024-06-24 09:13:07 -04:00
|
|
|
_, instDiags := inst.InstallModules(context.Background(), dir, "tests", true, false, initwd.ModuleInstallHooksImpl{}, configs.RootModuleCallForTesting())
|
2019-01-08 21:39:14 -05:00
|
|
|
if instDiags.HasErrors() {
|
|
|
|
|
t.Fatal(instDiags.Err())
|
2014-09-24 18:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 09:15:03 -04:00
|
|
|
config, snap, diags := loader.LoadConfigWithSnapshot(t.Context(), dir, configs.RootModuleCallForTesting())
|
2018-05-22 22:33:45 -04:00
|
|
|
if diags.HasErrors() {
|
|
|
|
|
t.Fatal(diags.Error())
|
2014-09-24 18:48:46 -04: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 17:24:45 -04:00
|
|
|
return config, snap
|
2014-09-24 18:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-28 20:51:05 -04:00
|
|
|
// testPlan returns a non-nil noop plan.
|
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 17:24:45 -04:00
|
|
|
func testPlan(t *testing.T) *plans.Plan {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
2018-10-11 20:58:46 -04:00
|
|
|
|
|
|
|
|
// This is what an empty configuration block would look like after being
|
|
|
|
|
// decoded with the schema of the "local" backend.
|
|
|
|
|
backendConfig := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
|
"path": cty.NullVal(cty.String),
|
|
|
|
|
"workspace_dir": cty.NullVal(cty.String),
|
|
|
|
|
})
|
|
|
|
|
backendConfigRaw, err := plans.NewDynamicValue(backendConfig, backendConfig.Type())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
return &plans.Plan{
|
2018-10-11 20:58:46 -04:00
|
|
|
Backend: plans.Backend{
|
|
|
|
|
// This is just a placeholder so that the plan file can be written
|
|
|
|
|
// out. Caller may wish to override it to something more "real"
|
|
|
|
|
// where the plan will actually be subsequently applied.
|
|
|
|
|
Type: "local",
|
|
|
|
|
Config: backendConfigRaw,
|
|
|
|
|
},
|
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 17:24:45 -04:00
|
|
|
Changes: plans.NewChanges(),
|
2016-10-28 20:51:05 -04: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 17:24:45 -04:00
|
|
|
func testPlanFile(t *testing.T, configSnap *configload.Snapshot, state *states.State, plan *plans.Plan) string {
|
2021-10-13 17:28:14 -04:00
|
|
|
return testPlanFileMatchState(t, configSnap, state, plan, statemgr.SnapshotMeta{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testPlanFileMatchState(t *testing.T, configSnap *configload.Snapshot, state *states.State, plan *plans.Plan, stateMeta statemgr.SnapshotMeta) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
stateFile := &statefile.File{
|
2021-10-13 17:28:14 -04:00
|
|
|
Lineage: stateMeta.Lineage,
|
|
|
|
|
Serial: stateMeta.Serial,
|
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 17:24:45 -04:00
|
|
|
State: state,
|
|
|
|
|
TerraformVersion: version.SemVer,
|
2014-06-27 17:43:23 -04:00
|
|
|
}
|
2021-05-04 18:59:58 -04:00
|
|
|
prevStateFile := &statefile.File{
|
2021-10-13 17:28:14 -04:00
|
|
|
Lineage: stateMeta.Lineage,
|
|
|
|
|
Serial: stateMeta.Serial,
|
2021-05-04 18:59:58 -04:00
|
|
|
State: state, // we just assume no changes detected during refresh
|
|
|
|
|
TerraformVersion: version.SemVer,
|
|
|
|
|
}
|
2014-06-27 17:43:23 -04: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 17:24:45 -04:00
|
|
|
path := testTempFile(t)
|
2021-09-29 19:03:10 -04:00
|
|
|
err := planfile.Create(path, planfile.CreateArgs{
|
|
|
|
|
ConfigSnapshot: configSnap,
|
|
|
|
|
PreviousRunStateFile: prevStateFile,
|
|
|
|
|
StateFile: stateFile,
|
|
|
|
|
Plan: plan,
|
backend/local: Check dependency lock consistency before any operations
In historical versions of Terraform the responsibility to check this was
inside the terraform.NewContext function, along with various other
assorted concerns that made that function particularly complicated.
More recently, we reduced the responsibility of the "terraform" package
only to instantiating particular named plugins, assuming that its caller
is responsible for selecting appropriate versions of any providers that
_are_ external. However, until this commit we were just assuming that
"terraform init" had correctly selected appropriate plugins and recorded
them in the lock file, and so nothing was dealing with the problem of
ensuring that there haven't been any changes to the lock file or config
since the most recent "terraform init" which would cause us to need to
re-evaluate those decisions.
Part of the game here is to slightly extend the role of the dependency
locks object to also carry information about a subset of provider
addresses whose lock entries we're intentionally disregarding as part of
the various little edge-case features we have for overridding providers:
dev_overrides, "unmanaged providers", and the testing overrides in our
own unit tests. This is an in-memory-only annotation, never included in
the serialized plan files on disk.
I had originally intended to create a new package to encapsulate all of
this plugin-selection logic, including both the version constraint
checking here and also the handling of the provider factory functions, but
as an interim step I've just made version constraint consistency checks
the responsibility of the backend/local package, which means that we'll
always catch problems as part of preparing for local operations, while
not imposing these additional checks on commands that _don't_ run local
operations, such as "terraform apply" when in remote operations mode.
2021-09-29 20:31:43 -04:00
|
|
|
DependencyLocks: depsfile.NewLocks(),
|
2024-03-04 09:00:29 -05:00
|
|
|
}, encryption.PlanEncryptionDisabled())
|
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 17:24:45 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to create temporary plan file: %s", err)
|
2014-06-27 17:43:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
// testPlanFileNoop is a shortcut function that creates a plan file that
|
|
|
|
|
// represents no changes and returns its path. This is useful when a test
|
|
|
|
|
// just needs any plan file, and it doesn't matter what is inside it.
|
|
|
|
|
func testPlanFileNoop(t *testing.T) string {
|
|
|
|
|
snap := &configload.Snapshot{
|
|
|
|
|
Modules: map[string]*configload.SnapshotModule{
|
|
|
|
|
"": {
|
|
|
|
|
Dir: ".",
|
|
|
|
|
Files: map[string][]byte{
|
|
|
|
|
"main.tf": nil,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
state := states.NewState()
|
|
|
|
|
plan := testPlan(t)
|
|
|
|
|
return testPlanFile(t, snap, state, plan)
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-12 18:05:00 -04:00
|
|
|
func testFileEquals(t *testing.T, got, want string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
actual, err := os.ReadFile(got)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("error reading %s", got)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expected, err := os.ReadFile(want)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("error reading %s", want)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if diff := cmp.Diff(string(actual), string(expected)); len(diff) > 0 {
|
|
|
|
|
t.Fatalf("got:\n%s\nwant:\n%s\ndiff:\n%s", actual, expected, diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-14 12:21:31 -04:00
|
|
|
func testReadPlan(t *testing.T, path string) *plans.Plan {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2024-03-04 09:00:29 -05:00
|
|
|
f, err := planfile.Open(path, encryption.PlanEncryptionDisabled())
|
2014-07-01 12:12:05 -04:00
|
|
|
if err != nil {
|
2018-10-14 12:21:31 -04:00
|
|
|
t.Fatalf("error opening plan file %q: %s", path, err)
|
2014-07-01 12:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-14 12:21:31 -04:00
|
|
|
p, err := f.ReadPlan()
|
2014-07-01 12:12:05 -04:00
|
|
|
if err != nil {
|
2018-10-14 12:21:31 -04:00
|
|
|
t.Fatalf("error reading plan from plan file %q: %s", path, err)
|
2014-07-01 12:12:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-17 14:15:07 -04:00
|
|
|
// testState returns a test State structure that we use for a lot of tests.
|
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 17:24:45 -04:00
|
|
|
func testState() *states.State {
|
|
|
|
|
return 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-14 19:07:42 -04:00
|
|
|
// The weird whitespace here is reflective of how this would
|
|
|
|
|
// get written out in a real state file, due to the indentation
|
|
|
|
|
// of all of the containing wrapping objects and arrays.
|
2024-05-21 15:04:10 -04:00
|
|
|
AttrsJSON: []byte(`{"id":"bar"}`),
|
2019-10-30 15:59:34 -04:00
|
|
|
Status: states.ObjectReady,
|
2020-03-23 15:26:18 -04:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2014-09-17 14:15:07 -04:00
|
|
|
},
|
2020-02-13 15:32:58 -05:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 15:55:25 -04:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 14:19:52 -04:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 15:32:58 -05:00
|
|
|
},
|
2024-11-05 18:08:23 -05:00
|
|
|
addrs.NoKey,
|
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 17:24:45 -04:00
|
|
|
)
|
2019-06-04 10:32:12 -04:00
|
|
|
// DeepCopy is used here to ensure our synthetic state matches exactly
|
|
|
|
|
// with a state that will have been copied during the command
|
|
|
|
|
// operation, and all fields have been copied correctly.
|
|
|
|
|
}).DeepCopy()
|
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 17:24:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// writeStateForTesting is a helper that writes the given naked state to the
|
|
|
|
|
// given writer, generating a stub *statefile.File wrapper which is then
|
|
|
|
|
// immediately discarded.
|
|
|
|
|
func writeStateForTesting(state *states.State, w io.Writer) error {
|
|
|
|
|
sf := &statefile.File{
|
|
|
|
|
Serial: 0,
|
|
|
|
|
Lineage: "fake-for-testing",
|
|
|
|
|
State: state,
|
|
|
|
|
}
|
2024-03-04 09:25:14 -05:00
|
|
|
return statefile.Write(sf, w, encryption.StateEncryptionDisabled())
|
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 17:24:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testStateMgrCurrentLineage returns the current lineage for the given state
|
|
|
|
|
// manager, or the empty string if it does not use lineage. This is primarily
|
|
|
|
|
// for testing against the local backend, which always supports lineage.
|
|
|
|
|
func testStateMgrCurrentLineage(mgr statemgr.Persistent) string {
|
|
|
|
|
if pm, ok := mgr.(statemgr.PersistentMeta); ok {
|
|
|
|
|
m := pm.StateSnapshotMeta()
|
|
|
|
|
return m.Lineage
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// markStateForMatching is a helper that writes a specific marker value to
|
|
|
|
|
// a state so that it can be recognized later with getStateMatchingMarker.
|
|
|
|
|
//
|
|
|
|
|
// Internally this just sets a root module output value called "testing_mark"
|
|
|
|
|
// to the given string value. If the state is being checked in other ways,
|
|
|
|
|
// the test code may need to compensate for the addition or overwriting of this
|
|
|
|
|
// special output value name.
|
|
|
|
|
//
|
|
|
|
|
// The given mark string is returned verbatim, to allow the following pattern
|
|
|
|
|
// in tests:
|
|
|
|
|
//
|
2022-08-17 14:46:02 -04:00
|
|
|
// mark := markStateForMatching(state, "foo")
|
|
|
|
|
// // (do stuff to the state)
|
|
|
|
|
// assertStateHasMarker(state, mark)
|
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 17:24:45 -04:00
|
|
|
func markStateForMatching(state *states.State, mark string) string {
|
2025-04-25 05:26:28 -04:00
|
|
|
state.RootModule().SetOutputValue("testing_mark", cty.StringVal(mark), false, "")
|
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 17:24:45 -04:00
|
|
|
return mark
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getStateMatchingMarker is used with markStateForMatching to retrieve the
|
|
|
|
|
// mark string previously added to the given state. If no such mark is present,
|
|
|
|
|
// the result is an empty string.
|
|
|
|
|
func getStateMatchingMarker(state *states.State) string {
|
|
|
|
|
os := state.RootModule().OutputValues["testing_mark"]
|
|
|
|
|
if os == nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
v := os.Value
|
|
|
|
|
if v.Type() == cty.String && v.IsKnown() && !v.IsNull() {
|
|
|
|
|
return v.AsString()
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// stateHasMarker is a helper around getStateMatchingMarker that also includes
|
|
|
|
|
// the equality test, for more convenient use in test assertion branches.
|
|
|
|
|
func stateHasMarker(state *states.State, want string) bool {
|
|
|
|
|
return getStateMatchingMarker(state) == want
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assertStateHasMarker wraps stateHasMarker to automatically generate a
|
|
|
|
|
// fatal test result (i.e. t.Fatal) if the marker doesn't match.
|
|
|
|
|
func assertStateHasMarker(t *testing.T, state *states.State, want string) {
|
|
|
|
|
if !stateHasMarker(state, want) {
|
|
|
|
|
t.Fatalf("wrong state marker\ngot: %q\nwant: %q", getStateMatchingMarker(state), want)
|
2014-09-17 14:15:07 -04: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 17:24:45 -04:00
|
|
|
func testStateFile(t *testing.T, s *states.State) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2014-06-27 17:43:23 -04:00
|
|
|
path := testTempFile(t)
|
|
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
|
if err != nil {
|
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 17:24:45 -04:00
|
|
|
t.Fatalf("failed to create temporary state file %s: %s", path, err)
|
2014-06-27 17:43:23 -04:00
|
|
|
}
|
|
|
|
|
defer f.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 17:24:45 -04:00
|
|
|
err = writeStateForTesting(s, f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to write state to temporary file %s: %s", path, err)
|
2014-06-27 17:43:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-26 13:29:23 -05:00
|
|
|
// testStateFileDefault writes the state out to the default statefile
|
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
2020-10-05 08:33:49 -04:00
|
|
|
func testStateFileDefault(t *testing.T, s *states.State) {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2026-03-02 11:58:11 -05:00
|
|
|
f, err := os.Create(arguments.DefaultStateFilename)
|
2015-02-26 13:29:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2020-10-05 08:33:49 -04:00
|
|
|
if err := writeStateForTesting(s, f); err != nil {
|
2015-02-26 13:29:23 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 13:09:37 -04:00
|
|
|
// testStateFileWorkspaceDefault writes the state out to the default statefile
|
|
|
|
|
// for the given workspace in the cwd. Use `testCwd` to change into a temp cwd.
|
|
|
|
|
func testStateFileWorkspaceDefault(t *testing.T, workspace string, s *states.State) string {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
workspaceDir := filepath.Join(backendLocal.DefaultWorkspaceDir, workspace)
|
|
|
|
|
err := os.MkdirAll(workspaceDir, os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 11:58:11 -05:00
|
|
|
path := filepath.Join(workspaceDir, arguments.DefaultStateFilename)
|
2020-09-28 13:09:37 -04:00
|
|
|
f, err := os.Create(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
|
|
if err := writeStateForTesting(s, f); err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-05 17:55:15 -05:00
|
|
|
// testStateFileRemote writes the state out to the remote statefile
|
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
2026-03-19 11:03:16 -04:00
|
|
|
func testStateFileRemote(t *testing.T, s *clistate.CLIState) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2026-03-02 11:58:11 -05:00
|
|
|
path := filepath.Join(workdir.DefaultDataDir, arguments.DefaultStateFilename)
|
2015-03-05 17:55:15 -05:00
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2026-03-19 11:03:16 -04:00
|
|
|
if err := clistate.WriteState(s, f); err != nil {
|
2015-03-05 17:55:15 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// testStateRead reads the state from a file
|
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 17:24:45 -04:00
|
|
|
func testStateRead(t *testing.T, path string) *states.State {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2015-02-26 13:29:23 -05:00
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
2017-07-05 17:59:42 -04:00
|
|
|
defer f.Close()
|
2015-02-26 13:29:23 -05:00
|
|
|
|
2024-03-04 09:25:14 -05:00
|
|
|
sf, err := statefile.Read(f, encryption.StateEncryptionDisabled())
|
2015-02-26 13:29:23 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
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 17:24:45 -04:00
|
|
|
return sf.State
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testDataStateRead reads a "data state", which is a file format resembling
|
|
|
|
|
// our state format v3 that is used only to track current backend settings.
|
|
|
|
|
//
|
2026-03-19 11:03:16 -04:00
|
|
|
// This uses *clistate.CLIState which is the specialized type for
|
|
|
|
|
// tracking backend configuration.
|
|
|
|
|
func testDataStateRead(t *testing.T, path string) *clistate.CLIState {
|
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 17:24:45 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2026-03-19 11:03:16 -04:00
|
|
|
s, err := clistate.ReadState(f)
|
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 17:24:45 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testStateOutput tests that the state at the given path contains
|
|
|
|
|
// the expected state string.
|
|
|
|
|
func testStateOutput(t *testing.T, path string, expected string) {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
newState := testStateRead(t, path)
|
2015-02-26 13:29:23 -05:00
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
|
expected = strings.TrimSpace(expected)
|
|
|
|
|
if actual != expected {
|
2016-03-08 15:37:34 -05:00
|
|
|
t.Fatalf("expected:\n%s\nactual:\n%s", expected, actual)
|
2015-02-26 13:29:23 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 08:16:53 -04:00
|
|
|
func testProvider() *tofu.MockProvider {
|
|
|
|
|
p := new(tofu.MockProvider)
|
2020-09-21 14:48:21 -04:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
|
return resp
|
2018-09-29 12:34:23 -04:00
|
|
|
}
|
2020-09-21 14:48:21 -04:00
|
|
|
|
2018-09-29 12:34:23 -04:00
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
|
return providers.ReadResourceResponse{
|
|
|
|
|
NewState: req.PriorState,
|
|
|
|
|
}
|
2014-06-19 00:36:44 -04:00
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
2014-06-27 17:43:23 -04:00
|
|
|
|
|
|
|
|
func testTempFile(t *testing.T) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2025-04-23 07:48:41 -04:00
|
|
|
return filepath.Join(testTempDirRealpath(t), "state.tfstate")
|
2014-06-27 17:43:23 -04:00
|
|
|
}
|
2014-08-05 12:32:01 -04:00
|
|
|
|
2025-04-23 07:48:41 -04:00
|
|
|
// testTempDirRealpath is like [testing.T.TempDir] but takes the
|
|
|
|
|
// extra step of ensuring that the result is a path that does not
|
|
|
|
|
// include any symlinks.
|
|
|
|
|
func testTempDirRealpath(t *testing.T) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
2022-04-08 12:34:16 -04:00
|
|
|
d, err := filepath.EvalSymlinks(t.TempDir())
|
2019-12-06 10:56:47 -05:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2014-08-05 12:32:01 -04:00
|
|
|
return d
|
|
|
|
|
}
|
2014-10-08 15:08:35 -04:00
|
|
|
|
2025-04-23 07:48:41 -04:00
|
|
|
// testCwdTemp is used to change the current working directory into a temporary
|
2022-04-08 12:34:16 -04:00
|
|
|
// directory. The cleanup is performed automatically after the test and all its
|
|
|
|
|
// subtests complete.
|
2025-04-23 07:48:41 -04:00
|
|
|
func testCwdTemp(t testing.TB) string {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2022-04-08 12:34:16 -04:00
|
|
|
tmp := t.TempDir()
|
2025-04-23 07:48:41 -04:00
|
|
|
t.Chdir(tmp)
|
2022-04-08 12:34:16 -04:00
|
|
|
return tmp
|
2014-10-08 15:08:35 -04:00
|
|
|
}
|
2016-11-30 14:36:54 -05:00
|
|
|
|
|
|
|
|
// testStdinPipe changes os.Stdin to be a pipe that sends the data from
|
|
|
|
|
// the reader before closing the pipe.
|
|
|
|
|
//
|
|
|
|
|
// The returned function should be deferred to properly clean up and restore
|
|
|
|
|
// the original stdin.
|
|
|
|
|
func testStdinPipe(t *testing.T, src io.Reader) func() {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2016-11-30 14:36:54 -05:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Modify stdin to point to our new pipe
|
|
|
|
|
old := os.Stdin
|
|
|
|
|
os.Stdin = r
|
|
|
|
|
|
|
|
|
|
// Copy the data from the reader to the pipe
|
|
|
|
|
go func() {
|
|
|
|
|
defer w.Close()
|
2025-05-15 07:39:11 -04:00
|
|
|
if _, err := io.Copy(w, src); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2016-11-30 14:36:54 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
|
// Close our read end
|
|
|
|
|
r.Close()
|
|
|
|
|
|
|
|
|
|
// Reset stdin
|
|
|
|
|
os.Stdin = old
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// testInteractiveInput configures tests so that the answers given are sent
|
|
|
|
|
// in order to interactive prompts. The returned function must be called
|
|
|
|
|
// in a defer to clean up.
|
|
|
|
|
func testInteractiveInput(t *testing.T, answers []string) func() {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// Disable test mode so input is called
|
|
|
|
|
test = false
|
|
|
|
|
|
2021-01-26 14:39:11 -05:00
|
|
|
// Set up reader/writers
|
2017-01-18 23:50:45 -05:00
|
|
|
testInputResponse = answers
|
|
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
|
return func() {
|
|
|
|
|
test = true
|
|
|
|
|
testInputResponse = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-01 13:59:17 -05:00
|
|
|
// testInputMap configures tests so that the given answers are returned
|
|
|
|
|
// for calls to Input when the right question is asked. The key is the
|
|
|
|
|
// question "Id" that is used.
|
|
|
|
|
func testInputMap(t *testing.T, answers map[string]string) func() {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2017-03-01 13:59:17 -05:00
|
|
|
// Disable test mode so input is called
|
|
|
|
|
test = false
|
|
|
|
|
|
2021-01-26 14:39:11 -05:00
|
|
|
// Set up reader/writers
|
2017-03-01 13:59:17 -05:00
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
// Setup answers
|
|
|
|
|
testInputResponse = nil
|
|
|
|
|
testInputResponseMap = answers
|
|
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
|
return func() {
|
2021-09-22 16:55:56 -04:00
|
|
|
var unusedAnswers = testInputResponseMap
|
2021-09-21 23:00:32 -04:00
|
|
|
|
2021-09-22 16:55:56 -04:00
|
|
|
// First, clean up!
|
2017-03-01 13:59:17 -05:00
|
|
|
test = true
|
|
|
|
|
testInputResponseMap = nil
|
2021-09-22 16:55:56 -04:00
|
|
|
|
|
|
|
|
if len(unusedAnswers) > 0 {
|
|
|
|
|
t.Fatalf("expected no unused answers provided to command.testInputMap, got: %v", unusedAnswers)
|
|
|
|
|
}
|
2017-03-01 13:59:17 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// testBackendState is used to make a test HTTP server to test a configured
|
|
|
|
|
// backend. This returns the complete state that can be saved. Use
|
|
|
|
|
// `testStateFileRemote` to write the returned state.
|
2018-11-07 20:20:46 -05:00
|
|
|
//
|
|
|
|
|
// When using this function, the configuration fixture for the test must
|
|
|
|
|
// include an empty configuration block for the HTTP backend, like this:
|
|
|
|
|
//
|
2022-08-17 14:46:02 -04:00
|
|
|
// terraform {
|
|
|
|
|
// backend "http" {
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2018-11-07 20:20:46 -05:00
|
|
|
//
|
|
|
|
|
// If such a block isn't present, or if it isn't empty, then an error will
|
|
|
|
|
// be returned about the backend configuration having changed and that
|
2023-09-26 13:09:27 -04:00
|
|
|
// "tofu init" must be run, since the test backend config cache created
|
2018-11-07 20:20:46 -05:00
|
|
|
// by this function contains the hash for an empty configuration.
|
2026-03-19 11:03:16 -04:00
|
|
|
func testBackendState(t *testing.T, s *states.State, c int) (*clistate.CLIState, *httptest.Server) {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
var b64md5 string
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
|
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
|
if req.Method == "PUT" {
|
|
|
|
|
resp.WriteHeader(c)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if s == nil {
|
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.Header().Set("Content-MD5", b64md5)
|
2025-05-15 07:39:11 -04:00
|
|
|
if _, err := resp.Write(buf.Bytes()); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If a state was given, make sure we calculate the proper b64md5
|
|
|
|
|
if s != nil {
|
2024-03-04 09:25:14 -05:00
|
|
|
err := statefile.Write(&statefile.File{State: s}, buf, encryption.StateEncryptionDisabled())
|
2020-04-02 12:58:44 -04:00
|
|
|
if err != nil {
|
2017-01-18 23:50:45 -05:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
|
}
|
|
|
|
|
md5 := md5.Sum(buf.Bytes())
|
|
|
|
|
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
|
|
|
|
|
2018-11-07 20:20:46 -05:00
|
|
|
backendConfig := &configs.Backend{
|
|
|
|
|
Type: "http",
|
|
|
|
|
Config: configs.SynthBody("<testBackendState>", map[string]cty.Value{}),
|
2024-06-24 09:13:07 -04:00
|
|
|
Eval: configs.NewStaticEvaluator(nil, configs.RootModuleCallForTesting()),
|
2018-11-07 20:20:46 -05:00
|
|
|
}
|
backend+command: Alias names for backend types
This introduces the concept of "backend aliases", which are alternative
names that can be used to refer to a given backend.
Each backend type has one canonical name and zero or more alias names. The
"backend" block in the root module can specify either a canonical backend
type or an alias, but internally OpenTofu will always track the backend
type using its canonical name.
In particular, the following are all true when the configuration specifies
an alias instead of a canonical backend type:
- The "tofu init" output includes a brief extra message saying which
backend type OpenTofu actually used, because that is the name that we'd
prioritize in our documentation and so an operator can use the canonical
type to find the relevant docs when needed.
- The .terraform/terraform.tfstate file that tracks the working directory's
currently-initialized backend settings always uses the canonical backend
type, and so it's possible to freely switch between aliases and canonical
without "tofu init" thinking that a state migration might be needed.
- Plan files similarly use the canonical backend type to track which
backend was active when the plan was created, which doesn't have any
significant user-facing purpose, but is consistent with the previous
point since the settings in the plan file effectively substitute for
the .terraform/terraform.tfstate file when applying a saved plan.
- The terraform_remote_state data source in the provider
terraform.io/builtin/terraform accepts both canonical and alias in its
backend type argument, treating both as equivalent for the purpose of
fetching the state snapshot for the configured workspace.
The primary motivation for this new facility is to allow the planned
"oracle_oci" backend to have an alias "oci" to allow writing configurations
that are cross-compatible with HashiCorp Terraform, since that software
has chosen to have unqualified OCI mean Oracle's system, whereas OpenTofu
has previously established that unqualified OCI means "Open Container
Initiative" in our ecosystem.
In particular, this design makes it possible in principle to bring an
existing Terraform configuration specifying backend "oci" over to OpenTofu
without modifications, and then to optionally switch it to specifying
backend "oracle-oci" at a later time without a spurious prompt to migrate
state snapshots to the same physical location where they are already
stored.
This commit doesn't actually introduce any aliases and therefore doesn't
have any tests for the new mechanism because our backend system uses a
global table that isn't friendly to mocking for testing purposes. I've
tested this manually using a placeholder alias to have confidence that it
works, and I expect that a subsequent commit introducing the new
"oracle_oci" backend will also introduce its "oci" alias and will include
tests that cover use of the alias and migration from the alias to the
canonical name and vice-versa.
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-09-10 17:16:24 -04:00
|
|
|
httpBackendInit, _ := backendInit.Backend("http")
|
|
|
|
|
b := httpBackendInit(encryption.StateEncryptionDisabled())
|
2023-11-08 16:09:14 -05:00
|
|
|
configSchema := b.ConfigSchema()
|
2025-06-19 05:46:31 -04:00
|
|
|
hash, _ := backendConfig.Hash(t.Context(), configSchema)
|
2018-11-07 20:20:46 -05:00
|
|
|
|
2026-03-19 11:03:16 -04:00
|
|
|
state := clistate.NewState()
|
|
|
|
|
state.Backend = &clistate.BackendState{
|
2018-03-27 18:31:05 -04:00
|
|
|
Type: "http",
|
|
|
|
|
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
|
2018-12-18 19:06:49 -05:00
|
|
|
Hash: uint64(hash),
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return state, srv
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testRemoteState is used to make a test HTTP server to return a given
|
2026-03-19 11:03:16 -04:00
|
|
|
// state file that can be used for testing remote backend state.
|
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 17:24:45 -04:00
|
|
|
//
|
2026-03-19 11:03:16 -04:00
|
|
|
// The return values are a *clistate.CLIState instance that should be written
|
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 17:24:45 -04:00
|
|
|
// as the "data state" (really: backend state) and the server that the
|
|
|
|
|
// returned data state refers to.
|
2026-03-19 11:03:16 -04:00
|
|
|
func testRemoteState(t *testing.T, s *states.State, c int) (*clistate.CLIState, *httptest.Server) {
|
2017-08-25 19:23:47 -04:00
|
|
|
t.Helper()
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
var b64md5 string
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
|
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
|
if req.Method == "PUT" {
|
|
|
|
|
resp.WriteHeader(c)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if s == nil {
|
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.Header().Set("Content-MD5", b64md5)
|
2025-05-15 07:39:11 -04:00
|
|
|
if _, err := resp.Write(buf.Bytes()); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
2026-03-19 11:03:16 -04:00
|
|
|
retState := clistate.NewState()
|
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 17:24:45 -04:00
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
2026-03-19 11:03:16 -04:00
|
|
|
b := &clistate.BackendState{
|
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 17:24:45 -04:00
|
|
|
Type: "http",
|
|
|
|
|
}
|
2025-05-15 07:39:11 -04:00
|
|
|
if err := b.SetConfig(cty.ObjectVal(map[string]cty.Value{
|
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 17:24:45 -04:00
|
|
|
"address": cty.StringVal(srv.URL),
|
|
|
|
|
}), &configschema.Block{
|
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
|
"address": {
|
|
|
|
|
Type: cty.String,
|
|
|
|
|
Required: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-05-15 07:39:11 -04:00
|
|
|
}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
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 17:24:45 -04:00
|
|
|
retState.Backend = b
|
2017-01-18 23:50:45 -05:00
|
|
|
|
|
|
|
|
if s != nil {
|
2024-03-04 09:25:14 -05:00
|
|
|
err := statefile.Write(&statefile.File{State: s}, buf, encryption.StateEncryptionDisabled())
|
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 17:24:45 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to write initial state: %v", err)
|
2017-01-18 23:50:45 -05: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 17:24:45 -04:00
|
|
|
return retState, srv
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
2017-02-03 15:32:40 -05:00
|
|
|
|
2024-09-09 07:51:39 -04:00
|
|
|
// testLockState calls a separate process to the lock the state file at path.
|
2017-02-03 15:32:40 -05:00
|
|
|
// deferFunc should be called in the caller to properly unlock the file.
|
2024-09-09 07:51:39 -04:00
|
|
|
// Since many tests change the working directory, the sourceDir argument must be
|
2017-02-06 13:26:03 -05:00
|
|
|
// supplied to locate the statelocker.go source.
|
2022-04-08 12:34:16 -04:00
|
|
|
func testLockState(t *testing.T, sourceDir, path string) (func(), error) {
|
2017-02-03 15:56:19 -05:00
|
|
|
// build and run the binary ourselves so we can quickly terminate it for cleanup
|
2022-04-08 12:34:16 -04:00
|
|
|
buildDir := t.TempDir()
|
2017-02-03 15:56:19 -05:00
|
|
|
|
2017-02-06 13:26:03 -05:00
|
|
|
source := filepath.Join(sourceDir, "statelocker.go")
|
2017-02-03 15:56:19 -05:00
|
|
|
lockBin := filepath.Join(buildDir, "statelocker")
|
2017-02-06 13:26:03 -05:00
|
|
|
|
2025-09-06 09:47:17 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
lockBin = lockBin + ".exe"
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 17:56:19 -04:00
|
|
|
cmd := exec.Command("go", "build", "-o", lockBin, source)
|
2025-09-06 09:47:17 -04:00
|
|
|
|
2018-11-20 03:58:59 -05:00
|
|
|
cmd.Dir = filepath.Dir(sourceDir)
|
|
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
2017-02-03 15:56:19 -05:00
|
|
|
if err != nil {
|
2023-09-27 04:19:16 -04:00
|
|
|
return nil, fmt.Errorf("%w %s", err, out)
|
2017-02-03 15:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
locker := exec.Command(lockBin, path)
|
2025-09-06 09:47:17 -04:00
|
|
|
stdin, err := locker.StdinPipe()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
stdout, err := locker.StdoutPipe()
|
2017-02-03 15:32:40 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := locker.Start(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-09-06 09:47:17 -04:00
|
|
|
|
|
|
|
|
reader := bufio.NewReader(stdout)
|
|
|
|
|
|
|
|
|
|
// callback function to unlock the state file
|
|
|
|
|
cbFunc := func() {
|
|
|
|
|
stdin.Close()
|
|
|
|
|
stdout.Close()
|
|
|
|
|
|
2025-05-15 07:39:11 -04:00
|
|
|
_ = locker.Wait()
|
2025-09-06 09:47:17 -04:00
|
|
|
|
|
|
|
|
t.Logf("closed statelocker stdin and finished.")
|
2025-09-17 08:06:57 -04:00
|
|
|
|
|
|
|
|
// Trigger garbage collection to ensure that all open file handles are closed.
|
|
|
|
|
// This prevents TempDir RemoveAll cleanup errors on Windows.
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
runtime.GC()
|
|
|
|
|
}
|
2017-02-03 15:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// wait for the process to lock
|
2025-09-06 09:47:17 -04:00
|
|
|
buf, err := reader.ReadString('\n')
|
2017-02-03 15:32:40 -05:00
|
|
|
if err != nil {
|
2025-09-06 09:47:17 -04:00
|
|
|
return cbFunc, fmt.Errorf("read from statelocker returned: %w", err)
|
2017-02-03 15:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2025-09-06 09:47:17 -04:00
|
|
|
output := string(buf)
|
2017-02-15 11:53:19 -05:00
|
|
|
if !strings.HasPrefix(output, "LOCKID") {
|
2025-09-06 09:47:17 -04:00
|
|
|
return cbFunc, fmt.Errorf("statelocker wrote: %s", output)
|
2017-02-03 15:32:40 -05:00
|
|
|
}
|
2025-09-06 09:47:17 -04:00
|
|
|
t.Logf("statelocker locked %s", output)
|
|
|
|
|
return cbFunc, nil
|
2017-02-03 15:32:40 -05:00
|
|
|
}
|
2018-10-14 19:35:59 -04:00
|
|
|
|
2020-10-07 12:48:25 -04:00
|
|
|
// testCopyDir recursively copies a directory tree, attempting to preserve
|
2021-09-01 20:01:44 -04:00
|
|
|
// permissions. Source directory must exist, destination directory may exist
|
|
|
|
|
// but will be created if not; it should typically be a temporary directory,
|
|
|
|
|
// and thus already created using os.MkdirTemp or similar.
|
|
|
|
|
// Symlinks are ignored and skipped.
|
2020-10-07 12:48:25 -04:00
|
|
|
func testCopyDir(t *testing.T, src, dst string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
src = filepath.Clean(src)
|
|
|
|
|
dst = filepath.Clean(dst)
|
|
|
|
|
|
|
|
|
|
si, err := os.Stat(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !si.IsDir() {
|
|
|
|
|
t.Fatal("source is not a directory")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err = os.Stat(dst)
|
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll(dst, si.Mode())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 12:53:12 -04:00
|
|
|
entries, err := os.ReadDir(src)
|
2020-10-07 12:48:25 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
srcPath := filepath.Join(src, entry.Name())
|
|
|
|
|
dstPath := filepath.Join(dst, entry.Name())
|
|
|
|
|
|
|
|
|
|
// If the entry is a symlink, we copy the contents
|
2023-09-07 12:53:12 -04:00
|
|
|
for entry.Type()&os.ModeSymlink != 0 {
|
2020-10-07 12:48:25 -04:00
|
|
|
target, err := os.Readlink(srcPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 12:53:12 -04:00
|
|
|
fi, err := os.Stat(target)
|
2020-10-07 12:48:25 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2023-09-07 12:53:12 -04:00
|
|
|
entry = fs.FileInfoToDirEntry(fi)
|
2020-10-07 12:48:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if entry.IsDir() {
|
|
|
|
|
testCopyDir(t, srcPath, dstPath)
|
|
|
|
|
} else {
|
|
|
|
|
err = copy.CopyFile(srcPath, dstPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 18:55:45 -04:00
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
// Trigger garbage collection to ensure that all open file handles are closed.
|
|
|
|
|
// This prevents TempDir RemoveAll cleanup errors on Windows.
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
runtime.GC()
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-10-07 12:48:25 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-14 19:35:59 -04:00
|
|
|
// normalizeJSON removes all insignificant whitespace from the given JSON buffer
|
|
|
|
|
// and returns it as a string for easier comparison.
|
|
|
|
|
func normalizeJSON(t *testing.T, src []byte) string {
|
|
|
|
|
t.Helper()
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
err := json.Compact(&buf, src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("error normalizing JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
return buf.String()
|
|
|
|
|
}
|
2020-01-04 08:29:25 -05:00
|
|
|
|
2020-03-23 15:26:18 -04:00
|
|
|
func mustResourceAddr(s string) addrs.ConfigResource {
|
2020-01-04 08:29:25 -05:00
|
|
|
addr, diags := addrs.ParseAbsResourceStr(s)
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
|
panic(diags.Err())
|
|
|
|
|
}
|
2020-03-23 15:26:18 -04:00
|
|
|
return addr.Config()
|
2020-01-04 08:29:25 -05:00
|
|
|
}
|
2020-05-25 15:24:35 -04:00
|
|
|
|
|
|
|
|
// This map from provider type name to namespace is used by the fake registry
|
|
|
|
|
// when called via LookupLegacyProvider. Providers not in this map will return
|
|
|
|
|
// a 404 Not Found error.
|
|
|
|
|
var legacyProviderNamespaces = map[string]string{
|
2023-12-11 15:10:03 -05:00
|
|
|
"foo": "hashicorp",
|
|
|
|
|
"bar": "hashicorp",
|
2020-05-25 15:24:35 -04:00
|
|
|
"baz": "terraform-providers",
|
2023-12-11 15:10:03 -05:00
|
|
|
"qux": "hashicorp",
|
2020-08-21 12:29:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This map is used to mock the provider redirect feature.
|
|
|
|
|
var movedProviderNamespaces = map[string]string{
|
|
|
|
|
"qux": "acme",
|
2020-05-25 15:24:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testServices starts up a local HTTP server running a fake provider registry
|
|
|
|
|
// service which responds only to discovery requests and legacy provider lookup
|
|
|
|
|
// API calls.
|
|
|
|
|
//
|
|
|
|
|
// The final return value is a function to call at the end of a test function
|
|
|
|
|
// to shut down the test server. After you call that function, the discovery
|
|
|
|
|
// object becomes useless.
|
|
|
|
|
func testServices(t *testing.T) (services *disco.Disco, cleanup func()) {
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(fakeRegistryHandler))
|
|
|
|
|
|
|
|
|
|
services = disco.New()
|
2026-02-20 13:35:52 -05:00
|
|
|
services.ForceHostServices(svchost.Hostname("registry.opentofu.org"), map[string]any{
|
2020-05-25 15:24:35 -04:00
|
|
|
"providers.v1": server.URL + "/providers/v1/",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return services, func() {
|
|
|
|
|
server.Close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// testRegistrySource is a wrapper around testServices that uses the created
|
|
|
|
|
// discovery object to produce a Source instance that is ready to use with the
|
|
|
|
|
// fake registry services.
|
|
|
|
|
//
|
|
|
|
|
// As with testServices, the final return value is a function to call at the end
|
|
|
|
|
// of your test in order to shut down the test server.
|
|
|
|
|
func testRegistrySource(t *testing.T) (source *getproviders.RegistrySource, cleanup func()) {
|
|
|
|
|
services, close := testServices(t)
|
2025-10-13 03:00:19 -04:00
|
|
|
source = getproviders.NewRegistrySource(t.Context(), services, nil, getproviders.LocationConfig{ProviderDownloadRetries: 0})
|
2020-05-25 15:24:35 -04:00
|
|
|
return source, close
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
|
path := req.URL.EscapedPath()
|
|
|
|
|
|
2025-05-15 07:39:11 -04:00
|
|
|
write := func(data string) {
|
|
|
|
|
if _, err := resp.Write([]byte(data)); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 15:24:35 -04:00
|
|
|
if !strings.HasPrefix(path, "/providers/v1/") {
|
|
|
|
|
resp.WriteHeader(404)
|
2025-05-15 07:39:11 -04:00
|
|
|
write(`not a provider registry endpoint`)
|
2020-05-25 15:24:35 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pathParts := strings.Split(path, "/")[3:]
|
|
|
|
|
|
|
|
|
|
if len(pathParts) != 3 {
|
|
|
|
|
resp.WriteHeader(404)
|
2025-05-15 07:39:11 -04:00
|
|
|
write(`unrecognized path scheme`)
|
2020-05-25 15:24:35 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 12:29:10 -04:00
|
|
|
if pathParts[2] != "versions" {
|
2020-05-25 15:24:35 -04:00
|
|
|
resp.WriteHeader(404)
|
2025-05-15 07:39:11 -04:00
|
|
|
write(`this registry only supports legacy namespace lookup requests`)
|
2020-08-21 12:29:10 -04:00
|
|
|
return
|
2020-05-25 15:24:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name := pathParts[1]
|
2020-08-21 12:29:10 -04:00
|
|
|
|
|
|
|
|
// Legacy lookup
|
|
|
|
|
if pathParts[0] == "-" {
|
|
|
|
|
if namespace, ok := legacyProviderNamespaces[name]; ok {
|
|
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
|
resp.WriteHeader(200)
|
|
|
|
|
if movedNamespace, ok := movedProviderNamespaces[name]; ok {
|
2025-05-15 07:39:11 -04:00
|
|
|
fmt.Fprintf(resp, `{"id":"%s/%s","moved_to":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name, movedNamespace, name)
|
2020-08-21 12:29:10 -04:00
|
|
|
} else {
|
2025-05-15 07:39:11 -04:00
|
|
|
fmt.Fprintf(resp, `{"id":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name)
|
2020-08-21 12:29:10 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
resp.WriteHeader(404)
|
2025-05-15 07:39:11 -04:00
|
|
|
write(`provider not found`)
|
2020-08-21 12:29:10 -04:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Also return versions for redirect target
|
|
|
|
|
if namespace, ok := movedProviderNamespaces[name]; ok && pathParts[0] == namespace {
|
2020-05-25 15:24:35 -04:00
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
|
resp.WriteHeader(200)
|
2025-05-15 07:39:11 -04:00
|
|
|
fmt.Fprintf(resp, `{"id":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name)
|
2020-05-25 15:24:35 -04:00
|
|
|
} else {
|
|
|
|
|
resp.WriteHeader(404)
|
2025-05-15 07:39:11 -04:00
|
|
|
write(`provider not found`)
|
2020-05-25 15:24:35 -04:00
|
|
|
}
|
|
|
|
|
}
|
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 15:51:40 -05:00
|
|
|
|
|
|
|
|
func testView(t *testing.T) (*views.View, func(*testing.T) *terminal.TestOutput) {
|
|
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
return views.NewView(streams), done
|
|
|
|
|
}
|
2022-07-12 17:00:36 -04:00
|
|
|
|
|
|
|
|
// checkGoldenReference compares the given test output with a known "golden" output log
|
|
|
|
|
// located under the specified fixture path.
|
|
|
|
|
//
|
|
|
|
|
// If any of these tests fail, please communicate with Terraform Cloud folks before resolving,
|
|
|
|
|
// as changes to UI output may also affect the behavior of Terraform Cloud's structured run output.
|
|
|
|
|
func checkGoldenReference(t *testing.T, output *terminal.TestOutput, fixturePathName string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
// Load the golden reference fixture
|
|
|
|
|
wantFile, err := os.Open(path.Join(testFixturePath(fixturePathName), "output.jsonlog"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to open output file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
defer wantFile.Close()
|
2023-09-07 12:53:12 -04:00
|
|
|
wantBytes, err := io.ReadAll(wantFile)
|
2022-07-12 17:00:36 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("failed to read output file: %s", err)
|
|
|
|
|
}
|
|
|
|
|
want := string(wantBytes)
|
|
|
|
|
|
|
|
|
|
got := output.Stdout()
|
|
|
|
|
|
|
|
|
|
// Split the output and the reference into lines so that we can compare
|
|
|
|
|
// messages
|
|
|
|
|
got = strings.TrimSuffix(got, "\n")
|
|
|
|
|
gotLines := strings.Split(got, "\n")
|
|
|
|
|
|
|
|
|
|
want = strings.TrimSuffix(want, "\n")
|
|
|
|
|
wantLines := strings.Split(want, "\n")
|
|
|
|
|
|
|
|
|
|
if len(gotLines) != len(wantLines) {
|
|
|
|
|
t.Errorf("unexpected number of log lines: got %d, want %d\n"+
|
|
|
|
|
"NOTE: This failure may indicate a UI change affecting the behavior of structured run output on TFC.\n"+
|
|
|
|
|
"Please communicate with Terraform Cloud team before resolving", len(gotLines), len(wantLines))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify that the log starts with a version message
|
|
|
|
|
type versionMessage struct {
|
2023-09-21 08:38:46 -04:00
|
|
|
Level string `json:"@level"`
|
|
|
|
|
Message string `json:"@message"`
|
|
|
|
|
Type string `json:"type"`
|
|
|
|
|
OpenTofu string `json:"tofu"`
|
|
|
|
|
UI string `json:"ui"`
|
2022-07-12 17:00:36 -04:00
|
|
|
}
|
|
|
|
|
var gotVersion versionMessage
|
|
|
|
|
if err := json.Unmarshal([]byte(gotLines[0]), &gotVersion); err != nil {
|
|
|
|
|
t.Errorf("failed to unmarshal version line: %s\n%s", err, gotLines[0])
|
|
|
|
|
}
|
|
|
|
|
wantVersion := versionMessage{
|
|
|
|
|
"info",
|
2023-09-21 08:38:46 -04:00
|
|
|
fmt.Sprintf("OpenTofu %s", version.String()),
|
2022-07-12 17:00:36 -04:00
|
|
|
"version",
|
|
|
|
|
version.String(),
|
|
|
|
|
views.JSON_UI_VERSION,
|
|
|
|
|
}
|
|
|
|
|
if !cmp.Equal(wantVersion, gotVersion) {
|
|
|
|
|
t.Errorf("unexpected first message:\n%s", cmp.Diff(wantVersion, gotVersion))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compare the rest of the lines against the golden reference
|
2026-02-20 13:35:52 -05:00
|
|
|
var gotLineMaps []map[string]any
|
2022-07-12 17:00:36 -04:00
|
|
|
for i, line := range gotLines[1:] {
|
|
|
|
|
index := i + 1
|
2026-02-20 13:35:52 -05:00
|
|
|
var gotMap map[string]any
|
2022-07-12 17:00:36 -04:00
|
|
|
if err := json.Unmarshal([]byte(line), &gotMap); err != nil {
|
|
|
|
|
t.Errorf("failed to unmarshal got line %d: %s\n%s", index, err, gotLines[index])
|
|
|
|
|
}
|
|
|
|
|
if _, ok := gotMap["@timestamp"]; !ok {
|
|
|
|
|
t.Errorf("missing @timestamp field in log: %s", gotLines[index])
|
|
|
|
|
}
|
2025-03-06 07:03:46 -05:00
|
|
|
gotMap = deleteMapField(gotMap, "hook", "elapsed_seconds")
|
2022-07-12 17:00:36 -04:00
|
|
|
delete(gotMap, "@timestamp")
|
|
|
|
|
gotLineMaps = append(gotLineMaps, gotMap)
|
|
|
|
|
}
|
2025-03-06 07:03:46 -05:00
|
|
|
|
2026-02-20 13:35:52 -05:00
|
|
|
var wantLineMaps []map[string]any
|
2022-07-12 17:00:36 -04:00
|
|
|
for i, line := range wantLines[1:] {
|
|
|
|
|
index := i + 1
|
2026-02-20 13:35:52 -05:00
|
|
|
var wantMap map[string]any
|
2022-07-12 17:00:36 -04:00
|
|
|
if err := json.Unmarshal([]byte(line), &wantMap); err != nil {
|
|
|
|
|
t.Errorf("failed to unmarshal want line %d: %s\n%s", index, err, gotLines[index])
|
|
|
|
|
}
|
2025-03-06 07:03:46 -05:00
|
|
|
wantMap = deleteMapField(wantMap, "hook", "elapsed_seconds")
|
2022-07-12 17:00:36 -04:00
|
|
|
wantLineMaps = append(wantLineMaps, wantMap)
|
|
|
|
|
}
|
2025-03-06 07:03:46 -05:00
|
|
|
|
2022-07-12 17:00:36 -04:00
|
|
|
if diff := cmp.Diff(wantLineMaps, gotLineMaps); diff != "" {
|
|
|
|
|
t.Errorf("wrong output lines\n%s\n"+
|
|
|
|
|
"NOTE: This failure may indicate a UI change affecting the behavior of structured run output on TFC.\n"+
|
|
|
|
|
"Please communicate with Terraform Cloud team before resolving", diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-06 07:03:46 -05:00
|
|
|
|
2026-02-20 13:35:52 -05:00
|
|
|
func deleteMapField(fieldMap map[string]any, rootField, field string) map[string]any {
|
|
|
|
|
rootMap, ok := fieldMap[rootField].(map[string]any)
|
2025-03-06 07:03:46 -05:00
|
|
|
if !ok {
|
|
|
|
|
return fieldMap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete(rootMap, field)
|
|
|
|
|
return rootMap
|
|
|
|
|
}
|
2025-04-17 15:14:54 -04:00
|
|
|
|
|
|
|
|
// testHangServer starts a local HTTP server that accepts incoming requests
|
|
|
|
|
// but then intentionally leaves the connection hanging without responding,
|
|
|
|
|
// writing the request to the returned channel so that the caller can then
|
|
|
|
|
// trigger some mechanism for cancelling that hung request.
|
|
|
|
|
//
|
|
|
|
|
// This is intended for testing anything that needs to be able to cancel
|
|
|
|
|
// slow requests to remote HTTP servers, so that the test can be sure that
|
|
|
|
|
// the request definitely will be "slow enough" that cancellation is
|
|
|
|
|
// definitely the only way the request could've halted.
|
|
|
|
|
//
|
|
|
|
|
// The returned server is automatically closed when the calling test
|
|
|
|
|
// is complete, but the caller is also allowed to optionally call Close
|
|
|
|
|
// directly itself. Note that the Close method alone will not close
|
|
|
|
|
// any active requests, but testHangServer guarantees that it will
|
|
|
|
|
// eventually terminate active requests once the calling test is
|
|
|
|
|
// complete.
|
|
|
|
|
func testHangServer(t testing.TB) (server *httptest.Server, reqs <-chan *http.Request) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
// We'll use this channel to signal any active requests to terminate
|
|
|
|
|
// during test cleanup, so that the active requests can't remain
|
|
|
|
|
// running indefinitely.
|
|
|
|
|
cleanupCh := make(chan struct{})
|
|
|
|
|
|
|
|
|
|
// This channel is how we'll notify the caller when we get a request.
|
|
|
|
|
// This has a buffer so that in the assumed-typical case where the
|
|
|
|
|
// test server will only start serving a few requests before they
|
|
|
|
|
// get cancelled the server's handler can be decoupled from the
|
|
|
|
|
// channel reads in the caller.
|
|
|
|
|
reqsCh := make(chan *http.Request, 8)
|
|
|
|
|
|
|
|
|
|
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
|
// We intentionally don't take any action on this request until
|
|
|
|
|
// the test cleanup function runs, but we will notify our
|
|
|
|
|
// caller that the request was started.
|
|
|
|
|
//
|
|
|
|
|
// We'll also accept getting told to clean up before the
|
|
|
|
|
// channel write succeeds just in case the calling test exits
|
|
|
|
|
// before it reads from reqsCh.
|
|
|
|
|
select {
|
|
|
|
|
case reqsCh <- req:
|
|
|
|
|
case <-cleanupCh:
|
|
|
|
|
}
|
|
|
|
|
// If we managed to send req to reqsCh above then we still
|
|
|
|
|
// need to wait for cleanupCh to close. The following is
|
|
|
|
|
// no-op if the channel is already closed.
|
|
|
|
|
<-cleanupCh
|
|
|
|
|
// If any client is still connected by the time we get here then
|
|
|
|
|
// we'll respond quickly just to get their connection closed.
|
|
|
|
|
// This is unlikely but could potentially happen if a new client
|
|
|
|
|
// connects in the narrow time window between us closing the
|
|
|
|
|
// existing client connections and fully closing the server,
|
|
|
|
|
// after cleanupCh is already closed: in that case the new client
|
|
|
|
|
// will get a 500 Internal Server Error response immediately.
|
|
|
|
|
w.WriteHeader(500)
|
|
|
|
|
}))
|
|
|
|
|
t.Logf("testHangServer is running at %s", server.URL)
|
|
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
t.Helper()
|
|
|
|
|
t.Log("shutting down testHangServer")
|
|
|
|
|
close(cleanupCh) // terminate any active handlers
|
|
|
|
|
close(reqsCh) // unblock any test that's awaiting a request notification
|
|
|
|
|
server.CloseClientConnections() // force any active clients to disconnect
|
|
|
|
|
server.Close() // stop accepting new requests and wait for existing ones to stop
|
|
|
|
|
})
|
|
|
|
|
return server, reqsCh
|
|
|
|
|
}
|
2026-03-31 07:22:31 -04:00
|
|
|
|
|
|
|
|
// TestVarsParsing checks that the -var/-var-file are parsed correctly and processed as expected.
|
|
|
|
|
// This was wrote while doing the Meta removal refactor, before removing all the temporary
|
|
|
|
|
// GatherVariables methods to ensure that the logic added to replace the removed method does
|
|
|
|
|
// not alter the way variable related arguments are parsed.
|
|
|
|
|
// Tested against commands with checkable outputs to validate that the right variable values reached the
|
|
|
|
|
// execution context.
|
|
|
|
|
func TestVarsParsing(t *testing.T) {
|
|
|
|
|
p := testProvider()
|
|
|
|
|
varArgs := []string{"-var", "snack=chips", "-var-file", "all.tfvars"}
|
|
|
|
|
t.Run("console", func(t *testing.T) {
|
2026-04-07 03:15:05 -04:00
|
|
|
td := t.TempDir()
|
|
|
|
|
testCopyDir(t, testFixturePath("variables"), td)
|
|
|
|
|
t.Chdir(td)
|
|
|
|
|
t.Cleanup(testStdinPipe(t, strings.NewReader("var.foo\nvar.snack\n")))
|
2026-03-31 07:22:31 -04:00
|
|
|
streams, done := terminal.StreamsForTesting(t)
|
|
|
|
|
c := &ConsoleCommand{
|
|
|
|
|
Meta: Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
|
View: views.NewView(streams),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args := append([]string{"-no-color"}, varArgs...)
|
|
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actual := output.Stdout()
|
|
|
|
|
expected := `"from tfvars"
|
|
|
|
|
"chips"
|
|
|
|
|
`
|
|
|
|
|
if diff := cmp.Diff(expected, actual); diff != "" {
|
|
|
|
|
t.Errorf("variables parsed incorrectly (-want,+got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
cases := map[string]struct {
|
|
|
|
|
cmdBuilder func(m Meta) cli.Command
|
|
|
|
|
expectedContent []string
|
2026-04-07 03:15:05 -04:00
|
|
|
confirmation bool
|
2026-03-31 07:22:31 -04:00
|
|
|
}{
|
|
|
|
|
"plan": {
|
|
|
|
|
cmdBuilder: func(m Meta) cli.Command {
|
|
|
|
|
return &PlanCommand{m}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"apply": {
|
|
|
|
|
cmdBuilder: func(m Meta) cli.Command {
|
|
|
|
|
return &ApplyCommand{Meta: m}
|
|
|
|
|
},
|
2026-04-07 03:15:05 -04:00
|
|
|
confirmation: true,
|
2026-03-31 07:22:31 -04:00
|
|
|
},
|
|
|
|
|
"output": {
|
|
|
|
|
cmdBuilder: func(m Meta) cli.Command {
|
|
|
|
|
return &OutputCommand{m}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"show": {
|
|
|
|
|
cmdBuilder: func(m Meta) cli.Command {
|
|
|
|
|
return &ShowCommand{Meta: m}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
"refresh": {
|
|
|
|
|
cmdBuilder: func(m Meta) cli.Command {
|
|
|
|
|
return &RefreshCommand{m}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2026-04-07 03:15:05 -04:00
|
|
|
td := t.TempDir()
|
|
|
|
|
testCopyDir(t, testFixturePath("variables"), td)
|
|
|
|
|
t.Chdir(td)
|
2026-03-31 07:22:31 -04:00
|
|
|
view, done := testView(t)
|
|
|
|
|
m := Meta{
|
|
|
|
|
WorkingDir: workdir.NewDir("."),
|
|
|
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
|
|
|
View: view,
|
|
|
|
|
}
|
|
|
|
|
c := tc.cmdBuilder(m)
|
|
|
|
|
|
|
|
|
|
args := append([]string{"-no-color"}, varArgs...)
|
2026-04-07 03:15:05 -04:00
|
|
|
if tc.confirmation {
|
|
|
|
|
t.Cleanup(testInputMap(t, map[string]string{"approve": "yes"}))
|
|
|
|
|
}
|
2026-03-31 07:22:31 -04:00
|
|
|
code := c.Run(args)
|
|
|
|
|
output := done(t)
|
|
|
|
|
if code != 0 {
|
|
|
|
|
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actual := output.Stdout()
|
|
|
|
|
for _, want := range tc.expectedContent {
|
|
|
|
|
if !strings.Contains(actual, want) {
|
|
|
|
|
t.Errorf("variables parsed incorrectly. Want %q to exist in the output, but it didn't\noutput:\n%s", want, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|