github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/resource/map_test.go (about)

     1  package resource
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform-plugin-sdk/internal/helper/config"
     8  	"github.com/hashicorp/terraform-plugin-sdk/terraform"
     9  )
    10  
    11  func TestMapResources(t *testing.T) {
    12  	m := &Map{
    13  		Mapping: map[string]Resource{
    14  			"aws_elb":      {},
    15  			"aws_instance": {},
    16  		},
    17  	}
    18  
    19  	rts := m.Resources()
    20  
    21  	expected := []terraform.ResourceType{
    22  		{
    23  			Name: "aws_elb",
    24  		},
    25  		{
    26  			Name: "aws_instance",
    27  		},
    28  	}
    29  
    30  	if !reflect.DeepEqual(rts, expected) {
    31  		t.Fatalf("bad: %#v", rts)
    32  	}
    33  }
    34  
    35  func TestMapValidate(t *testing.T) {
    36  	m := &Map{
    37  		Mapping: map[string]Resource{
    38  			"aws_elb": {
    39  				ConfigValidator: &config.Validator{
    40  					Required: []string{"foo"},
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	var c *terraform.ResourceConfig
    47  	var ws []string
    48  	var es []error
    49  
    50  	// Valid
    51  	c = testConfigForMap(t, map[string]interface{}{"foo": "bar"})
    52  	ws, es = m.Validate("aws_elb", c)
    53  	if len(ws) > 0 {
    54  		t.Fatalf("bad: %#v", ws)
    55  	}
    56  	if len(es) > 0 {
    57  		t.Fatalf("bad: %#v", es)
    58  	}
    59  
    60  	// Invalid
    61  	c = testConfigForMap(t, map[string]interface{}{})
    62  	ws, es = m.Validate("aws_elb", c)
    63  	if len(ws) > 0 {
    64  		t.Fatalf("bad: %#v", ws)
    65  	}
    66  	if len(es) == 0 {
    67  		t.Fatalf("bad: %#v", es)
    68  	}
    69  }
    70  
    71  func testConfigForMap(t *testing.T, c map[string]interface{}) *terraform.ResourceConfig {
    72  	return terraform.NewResourceConfigRaw(c)
    73  }