2018-04-24 01:58:01 +02:00
|
|
|
package addrs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-test/deep"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
2018-04-24 01:58:01 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseAbsProviderConfig(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Input string
|
|
|
|
Want AbsProviderConfig
|
|
|
|
WantDiag string
|
|
|
|
}{
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`provider["registry.terraform.io/hashicorp/aws"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`provider["registry.terraform.io/hashicorp/aws"].foo`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Alias: "foo",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`module.baz.provider["registry.terraform.io/hashicorp/aws"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: ModuleInstance{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`module.baz.provider["registry.terraform.io/hashicorp/aws"].foo`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: ModuleInstance{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Alias: "foo",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`module.baz["foo"].provider["registry.terraform.io/hashicorp/aws"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: ModuleInstance{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
InstanceKey: StringKey("foo"),
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`module.baz[1].provider["registry.terraform.io/hashicorp/aws"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: ModuleInstance{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
InstanceKey: IntKey(1),
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`module.baz[1].module.bar.provider["registry.terraform.io/hashicorp/aws"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{
|
|
|
|
Module: ModuleInstance{
|
|
|
|
{
|
|
|
|
Name: "baz",
|
|
|
|
InstanceKey: IntKey(1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
2020-02-13 21:32:58 +01:00
|
|
|
Provider: Provider{
|
|
|
|
Type: "aws",
|
|
|
|
Namespace: "hashicorp",
|
|
|
|
Hostname: "registry.terraform.io",
|
2018-04-24 01:58:01 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`aws`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`Provider address must begin with "provider.", followed by a provider type name.`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`aws.foo`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`Provider address must begin with "provider.", followed by a provider type name.`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`provider`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`Provider address must begin with "provider.", followed by a provider type name.`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`provider.aws.foo.bar`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`Extraneous operators after provider configuration alias.`,
|
|
|
|
},
|
|
|
|
{
|
2020-02-13 21:32:58 +01:00
|
|
|
`provider["aws"]["foo"]`,
|
2018-04-24 01:58:01 +02:00
|
|
|
AbsProviderConfig{},
|
|
|
|
`Provider type name must be followed by a configuration alias name.`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`module.foo`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`Provider address must begin with "provider.", followed by a provider type name.`,
|
|
|
|
},
|
2020-02-14 15:06:50 +01:00
|
|
|
{
|
|
|
|
`provider[0]`,
|
|
|
|
AbsProviderConfig{},
|
|
|
|
`The prefix "provider." must be followed by a provider type name.`,
|
|
|
|
},
|
2018-04-24 01:58:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Input, func(t *testing.T) {
|
|
|
|
traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{})
|
|
|
|
if len(parseDiags) != 0 {
|
|
|
|
t.Errorf("unexpected diagnostics during parse")
|
|
|
|
for _, diag := range parseDiags {
|
|
|
|
t.Logf("- %s", diag)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
got, diags := ParseAbsProviderConfig(traversal)
|
|
|
|
|
|
|
|
if test.WantDiag != "" {
|
|
|
|
if len(diags) != 1 {
|
|
|
|
t.Fatalf("got %d diagnostics; want 1", len(diags))
|
|
|
|
}
|
|
|
|
gotDetail := diags[0].Description().Detail
|
|
|
|
if gotDetail != test.WantDiag {
|
|
|
|
t.Fatalf("wrong diagnostic detail\ngot: %s\nwant: %s", gotDetail, test.WantDiag)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if len(diags) != 0 {
|
|
|
|
t.Fatalf("got %d diagnostics; want 0", len(diags))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, problem := range deep.Equal(got, test.Want) {
|
|
|
|
t.Error(problem)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-13 21:32:58 +01:00
|
|
|
|
|
|
|
func TestAbsProviderConfigString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Config AbsProviderConfig
|
|
|
|
Want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`provider["registry.terraform.io/-/foo"]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance.Child("child_module", NoKey),
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`module.child_module.provider["registry.terraform.io/-/foo"]`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
|
|
|
Alias: "bar",
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`provider["registry.terraform.io/-/foo"].bar`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance.Child("child_module", NoKey),
|
|
|
|
Alias: "bar",
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`module.child_module.provider["registry.terraform.io/-/foo"].bar`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := test.Config.String()
|
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf("wrong result. Got %s, want %s\n", got, test.Want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAbsProviderConfigLegacyString(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Config AbsProviderConfig
|
|
|
|
Want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`provider.foo`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance.Child("child_module", NoKey),
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`module.child_module.provider.foo`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance,
|
|
|
|
Alias: "bar",
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`provider.foo.bar`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AbsProviderConfig{
|
|
|
|
Module: RootModuleInstance.Child("child_module", NoKey),
|
|
|
|
Alias: "bar",
|
|
|
|
Provider: NewLegacyProvider("foo"),
|
|
|
|
},
|
|
|
|
`module.child_module.provider.foo.bar`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
got := test.Config.LegacyString()
|
|
|
|
if got != test.Want {
|
|
|
|
t.Errorf("wrong result. Got %s, want %s\n", got, test.Want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|