Skip to content

Commit

Permalink
add a command package test that shows that we can still have provider…
Browse files Browse the repository at this point in the history
…s with dynamic configuration + required + interactive input merging

This test failed when buildProviderConfig still used configs.MergeBodies instead of hcl.MergeBodies
  • Loading branch information
mildwonkey committed Jun 22, 2021
1 parent 3bb8398 commit 50cb740
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
92 changes: 92 additions & 0 deletions internal/command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,98 @@ func TestPlan_providerArgumentUnset(t *testing.T) {
}
}

// Test that terraform properly merges provider configuration that's split
// between config files and interactive input variables.
// https:/hashicorp/terraform/issues/28956
func TestPlan_providerConfigMerge(t *testing.T) {
td := tempDir(t)
testCopyDir(t, testFixturePath("plan-provider-input"), td)
defer os.RemoveAll(td)
defer testChdir(t, td)()

// Disable test mode so input would be asked
test = false
defer func() { test = true }()

// The plan command will prompt for interactive input of provider.test.region
defaultInputReader = bytes.NewBufferString("us-east-1\n")

p := planFixtureProvider()
// override the planFixtureProvider schema to include a required provider argument and a nested block
p.GetProviderSchemaResponse = &providers.GetProviderSchemaResponse{
Provider: providers.Schema{
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"region": {Type: cty.String, Required: true},
"url": {Type: cty.String, Required: true},
},
BlockTypes: map[string]*configschema.NestedBlock{
"auth": {
Nesting: configschema.NestingList,
Block: configschema.Block{
Attributes: map[string]*configschema.Attribute{
"user": {Type: cty.String, Required: true},
"password": {Type: cty.String, Required: true},
},
},
},
},
},
},
ResourceTypes: map[string]providers.Schema{
"test_instance": {
Block: &configschema.Block{
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Optional: true, Computed: true},
},
},
},
},
}

view, done := testView(t)
c := &PlanCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(p),
View: view,
},
}

args := []string{}
code := c.Run(args)
output := done(t)
if code != 0 {
t.Fatalf("bad: %d\n\n%s", code, output.Stderr())
}

if !p.ConfigureProviderCalled {
t.Fatal("configure provider not called")
}

// For this test, we want to confirm that we've sent the expected config
// value *to* the provider.
got := p.ConfigureProviderRequest.Config
want := cty.ObjectVal(map[string]cty.Value{
"auth": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("one"),
"password": cty.StringVal("onepw"),
}),
cty.ObjectVal(map[string]cty.Value{
"user": cty.StringVal("two"),
"password": cty.StringVal("twopw"),
}),
}),
"region": cty.StringVal("us-east-1"),
"url": cty.StringVal("example.com"),
})

if !got.RawEquals(want) {
t.Fatal("wrong provider config")
}

}

func TestPlan_varFile(t *testing.T) {
// Create a temporary working directory that is empty
td := tempDir(t)
Expand Down
20 changes: 20 additions & 0 deletions internal/command/testdata/plan-provider-input/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "users" {
default = {
one = "onepw"
two = "twopw"
}
}

provider "test" {
url = "example.com"

dynamic "auth" {
for_each = var.users
content {
user = auth.key
password = auth.value
}
}
}

resource "test_instance" "test" {}

0 comments on commit 50cb740

Please sign in to comment.