mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-04-29 15:01:44 -04:00
* Introduce "Local" terminology for non-absolute provider config addresses In a future change AbsProviderConfig and LocalProviderConfig are going to become two entirely distinct types, rather than Abs embedding Local as written here. This naming change is in preparation for that subsequent work, which will also include introducing a new "ProviderConfig" type that is an interface that AbsProviderConfig and LocalProviderConfig both implement. This is intended to be largely just a naming change to get started, so we can deal with all of the messy renaming. However, this did also require a slight change in modeling where the Resource.DefaultProviderConfig method has become Resource.DefaultProvider returning a Provider address directly, because this method doesn't have enough information to construct a true and accurate LocalProviderConfig -- it would need to refer to the configuration to know what this module is calling the provider it has selected. In order to leave a trail to follow for subsequent work, all of the changes here are intended to ensure that remaining work will become obvious via compile-time errors when all of the following changes happen: - The concept of "legacy" provider addresses is removed from the addrs package, including removing addrs.NewLegacyProvider and addrs.Provider.LegacyString. - addrs.AbsProviderConfig stops having addrs.LocalProviderConfig embedded in it and has an addrs.Provider and a string alias directly instead. - The provider-schema-handling parts of Terraform core are updated to work with addrs.Provider to identify providers, rather than legacy strings. In particular, there are still several codepaths here making legacy provider address assumptions (in order to limit the scope of this change) but I've made sure each one is doing something that relies on at least one of the above changes not having been made yet. * addrs: ProviderConfig interface In a (very) few special situations in the main "terraform" package we need to make runtime decisions about whether a provider config is absolute or local. We currently do that by exploiting the fact that AbsProviderConfig has LocalProviderConfig nested inside of it and so in the local case we can just ignore the wrapping AbsProviderConfig and use the embedded value. In a future change we'll be moving away from that embedding and making these two types distinct in order to represent that mapping between them requires consulting a lookup table in the configuration, and so here we introduce a new interface type ProviderConfig that can represent either AbsProviderConfig or LocalProviderConfig decided dynamically at runtime. This also includes the Config.ResolveAbsProviderAddr method that will eventually be responsible for that local-to-absolute translation, so that callers with access to the configuration can normalize to an addrs.AbsProviderConfig given a non-nil addrs.ProviderConfig. That's currently unused because existing callers are still relying on the simplistic structural transform, but we'll switch them over in a later commit. * rename LocalType to LocalName Co-authored-by: Kristin Laemmert <mildwonkey@users.noreply.github.com>
525 lines
14 KiB
Go
525 lines
14 KiB
Go
package command
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/hashicorp/terraform/addrs"
|
|
"github.com/hashicorp/terraform/configs/configschema"
|
|
"github.com/hashicorp/terraform/helper/copy"
|
|
"github.com/hashicorp/terraform/plans"
|
|
"github.com/hashicorp/terraform/providers"
|
|
"github.com/hashicorp/terraform/states"
|
|
"github.com/hashicorp/terraform/terraform"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/zclconf/go-cty/cty"
|
|
)
|
|
|
|
func TestShow(t *testing.T) {
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"bad",
|
|
"bad",
|
|
}
|
|
if code := c.Run(args); code != 1 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_noArgs(t *testing.T) {
|
|
// Create the default state
|
|
statePath := testStateFile(t, testState())
|
|
stateDir := filepath.Dir(statePath)
|
|
defer os.RemoveAll(stateDir)
|
|
defer testChdir(t, stateDir)()
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
// the statefile created by testStateFile is named state.tfstate
|
|
// so one arg is required
|
|
args := []string{"state.tfstate"}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "# test_instance.foo:") {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
// https://github.com/hashicorp/terraform/issues/21462
|
|
func TestShow_aliasedProvider(t *testing.T) {
|
|
// Create the default state with aliased resource
|
|
testState := states.BuildState(func(s *states.SyncState) {
|
|
s.SetResourceInstanceCurrent(
|
|
addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
&states.ResourceInstanceObjectSrc{
|
|
// The weird whitespace here is reflective of how this would
|
|
// get written out in a real state file, due to the indentation
|
|
// of all of the containing wrapping objects and arrays.
|
|
AttrsJSON: []byte("{\n \"id\": \"bar\"\n }"),
|
|
Status: states.ObjectReady,
|
|
Dependencies: []addrs.AbsResource{},
|
|
DependsOn: []addrs.Referenceable{},
|
|
},
|
|
addrs.RootModuleInstance.ProviderConfigAliased("test", "alias"),
|
|
)
|
|
})
|
|
|
|
statePath := testStateFile(t, testState)
|
|
stateDir := filepath.Dir(statePath)
|
|
defer os.RemoveAll(stateDir)
|
|
defer testChdir(t, stateDir)()
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
fmt.Println(os.Getwd())
|
|
|
|
// the statefile created by testStateFile is named state.tfstate
|
|
args := []string{"state.tfstate"}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad exit code: \n%s", ui.OutputWriter.String())
|
|
}
|
|
|
|
if strings.Contains(ui.OutputWriter.String(), "# missing schema for provider \"test.alias\"") {
|
|
t.Fatalf("bad output: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_noArgsNoState(t *testing.T) {
|
|
// Create the default state
|
|
statePath := testStateFile(t, testState())
|
|
stateDir := filepath.Dir(statePath)
|
|
defer os.RemoveAll(stateDir)
|
|
defer testChdir(t, stateDir)()
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
// the statefile created by testStateFile is named state.tfstate
|
|
args := []string{"state.tfstate"}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_plan(t *testing.T) {
|
|
planPath := testPlanFileNoop(t)
|
|
|
|
ui := cli.NewMockUi()
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
planPath,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
|
|
want := `Terraform will perform the following actions`
|
|
got := ui.OutputWriter.String()
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
|
|
}
|
|
}
|
|
|
|
func TestShow_planWithChanges(t *testing.T) {
|
|
planPathWithChanges := showFixturePlanFile(t, plans.DeleteThenCreate)
|
|
|
|
ui := cli.NewMockUi()
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
planPathWithChanges,
|
|
}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
|
|
want := `test_instance.foo must be replaced`
|
|
got := ui.OutputWriter.String()
|
|
if !strings.Contains(got, want) {
|
|
t.Errorf("missing expected output\nwant: %s\ngot:\n%s", want, got)
|
|
}
|
|
}
|
|
|
|
func TestShow_plan_json(t *testing.T) {
|
|
planPath := showFixturePlanFile(t, plans.Create)
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(showFixtureProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
"-json",
|
|
planPath,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_state(t *testing.T) {
|
|
originalState := testState()
|
|
statePath := testStateFile(t, originalState)
|
|
defer os.RemoveAll(filepath.Dir(statePath))
|
|
|
|
ui := new(cli.MockUi)
|
|
c := &ShowCommand{
|
|
Meta: Meta{
|
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
|
Ui: ui,
|
|
},
|
|
}
|
|
|
|
args := []string{
|
|
statePath,
|
|
}
|
|
if code := c.Run(args); code != 0 {
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
}
|
|
}
|
|
|
|
func TestShow_json_output(t *testing.T) {
|
|
fixtureDir := "testdata/show-json"
|
|
testDirs, err := ioutil.ReadDir(fixtureDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, entry := range testDirs {
|
|
if !entry.IsDir() {
|
|
continue
|
|
}
|
|
|
|
t.Run(entry.Name(), func(t *testing.T) {
|
|
td := tempDir(t)
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
|
copy.CopyDir(inputDir, td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
expectError := strings.Contains(entry.Name(), "error")
|
|
|
|
p := showFixtureProvider()
|
|
ui := new(cli.MockUi)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
}
|
|
|
|
// init
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
providerInstaller: &mockProviderInstaller{
|
|
Providers: map[string][]string{
|
|
"test": []string{"1.2.3"},
|
|
},
|
|
Dir: m.pluginDir(),
|
|
},
|
|
}
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
if expectError {
|
|
// this should error, but not panic.
|
|
return
|
|
}
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
pc := &PlanCommand{
|
|
Meta: m,
|
|
}
|
|
|
|
args := []string{
|
|
"-out=terraform.plan",
|
|
}
|
|
|
|
if code := pc.Run(args); code != 0 {
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// flush the plan output from the mock ui
|
|
ui.OutputWriter.Reset()
|
|
sc := &ShowCommand{
|
|
Meta: m,
|
|
}
|
|
|
|
args = []string{
|
|
"-json",
|
|
"terraform.plan",
|
|
}
|
|
defer os.Remove("terraform.plan")
|
|
|
|
if code := sc.Run(args); code != 0 {
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// compare ui output to wanted output
|
|
var got, want plan
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer wantFile.Close()
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
if !cmp.Equal(got, want) {
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
}
|
|
|
|
// similar test as above, without the plan
|
|
func TestShow_json_output_state(t *testing.T) {
|
|
fixtureDir := "testdata/show-json-state"
|
|
testDirs, err := ioutil.ReadDir(fixtureDir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
for _, entry := range testDirs {
|
|
if !entry.IsDir() {
|
|
continue
|
|
}
|
|
|
|
t.Run(entry.Name(), func(t *testing.T) {
|
|
td := tempDir(t)
|
|
inputDir := filepath.Join(fixtureDir, entry.Name())
|
|
copy.CopyDir(inputDir, td)
|
|
defer os.RemoveAll(td)
|
|
defer testChdir(t, td)()
|
|
|
|
p := showFixtureProvider()
|
|
ui := new(cli.MockUi)
|
|
m := Meta{
|
|
testingOverrides: metaOverridesForProvider(p),
|
|
Ui: ui,
|
|
}
|
|
|
|
// init
|
|
ic := &InitCommand{
|
|
Meta: m,
|
|
providerInstaller: &mockProviderInstaller{
|
|
Providers: map[string][]string{
|
|
"test": []string{"1.2.3"},
|
|
},
|
|
Dir: m.pluginDir(),
|
|
},
|
|
}
|
|
if code := ic.Run([]string{}); code != 0 {
|
|
t.Fatalf("init failed\n%s", ui.ErrorWriter)
|
|
}
|
|
|
|
// flush the plan output from the mock ui
|
|
ui.OutputWriter.Reset()
|
|
sc := &ShowCommand{
|
|
Meta: m,
|
|
}
|
|
|
|
if code := sc.Run([]string{"-json"}); code != 0 {
|
|
t.Fatalf("wrong exit status %d; want 0\nstderr: %s", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
// compare ui output to wanted output
|
|
type state struct {
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
TerraformVersion string `json:"terraform_version"`
|
|
Values map[string]interface{} `json:"values,omitempty"`
|
|
}
|
|
var got, want state
|
|
|
|
gotString := ui.OutputWriter.String()
|
|
json.Unmarshal([]byte(gotString), &got)
|
|
|
|
wantFile, err := os.Open("output.json")
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
defer wantFile.Close()
|
|
byteValue, err := ioutil.ReadAll(wantFile)
|
|
if err != nil {
|
|
t.Fatalf("err: %s", err)
|
|
}
|
|
json.Unmarshal([]byte(byteValue), &want)
|
|
|
|
if !cmp.Equal(got, want) {
|
|
t.Fatalf("wrong result:\n %v\n", cmp.Diff(got, want))
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
}
|
|
|
|
// showFixtureSchema returns a schema suitable for processing the configuration
|
|
// in testdata/show. This schema should be assigned to a mock provider
|
|
// named "test".
|
|
func showFixtureSchema() *terraform.ProviderSchema {
|
|
return &terraform.ProviderSchema{
|
|
ResourceTypes: map[string]*configschema.Block{
|
|
"test_instance": {
|
|
Attributes: map[string]*configschema.Attribute{
|
|
"id": {Type: cty.String, Optional: true, Computed: true},
|
|
"ami": {Type: cty.String, Optional: true},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// showFixtureProvider returns a mock provider that is configured for basic
|
|
// operation with the configuration in testdata/show. This mock has
|
|
// GetSchemaReturn, PlanResourceChangeFn, and ApplyResourceChangeFn populated,
|
|
// with the plan/apply steps just passing through the data determined by
|
|
// Terraform Core.
|
|
func showFixtureProvider() *terraform.MockProvider {
|
|
p := testProvider()
|
|
p.GetSchemaReturn = showFixtureSchema()
|
|
p.PlanResourceChangeFn = func(req providers.PlanResourceChangeRequest) providers.PlanResourceChangeResponse {
|
|
idVal := req.ProposedNewState.GetAttr("id")
|
|
amiVal := req.ProposedNewState.GetAttr("ami")
|
|
if idVal.IsNull() {
|
|
idVal = cty.UnknownVal(cty.String)
|
|
}
|
|
return providers.PlanResourceChangeResponse{
|
|
PlannedState: cty.ObjectVal(map[string]cty.Value{
|
|
"id": idVal,
|
|
"ami": amiVal,
|
|
}),
|
|
}
|
|
}
|
|
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
|
|
idVal := req.PlannedState.GetAttr("id")
|
|
amiVal := req.PlannedState.GetAttr("ami")
|
|
if !idVal.IsKnown() {
|
|
idVal = cty.StringVal("placeholder")
|
|
}
|
|
return providers.ApplyResourceChangeResponse{
|
|
NewState: cty.ObjectVal(map[string]cty.Value{
|
|
"id": idVal,
|
|
"ami": amiVal,
|
|
}),
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// showFixturePlanFile creates a plan file at a temporary location containing a
|
|
// single change to create or update the test_instance.foo that is included in the "show"
|
|
// test fixture, returning the location of that plan file.
|
|
// `action` is the planned change you would like to elicit
|
|
func showFixturePlanFile(t *testing.T, action plans.Action) string {
|
|
_, snap := testModuleWithSnapshot(t, "show")
|
|
plannedVal := cty.ObjectVal(map[string]cty.Value{
|
|
"id": cty.UnknownVal(cty.String),
|
|
"ami": cty.StringVal("bar"),
|
|
})
|
|
priorValRaw, err := plans.NewDynamicValue(cty.NullVal(plannedVal.Type()), plannedVal.Type())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plannedValRaw, err := plans.NewDynamicValue(plannedVal, plannedVal.Type())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan := testPlan(t)
|
|
plan.Changes.SyncWrapper().AppendResourceInstanceChange(&plans.ResourceInstanceChangeSrc{
|
|
Addr: addrs.Resource{
|
|
Mode: addrs.ManagedResourceMode,
|
|
Type: "test_instance",
|
|
Name: "foo",
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
ProviderAddr: addrs.LocalProviderConfig{LocalName: "test"}.Absolute(addrs.RootModuleInstance),
|
|
ChangeSrc: plans.ChangeSrc{
|
|
Action: action,
|
|
Before: priorValRaw,
|
|
After: plannedValRaw,
|
|
},
|
|
})
|
|
return testPlanFile(
|
|
t,
|
|
snap,
|
|
states.NewState(),
|
|
plan,
|
|
)
|
|
}
|
|
|
|
// this simplified plan struct allows us to preserve field order when marshaling
|
|
// the command output. NOTE: we are leaving "terraform_version" out of this test
|
|
// to avoid needing to constantly update the expected output; as a potential
|
|
// TODO we could write a jsonplan compare function.
|
|
type plan struct {
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
Variables map[string]interface{} `json:"variables,omitempty"`
|
|
PlannedValues map[string]interface{} `json:"planned_values,omitempty"`
|
|
ResourceChanges []interface{} `json:"resource_changes,omitempty"`
|
|
OutputChanges map[string]interface{} `json:"output_changes,omitempty"`
|
|
PriorState priorState `json:"prior_state,omitempty"`
|
|
Config map[string]interface{} `json:"configuration,omitempty"`
|
|
}
|
|
|
|
type priorState struct {
|
|
FormatVersion string `json:"format_version,omitempty"`
|
|
Values map[string]interface{} `json:"values,omitempty"`
|
|
}
|