github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/helper/resource/map_test.go (about)

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