mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-06 14:13:16 -06:00
89b05050ec
Previously terraform.Context was built in an unfortunate way where all of the data was provided up front in terraform.NewContext and then mutated directly by subsequent operations. That made the data flow hard to follow, commonly leading to bugs, and also meant that we were forced to take various actions too early in terraform.NewContext, rather than waiting until a more appropriate time during an operation. This (enormous) commit changes terraform.Context so that its fields are broadly just unchanging data about the execution context (current workspace name, available plugins, etc) whereas the main data Terraform works with arrives via individual method arguments and is returned in return values. Specifically, this means that terraform.Context no longer "has-a" config, state, and "planned changes", instead holding on to those only temporarily during an operation. The caller is responsible for propagating the outcome of one step into the next step so that the data flow between operations is actually visible. However, since that's a change to the main entry points in the "terraform" package, this commit also touches every file in the codebase which interacted with those APIs. Most of the noise here is in updating tests to take the same actions using the new API style, but this also affects the main-code callers in the backends and in the command package. My goal here was to refactor without changing observable behavior, but in practice there are a couple externally-visible behavior variations here that seemed okay in service of the broader goal: - The "terraform graph" command is no longer hooked directly into the core graph builders, because that's no longer part of the public API. However, I did include a couple new Context functions whose contract is to produce a UI-oriented graph, and _for now_ those continue to return the physical graph we use for those operations. There's no exported API for generating the "validate" and "eval" graphs, because neither is particularly interesting in its own right, and so "terraform graph" no longer supports those graph types. - terraform.NewContext no longer has the responsibility for collecting all of the provider schemas up front. Instead, we wait until we need them. However, that means that some of our error messages now have a slightly different shape due to unwinding through a differently-shaped call stack. As of this commit we also end up reloading the schemas multiple times in some cases, which is functionally acceptable but likely represents a performance regression. I intend to rework this to use caching, but I'm saving that for a later commit because this one is big enough already. The proximal reason for this change is to resolve the chicken/egg problem whereby there was previously no single point where we could apply "moved" statements to the previous run state before creating a plan. With this change in place, we can now do that as part of Context.Plan, prior to forking the input state into the three separate state artifacts we use during planning. However, this is at least the third project in a row where the previous API design led to piling more functionality into terraform.NewContext and then working around the incorrect order of operations that produces, so I intend that by paying the cost/risk of this large diff now we can in turn reduce the cost/risk of future projects that relate to our main workflow actions.
452 lines
12 KiB
Go
452 lines
12 KiB
Go
package terraform
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
|
"github.com/hashicorp/terraform/internal/plans"
|
|
"github.com/hashicorp/terraform/internal/providers"
|
|
"github.com/hashicorp/terraform/internal/states"
|
|
)
|
|
|
|
func TestContext2Input_provider(t *testing.T) {
|
|
m := testModule(t, "input-provider")
|
|
p := testProvider("aws")
|
|
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
Provider: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {
|
|
Type: cty.String,
|
|
Required: true,
|
|
Description: "something something",
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"aws_instance": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {
|
|
Type: cty.String,
|
|
Computed: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
inp := &MockUIInput{
|
|
InputReturnMap: map[string]string{
|
|
"provider.aws.foo": "bar",
|
|
},
|
|
}
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: inp,
|
|
})
|
|
|
|
var actual interface{}
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
actual = req.Config.GetAttr("foo").AsString()
|
|
return
|
|
}
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
|
|
if !inp.InputCalled {
|
|
t.Fatal("no input prompt; want prompt for argument \"foo\"")
|
|
}
|
|
if got, want := inp.InputOpts.Description, "something something"; got != want {
|
|
t.Errorf("wrong description\ngot: %q\nwant: %q", got, want)
|
|
}
|
|
|
|
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
|
|
assertNoErrors(t, diags)
|
|
|
|
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
|
t.Fatalf("apply errors: %s", diags.Err())
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, "bar") {
|
|
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", actual, "bar")
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerMulti(t *testing.T) {
|
|
m := testModule(t, "input-provider-multi")
|
|
|
|
p := testProvider("aws")
|
|
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
Provider: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {
|
|
Type: cty.String,
|
|
Required: true,
|
|
Description: "something something",
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"aws_instance": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {
|
|
Type: cty.String,
|
|
Computed: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
inp := &MockUIInput{
|
|
InputReturnMap: map[string]string{
|
|
"provider.aws.foo": "bar",
|
|
"provider.aws.east.foo": "bar",
|
|
},
|
|
}
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: inp,
|
|
})
|
|
|
|
var actual []interface{}
|
|
var lock sync.Mutex
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
|
|
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
|
|
assertNoErrors(t, diags)
|
|
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
lock.Lock()
|
|
defer lock.Unlock()
|
|
actual = append(actual, req.Config.GetAttr("foo").AsString())
|
|
return
|
|
}
|
|
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
|
t.Fatalf("apply errors: %s", diags.Err())
|
|
}
|
|
|
|
expected := []interface{}{"bar", "bar"}
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerOnce(t *testing.T) {
|
|
m := testModule(t, "input-provider-once")
|
|
p := testProvider("aws")
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
})
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerId(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
|
|
m := testModule(t, "input-provider")
|
|
|
|
p := testProvider("aws")
|
|
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
Provider: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {
|
|
Type: cty.String,
|
|
Required: true,
|
|
Description: "something something",
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"aws_instance": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {
|
|
Type: cty.String,
|
|
Computed: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
var actual interface{}
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
actual = req.Config.GetAttr("foo").AsString()
|
|
return
|
|
}
|
|
|
|
input.InputReturnMap = map[string]string{
|
|
"provider.aws.foo": "bar",
|
|
}
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
|
|
plan, diags := ctx.Plan(m, states.NewState(), DefaultPlanOpts)
|
|
assertNoErrors(t, diags)
|
|
|
|
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
|
t.Fatalf("apply errors: %s", diags.Err())
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, "bar") {
|
|
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", actual, "bar")
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerOnly(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
|
|
m := testModule(t, "input-provider-vars")
|
|
p := testProvider("aws")
|
|
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
Provider: &configschema.Block{
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {
|
|
Type: cty.String,
|
|
Required: true,
|
|
},
|
|
},
|
|
},
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"aws_instance": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.String, Required: true},
|
|
"id": {Type: cty.String, Computed: true},
|
|
"type": {Type: cty.String, Computed: true},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
input.InputReturnMap = map[string]string{
|
|
"provider.aws.foo": "bar",
|
|
}
|
|
|
|
var actual interface{}
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
actual = req.Config.GetAttr("foo").AsString()
|
|
return
|
|
}
|
|
|
|
if err := ctx.Input(m, InputModeProvider); err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
// NOTE: This is a stale test case from an older version of Terraform
|
|
// where Input was responsible for prompting for both input variables _and_
|
|
// provider configuration arguments, where it was trying to test the case
|
|
// where we were turning off the mode of prompting for input variables.
|
|
// That's now always disabled, and so this is essentially the same as the
|
|
// normal Input test, but we're preserving it until we have time to review
|
|
// and make sure this isn't inadvertently providing unique test coverage
|
|
// other than what it set out to test.
|
|
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
|
|
Mode: plans.NormalMode,
|
|
SetVariables: InputValues{
|
|
"foo": &InputValue{
|
|
Value: cty.StringVal("us-west-2"),
|
|
SourceType: ValueFromCaller,
|
|
},
|
|
},
|
|
})
|
|
assertNoErrors(t, diags)
|
|
|
|
state, err := ctx.Apply(plan, m)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, "bar") {
|
|
t.Fatalf("wrong result\ngot: %#v\nwant: %#v", actual, "bar")
|
|
}
|
|
|
|
actualStr := strings.TrimSpace(state.String())
|
|
expectedStr := strings.TrimSpace(testTerraformInputProviderOnlyStr)
|
|
if actualStr != expectedStr {
|
|
t.Fatalf("wrong result\n\ngot:\n%s\n\nwant:\n%s", actualStr, expectedStr)
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerVars(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
m := testModule(t, "input-provider-with-vars")
|
|
p := testProvider("aws")
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
input.InputReturnMap = map[string]string{
|
|
"var.foo": "bar",
|
|
}
|
|
|
|
var actual interface{}
|
|
p.ConfigureProviderFn = func(req providers.ConfigureProviderRequest) (resp providers.ConfigureProviderResponse) {
|
|
actual = req.Config.GetAttr("foo").AsString()
|
|
return
|
|
}
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
|
|
plan, diags := ctx.Plan(m, states.NewState(), &PlanOpts{
|
|
Mode: plans.NormalMode,
|
|
SetVariables: InputValues{
|
|
"foo": &InputValue{
|
|
Value: cty.StringVal("bar"),
|
|
SourceType: ValueFromCaller,
|
|
},
|
|
},
|
|
})
|
|
assertNoErrors(t, diags)
|
|
|
|
if _, diags := ctx.Apply(plan, m); diags.HasErrors() {
|
|
t.Fatalf("apply errors: %s", diags.Err())
|
|
}
|
|
|
|
if !reflect.DeepEqual(actual, "bar") {
|
|
t.Fatalf("bad: %#v", actual)
|
|
}
|
|
}
|
|
|
|
func TestContext2Input_providerVarsModuleInherit(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
m := testModule(t, "input-provider-with-vars-and-module")
|
|
p := testProvider("aws")
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
}
|
|
|
|
// adding a list interpolation in fails to interpolate the count variable
|
|
func TestContext2Input_submoduleTriggersInvalidCount(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
m := testModule(t, "input-submodule-count")
|
|
p := testProvider("aws")
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
}
|
|
|
|
// In this case, a module variable can't be resolved from a data source until
|
|
// it's refreshed, but it can't be refreshed during Input.
|
|
func TestContext2Input_dataSourceRequiresRefresh(t *testing.T) {
|
|
input := new(MockUIInput)
|
|
p := testProvider("null")
|
|
m := testModule(t, "input-module-data-vars")
|
|
|
|
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&ProviderSchema{
|
|
DataSources: map[string]*configschema.Block{
|
|
"null_data_source": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"foo": {Type: cty.List(cty.String), Optional: true},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
|
|
return providers.ReadDataSourceResponse{
|
|
State: req.Config,
|
|
}
|
|
}
|
|
|
|
state := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.DataResourceMode,
|
|
Type: "null_data_source",
|
|
Name: "bar",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
AttrsFlat: map[string]string{
|
|
"id": "-",
|
|
"foo.#": "1",
|
|
"foo.0": "a",
|
|
// foo.1 exists in the data source, but needs to be refreshed.
|
|
},
|
|
Status: states.ObjectReady,
|
|
},
|
|
addrs.AbsProviderConfig{
|
|
Provider: addrs.NewDefaultProvider("null"),
|
|
Module: addrs.RootModule,
|
|
},
|
|
)
|
|
})
|
|
|
|
ctx := testContext2(t, &ContextOpts{
|
|
Providers: map[addrs.Provider]providers.Factory{
|
|
addrs.NewDefaultProvider("null"): testProviderFuncFixed(p),
|
|
},
|
|
UIInput: input,
|
|
})
|
|
|
|
if diags := ctx.Input(m, InputModeStd); diags.HasErrors() {
|
|
t.Fatalf("input errors: %s", diags.Err())
|
|
}
|
|
|
|
// ensure that plan works after Refresh. This is a legacy test that
|
|
// doesn't really make sense anymore, because Refresh is really just
|
|
// a wrapper around plan anyway, but we're keeping it until we get a
|
|
// chance to review and check whether it's giving us any additional
|
|
// test coverage aside from what it's specifically intending to test.
|
|
if _, diags := ctx.Refresh(m, state, DefaultPlanOpts); diags.HasErrors() {
|
|
t.Fatalf("refresh errors: %s", diags.Err())
|
|
}
|
|
if _, diags := ctx.Plan(m, state, DefaultPlanOpts); diags.HasErrors() {
|
|
t.Fatalf("plan errors: %s", diags.Err())
|
|
}
|
|
}
|