github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/resource_provider_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestResourceConfig_CheckSet(t *testing.T) {
     9  	cases := []struct {
    10  		Raw      map[string]interface{}
    11  		Computed []string
    12  		Input    []string
    13  		Errs     bool
    14  	}{
    15  		{
    16  			map[string]interface{}{
    17  				"foo": "bar",
    18  			},
    19  			nil,
    20  			[]string{"foo"},
    21  			false,
    22  		},
    23  		{
    24  			map[string]interface{}{
    25  				"foo": "bar",
    26  			},
    27  			nil,
    28  			[]string{"foo", "bar"},
    29  			true,
    30  		},
    31  		{
    32  			map[string]interface{}{
    33  				"foo": "bar",
    34  			},
    35  			[]string{"bar"},
    36  			[]string{"foo", "bar"},
    37  			false,
    38  		},
    39  	}
    40  
    41  	for i, tc := range cases {
    42  		rc := &ResourceConfig{
    43  			ComputedKeys: tc.Computed,
    44  			Raw:          tc.Raw,
    45  		}
    46  
    47  		errs := rc.CheckSet(tc.Input)
    48  		if tc.Errs != (len(errs) > 0) {
    49  			t.Fatalf("bad: %d", i)
    50  		}
    51  	}
    52  }
    53  
    54  func TestResourceConfig_Get(t *testing.T) {
    55  	cases := []struct {
    56  		Raw      map[string]interface{}
    57  		Computed []string
    58  		Input    string
    59  		Output   interface{}
    60  		OutputOk bool
    61  	}{
    62  		{
    63  			map[string]interface{}{
    64  				"foo": "bar",
    65  			},
    66  			nil,
    67  			"foo",
    68  			"bar",
    69  			true,
    70  		},
    71  		{
    72  			map[string]interface{}{},
    73  			nil,
    74  			"foo",
    75  			nil,
    76  			false,
    77  		},
    78  		{
    79  			map[string]interface{}{
    80  				"foo": map[interface{}]interface{}{
    81  					"bar": "baz",
    82  				},
    83  			},
    84  			nil,
    85  			"foo.bar",
    86  			"baz",
    87  			true,
    88  		},
    89  		{
    90  			map[string]interface{}{
    91  				"foo": []string{
    92  					"one",
    93  					"two",
    94  				},
    95  			},
    96  			nil,
    97  			"foo.1",
    98  			"two",
    99  			true,
   100  		},
   101  	}
   102  
   103  	for i, tc := range cases {
   104  		rc := &ResourceConfig{
   105  			ComputedKeys: tc.Computed,
   106  			Raw:          tc.Raw,
   107  		}
   108  
   109  		actual, ok := rc.Get(tc.Input)
   110  		if tc.OutputOk != ok {
   111  			t.Fatalf("bad ok: %d", i)
   112  		}
   113  		if !reflect.DeepEqual(tc.Output, actual) {
   114  			t.Fatalf("bad %d: %#v", i, actual)
   115  		}
   116  	}
   117  }
   118  
   119  func TestResourceConfig_IsSet(t *testing.T) {
   120  	cases := []struct {
   121  		Raw      map[string]interface{}
   122  		Computed []string
   123  		Input    string
   124  		Output   bool
   125  	}{
   126  		{
   127  			map[string]interface{}{
   128  				"foo": "bar",
   129  			},
   130  			nil,
   131  			"foo",
   132  			true,
   133  		},
   134  		{
   135  			map[string]interface{}{},
   136  			nil,
   137  			"foo",
   138  			false,
   139  		},
   140  		{
   141  			map[string]interface{}{},
   142  			[]string{"foo"},
   143  			"foo",
   144  			true,
   145  		},
   146  		{
   147  			map[string]interface{}{
   148  				"foo": map[interface{}]interface{}{
   149  					"bar": "baz",
   150  				},
   151  			},
   152  			nil,
   153  			"foo.bar",
   154  			true,
   155  		},
   156  	}
   157  
   158  	for i, tc := range cases {
   159  		rc := &ResourceConfig{
   160  			ComputedKeys: tc.Computed,
   161  			Raw:          tc.Raw,
   162  		}
   163  
   164  		actual := rc.IsSet(tc.Input)
   165  		if actual != tc.Output {
   166  			t.Fatalf("fail case: %d", i)
   167  		}
   168  	}
   169  }
   170  
   171  func TestResourceConfig_IsSet_nil(t *testing.T) {
   172  	var rc *ResourceConfig
   173  
   174  	if rc.IsSet("foo") {
   175  		t.Fatal("bad")
   176  	}
   177  }
   178  
   179  func TestResourceProviderFactoryFixed(t *testing.T) {
   180  	p := new(MockResourceProvider)
   181  	var f ResourceProviderFactory = ResourceProviderFactoryFixed(p)
   182  	actual, err := f()
   183  	if err != nil {
   184  		t.Fatalf("err: %s", err)
   185  	}
   186  	if actual != p {
   187  		t.Fatal("should be identical")
   188  	}
   189  }