opentofu/helper/resource/map_test.go
Martin Atkins 1908aff476 helper/resource: Fix duplicated function testConfig
An earlier change introduced a new function testConfig to the main code
for this package, which conflicted with a function of the same name in
the test code.

Here we rename the function from the test code, allowing for the more
generally-named testConfig to be the one in the main code.
2018-10-16 19:14:11 -07:00

80 lines
1.4 KiB
Go

package resource
import (
"reflect"
"testing"
tfconfig "github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/helper/config"
"github.com/hashicorp/terraform/terraform"
)
func TestMapResources(t *testing.T) {
m := &Map{
Mapping: map[string]Resource{
"aws_elb": Resource{},
"aws_instance": Resource{},
},
}
rts := m.Resources()
expected := []terraform.ResourceType{
terraform.ResourceType{
Name: "aws_elb",
},
terraform.ResourceType{
Name: "aws_instance",
},
}
if !reflect.DeepEqual(rts, expected) {
t.Fatalf("bad: %#v", rts)
}
}
func TestMapValidate(t *testing.T) {
m := &Map{
Mapping: map[string]Resource{
"aws_elb": Resource{
ConfigValidator: &config.Validator{
Required: []string{"foo"},
},
},
},
}
var c *terraform.ResourceConfig
var ws []string
var es []error
// Valid
c = testConfigForMap(t, map[string]interface{}{"foo": "bar"})
ws, es = m.Validate("aws_elb", c)
if len(ws) > 0 {
t.Fatalf("bad: %#v", ws)
}
if len(es) > 0 {
t.Fatalf("bad: %#v", es)
}
// Invalid
c = testConfigForMap(t, map[string]interface{}{})
ws, es = m.Validate("aws_elb", c)
if len(ws) > 0 {
t.Fatalf("bad: %#v", ws)
}
if len(es) == 0 {
t.Fatalf("bad: %#v", es)
}
}
func testConfigForMap(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
r, err := tfconfig.NewRawConfig(c)
if err != nil {
t.Fatalf("bad: %s", err)
}
return terraform.NewResourceConfig(r)
}