2014-07-10 19:20:21 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
2018-12-07 21:17:55 +01:00
|
|
|
"bytes"
|
2017-06-06 17:34:17 +02:00
|
|
|
"flag"
|
2014-07-10 19:20:21 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-07-10 20:31:07 +02:00
|
|
|
"log"
|
2014-07-10 19:20:21 +02:00
|
|
|
"os"
|
2014-09-24 23:23:29 +02:00
|
|
|
"path/filepath"
|
2016-04-20 18:16:48 +02:00
|
|
|
"reflect"
|
2015-07-12 15:31:09 +02:00
|
|
|
"regexp"
|
2014-07-10 19:20:21 +02:00
|
|
|
"strings"
|
2017-10-14 15:35:36 +02:00
|
|
|
"syscall"
|
2014-07-10 19:20:21 +02:00
|
|
|
"testing"
|
|
|
|
|
2016-04-20 18:16:48 +02:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2017-11-22 00:08:00 +01:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-08-16 20:50:04 +02:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2018-12-07 21:17:55 +01:00
|
|
|
"github.com/mitchellh/colorstring"
|
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-12-07 21:17:55 +01:00
|
|
|
"github.com/hashicorp/terraform/command/format"
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
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/configload"
|
2015-12-08 01:10:30 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/logging"
|
2019-01-09 03:39:14 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/initwd"
|
2018-12-07 02:53:42 +01:00
|
|
|
"github.com/hashicorp/terraform/providers"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2014-07-10 19:20:21 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2018-12-07 21:17:55 +01:00
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
2014-07-10 19:20:21 +02:00
|
|
|
)
|
|
|
|
|
2017-06-06 17:34:17 +02:00
|
|
|
// flagSweep is a flag available when running tests on the command line. It
|
|
|
|
// contains a comma seperated list of regions to for the sweeper functions to
|
|
|
|
// run in. This flag bypasses the normal Test path and instead runs functions designed to
|
|
|
|
// clean up any leaked resources a testing environment could have created. It is
|
|
|
|
// a best effort attempt, and relies on Provider authors to implement "Sweeper"
|
|
|
|
// methods for resources.
|
|
|
|
|
|
|
|
// Adding Sweeper methods with AddTestSweepers will
|
|
|
|
// construct a list of sweeper funcs to be called here. We iterate through
|
|
|
|
// regions provided by the sweep flag, and for each region we iterate through the
|
|
|
|
// tests, and exit on any errors. At time of writing, sweepers are ran
|
|
|
|
// sequentially, however they can list dependencies to be ran first. We track
|
|
|
|
// the sweepers that have been ran, so as to not run a sweeper twice for a given
|
|
|
|
// region.
|
|
|
|
//
|
|
|
|
// WARNING:
|
|
|
|
// Sweepers are designed to be destructive. You should not use the -sweep flag
|
|
|
|
// in any environment that is not strictly a test environment. Resources will be
|
|
|
|
// destroyed.
|
|
|
|
|
|
|
|
var flagSweep = flag.String("sweep", "", "List of Regions to run available Sweepers")
|
|
|
|
var flagSweepRun = flag.String("sweep-run", "", "Comma seperated list of Sweeper Tests to run")
|
|
|
|
var sweeperFuncs map[string]*Sweeper
|
|
|
|
|
|
|
|
// map of sweepers that have ran, and the success/fail status based on any error
|
|
|
|
// raised
|
|
|
|
var sweeperRunList map[string]bool
|
|
|
|
|
|
|
|
// type SweeperFunc is a signature for a function that acts as a sweeper. It
|
|
|
|
// accepts a string for the region that the sweeper is to be ran in. This
|
|
|
|
// function must be able to construct a valid client for that region.
|
|
|
|
type SweeperFunc func(r string) error
|
|
|
|
|
|
|
|
type Sweeper struct {
|
|
|
|
// Name for sweeper. Must be unique to be ran by the Sweeper Runner
|
|
|
|
Name string
|
|
|
|
|
|
|
|
// Dependencies list the const names of other Sweeper functions that must be ran
|
|
|
|
// prior to running this Sweeper. This is an ordered list that will be invoked
|
|
|
|
// recursively at the helper/resource level
|
|
|
|
Dependencies []string
|
|
|
|
|
|
|
|
// Sweeper function that when invoked sweeps the Provider of specific
|
|
|
|
// resources
|
|
|
|
F SweeperFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sweeperFuncs = make(map[string]*Sweeper)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTestSweepers function adds a given name and Sweeper configuration
|
|
|
|
// pair to the internal sweeperFuncs map. Invoke this function to register a
|
|
|
|
// resource sweeper to be available for running when the -sweep flag is used
|
|
|
|
// with `go test`. Sweeper names must be unique to help ensure a given sweeper
|
|
|
|
// is only ran once per run.
|
|
|
|
func AddTestSweepers(name string, s *Sweeper) {
|
|
|
|
if _, ok := sweeperFuncs[name]; ok {
|
|
|
|
log.Fatalf("[ERR] Error adding (%s) to sweeperFuncs: function already exists in map", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sweeperFuncs[name] = s
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
if *flagSweep != "" {
|
|
|
|
// parse flagSweep contents for regions to run
|
|
|
|
regions := strings.Split(*flagSweep, ",")
|
|
|
|
|
|
|
|
// get filtered list of sweepers to run based on sweep-run flag
|
|
|
|
sweepers := filterSweepers(*flagSweepRun, sweeperFuncs)
|
|
|
|
for _, region := range regions {
|
|
|
|
region = strings.TrimSpace(region)
|
|
|
|
// reset sweeperRunList for each region
|
|
|
|
sweeperRunList = map[string]bool{}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Running Sweepers for region (%s):\n", region)
|
|
|
|
for _, sweeper := range sweepers {
|
|
|
|
if err := runSweeperWithRegion(region, sweeper); err != nil {
|
|
|
|
log.Fatalf("[ERR] error running (%s): %s", sweeper.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Sweeper Tests ran:\n")
|
|
|
|
for s, _ := range sweeperRunList {
|
|
|
|
fmt.Printf("\t- %s\n", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterSweepers takes a comma seperated string listing the names of sweepers
|
|
|
|
// to be ran, and returns a filtered set from the list of all of sweepers to
|
|
|
|
// run based on the names given.
|
|
|
|
func filterSweepers(f string, source map[string]*Sweeper) map[string]*Sweeper {
|
|
|
|
filterSlice := strings.Split(strings.ToLower(f), ",")
|
|
|
|
if len(filterSlice) == 1 && filterSlice[0] == "" {
|
|
|
|
// if the filter slice is a single element of "" then no sweeper list was
|
|
|
|
// given, so just return the full list
|
|
|
|
return source
|
|
|
|
}
|
|
|
|
|
|
|
|
sweepers := make(map[string]*Sweeper)
|
|
|
|
for name, sweeper := range source {
|
|
|
|
for _, s := range filterSlice {
|
|
|
|
if strings.Contains(strings.ToLower(name), s) {
|
|
|
|
sweepers[name] = sweeper
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sweepers
|
|
|
|
}
|
|
|
|
|
|
|
|
// runSweeperWithRegion recieves a sweeper and a region, and recursively calls
|
|
|
|
// itself with that region for every dependency found for that sweeper. If there
|
|
|
|
// are no dependencies, invoke the contained sweeper fun with the region, and
|
|
|
|
// add the success/fail status to the sweeperRunList.
|
|
|
|
func runSweeperWithRegion(region string, s *Sweeper) error {
|
|
|
|
for _, dep := range s.Dependencies {
|
|
|
|
if depSweeper, ok := sweeperFuncs[dep]; ok {
|
|
|
|
log.Printf("[DEBUG] Sweeper (%s) has dependency (%s), running..", s.Name, dep)
|
|
|
|
if err := runSweeperWithRegion(region, depSweeper); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[DEBUG] Sweeper (%s) has dependency (%s), but that sweeper was not found", s.Name, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := sweeperRunList[s.Name]; ok {
|
|
|
|
log.Printf("[DEBUG] Sweeper (%s) already ran in region (%s)", s.Name, region)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
runE := s.F(region)
|
|
|
|
if runE == nil {
|
|
|
|
sweeperRunList[s.Name] = true
|
|
|
|
} else {
|
|
|
|
sweeperRunList[s.Name] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return runE
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
const TestEnvVar = "TF_ACC"
|
|
|
|
|
2017-03-08 23:41:35 +01:00
|
|
|
// TestProvider can be implemented by any ResourceProvider to provide custom
|
|
|
|
// reset functionality at the start of an acceptance test.
|
|
|
|
// The helper/schema Provider implements this interface.
|
2017-03-07 16:03:03 +01:00
|
|
|
type TestProvider interface {
|
|
|
|
TestReset() error
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
// TestCheckFunc is the callback type used with acceptance tests to check
|
|
|
|
// the state of a resource. The state passed in is the latest state known,
|
|
|
|
// or in the case of being after a destroy, it is the last known state when
|
|
|
|
// it was created.
|
|
|
|
type TestCheckFunc func(*terraform.State) error
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
// ImportStateCheckFunc is the check function for ImportState tests
|
|
|
|
type ImportStateCheckFunc func([]*terraform.InstanceState) error
|
|
|
|
|
2017-09-07 04:42:16 +02:00
|
|
|
// ImportStateIdFunc is an ID generation function to help with complex ID
|
|
|
|
// generation for ImportState tests.
|
|
|
|
type ImportStateIdFunc func(*terraform.State) (string, error)
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
// TestCase is a single acceptance test case used to test the apply/destroy
|
|
|
|
// lifecycle of a resource in a specific configuration.
|
2014-07-10 19:30:41 +02:00
|
|
|
//
|
|
|
|
// When the destroy plan is executed, the config from the last TestStep
|
|
|
|
// is used to plan it.
|
2014-07-10 19:20:21 +02:00
|
|
|
type TestCase struct {
|
2016-07-01 20:08:43 +02:00
|
|
|
// IsUnitTest allows a test to run regardless of the TF_ACC
|
2016-06-11 13:55:14 +02:00
|
|
|
// environment variable. This should be used with care - only for
|
|
|
|
// fast tests on local resources (e.g. remote state with a local
|
|
|
|
// backend) but can be used to increase confidence in correct
|
|
|
|
// operation of Terraform without waiting for a full acctest run.
|
2016-07-01 20:08:43 +02:00
|
|
|
IsUnitTest bool
|
2016-06-11 13:55:14 +02:00
|
|
|
|
2014-07-10 22:12:47 +02:00
|
|
|
// PreCheck, if non-nil, will be called before any test steps are
|
|
|
|
// executed. It will only be executed in the case that the steps
|
|
|
|
// would run, so it can be used for some validation before running
|
|
|
|
// acceptance tests, such as verifying that keys are setup.
|
|
|
|
PreCheck func()
|
|
|
|
|
2015-04-20 23:08:28 +02:00
|
|
|
// Providers is the ResourceProvider that will be under test.
|
|
|
|
//
|
|
|
|
// Alternately, ProviderFactories can be specified for the providers
|
|
|
|
// that are valid. This takes priority over Providers.
|
|
|
|
//
|
|
|
|
// The end effect of each is the same: specifying the providers that
|
|
|
|
// are used within the tests.
|
2015-03-23 23:36:53 +01:00
|
|
|
Providers map[string]terraform.ResourceProvider
|
|
|
|
ProviderFactories map[string]terraform.ResourceProviderFactory
|
2014-07-10 19:20:21 +02:00
|
|
|
|
2016-05-31 11:13:06 +02:00
|
|
|
// PreventPostDestroyRefresh can be set to true for cases where data sources
|
|
|
|
// are tested alongside real resources
|
|
|
|
PreventPostDestroyRefresh bool
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
// CheckDestroy is called after the resource is finally destroyed
|
|
|
|
// to allow the tester to test that the resource is truly gone.
|
|
|
|
CheckDestroy TestCheckFunc
|
|
|
|
|
|
|
|
// Steps are the apply sequences done within the context of the
|
|
|
|
// same state. Each step can have its own check to verify correctness.
|
|
|
|
Steps []TestStep
|
2016-04-20 20:09:54 +02:00
|
|
|
|
|
|
|
// The settings below control the "ID-only refresh test." This is
|
|
|
|
// an enabled-by-default test that tests that a refresh can be
|
|
|
|
// refreshed with only an ID to result in the same attributes.
|
|
|
|
// This validates completeness of Refresh.
|
|
|
|
//
|
|
|
|
// IDRefreshName is the name of the resource to check. This will
|
|
|
|
// default to the first non-nil primary resource in the state.
|
2016-04-20 21:19:21 +02:00
|
|
|
//
|
|
|
|
// IDRefreshIgnore is a list of configuration keys that will be ignored.
|
2016-04-21 17:32:57 +02:00
|
|
|
IDRefreshName string
|
|
|
|
IDRefreshIgnore []string
|
2014-07-10 19:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestStep is a single apply sequence of a test, done within the
|
|
|
|
// context of a state.
|
|
|
|
//
|
|
|
|
// Multiple TestSteps can be sequenced in a Test to allow testing
|
|
|
|
// potentially complex update logic. In general, simply create/destroy
|
|
|
|
// tests will only need one step.
|
|
|
|
type TestStep struct {
|
2016-05-10 05:32:31 +02:00
|
|
|
// ResourceName should be set to the name of the resource
|
|
|
|
// that is being tested. Example: "aws_instance.foo". Various test
|
|
|
|
// modes use this to auto-detect state information.
|
|
|
|
//
|
|
|
|
// This is only required if the test mode settings below say it is
|
|
|
|
// for the mode you're using.
|
|
|
|
ResourceName string
|
|
|
|
|
2015-06-17 20:58:01 +02:00
|
|
|
// PreConfig is called before the Config is applied to perform any per-step
|
2016-05-09 23:43:39 +02:00
|
|
|
// setup that needs to happen. This is called regardless of "test mode"
|
|
|
|
// below.
|
2015-06-17 20:58:01 +02:00
|
|
|
PreConfig func()
|
|
|
|
|
2018-05-25 16:50:30 +02:00
|
|
|
// Taint is a list of resource addresses to taint prior to the execution of
|
|
|
|
// the step. Be sure to only include this at a step where the referenced
|
|
|
|
// address will be present in state, as it will fail the test if the resource
|
|
|
|
// is missing.
|
|
|
|
//
|
|
|
|
// This option is ignored on ImportState tests, and currently only works for
|
|
|
|
// resources in the root module path.
|
|
|
|
Taint []string
|
|
|
|
|
2016-05-09 23:43:39 +02:00
|
|
|
//---------------------------------------------------------------
|
|
|
|
// Test modes. One of the following groups of settings must be
|
|
|
|
// set to determine what the test step will do. Ideally we would've
|
|
|
|
// used Go interfaces here but there are now hundreds of tests we don't
|
|
|
|
// want to re-type so instead we just determine which step logic
|
|
|
|
// to run based on what settings below are set.
|
|
|
|
//---------------------------------------------------------------
|
|
|
|
|
|
|
|
//---------------------------------------------------------------
|
|
|
|
// Plan, Apply testing
|
|
|
|
//---------------------------------------------------------------
|
|
|
|
|
|
|
|
// Config a string of the configuration to give to Terraform. If this
|
|
|
|
// is set, then the TestCase will execute this step with the same logic
|
|
|
|
// as a `terraform apply`.
|
2014-07-10 19:20:21 +02:00
|
|
|
Config string
|
|
|
|
|
|
|
|
// Check is called after the Config is applied. Use this step to
|
|
|
|
// make your own API calls to check the status of things, and to
|
|
|
|
// inspect the format of the ResourceState itself.
|
|
|
|
//
|
|
|
|
// If an error is returned, the test will fail. In this case, a
|
|
|
|
// destroy plan will still be attempted.
|
|
|
|
//
|
|
|
|
// If this is nil, no check is done on this step.
|
|
|
|
Check TestCheckFunc
|
|
|
|
|
|
|
|
// Destroy will create a destroy plan if set to true.
|
|
|
|
Destroy bool
|
2016-01-06 00:37:54 +01:00
|
|
|
|
|
|
|
// ExpectNonEmptyPlan can be set to true for specific types of tests that are
|
|
|
|
// looking to verify that a diff occurs
|
|
|
|
ExpectNonEmptyPlan bool
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2016-07-01 01:22:20 +02:00
|
|
|
// ExpectError allows the construction of test cases that we expect to fail
|
|
|
|
// with an error. The specified regexp must match against the error for the
|
|
|
|
// test to pass.
|
|
|
|
ExpectError *regexp.Regexp
|
|
|
|
|
2017-03-22 20:42:01 +01:00
|
|
|
// PlanOnly can be set to only run `plan` with this configuration, and not
|
|
|
|
// actually apply it. This is useful for ensuring config changes result in
|
|
|
|
// no-op plans
|
|
|
|
PlanOnly bool
|
|
|
|
|
2018-02-16 20:24:01 +01:00
|
|
|
// PreventDiskCleanup can be set to true for testing terraform modules which
|
|
|
|
// require access to disk at runtime. Note that this will leave files in the
|
|
|
|
// temp folder
|
|
|
|
PreventDiskCleanup bool
|
|
|
|
|
2016-05-31 11:13:06 +02:00
|
|
|
// PreventPostDestroyRefresh can be set to true for cases where data sources
|
|
|
|
// are tested alongside real resources
|
|
|
|
PreventPostDestroyRefresh bool
|
|
|
|
|
2017-08-30 08:59:19 +02:00
|
|
|
// SkipFunc is called before applying config, but after PreConfig
|
|
|
|
// This is useful for defining test steps with platform-dependent checks
|
|
|
|
SkipFunc func() (bool, error)
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
//---------------------------------------------------------------
|
|
|
|
// ImportState testing
|
|
|
|
//---------------------------------------------------------------
|
|
|
|
|
|
|
|
// ImportState, if true, will test the functionality of ImportState
|
|
|
|
// by importing the resource with ResourceName (must be set) and the
|
|
|
|
// ID of that resource.
|
|
|
|
ImportState bool
|
|
|
|
|
|
|
|
// ImportStateId is the ID to perform an ImportState operation with.
|
|
|
|
// This is optional. If it isn't set, then the resource ID is automatically
|
|
|
|
// determined by inspecting the state for ResourceName's ID.
|
|
|
|
ImportStateId string
|
|
|
|
|
2017-03-31 00:11:10 +02:00
|
|
|
// ImportStateIdPrefix is the prefix added in front of ImportStateId.
|
|
|
|
// This can be useful in complex import cases, where more than one
|
|
|
|
// attribute needs to be passed on as the Import ID. Mainly in cases
|
|
|
|
// where the ID is not known, and a known prefix needs to be added to
|
|
|
|
// the unset ImportStateId field.
|
|
|
|
ImportStateIdPrefix string
|
|
|
|
|
2017-09-07 04:42:16 +02:00
|
|
|
// ImportStateIdFunc is a function that can be used to dynamically generate
|
|
|
|
// the ID for the ImportState tests. It is sent the state, which can be
|
|
|
|
// checked to derive the attributes necessary and generate the string in the
|
|
|
|
// desired format.
|
|
|
|
ImportStateIdFunc ImportStateIdFunc
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
// ImportStateCheck checks the results of ImportState. It should be
|
|
|
|
// used to verify that the resulting value of ImportState has the
|
|
|
|
// proper resources, IDs, and attributes.
|
|
|
|
ImportStateCheck ImportStateCheckFunc
|
2016-05-10 18:25:54 +02:00
|
|
|
|
|
|
|
// ImportStateVerify, if true, will also check that the state values
|
|
|
|
// that are finally put into the state after import match for all the
|
2018-06-18 20:30:29 +02:00
|
|
|
// IDs returned by the Import. Note that this checks for strict equality
|
|
|
|
// and does not respect DiffSuppressFunc or CustomizeDiff.
|
2016-05-13 20:27:23 +02:00
|
|
|
//
|
2018-06-18 20:30:29 +02:00
|
|
|
// ImportStateVerifyIgnore is a list of prefixes of fields that should
|
|
|
|
// not be verified to be equal. These can be set to ephemeral fields or
|
|
|
|
// fields that can't be refreshed and don't matter.
|
2016-05-13 20:27:23 +02:00
|
|
|
ImportStateVerify bool
|
|
|
|
ImportStateVerifyIgnore []string
|
2019-01-10 18:20:03 +01:00
|
|
|
|
|
|
|
// provider s is used internally to maintain a reference to the
|
|
|
|
// underlying providers during the tests
|
|
|
|
providers map[string]terraform.ResourceProvider
|
2014-07-10 19:20:21 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 15:35:36 +02:00
|
|
|
// Set to a file mask in sprintf format where %s is test name
|
|
|
|
const EnvLogPathMask = "TF_LOG_PATH_MASK"
|
|
|
|
|
|
|
|
func LogOutput(t TestT) (logOutput io.Writer, err error) {
|
|
|
|
logOutput = ioutil.Discard
|
|
|
|
|
2019-12-05 20:13:37 +01:00
|
|
|
logLevel := logging.CurrentLogLevel()
|
2017-10-14 15:35:36 +02:00
|
|
|
if logLevel == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOutput = os.Stderr
|
2017-10-30 16:58:13 +01:00
|
|
|
|
|
|
|
if logPath := os.Getenv(logging.EnvLogFile); logPath != "" {
|
|
|
|
var err error
|
|
|
|
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 15:35:36 +02:00
|
|
|
if logPathMask := os.Getenv(EnvLogPathMask); logPathMask != "" {
|
2017-10-31 07:48:57 +01:00
|
|
|
// Escape special characters which may appear if we have subtests
|
|
|
|
testName := strings.Replace(t.Name(), "/", "__", -1)
|
|
|
|
|
|
|
|
logPath := fmt.Sprintf(logPathMask, testName)
|
2017-10-14 15:35:36 +02:00
|
|
|
var err error
|
|
|
|
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This was the default since the beginning
|
2019-12-05 20:13:37 +01:00
|
|
|
logOutput = &logging.LevelFilter{
|
2017-10-14 15:35:36 +02:00
|
|
|
Levels: logging.ValidLevels,
|
2019-12-05 20:13:37 +01:00
|
|
|
MinLevel: logging.LogLevel(logLevel),
|
2017-10-14 15:35:36 +02:00
|
|
|
Writer: logOutput,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:03:13 +02:00
|
|
|
// ParallelTest performs an acceptance test on a resource, allowing concurrency
|
|
|
|
// with other ParallelTest.
|
|
|
|
//
|
|
|
|
// Tests will fail if they do not properly handle conditions to allow multiple
|
|
|
|
// tests to occur against the same resource or service (e.g. random naming).
|
|
|
|
// All other requirements of the Test function also apply to this function.
|
|
|
|
func ParallelTest(t TestT, c TestCase) {
|
|
|
|
t.Parallel()
|
|
|
|
Test(t, c)
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
// Test performs an acceptance test on a resource.
|
|
|
|
//
|
|
|
|
// Tests are not run unless an environmental variable "TF_ACC" is
|
|
|
|
// set to some non-empty value. This is to avoid test cases surprising
|
|
|
|
// a user by creating real resources.
|
|
|
|
//
|
|
|
|
// Tests will fail unless the verbose flag (`go test -v`, or explicitly
|
|
|
|
// the "-test.v" flag) is set. Because some acceptance tests take quite
|
|
|
|
// long, we require the verbose flag so users are able to see progress
|
|
|
|
// output.
|
|
|
|
func Test(t TestT, c TestCase) {
|
2018-10-16 03:15:08 +02:00
|
|
|
// We only run acceptance tests if an env var is set because they're
|
|
|
|
// slow and generally require some outside configuration. You can opt out
|
|
|
|
// of this with OverrideEnvVar on individual TestCases.
|
|
|
|
if os.Getenv(TestEnvVar) == "" && !c.IsUnitTest {
|
|
|
|
t.Skip(fmt.Sprintf(
|
|
|
|
"Acceptance tests skipped unless env '%s' set",
|
|
|
|
TestEnvVar))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logWriter, err := LogOutput(t)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(fmt.Errorf("error setting up logging: %s", err))
|
|
|
|
}
|
|
|
|
log.SetOutput(logWriter)
|
|
|
|
|
|
|
|
// We require verbose mode so that the user knows what is going on.
|
|
|
|
if !testTesting && !testing.Verbose() && !c.IsUnitTest {
|
|
|
|
t.Fatal("Acceptance tests must be run with the -v flag on tests")
|
|
|
|
return
|
|
|
|
}
|
2016-05-10 05:32:31 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// Run the PreCheck if we have it
|
|
|
|
if c.PreCheck != nil {
|
|
|
|
c.PreCheck()
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
providerFactories, err := testProviderFactories(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:20:03 +01:00
|
|
|
// get instances of all providers, so we can use the individual
|
|
|
|
// resources to shim the state during the tests.
|
|
|
|
providers := make(map[string]terraform.ResourceProvider)
|
2020-03-31 00:30:56 +02:00
|
|
|
legacyProviderFactories, err := testProviderFactoriesLegacy(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for name, pf := range legacyProviderFactories {
|
2019-01-10 18:20:03 +01:00
|
|
|
p, err := pf()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
providers[name] = p
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
opts := terraform.ContextOpts{Providers: providerFactories}
|
2018-10-16 03:15:08 +02:00
|
|
|
|
|
|
|
// A single state variable to track the lifecycle, starting with no state
|
|
|
|
var state *terraform.State
|
|
|
|
|
|
|
|
// Go through each step and run it
|
|
|
|
var idRefreshCheck *terraform.ResourceState
|
|
|
|
idRefresh := c.IDRefreshName != ""
|
|
|
|
errored := false
|
|
|
|
for i, step := range c.Steps {
|
2019-01-10 18:20:03 +01:00
|
|
|
// insert the providers into the step so we can get the resources for
|
|
|
|
// shimming the state
|
|
|
|
step.providers = providers
|
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
var err error
|
|
|
|
log.Printf("[DEBUG] Test: Executing step %d", i)
|
|
|
|
|
|
|
|
if step.SkipFunc != nil {
|
|
|
|
skip, err := step.SkipFunc()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if skip {
|
|
|
|
log.Printf("[WARN] Skipping step %d", i)
|
|
|
|
continue
|
2018-08-25 01:13:50 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2018-08-25 01:13:50 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
if step.Config == "" && !step.ImportState {
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"unknown test mode for step. Please see TestStep docs\n\n%#v",
|
|
|
|
step)
|
|
|
|
} else {
|
|
|
|
if step.ImportState {
|
|
|
|
if step.Config == "" {
|
|
|
|
step.Config = testProviderConfig(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can optionally set step.Config in addition to
|
|
|
|
// step.ImportState, to provide config for the import.
|
2018-12-07 03:14:31 +01:00
|
|
|
state, err = testStepImportState(opts, state, step)
|
2018-10-16 03:15:08 +02:00
|
|
|
} else {
|
2018-12-07 03:14:31 +01:00
|
|
|
state, err = testStepConfig(opts, state, step)
|
2016-07-01 01:22:20 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-04-20 18:16:48 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// If we expected an error, but did not get one, fail
|
|
|
|
if err == nil && step.ExpectError != nil {
|
|
|
|
errored = true
|
|
|
|
t.Error(fmt.Sprintf(
|
|
|
|
"Step %d, no error received, but expected a match to:\n\n%s\n\n",
|
|
|
|
i, step.ExpectError))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was an error, exit
|
|
|
|
if err != nil {
|
|
|
|
// Perhaps we expected an error? Check if it matches
|
|
|
|
if step.ExpectError != nil {
|
|
|
|
if !step.ExpectError.MatchString(err.Error()) {
|
2018-08-25 01:13:50 +02:00
|
|
|
errored = true
|
|
|
|
t.Error(fmt.Sprintf(
|
2018-10-16 03:15:08 +02:00
|
|
|
"Step %d, expected error:\n\n%s\n\nTo match:\n\n%s\n\n",
|
|
|
|
i, err, step.ExpectError))
|
2016-04-21 17:32:57 +02:00
|
|
|
break
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
} else {
|
|
|
|
errored = true
|
2018-12-07 21:17:55 +01:00
|
|
|
t.Error(fmt.Sprintf("Step %d error: %s", i, detailedErrorMessage(err)))
|
2018-10-16 03:15:08 +02:00
|
|
|
break
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-04-20 18:16:48 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// If we've never checked an id-only refresh and our state isn't
|
|
|
|
// empty, find the first resource and test it.
|
|
|
|
if idRefresh && idRefreshCheck == nil && !state.Empty() {
|
|
|
|
// Find the first non-nil resource in the state
|
|
|
|
for _, m := range state.Modules {
|
|
|
|
if len(m.Resources) > 0 {
|
|
|
|
if v, ok := m.Resources[c.IDRefreshName]; ok {
|
|
|
|
idRefreshCheck = v
|
2018-08-25 01:13:50 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
|
|
|
|
break
|
2018-08-25 01:13:50 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2018-08-25 01:13:50 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// If we have an instance to check for refreshes, do it
|
|
|
|
// immediately. We do it in the middle of another test
|
|
|
|
// because it shouldn't affect the overall state (refresh
|
|
|
|
// is read-only semantically) and we want to fail early if
|
|
|
|
// this fails. If refresh isn't read-only, then this will have
|
|
|
|
// caught a different bug.
|
|
|
|
if idRefreshCheck != nil {
|
|
|
|
log.Printf(
|
|
|
|
"[WARN] Test: Running ID-only refresh check on %s",
|
|
|
|
idRefreshCheck.Primary.ID)
|
|
|
|
if err := testIDOnlyRefresh(c, opts, step, idRefreshCheck); err != nil {
|
|
|
|
log.Printf("[ERROR] Test: ID-only test failed: %s", err)
|
|
|
|
t.Error(fmt.Sprintf(
|
|
|
|
"[ERROR] Test: ID-only test failed: %s", err))
|
|
|
|
break
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2014-07-10 19:20:21 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// If we never checked an id-only refresh, it is a failure.
|
|
|
|
if idRefresh {
|
|
|
|
if !errored && len(c.Steps) > 0 && idRefreshCheck == nil {
|
|
|
|
t.Error("ID-only refresh check never ran.")
|
2016-04-20 20:09:54 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2016-04-20 18:25:23 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
// If we have a state, then run the destroy
|
|
|
|
if state != nil {
|
|
|
|
lastStep := c.Steps[len(c.Steps)-1]
|
|
|
|
destroyStep := TestStep{
|
|
|
|
Config: lastStep.Config,
|
|
|
|
Check: c.CheckDestroy,
|
|
|
|
Destroy: true,
|
|
|
|
PreventDiskCleanup: lastStep.PreventDiskCleanup,
|
|
|
|
PreventPostDestroyRefresh: c.PreventPostDestroyRefresh,
|
2019-01-10 18:20:03 +01:00
|
|
|
providers: providers,
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
2014-07-10 19:20:21 +02:00
|
|
|
|
2018-10-16 03:15:08 +02:00
|
|
|
log.Printf("[WARN] Test: Executing destroy step")
|
2018-12-07 03:14:31 +01:00
|
|
|
state, err := testStep(opts, state, destroyStep)
|
2018-10-16 03:15:08 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(fmt.Sprintf(
|
|
|
|
"Error destroying resource! WARNING: Dangling resources\n"+
|
|
|
|
"may exist. The full state and error is shown below.\n\n"+
|
|
|
|
"Error: %s\n\nState: %s",
|
|
|
|
err,
|
|
|
|
state))
|
2014-07-10 19:20:21 +02:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
} else {
|
|
|
|
log.Printf("[WARN] Skipping destroy test since there is no state.")
|
|
|
|
}
|
2014-07-10 19:20:21 +02:00
|
|
|
}
|
|
|
|
|
2017-07-07 21:24:20 +02:00
|
|
|
// testProviderConfig takes the list of Providers in a TestCase and returns a
|
|
|
|
// config with only empty provider blocks. This is useful for Import, where no
|
|
|
|
// config is provided, but the providers must be defined.
|
|
|
|
func testProviderConfig(c TestCase) string {
|
|
|
|
var lines []string
|
|
|
|
for p := range c.Providers {
|
|
|
|
lines = append(lines, fmt.Sprintf("provider %q {}\n", p))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(lines, "")
|
|
|
|
}
|
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
// testProviderFactoriesLegacy is like testProviderFactories but it returns
|
|
|
|
// providers implementing the legacy interface terraform.ResourceProvider,
|
|
|
|
// rather than the current providers.Interface.
|
|
|
|
//
|
|
|
|
// It also identifies all providers as legacy-style single names rather than
|
|
|
|
// full addresses, for compatibility with legacy code that doesn't understand
|
|
|
|
// FQNs.
|
|
|
|
func testProviderFactoriesLegacy(c TestCase) (map[string]terraform.ResourceProviderFactory, error) {
|
2019-01-08 00:29:51 +01:00
|
|
|
ctxProviders := make(map[string]terraform.ResourceProviderFactory)
|
|
|
|
for k, pf := range c.ProviderFactories {
|
|
|
|
ctxProviders[k] = pf
|
2017-04-19 17:18:30 +02:00
|
|
|
}
|
2017-04-22 02:40:46 +02:00
|
|
|
|
2017-03-07 16:03:03 +01:00
|
|
|
// add any fixed providers
|
|
|
|
for k, p := range c.Providers {
|
|
|
|
ctxProviders[k] = terraform.ResourceProviderFactoryFixed(p)
|
|
|
|
}
|
2020-03-31 00:30:56 +02:00
|
|
|
return ctxProviders, nil
|
2019-01-10 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
// testProviderFactories combines the fixed Providers and
|
|
|
|
// ResourceProviderFactory functions into a single map of
|
|
|
|
// ResourceProviderFactory functions.
|
|
|
|
func testProviderFactories(c TestCase) (map[addrs.Provider]providers.Factory, error) {
|
|
|
|
ctxProviders, err := testProviderFactoriesLegacy(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-07 16:03:03 +01:00
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
// We additionally wrap all of the factories as a GRPCTestProvider, which
|
|
|
|
// allows them to appear as a new-style providers.Interface, rather than
|
|
|
|
// the legacy terraform.ResourceProvider.
|
2019-12-04 17:30:20 +01:00
|
|
|
newProviders := make(map[addrs.Provider]providers.Factory)
|
2020-03-31 00:30:56 +02:00
|
|
|
for legacyName, pf := range ctxProviders {
|
2019-01-08 00:29:51 +01:00
|
|
|
factory := pf // must copy to ensure each closure sees its own value
|
2020-04-03 20:20:24 +02:00
|
|
|
newProviders[addrs.NewDefaultProvider(legacyName)] = func() (providers.Interface, error) {
|
2019-01-08 00:29:51 +01:00
|
|
|
p, err := factory()
|
2017-03-07 16:03:03 +01:00
|
|
|
if err != nil {
|
2018-12-07 02:53:42 +01:00
|
|
|
return nil, err
|
2017-03-07 16:03:03 +01:00
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
|
2018-12-07 02:53:42 +01:00
|
|
|
// The provider is wrapped in a GRPCTestProvider so that it can be
|
|
|
|
// passed back to terraform core as a providers.Interface, rather
|
|
|
|
// than the legacy ResourceProvider.
|
|
|
|
return GRPCTestProvider(p), nil
|
|
|
|
}
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 00:30:56 +02:00
|
|
|
return newProviders, nil
|
2018-10-16 03:15:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 16:11:28 +01:00
|
|
|
// UnitTest is a helper to force the acceptance testing harness to run in the
|
|
|
|
// normal unit test suite. This should only be used for resource that don't
|
|
|
|
// have any external dependencies.
|
|
|
|
func UnitTest(t TestT, c TestCase) {
|
2016-07-01 20:08:43 +02:00
|
|
|
c.IsUnitTest = true
|
2016-03-15 16:11:28 +01:00
|
|
|
Test(t, c)
|
|
|
|
}
|
|
|
|
|
2016-04-20 21:19:21 +02:00
|
|
|
func testIDOnlyRefresh(c TestCase, opts terraform.ContextOpts, step TestStep, r *terraform.ResourceState) error {
|
2016-04-20 18:34:54 +02:00
|
|
|
// TODO: We guard by this right now so master doesn't explode. We
|
|
|
|
// need to remove this eventually to make this part of the normal tests.
|
|
|
|
if os.Getenv("TF_ACC_IDONLY") == "" {
|
|
|
|
return 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
|
|
|
addr := addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: r.Type,
|
|
|
|
Name: "foo",
|
|
|
|
}.Instance(addrs.NoKey)
|
|
|
|
absAddr := addr.Absolute(addrs.RootModuleInstance)
|
2016-04-20 18:16:48 +02:00
|
|
|
|
|
|
|
// Build the state. The state is just the resource with an ID. There
|
|
|
|
// are no attributes. We only set what is needed to perform a refresh.
|
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 := states.NewState()
|
|
|
|
state.RootModule().SetResourceInstanceCurrent(
|
|
|
|
addr,
|
|
|
|
&states.ResourceInstanceObjectSrc{
|
|
|
|
AttrsFlat: r.Primary.Attributes,
|
|
|
|
Status: states.ObjectReady,
|
2016-04-20 18:16:48 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
addrs.AbsProviderConfig{
|
|
|
|
Provider: addrs.NewDefaultProvider("placeholder"),
|
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
|
|
|
)
|
2016-04-20 18:16:48 +02:00
|
|
|
|
2016-04-20 20:18:13 +02:00
|
|
|
// Create the config module. We use the full config because Refresh
|
|
|
|
// doesn't have access to it and we may need things like provider
|
|
|
|
// configurations. The initial implementation of id-only checks used
|
|
|
|
// an empty config module, but that caused the aforementioned problems.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
cfg, err := testConfig(opts, step)
|
2016-04-20 20:18:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the context
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
opts.Config = cfg
|
2016-04-20 18:16:48 +02:00
|
|
|
opts.State = state
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
ctx, ctxDiags := terraform.NewContext(&opts)
|
|
|
|
if ctxDiags.HasErrors() {
|
|
|
|
return ctxDiags.Err()
|
2016-03-11 20:07:54 +01:00
|
|
|
}
|
2017-11-22 00:08:00 +01:00
|
|
|
if diags := ctx.Validate(); len(diags) > 0 {
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return errwrap.Wrapf("config is invalid: {{err}}", diags.Err())
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
2017-11-22 00:08:00 +01:00
|
|
|
log.Printf("[WARN] Config warnings:\n%s", diags.Err().Error())
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh!
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
state, refreshDiags := ctx.Refresh()
|
|
|
|
if refreshDiags.HasErrors() {
|
|
|
|
return refreshDiags.Err()
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify attribute equivalence.
|
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
|
|
|
actualR := state.ResourceInstance(absAddr)
|
2016-04-20 19:48:22 +02:00
|
|
|
if actualR == nil {
|
|
|
|
return fmt.Errorf("Resource gone!")
|
|
|
|
}
|
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
|
|
|
if actualR.Current == nil {
|
2016-04-20 19:48:22 +02:00
|
|
|
return fmt.Errorf("Resource has no primary instance")
|
|
|
|
}
|
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
|
|
|
actual := actualR.Current.AttrsFlat
|
2016-04-20 18:16:48 +02:00
|
|
|
expected := r.Primary.Attributes
|
2016-04-20 21:19:21 +02:00
|
|
|
// Remove fields we're ignoring
|
|
|
|
for _, v := range c.IDRefreshIgnore {
|
2016-04-22 18:37:27 +02:00
|
|
|
for k, _ := range actual {
|
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(actual, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range expected {
|
|
|
|
if strings.HasPrefix(k, v) {
|
|
|
|
delete(expected, k)
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 21:19:21 +02:00
|
|
|
}
|
|
|
|
|
2016-04-20 18:16:48 +02:00
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
2016-04-20 18:34:54 +02:00
|
|
|
// Determine only the different attributes
|
|
|
|
for k, v := range expected {
|
|
|
|
if av, ok := actual[k]; ok && v == av {
|
|
|
|
delete(expected, k)
|
|
|
|
delete(actual, k)
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 18:16:48 +02:00
|
|
|
|
2016-04-20 18:34:54 +02:00
|
|
|
spewConf := spew.NewDefaultConfig()
|
|
|
|
spewConf.SortKeys = true
|
2016-04-20 18:16:48 +02:00
|
|
|
return fmt.Errorf(
|
2016-04-20 18:34:54 +02:00
|
|
|
"Attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
|
2016-04-20 18:16:48 +02:00
|
|
|
"\n\n%s\n\n%s",
|
2016-04-20 18:34:54 +02:00
|
|
|
spewConf.Sdump(actual), spewConf.Sdump(expected))
|
2016-04-20 18:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func testConfig(opts terraform.ContextOpts, step TestStep) (*configs.Config, error) {
|
2016-04-20 20:18:13 +02:00
|
|
|
if step.PreConfig != nil {
|
|
|
|
step.PreConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgPath, err := ioutil.TempDir("", "tf-test")
|
|
|
|
if err != nil {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return nil, fmt.Errorf("Error creating temporary directory for config: %s", err)
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
2018-02-16 20:24:01 +01:00
|
|
|
|
|
|
|
if step.PreventDiskCleanup {
|
|
|
|
log.Printf("[INFO] Skipping defer os.RemoveAll call")
|
|
|
|
} else {
|
|
|
|
defer os.RemoveAll(cfgPath)
|
|
|
|
}
|
2016-04-20 20:18:13 +02:00
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// Write the main configuration file
|
|
|
|
err = ioutil.WriteFile(filepath.Join(cfgPath, "main.tf"), []byte(step.Config), os.ModePerm)
|
2016-04-20 20:18:13 +02:00
|
|
|
if err != nil {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return nil, fmt.Errorf("Error creating temporary file for config: %s", err)
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// Create directory for our child modules, if any.
|
|
|
|
modulesDir := filepath.Join(cfgPath, ".modules")
|
|
|
|
err = os.Mkdir(modulesDir, os.ModePerm)
|
2016-04-20 20:18:13 +02:00
|
|
|
if err != nil {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return nil, fmt.Errorf("Error creating child modules directory: %s", err)
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
2019-01-09 03:39:14 +01:00
|
|
|
inst := initwd.NewModuleInstaller(modulesDir, 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
|
|
|
_, installDiags := inst.InstallModules(cfgPath, true, initwd.ModuleInstallHooksImpl{})
|
2019-01-09 03:39:14 +01:00
|
|
|
if installDiags.HasErrors() {
|
|
|
|
return nil, installDiags.Err()
|
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
loader, err := configload.NewLoader(&configload.Config{
|
|
|
|
ModulesDir: modulesDir,
|
|
|
|
})
|
2016-04-20 20:18:13 +02:00
|
|
|
if err != nil {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return nil, fmt.Errorf("failed to create config loader: %s", err)
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
config, configDiags := loader.LoadConfig(cfgPath)
|
|
|
|
if configDiags.HasErrors() {
|
|
|
|
return nil, configDiags
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return config, nil
|
2016-04-20 20:18:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 05:32:31 +02:00
|
|
|
func testResource(c TestStep, state *terraform.State) (*terraform.ResourceState, error) {
|
|
|
|
if c.ResourceName == "" {
|
|
|
|
return nil, fmt.Errorf("ResourceName must be set in TestStep")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range state.Modules {
|
|
|
|
if len(m.Resources) > 0 {
|
|
|
|
if v, ok := m.Resources[c.ResourceName]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Resource specified by ResourceName couldn't be found: %s", c.ResourceName)
|
|
|
|
}
|
|
|
|
|
2014-07-10 22:29:38 +02:00
|
|
|
// ComposeTestCheckFunc lets you compose multiple TestCheckFuncs into
|
|
|
|
// a single TestCheckFunc.
|
|
|
|
//
|
|
|
|
// As a user testing their provider, this lets you decompose your checks
|
|
|
|
// into smaller pieces more easily.
|
|
|
|
func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2016-01-29 00:56:19 +01:00
|
|
|
for i, f := range fs {
|
2014-07-10 22:29:38 +02:00
|
|
|
if err := f(s); err != nil {
|
2016-01-29 00:56:19 +01:00
|
|
|
return fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)
|
2014-07-10 22:29:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-16 20:50:04 +02:00
|
|
|
// ComposeAggregateTestCheckFunc lets you compose multiple TestCheckFuncs into
|
|
|
|
// a single TestCheckFunc.
|
|
|
|
//
|
|
|
|
// As a user testing their provider, this lets you decompose your checks
|
|
|
|
// into smaller pieces more easily.
|
|
|
|
//
|
|
|
|
// Unlike ComposeTestCheckFunc, ComposeAggergateTestCheckFunc runs _all_ of the
|
|
|
|
// TestCheckFuncs and aggregates failures.
|
|
|
|
func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
var result *multierror.Error
|
|
|
|
|
|
|
|
for i, f := range fs {
|
|
|
|
if err := f(s); err != nil {
|
|
|
|
result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.ErrorOrNil()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:42:25 +02:00
|
|
|
// TestCheckResourceAttrSet is a TestCheckFunc which ensures a value
|
|
|
|
// exists in state for the given name/key combination. It is useful when
|
|
|
|
// testing that computed values were set, when it is not possible to
|
|
|
|
// know ahead of time what the values will be.
|
|
|
|
func TestCheckResourceAttrSet(name, key string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2017-02-13 17:24:55 +01:00
|
|
|
is, err := primaryInstanceState(s, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-15 22:42:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckResourceAttrSet(is, name, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckModuleResourceAttrSet - as per TestCheckResourceAttrSet but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestCheckModuleResourceAttrSet(mp []string, name string, key string) TestCheckFunc {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
mpt := addrs.Module(mp).UnkeyedInstanceShim()
|
2017-12-18 14:58:10 +01:00
|
|
|
return func(s *terraform.State) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
is, err := modulePathPrimaryInstanceState(s, mpt, name)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-15 22:42:25 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckResourceAttrSet(is, name, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCheckResourceAttrSet(is *terraform.InstanceState, name string, key string) error {
|
|
|
|
if val, ok := is.Attributes[key]; !ok || val == "" {
|
2016-08-15 22:42:25 +02:00
|
|
|
return fmt.Errorf("%s: Attribute '%s' expected to be set", name, key)
|
|
|
|
}
|
2017-12-18 14:58:10 +01:00
|
|
|
|
|
|
|
return nil
|
2016-08-15 22:42:25 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 17:24:55 +01:00
|
|
|
// TestCheckResourceAttr is a TestCheckFunc which validates
|
|
|
|
// the value in state for the given name/key combination.
|
2014-07-10 23:00:20 +02:00
|
|
|
func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2017-02-13 17:24:55 +01:00
|
|
|
is, err := primaryInstanceState(s, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-09-17 02:02:05 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckResourceAttr(is, name, key, value)
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 08:28:48 +01:00
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
// TestCheckModuleResourceAttr - as per TestCheckResourceAttr but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestCheckModuleResourceAttr(mp []string, name string, key string, value string) TestCheckFunc {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
mpt := addrs.Module(mp).UnkeyedInstanceShim()
|
2017-12-18 14:58:10 +01:00
|
|
|
return func(s *terraform.State) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
is, err := modulePathPrimaryInstanceState(s, mpt, name)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-10 23:00:20 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckResourceAttr(is, name, key, value)
|
2014-07-10 23:00:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
func testCheckResourceAttr(is *terraform.InstanceState, name string, key string, value string) error {
|
2019-01-09 19:01:17 +01:00
|
|
|
// Empty containers may be elided from the state.
|
|
|
|
// If the intent here is to check for an empty container, allow the key to
|
|
|
|
// also be non-existent.
|
|
|
|
emptyCheck := false
|
|
|
|
if value == "0" && (strings.HasSuffix(key, ".#") || strings.HasSuffix(key, ".%")) {
|
|
|
|
emptyCheck = true
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
if v, ok := is.Attributes[key]; !ok || v != value {
|
2019-01-09 19:01:17 +01:00
|
|
|
if emptyCheck && !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%s: Attribute '%s' not found", name, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: Attribute '%s' expected %#v, got %#v",
|
|
|
|
name,
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-23 22:53:02 +01:00
|
|
|
// TestCheckNoResourceAttr is a TestCheckFunc which ensures that
|
|
|
|
// NO value exists in state for the given name/key combination.
|
|
|
|
func TestCheckNoResourceAttr(name, key string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2017-02-13 17:24:55 +01:00
|
|
|
is, err := primaryInstanceState(s, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-23 22:53:02 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckNoResourceAttr(is, name, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckModuleNoResourceAttr - as per TestCheckNoResourceAttr but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestCheckModuleNoResourceAttr(mp []string, name string, key string) TestCheckFunc {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
mpt := addrs.Module(mp).UnkeyedInstanceShim()
|
2017-12-18 14:58:10 +01:00
|
|
|
return func(s *terraform.State) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
is, err := modulePathPrimaryInstanceState(s, mpt, name)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-23 22:53:02 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckNoResourceAttr(is, name, key)
|
2017-01-23 22:53:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
func testCheckNoResourceAttr(is *terraform.InstanceState, name string, key string) error {
|
2019-01-09 19:09:02 +01:00
|
|
|
// Empty containers may sometimes be included in the state.
|
|
|
|
// If the intent here is to check for an empty container, allow the value to
|
|
|
|
// also be "0".
|
|
|
|
emptyCheck := false
|
|
|
|
if strings.HasSuffix(key, ".#") || strings.HasSuffix(key, ".%") {
|
|
|
|
emptyCheck = true
|
|
|
|
}
|
|
|
|
|
|
|
|
val, exists := is.Attributes[key]
|
|
|
|
if emptyCheck && val == "0" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2017-12-18 14:58:10 +01:00
|
|
|
return fmt.Errorf("%s: Attribute '%s' found when not expected", name, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-13 17:24:55 +01:00
|
|
|
// TestMatchResourceAttr is a TestCheckFunc which checks that the value
|
|
|
|
// in state for the given name/key combination matches the given regex.
|
2015-07-12 15:31:09 +02:00
|
|
|
func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2017-02-13 17:24:55 +01:00
|
|
|
is, err := primaryInstanceState(s, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-12 15:31:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testMatchResourceAttr(is, name, key, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestModuleMatchResourceAttr - as per TestMatchResourceAttr but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestModuleMatchResourceAttr(mp []string, name string, key string, r *regexp.Regexp) TestCheckFunc {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
mpt := addrs.Module(mp).UnkeyedInstanceShim()
|
2017-12-18 14:58:10 +01:00
|
|
|
return func(s *terraform.State) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
is, err := modulePathPrimaryInstanceState(s, mpt, name)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-12 15:31:09 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testMatchResourceAttr(is, name, key, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMatchResourceAttr(is *terraform.InstanceState, name string, key string, r *regexp.Regexp) error {
|
|
|
|
if !r.MatchString(is.Attributes[key]) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: Attribute '%s' didn't match %q, got %#v",
|
|
|
|
name,
|
|
|
|
key,
|
|
|
|
r.String(),
|
|
|
|
is.Attributes[key])
|
2015-07-12 15:31:09 +02:00
|
|
|
}
|
2017-12-18 14:58:10 +01:00
|
|
|
|
|
|
|
return nil
|
2015-07-12 15:31:09 +02:00
|
|
|
}
|
|
|
|
|
2015-02-18 01:12:02 +01:00
|
|
|
// TestCheckResourceAttrPtr is like TestCheckResourceAttr except the
|
|
|
|
// value is a pointer so that it can be updated while the test is running.
|
|
|
|
// It will only be dereferenced at the point this step is run.
|
|
|
|
func TestCheckResourceAttrPtr(name string, key string, value *string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
return TestCheckResourceAttr(name, key, *value)(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
// TestCheckModuleResourceAttrPtr - as per TestCheckResourceAttrPtr but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestCheckModuleResourceAttrPtr(mp []string, name string, key string, value *string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
return TestCheckModuleResourceAttr(mp, name, key, *value)(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 17:24:55 +01:00
|
|
|
// TestCheckResourceAttrPair is a TestCheckFunc which validates that the values
|
|
|
|
// in state for a pair of name/key combinations are equal.
|
|
|
|
func TestCheckResourceAttrPair(nameFirst, keyFirst, nameSecond, keySecond string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
isFirst, err := primaryInstanceState(s, nameFirst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
isSecond, err := primaryInstanceState(s, nameSecond)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-12-18 14:58:10 +01:00
|
|
|
|
|
|
|
return testCheckResourceAttrPair(isFirst, nameFirst, keyFirst, isSecond, nameSecond, keySecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCheckModuleResourceAttrPair - as per TestCheckResourceAttrPair but with
|
|
|
|
// support for non-root modules
|
|
|
|
func TestCheckModuleResourceAttrPair(mpFirst []string, nameFirst string, keyFirst string, mpSecond []string, nameSecond string, keySecond string) TestCheckFunc {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
mptFirst := addrs.Module(mpFirst).UnkeyedInstanceShim()
|
|
|
|
mptSecond := addrs.Module(mpSecond).UnkeyedInstanceShim()
|
2017-12-18 14:58:10 +01:00
|
|
|
return func(s *terraform.State) error {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
isFirst, err := modulePathPrimaryInstanceState(s, mptFirst, nameFirst)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-13 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
isSecond, err := modulePathPrimaryInstanceState(s, mptSecond, nameSecond)
|
2017-12-18 14:58:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-02-13 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return testCheckResourceAttrPair(isFirst, nameFirst, keyFirst, isSecond, nameSecond, keySecond)
|
2017-02-13 17:24:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
func testCheckResourceAttrPair(isFirst *terraform.InstanceState, nameFirst string, keyFirst string, isSecond *terraform.InstanceState, nameSecond string, keySecond string) error {
|
2019-02-01 17:03:07 +01:00
|
|
|
vFirst, okFirst := isFirst.Attributes[keyFirst]
|
|
|
|
vSecond, okSecond := isSecond.Attributes[keySecond]
|
2019-02-11 17:27:32 +01:00
|
|
|
|
|
|
|
// Container count values of 0 should not be relied upon, and not reliably
|
|
|
|
// maintained by helper/schema. For the purpose of tests, consider unset and
|
|
|
|
// 0 to be equal.
|
|
|
|
if len(keyFirst) > 2 && len(keySecond) > 2 && keyFirst[len(keyFirst)-2:] == keySecond[len(keySecond)-2:] &&
|
|
|
|
(strings.HasSuffix(keyFirst, ".#") || strings.HasSuffix(keyFirst, ".%")) {
|
|
|
|
// they have the same suffix, and it is a collection count key.
|
|
|
|
if vFirst == "0" || vFirst == "" {
|
|
|
|
okFirst = false
|
|
|
|
}
|
|
|
|
if vSecond == "0" || vSecond == "" {
|
|
|
|
okSecond = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 17:03:07 +01:00
|
|
|
if okFirst != okSecond {
|
|
|
|
if !okFirst {
|
|
|
|
return fmt.Errorf("%s: Attribute %q not set, but %q is set in %s as %q", nameFirst, keyFirst, keySecond, nameSecond, vSecond)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s: Attribute %q is %q, but %q is not set in %s", nameFirst, keyFirst, vFirst, keySecond, nameSecond)
|
2017-12-18 14:58:10 +01:00
|
|
|
}
|
2019-02-01 17:03:07 +01:00
|
|
|
if !(okFirst || okSecond) {
|
|
|
|
// If they both don't exist then they are equally unset, so that's okay.
|
|
|
|
return nil
|
2017-12-18 14:58:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if vFirst != vSecond {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: Attribute '%s' expected %#v, got %#v",
|
|
|
|
nameFirst,
|
|
|
|
keyFirst,
|
|
|
|
vSecond,
|
|
|
|
vFirst)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:54:02 +01:00
|
|
|
// TestCheckOutput checks an output in the Terraform configuration
|
|
|
|
func TestCheckOutput(name, value string) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
ms := s.RootModule()
|
|
|
|
rs, ok := ms.Outputs[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
2016-05-12 02:05:02 +02:00
|
|
|
if rs.Value != value {
|
2016-03-21 17:54:02 +01:00
|
|
|
return fmt.Errorf(
|
|
|
|
"Output '%s': expected %#v, got %#v",
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
rs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-26 23:18:17 +02:00
|
|
|
func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
ms := s.RootModule()
|
|
|
|
rs, ok := ms.Outputs[name]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.MatchString(rs.Value.(string)) {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Output '%s': %#v didn't match %q",
|
|
|
|
name,
|
|
|
|
rs,
|
|
|
|
r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-10 19:20:21 +02:00
|
|
|
// TestT is the interface used to handle the test lifecycle of a test.
|
|
|
|
//
|
|
|
|
// Users should just use a *testing.T object, which implements this.
|
|
|
|
type TestT interface {
|
|
|
|
Error(args ...interface{})
|
|
|
|
Fatal(args ...interface{})
|
|
|
|
Skip(args ...interface{})
|
2017-10-14 15:35:36 +02:00
|
|
|
Name() string
|
2018-08-15 20:03:13 +02:00
|
|
|
Parallel()
|
2014-07-10 19:20:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is set to true by unit tests to alter some behavior
|
|
|
|
var testTesting = false
|
2017-02-13 17:24:55 +01:00
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
// modulePrimaryInstanceState returns the instance state for the given resource
|
|
|
|
// name in a ModuleState
|
|
|
|
func modulePrimaryInstanceState(s *terraform.State, ms *terraform.ModuleState, name string) (*terraform.InstanceState, error) {
|
2017-02-13 17:24:55 +01:00
|
|
|
rs, ok := ms.Resources[name]
|
|
|
|
if !ok {
|
2017-12-18 14:58:10 +01:00
|
|
|
return nil, fmt.Errorf("Not found: %s in %s", name, ms.Path)
|
2017-02-13 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
is := rs.Primary
|
|
|
|
if is == nil {
|
2017-12-18 14:58:10 +01:00
|
|
|
return nil, fmt.Errorf("No primary instance: %s in %s", name, ms.Path)
|
2017-02-13 17:24:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return is, nil
|
|
|
|
}
|
2017-12-18 14:58:10 +01:00
|
|
|
|
|
|
|
// modulePathPrimaryInstanceState returns the primary instance state for the
|
|
|
|
// given resource name in a given module path.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func modulePathPrimaryInstanceState(s *terraform.State, mp addrs.ModuleInstance, name string) (*terraform.InstanceState, error) {
|
2017-12-18 14:58:10 +01:00
|
|
|
ms := s.ModuleByPath(mp)
|
2018-02-16 20:32:49 +01:00
|
|
|
if ms == nil {
|
|
|
|
return nil, fmt.Errorf("No module found at: %s", mp)
|
|
|
|
}
|
|
|
|
|
2017-12-18 14:58:10 +01:00
|
|
|
return modulePrimaryInstanceState(s, ms, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// primaryInstanceState returns the primary instance state for the given
|
|
|
|
// resource name in the root module.
|
|
|
|
func primaryInstanceState(s *terraform.State, name string) (*terraform.InstanceState, error) {
|
|
|
|
ms := s.RootModule()
|
|
|
|
return modulePrimaryInstanceState(s, ms, name)
|
|
|
|
}
|
2018-12-07 21:17:55 +01:00
|
|
|
|
|
|
|
// operationError is a specialized implementation of error used to describe
|
|
|
|
// failures during one of the several operations performed for a particular
|
|
|
|
// test case.
|
|
|
|
type operationError struct {
|
|
|
|
OpName string
|
|
|
|
Diags tfdiags.Diagnostics
|
|
|
|
}
|
|
|
|
|
|
|
|
func newOperationError(opName string, diags tfdiags.Diagnostics) error {
|
|
|
|
return operationError{opName, diags}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a terse error string containing just the basic diagnostic
|
|
|
|
// messages, for situations where normal Go error behavior is appropriate.
|
|
|
|
func (err operationError) Error() string {
|
|
|
|
return fmt.Sprintf("errors during %s: %s", err.OpName, err.Diags.Err().Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorDetail is like Error except it includes verbosely-rendered diagnostics
|
|
|
|
// similar to what would come from a normal Terraform run, which include
|
|
|
|
// additional context not included in Error().
|
|
|
|
func (err operationError) ErrorDetail() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "errors during %s:", err.OpName)
|
|
|
|
clr := &colorstring.Colorize{Disable: true, Colors: colorstring.DefaultColors}
|
|
|
|
for _, diag := range err.Diags {
|
|
|
|
diagStr := format.Diagnostic(diag, nil, clr, 78)
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteString(diagStr)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// detailedErrorMessage is a helper for calling ErrorDetail on an error if
|
|
|
|
// it is an operationError or just taking Error otherwise.
|
|
|
|
func detailedErrorMessage(err error) string {
|
|
|
|
switch tErr := err.(type) {
|
|
|
|
case operationError:
|
|
|
|
return tErr.ErrorDetail()
|
|
|
|
default:
|
|
|
|
return err.Error()
|
|
|
|
}
|
|
|
|
}
|