2014-06-19 06:36:44 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-01-19 05:50:45 +01:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2016-08-01 23:16:22 +02:00
|
|
|
"flag"
|
2017-02-03 21:32:40 +01:00
|
|
|
"fmt"
|
2016-11-30 20:36:54 +01:00
|
|
|
"io"
|
2014-06-27 23:43:23 +02:00
|
|
|
"io/ioutil"
|
2016-08-01 23:16:22 +02:00
|
|
|
"log"
|
2017-01-19 05:50:45 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2014-06-27 23:43:23 +02:00
|
|
|
"os"
|
2017-02-03 21:32:40 +01:00
|
|
|
"os/exec"
|
2014-06-19 06:36:44 +02:00
|
|
|
"path/filepath"
|
2015-02-26 19:29:23 +01:00
|
|
|
"strings"
|
2017-02-03 21:32:40 +01:00
|
|
|
"syscall"
|
2014-06-27 23:43:23 +02:00
|
|
|
"testing"
|
2014-06-19 06:36:44 +02:00
|
|
|
|
2020-05-25 21:24:35 +02:00
|
|
|
svchost "github.com/hashicorp/terraform-svchost"
|
|
|
|
"github.com/hashicorp/terraform-svchost/disco"
|
|
|
|
"github.com/hashicorp/terraform/internal/getproviders"
|
2019-06-04 16:32:12 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/initwd"
|
|
|
|
"github.com/hashicorp/terraform/registry"
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/addrs"
|
2018-05-23 04:33:45 +02:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
|
|
|
"github.com/hashicorp/terraform/configs/configload"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
2016-08-01 23:16:22 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/logging"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/plans"
|
|
|
|
"github.com/hashicorp/terraform/plans/planfile"
|
2018-09-29 18:34:23 +02:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
|
|
|
"github.com/hashicorp/terraform/provisioners"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
|
|
|
"github.com/hashicorp/terraform/states/statefile"
|
|
|
|
"github.com/hashicorp/terraform/states/statemgr"
|
2014-06-19 06:36:44 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/version"
|
2018-10-31 16:45:03 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
backendInit "github.com/hashicorp/terraform/backend/init"
|
2014-06-19 06:36:44 +02:00
|
|
|
)
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
// These are the directories for our test data and fixtures.
|
|
|
|
var (
|
2019-06-30 09:38:36 +02:00
|
|
|
fixtureDir = "./testdata"
|
2018-11-20 09:58:59 +01:00
|
|
|
testDataDir = "./testdata"
|
|
|
|
)
|
2014-07-12 06:03:56 +02:00
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
// a top level temp directory which will be cleaned after all tests
|
|
|
|
var testingDir string
|
|
|
|
|
2014-07-12 06:03:56 +02:00
|
|
|
func init() {
|
2014-09-29 20:24:16 +02:00
|
|
|
test = true
|
|
|
|
|
2018-10-31 16:45:03 +01:00
|
|
|
// Initialize the backends
|
|
|
|
backendInit.Init(nil)
|
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
// Expand the data and fixture dirs on init because
|
|
|
|
// we change the working directory in some tests.
|
2014-07-12 06:03:56 +02:00
|
|
|
var err error
|
|
|
|
fixtureDir, err = filepath.Abs(fixtureDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-28 19:08:38 +02:00
|
|
|
|
2018-11-20 09:58:59 +01:00
|
|
|
testDataDir, err = filepath.Abs(testDataDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
testingDir, err = ioutil.TempDir(testingDir, "tf")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-12 06:03:56 +02:00
|
|
|
}
|
2014-06-19 06:36:44 +02:00
|
|
|
|
2016-08-01 23:16:22 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2018-03-28 19:08:38 +02:00
|
|
|
defer os.RemoveAll(testingDir)
|
|
|
|
|
2016-08-01 23:16:22 +02:00
|
|
|
flag.Parse()
|
|
|
|
if testing.Verbose() {
|
|
|
|
// if we're verbose, use the logging requested by TF_LOG
|
|
|
|
logging.SetOutput()
|
|
|
|
} else {
|
|
|
|
// otherwise silence all logs
|
|
|
|
log.SetOutput(ioutil.Discard)
|
|
|
|
}
|
|
|
|
|
2018-10-17 04:48:28 +02:00
|
|
|
// Make sure backend init is initialized, since our tests tend to assume it.
|
2018-10-31 16:45:03 +01:00
|
|
|
backendInit.Init(nil)
|
2018-10-17 04:48:28 +02:00
|
|
|
|
2016-08-01 23:16:22 +02:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2014-09-25 00:48:46 +02:00
|
|
|
func tempDir(t *testing.T) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
dir, err := ioutil.TempDir(testingDir, "tf")
|
2014-09-25 00:48:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2019-12-06 16:56:47 +01:00
|
|
|
|
|
|
|
dir, err = filepath.EvalSymlinks(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-25 00:48:46 +02:00
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2014-06-19 06:36:44 +02:00
|
|
|
func testFixturePath(name string) string {
|
2014-07-12 05:38:03 +02:00
|
|
|
return filepath.Join(fixtureDir, name)
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-29 18:34:23 +02:00
|
|
|
func metaOverridesForProvider(p providers.Interface) *testingOverrides {
|
2017-04-14 03:05:58 +02:00
|
|
|
return &testingOverrides{
|
2020-03-31 00:30:56 +02:00
|
|
|
Providers: map[addrs.Provider]providers.Factory{
|
2020-04-01 21:55:25 +02:00
|
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(p),
|
2020-03-31 00:30:56 +02:00
|
|
|
},
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:34:23 +02:00
|
|
|
func metaOverridesForProviderAndProvisioner(p providers.Interface, pr provisioners.Interface) *testingOverrides {
|
2017-04-14 03:05:58 +02:00
|
|
|
return &testingOverrides{
|
2020-03-31 00:30:56 +02:00
|
|
|
Providers: map[addrs.Provider]providers.Factory{
|
2020-04-01 21:55:25 +02:00
|
|
|
addrs.NewDefaultProvider("test"): providers.FactoryFixed(p),
|
2020-03-31 00:30:56 +02:00
|
|
|
},
|
2018-09-29 18:34:23 +02:00
|
|
|
Provisioners: map[string]provisioners.Factory{
|
|
|
|
"shell": provisioners.FactoryFixed(pr),
|
2015-05-08 09:01:21 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 04:33:45 +02:00
|
|
|
func testModule(t *testing.T, name string) *configs.Config {
|
2017-08-26 01:23:47 +02: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 23:24:45 +02:00
|
|
|
c, _ := testModuleWithSnapshot(t, name)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *configload.Snapshot) {
|
|
|
|
t.Helper()
|
2017-08-26 01:23:47 +02:00
|
|
|
|
2018-05-23 04:33:45 +02:00
|
|
|
dir := filepath.Join(fixtureDir, name)
|
|
|
|
// FIXME: We're not dealing with the cleanup function here because
|
|
|
|
// this testModule function is used all over and so we don't want to
|
|
|
|
// change its interface at this late stage.
|
|
|
|
loader, _ := configload.NewLoaderForTests(t)
|
|
|
|
|
|
|
|
// 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.
|
2019-01-09 03:39:14 +01:00
|
|
|
inst := initwd.NewModuleInstaller(loader.ModulesDir(), registry.NewClient(nil, nil))
|
command: "terraform init" can partially initialize for 0.12upgrade
There are a few constructs from 0.11 and prior that cause 0.12 parsing to
fail altogether, which previously created a chicken/egg problem because
we need to install the providers in order to run "terraform 0.12upgrade"
and thus fix the problem.
This changes "terraform init" to use the new "early configuration" loader
for module and provider installation. This is built on the more permissive
parser in the terraform-config-inspect package, and so it allows us to
read out the top-level blocks from the configuration while accepting
legacy HCL syntax.
In the long run this will let us do version compatibility detection before
attempting a "real" config load, giving us better error messages for any
future syntax additions, but in the short term the key thing is that it
allows us to install the dependencies even if the configuration isn't
fully valid.
Because backend init still requires full configuration, this introduces a
new mode of terraform init where it detects heuristically if it seems like
we need to do a configuration upgrade and does a partial init if so,
before finally directing the user to run "terraform 0.12upgrade" before
running any other commands.
The heuristic here is based on two assumptions:
- If the "early" loader finds no errors but the normal loader does, the
configuration is likely to be valid for Terraform 0.11 but not 0.12.
- If there's already a version constraint in the configuration that
excludes Terraform versions prior to v0.12 then the configuration is
probably _already_ upgraded and so it's just a normal syntax error,
even if the early loader didn't detect it.
Once the upgrade process is removed in 0.13.0 (users will be required to
go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can
be simplified to remove that special mode, but the idea of doing the
dependency version checks against the liberal parser will remain valuable
to increase our chances of reporting version-based incompatibilities
rather than syntax errors as we add new features in future.
2019-01-14 20:11:00 +01:00
|
|
|
_, instDiags := inst.InstallModules(dir, true, initwd.ModuleInstallHooksImpl{})
|
2019-01-09 03:39:14 +01:00
|
|
|
if instDiags.HasErrors() {
|
|
|
|
t.Fatal(instDiags.Err())
|
2014-09-25 00:48:46 +02:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
config, snap, diags := loader.LoadConfigWithSnapshot(dir)
|
2018-05-23 04:33:45 +02:00
|
|
|
if diags.HasErrors() {
|
|
|
|
t.Fatal(diags.Error())
|
2014-09-25 00:48:46 +02:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return config, snap
|
2014-09-25 00:48:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 02:51:05 +02: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 23:24:45 +02:00
|
|
|
func testPlan(t *testing.T) *plans.Plan {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
2018-10-12 02:58:46 +02: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 23:24:45 +02:00
|
|
|
return &plans.Plan{
|
2018-10-12 02:58:46 +02: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 23:24:45 +02:00
|
|
|
Changes: plans.NewChanges(),
|
2016-10-29 02:51:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func testPlanFile(t *testing.T, configSnap *configload.Snapshot, state *states.State, plan *plans.Plan) string {
|
2017-08-26 01:23:47 +02: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 23:24:45 +02:00
|
|
|
stateFile := &statefile.File{
|
2018-10-13 03:04:26 +02:00
|
|
|
Lineage: "",
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
State: state,
|
|
|
|
TerraformVersion: version.SemVer,
|
2014-06-27 23:43:23 +02:00
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
path := testTempFile(t)
|
|
|
|
err := planfile.Create(path, configSnap, stateFile, plan)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create temporary plan file: %s", err)
|
2014-06-27 23:43:23 +02: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 23:24:45 +02: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)
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
func testReadPlan(t *testing.T, path string) *plans.Plan {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
f, err := planfile.Open(path)
|
2014-07-01 18:12:05 +02:00
|
|
|
if err != nil {
|
2018-10-14 18:21:31 +02:00
|
|
|
t.Fatalf("error opening plan file %q: %s", path, err)
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2018-10-14 18:21:31 +02:00
|
|
|
p, err := f.ReadPlan()
|
2014-07-01 18:12:05 +02:00
|
|
|
if err != nil {
|
2018-10-14 18:21:31 +02:00
|
|
|
t.Fatalf("error reading plan from plan file %q: %s", path, err)
|
2014-07-01 18:12:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2014-09-17 20:15:07 +02: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 23:24:45 +02: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-15 01:07:42 +02: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.
|
2019-10-30 20:59:34 +01:00
|
|
|
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
|
|
|
|
Status: states.ObjectReady,
|
2020-03-23 20:26:18 +01:00
|
|
|
Dependencies: []addrs.ConfigResource{},
|
2014-09-17 20:15:07 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
2020-04-01 21:55:25 +02:00
|
|
|
Provider: addrs.NewDefaultProvider("test"),
|
2020-03-11 19:19:52 +01:00
|
|
|
Module: addrs.RootModule,
|
2020-02-13 21:32:58 +01:00
|
|
|
},
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
)
|
2019-06-04 16:32:12 +02: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 23:24:45 +02: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,
|
|
|
|
}
|
|
|
|
return statefile.Write(sf, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
//
|
|
|
|
// mark := markStateForMatching(state, "foo")
|
|
|
|
// // (do stuff to the state)
|
|
|
|
// assertStateHasMarker(state, mark)
|
|
|
|
func markStateForMatching(state *states.State, mark string) string {
|
|
|
|
state.RootModule().SetOutputValue("testing_mark", cty.StringVal(mark), false)
|
|
|
|
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 20:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
func testStateFile(t *testing.T, s *states.State) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2014-06-27 23:43:23 +02: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 23:24:45 +02:00
|
|
|
t.Fatalf("failed to create temporary state file %s: %s", path, err)
|
2014-06-27 23:43:23 +02: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 23:24:45 +02:00
|
|
|
err = writeStateForTesting(s, f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write state to temporary file %s: %s", path, err)
|
2014-06-27 23:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
// testStateFileDefault writes the state out to the default statefile
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
|
|
|
func testStateFileDefault(t *testing.T, s *terraform.State) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
f, err := os.Create(DefaultStateFilename)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if err := terraform.WriteState(s, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultStateFilename
|
|
|
|
}
|
|
|
|
|
2015-03-05 23:55:15 +01:00
|
|
|
// testStateFileRemote writes the state out to the remote statefile
|
|
|
|
// in the cwd. Use `testCwd` to change into a temp cwd.
|
|
|
|
func testStateFileRemote(t *testing.T, s *terraform.State) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2015-03-05 23:55:15 +01:00
|
|
|
path := filepath.Join(DefaultDataDir, DefaultStateFilename)
|
|
|
|
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()
|
|
|
|
|
|
|
|
if err := terraform.WriteState(s, f); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01: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 23:24:45 +02:00
|
|
|
func testStateRead(t *testing.T, path string) *states.State {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-07-05 23:59:42 +02:00
|
|
|
defer f.Close()
|
2015-02-26 19:29:23 +01:00
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
sf, err := statefile.Read(f)
|
2015-02-26 19:29:23 +01: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 23:24:45 +02: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.
|
|
|
|
//
|
|
|
|
// This old format still uses *terraform.State, but should be replaced with
|
|
|
|
// a more specialized type in a later release.
|
|
|
|
func testDataStateRead(t *testing.T, path string) *terraform.State {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
s, err := terraform.ReadState(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2017-01-19 05:50:45 +01: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-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
newState := testStateRead(t, path)
|
2015-02-26 19:29:23 +01:00
|
|
|
actual := strings.TrimSpace(newState.String())
|
|
|
|
expected = strings.TrimSpace(expected)
|
|
|
|
if actual != expected {
|
2016-03-08 21:37:34 +01:00
|
|
|
t.Fatalf("expected:\n%s\nactual:\n%s", expected, actual)
|
2015-02-26 19:29:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-29 18:34:23 +02:00
|
|
|
func testProvider() *terraform.MockProvider {
|
|
|
|
p := new(terraform.MockProvider)
|
2020-09-21 20:48:21 +02:00
|
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) (resp providers.PlanResourceChangeResponse) {
|
|
|
|
resp.PlannedState = req.ProposedNewState
|
|
|
|
return resp
|
2018-09-29 18:34:23 +02:00
|
|
|
}
|
2020-09-21 20:48:21 +02:00
|
|
|
|
2018-09-29 18:34:23 +02:00
|
|
|
p.ReadResourceFn = func(req providers.ReadResourceRequest) providers.ReadResourceResponse {
|
|
|
|
return providers.ReadResourceResponse{
|
|
|
|
NewState: req.PriorState,
|
|
|
|
}
|
2014-06-19 06:36:44 +02:00
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
2014-06-27 23:43:23 +02:00
|
|
|
|
|
|
|
func testTempFile(t *testing.T) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2016-05-11 02:03:58 +02:00
|
|
|
return filepath.Join(testTempDir(t), "state.tfstate")
|
2014-06-27 23:43:23 +02:00
|
|
|
}
|
2014-08-05 18:32:01 +02:00
|
|
|
|
|
|
|
func testTempDir(t *testing.T) string {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
d, err := ioutil.TempDir(testingDir, "tf")
|
2014-08-05 18:32:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2019-12-06 16:56:47 +01:00
|
|
|
d, err = filepath.EvalSymlinks(d)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:01 +02:00
|
|
|
return d
|
|
|
|
}
|
2014-10-08 21:08:35 +02:00
|
|
|
|
2016-08-24 07:11:21 +02:00
|
|
|
// testRename renames the path to new and returns a function to defer to
|
|
|
|
// revert the rename.
|
|
|
|
func testRename(t *testing.T, base, path, new string) func() {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2016-08-24 07:11:21 +02:00
|
|
|
if base != "" {
|
|
|
|
path = filepath.Join(base, path)
|
|
|
|
new = filepath.Join(base, new)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(path, new); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Just re-rename and ignore the return value
|
|
|
|
testRename(t, "", new, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testChdir changes the directory and returns a function to defer to
|
|
|
|
// revert the old cwd.
|
|
|
|
func testChdir(t *testing.T, new string) func() {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2016-08-24 07:11:21 +02:00
|
|
|
old, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chdir(new); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Re-run the function ignoring the defer result
|
|
|
|
testChdir(t, old)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:29:23 +01:00
|
|
|
// testCwd is used to change the current working directory
|
2020-06-18 23:09:20 +02:00
|
|
|
// into a test directory that should be removed after
|
2014-10-08 21:08:35 +02:00
|
|
|
func testCwd(t *testing.T) (string, string) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2018-03-28 19:08:38 +02:00
|
|
|
tmp, err := ioutil.TempDir(testingDir, "tf")
|
2014-10-08 21:08:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 19:49:31 +01:00
|
|
|
|
2014-10-08 21:08:35 +02:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 19:49:31 +01:00
|
|
|
|
2014-10-09 23:45:08 +02:00
|
|
|
if err := os.Chdir(tmp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 19:49:31 +01:00
|
|
|
|
2014-10-08 21:08:35 +02:00
|
|
|
return tmp, cwd
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:45:08 +02:00
|
|
|
// testFixCwd is used to as a defer to testDir
|
|
|
|
func testFixCwd(t *testing.T, tmp, cwd string) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2014-10-09 23:45:08 +02:00
|
|
|
if err := os.Chdir(cwd); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2015-02-22 19:49:31 +01:00
|
|
|
|
2014-10-09 23:45:08 +02:00
|
|
|
if err := os.RemoveAll(tmp); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2014-10-08 21:08:35 +02:00
|
|
|
}
|
2016-11-30 20:36:54 +01: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-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2016-11-30 20:36:54 +01: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()
|
|
|
|
io.Copy(w, src)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Close our read end
|
|
|
|
r.Close()
|
|
|
|
|
|
|
|
// Reset stdin
|
|
|
|
os.Stdin = old
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify os.Stdout to write to the given buffer. Note that this is generally
|
|
|
|
// not useful since the commands are configured to write to a cli.Ui, not
|
|
|
|
// Stdout directly. Commands like `console` though use the raw stdout.
|
|
|
|
func testStdoutCapture(t *testing.T, dst io.Writer) func() {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2016-11-30 20:36:54 +01:00
|
|
|
r, w, err := os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify stdout
|
|
|
|
old := os.Stdout
|
|
|
|
os.Stdout = w
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
defer r.Close()
|
|
|
|
io.Copy(dst, r)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
// Close the writer end of the pipe
|
|
|
|
w.Sync()
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
// Reset stdout
|
|
|
|
os.Stdout = old
|
|
|
|
|
|
|
|
// Wait for the data copy to complete to avoid a race reading data
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 05:50:45 +01: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-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
// Disable test mode so input is called
|
|
|
|
test = false
|
|
|
|
|
|
|
|
// Setup reader/writers
|
|
|
|
testInputResponse = answers
|
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
return func() {
|
|
|
|
test = true
|
|
|
|
testInputResponse = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 19:59:17 +01: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-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-03-01 19:59:17 +01:00
|
|
|
// Disable test mode so input is called
|
|
|
|
test = false
|
|
|
|
|
|
|
|
// Setup reader/writers
|
|
|
|
defaultInputReader = bytes.NewBufferString("")
|
|
|
|
defaultInputWriter = new(bytes.Buffer)
|
|
|
|
|
|
|
|
// Setup answers
|
|
|
|
testInputResponse = nil
|
|
|
|
testInputResponseMap = answers
|
|
|
|
|
|
|
|
// Return the cleanup
|
|
|
|
return func() {
|
|
|
|
test = true
|
|
|
|
testInputResponseMap = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:50:45 +01: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-08 02:20:46 +01:00
|
|
|
//
|
|
|
|
// When using this function, the configuration fixture for the test must
|
|
|
|
// include an empty configuration block for the HTTP backend, like this:
|
|
|
|
//
|
|
|
|
// terraform {
|
|
|
|
// backend "http" {
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// "terraform init" must be run, since the test backend config cache created
|
|
|
|
// by this function contains the hash for an empty configuration.
|
2020-04-02 18:58:44 +02:00
|
|
|
func testBackendState(t *testing.T, s *states.State, c int) (*terraform.State, *httptest.Server) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-19 05:50:45 +01: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)
|
|
|
|
resp.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a state was given, make sure we calculate the proper b64md5
|
|
|
|
if s != nil {
|
2020-04-02 18:58:44 +02:00
|
|
|
err := statefile.Write(&statefile.File{State: s}, buf)
|
|
|
|
if err != nil {
|
2017-01-19 05:50:45 +01: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-08 02:20:46 +01:00
|
|
|
backendConfig := &configs.Backend{
|
|
|
|
Type: "http",
|
|
|
|
Config: configs.SynthBody("<testBackendState>", map[string]cty.Value{}),
|
|
|
|
}
|
2018-11-08 23:38:56 +01:00
|
|
|
b := backendInit.Backend("http")()
|
2018-11-08 02:20:46 +01:00
|
|
|
configSchema := b.ConfigSchema()
|
|
|
|
hash := backendConfig.Hash(configSchema)
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
state := terraform.NewState()
|
|
|
|
state.Backend = &terraform.BackendState{
|
2018-03-28 00:31:05 +02:00
|
|
|
Type: "http",
|
|
|
|
ConfigRaw: json.RawMessage(fmt.Sprintf(`{"address":%q}`, srv.URL)),
|
2018-12-19 01:06:49 +01:00
|
|
|
Hash: uint64(hash),
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return state, srv
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRemoteState is used to make a test HTTP server to return a given
|
|
|
|
// state file that can be used for testing legacy remote 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 23:24:45 +02:00
|
|
|
//
|
|
|
|
// The return values are a *terraform.State instance that should be written
|
|
|
|
// as the "data state" (really: backend state) and the server that the
|
|
|
|
// returned data state refers to.
|
|
|
|
func testRemoteState(t *testing.T, s *states.State, c int) (*terraform.State, *httptest.Server) {
|
2017-08-26 01:23:47 +02:00
|
|
|
t.Helper()
|
|
|
|
|
2017-01-19 05:50:45 +01: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)
|
|
|
|
resp.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
retState := terraform.NewState()
|
|
|
|
|
2017-01-19 05:50:45 +01:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
b := &terraform.BackendState{
|
|
|
|
Type: "http",
|
|
|
|
}
|
|
|
|
b.SetConfig(cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"address": cty.StringVal(srv.URL),
|
|
|
|
}), &configschema.Block{
|
|
|
|
Attributes: map[string]*configschema.Attribute{
|
|
|
|
"address": {
|
|
|
|
Type: cty.String,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
retState.Backend = b
|
2017-01-19 05:50:45 +01:00
|
|
|
|
|
|
|
if s != 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 23:24:45 +02:00
|
|
|
err := statefile.Write(&statefile.File{State: s}, buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to write initial state: %v", err)
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
return retState, srv
|
2017-01-19 05:50:45 +01:00
|
|
|
}
|
2017-02-03 21:32:40 +01:00
|
|
|
|
|
|
|
// testlockState calls a separate process to the lock the state file at path.
|
|
|
|
// deferFunc should be called in the caller to properly unlock the file.
|
2018-11-20 09:58:59 +01:00
|
|
|
// Since many tests change the working directory, the sourcedir argument must be
|
2017-02-06 19:26:03 +01:00
|
|
|
// supplied to locate the statelocker.go source.
|
|
|
|
func testLockState(sourceDir, path string) (func(), error) {
|
2017-02-03 21:56:19 +01:00
|
|
|
// build and run the binary ourselves so we can quickly terminate it for cleanup
|
2018-03-28 19:08:38 +02:00
|
|
|
buildDir, err := ioutil.TempDir(testingDir, "locker")
|
2017-02-03 21:56:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cleanFunc := func() {
|
|
|
|
os.RemoveAll(buildDir)
|
|
|
|
}
|
|
|
|
|
2017-02-06 19:26:03 +01:00
|
|
|
source := filepath.Join(sourceDir, "statelocker.go")
|
2017-02-03 21:56:19 +01:00
|
|
|
lockBin := filepath.Join(buildDir, "statelocker")
|
2017-02-06 19:26:03 +01:00
|
|
|
|
2020-09-23 23:56:19 +02:00
|
|
|
cmd := exec.Command("go", "build", "-o", lockBin, source)
|
2018-11-20 09:58:59 +01:00
|
|
|
cmd.Dir = filepath.Dir(sourceDir)
|
|
|
|
|
|
|
|
out, err := cmd.CombinedOutput()
|
2017-02-03 21:56:19 +01:00
|
|
|
if err != nil {
|
|
|
|
cleanFunc()
|
|
|
|
return nil, fmt.Errorf("%s %s", err, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
locker := exec.Command(lockBin, path)
|
2017-02-03 21:32:40 +01:00
|
|
|
pr, pw, err := os.Pipe()
|
|
|
|
if err != nil {
|
2017-02-03 21:56:19 +01:00
|
|
|
cleanFunc()
|
2017-02-03 21:32:40 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer pr.Close()
|
|
|
|
defer pw.Close()
|
|
|
|
locker.Stderr = pw
|
|
|
|
locker.Stdout = pw
|
|
|
|
|
|
|
|
if err := locker.Start(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deferFunc := func() {
|
2017-02-03 21:56:19 +01:00
|
|
|
cleanFunc()
|
2017-02-03 21:32:40 +01:00
|
|
|
locker.Process.Signal(syscall.SIGTERM)
|
|
|
|
locker.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for the process to lock
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
n, err := pr.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return deferFunc, fmt.Errorf("read from statelocker returned: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-02-15 17:53:19 +01:00
|
|
|
output := string(buf[:n])
|
|
|
|
if !strings.HasPrefix(output, "LOCKID") {
|
2017-02-03 21:56:19 +01:00
|
|
|
return deferFunc, fmt.Errorf("statelocker wrote: %s", string(buf[:n]))
|
2017-02-03 21:32:40 +01:00
|
|
|
}
|
|
|
|
return deferFunc, nil
|
|
|
|
}
|
2018-10-15 01:35:59 +02: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 14:29:25 +01:00
|
|
|
|
2020-03-23 20:26:18 +01:00
|
|
|
func mustResourceAddr(s string) addrs.ConfigResource {
|
2020-01-04 14:29:25 +01:00
|
|
|
addr, diags := addrs.ParseAbsResourceStr(s)
|
|
|
|
if diags.HasErrors() {
|
|
|
|
panic(diags.Err())
|
|
|
|
}
|
2020-03-23 20:26:18 +01:00
|
|
|
return addr.Config()
|
2020-01-04 14:29:25 +01:00
|
|
|
}
|
2020-05-25 21:24:35 +02: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{
|
|
|
|
"foo": "hashicorp",
|
|
|
|
"bar": "hashicorp",
|
|
|
|
"baz": "terraform-providers",
|
2020-08-21 18:29:10 +02:00
|
|
|
"qux": "hashicorp",
|
|
|
|
}
|
|
|
|
|
|
|
|
// This map is used to mock the provider redirect feature.
|
|
|
|
var movedProviderNamespaces = map[string]string{
|
|
|
|
"qux": "acme",
|
2020-05-25 21:24:35 +02: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()
|
|
|
|
services.ForceHostServices(svchost.Hostname("registry.terraform.io"), map[string]interface{}{
|
|
|
|
"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)
|
|
|
|
source = getproviders.NewRegistrySource(services)
|
|
|
|
return source, close
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeRegistryHandler(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
path := req.URL.EscapedPath()
|
|
|
|
|
|
|
|
if !strings.HasPrefix(path, "/providers/v1/") {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
resp.Write([]byte(`not a provider registry endpoint`))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pathParts := strings.Split(path, "/")[3:]
|
|
|
|
|
|
|
|
if len(pathParts) != 3 {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
resp.Write([]byte(`unrecognized path scheme`))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-21 18:29:10 +02:00
|
|
|
if pathParts[2] != "versions" {
|
2020-05-25 21:24:35 +02:00
|
|
|
resp.WriteHeader(404)
|
|
|
|
resp.Write([]byte(`this registry only supports legacy namespace lookup requests`))
|
2020-08-21 18:29:10 +02:00
|
|
|
return
|
2020-05-25 21:24:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
name := pathParts[1]
|
2020-08-21 18:29:10 +02: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 {
|
|
|
|
resp.Write([]byte(fmt.Sprintf(`{"id":"%s/%s","moved_to":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name, movedNamespace, name)))
|
|
|
|
} else {
|
|
|
|
resp.Write([]byte(fmt.Sprintf(`{"id":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name)))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
resp.Write([]byte(`provider not found`))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also return versions for redirect target
|
|
|
|
if namespace, ok := movedProviderNamespaces[name]; ok && pathParts[0] == namespace {
|
2020-05-25 21:24:35 +02:00
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
|
|
|
resp.WriteHeader(200)
|
2020-08-21 18:29:10 +02:00
|
|
|
resp.Write([]byte(fmt.Sprintf(`{"id":"%s/%s","versions":[{"version":"1.0.0","protocols":["4"]}]}`, namespace, name)))
|
2020-05-25 21:24:35 +02:00
|
|
|
} else {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
resp.Write([]byte(`provider not found`))
|
|
|
|
}
|
|
|
|
}
|