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

     1  package schema
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/config"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestProvider_impl(t *testing.T) {
    13  	var _ terraform.ResourceProvider = new(Provider)
    14  }
    15  
    16  func TestProviderConfigure(t *testing.T) {
    17  	cases := []struct {
    18  		P      *Provider
    19  		Config map[string]interface{}
    20  		Err    bool
    21  	}{
    22  		{
    23  			P:      &Provider{},
    24  			Config: nil,
    25  			Err:    false,
    26  		},
    27  
    28  		{
    29  			P: &Provider{
    30  				Schema: map[string]*Schema{
    31  					"foo": &Schema{
    32  						Type:     TypeInt,
    33  						Optional: true,
    34  					},
    35  				},
    36  
    37  				ConfigureFunc: func(d *ResourceData) (interface{}, error) {
    38  					if d.Get("foo").(int) == 42 {
    39  						return nil, nil
    40  					}
    41  
    42  					return nil, fmt.Errorf("nope")
    43  				},
    44  			},
    45  			Config: map[string]interface{}{
    46  				"foo": 42,
    47  			},
    48  			Err: false,
    49  		},
    50  
    51  		{
    52  			P: &Provider{
    53  				Schema: map[string]*Schema{
    54  					"foo": &Schema{
    55  						Type:     TypeInt,
    56  						Optional: true,
    57  					},
    58  				},
    59  
    60  				ConfigureFunc: func(d *ResourceData) (interface{}, error) {
    61  					if d.Get("foo").(int) == 42 {
    62  						return nil, nil
    63  					}
    64  
    65  					return nil, fmt.Errorf("nope")
    66  				},
    67  			},
    68  			Config: map[string]interface{}{
    69  				"foo": 52,
    70  			},
    71  			Err: true,
    72  		},
    73  	}
    74  
    75  	for i, tc := range cases {
    76  		c, err := config.NewRawConfig(tc.Config)
    77  		if err != nil {
    78  			t.Fatalf("err: %s", err)
    79  		}
    80  
    81  		err = tc.P.Configure(terraform.NewResourceConfig(c))
    82  		if (err != nil) != tc.Err {
    83  			t.Fatalf("%d: %s", i, err)
    84  		}
    85  	}
    86  }
    87  
    88  func TestProviderResources(t *testing.T) {
    89  	cases := []struct {
    90  		P      *Provider
    91  		Result []terraform.ResourceType
    92  	}{
    93  		{
    94  			P:      &Provider{},
    95  			Result: []terraform.ResourceType{},
    96  		},
    97  
    98  		{
    99  			P: &Provider{
   100  				ResourcesMap: map[string]*Resource{
   101  					"foo": nil,
   102  					"bar": nil,
   103  				},
   104  			},
   105  			Result: []terraform.ResourceType{
   106  				terraform.ResourceType{Name: "bar"},
   107  				terraform.ResourceType{Name: "foo"},
   108  			},
   109  		},
   110  	}
   111  
   112  	for i, tc := range cases {
   113  		actual := tc.P.Resources()
   114  		if !reflect.DeepEqual(actual, tc.Result) {
   115  			t.Fatalf("%d: %#v", i, actual)
   116  		}
   117  	}
   118  }
   119  
   120  func TestProviderValidateResource(t *testing.T) {
   121  	cases := []struct {
   122  		P      *Provider
   123  		Type   string
   124  		Config map[string]interface{}
   125  		Err    bool
   126  	}{
   127  		{
   128  			P:      &Provider{},
   129  			Type:   "foo",
   130  			Config: nil,
   131  			Err:    true,
   132  		},
   133  
   134  		{
   135  			P: &Provider{
   136  				ResourcesMap: map[string]*Resource{
   137  					"foo": &Resource{},
   138  				},
   139  			},
   140  			Type:   "foo",
   141  			Config: nil,
   142  			Err:    false,
   143  		},
   144  	}
   145  
   146  	for i, tc := range cases {
   147  		c, err := config.NewRawConfig(tc.Config)
   148  		if err != nil {
   149  			t.Fatalf("err: %s", err)
   150  		}
   151  
   152  		_, es := tc.P.ValidateResource(tc.Type, terraform.NewResourceConfig(c))
   153  		if (len(es) > 0) != tc.Err {
   154  			t.Fatalf("%d: %#v", i, es)
   155  		}
   156  	}
   157  }
   158  
   159  func TestProviderMeta(t *testing.T) {
   160  	p := new(Provider)
   161  	if v := p.Meta(); v != nil {
   162  		t.Fatalf("bad: %#v", v)
   163  	}
   164  
   165  	expected := 42
   166  	p.SetMeta(42)
   167  	if v := p.Meta(); !reflect.DeepEqual(v, expected) {
   168  		t.Fatalf("bad: %#v", v)
   169  	}
   170  }