github.com/bigkraig/terraform@v0.6.4-0.20151219155159-c90d1b074e31/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 TestProviderValidate(t *testing.T) {
   121  	cases := []struct {
   122  		P      *Provider
   123  		Config map[string]interface{}
   124  		Err    bool
   125  	}{
   126  		{
   127  			P: &Provider{
   128  				Schema: map[string]*Schema{
   129  					"foo": &Schema{},
   130  				},
   131  			},
   132  			Config: nil,
   133  			Err:    true,
   134  		},
   135  	}
   136  
   137  	for i, tc := range cases {
   138  		c, err := config.NewRawConfig(tc.Config)
   139  		if err != nil {
   140  			t.Fatalf("err: %s", err)
   141  		}
   142  
   143  		_, es := tc.P.Validate(terraform.NewResourceConfig(c))
   144  		if len(es) > 0 != tc.Err {
   145  			t.Fatalf("%d: %#v", i, es)
   146  		}
   147  	}
   148  }
   149  
   150  func TestProviderValidateResource(t *testing.T) {
   151  	cases := []struct {
   152  		P      *Provider
   153  		Type   string
   154  		Config map[string]interface{}
   155  		Err    bool
   156  	}{
   157  		{
   158  			P:      &Provider{},
   159  			Type:   "foo",
   160  			Config: nil,
   161  			Err:    true,
   162  		},
   163  
   164  		{
   165  			P: &Provider{
   166  				ResourcesMap: map[string]*Resource{
   167  					"foo": &Resource{},
   168  				},
   169  			},
   170  			Type:   "foo",
   171  			Config: nil,
   172  			Err:    false,
   173  		},
   174  	}
   175  
   176  	for i, tc := range cases {
   177  		c, err := config.NewRawConfig(tc.Config)
   178  		if err != nil {
   179  			t.Fatalf("err: %s", err)
   180  		}
   181  
   182  		_, es := tc.P.ValidateResource(tc.Type, terraform.NewResourceConfig(c))
   183  		if len(es) > 0 != tc.Err {
   184  			t.Fatalf("%d: %#v", i, es)
   185  		}
   186  	}
   187  }
   188  
   189  func TestProviderMeta(t *testing.T) {
   190  	p := new(Provider)
   191  	if v := p.Meta(); v != nil {
   192  		t.Fatalf("bad: %#v", v)
   193  	}
   194  
   195  	expected := 42
   196  	p.SetMeta(42)
   197  	if v := p.Meta(); !reflect.DeepEqual(v, expected) {
   198  		t.Fatalf("bad: %#v", v)
   199  	}
   200  }