github.com/Hashicorp/terraform@v0.11.12-beta1/helper/schema/field_reader_multi_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  func TestMultiLevelFieldReaderReadFieldExact(t *testing.T) {
    12  	cases := map[string]struct {
    13  		Addr    []string
    14  		Readers []FieldReader
    15  		Level   string
    16  		Result  FieldReadResult
    17  	}{
    18  		"specific": {
    19  			Addr: []string{"foo"},
    20  
    21  			Readers: []FieldReader{
    22  				&MapFieldReader{
    23  					Schema: map[string]*Schema{
    24  						"foo": &Schema{Type: TypeString},
    25  					},
    26  					Map: BasicMapReader(map[string]string{
    27  						"foo": "bar",
    28  					}),
    29  				},
    30  				&MapFieldReader{
    31  					Schema: map[string]*Schema{
    32  						"foo": &Schema{Type: TypeString},
    33  					},
    34  					Map: BasicMapReader(map[string]string{
    35  						"foo": "baz",
    36  					}),
    37  				},
    38  				&MapFieldReader{
    39  					Schema: map[string]*Schema{
    40  						"foo": &Schema{Type: TypeString},
    41  					},
    42  					Map: BasicMapReader(map[string]string{}),
    43  				},
    44  			},
    45  
    46  			Level: "1",
    47  			Result: FieldReadResult{
    48  				Value:  "baz",
    49  				Exists: true,
    50  			},
    51  		},
    52  	}
    53  
    54  	for name, tc := range cases {
    55  		readers := make(map[string]FieldReader)
    56  		levels := make([]string, len(tc.Readers))
    57  		for i, r := range tc.Readers {
    58  			is := strconv.FormatInt(int64(i), 10)
    59  			readers[is] = r
    60  			levels[i] = is
    61  		}
    62  
    63  		r := &MultiLevelFieldReader{
    64  			Readers: readers,
    65  			Levels:  levels,
    66  		}
    67  
    68  		out, err := r.ReadFieldExact(tc.Addr, tc.Level)
    69  		if err != nil {
    70  			t.Fatalf("%s: err: %s", name, err)
    71  		}
    72  
    73  		if !reflect.DeepEqual(tc.Result, out) {
    74  			t.Fatalf("%s: bad: %#v", name, out)
    75  		}
    76  	}
    77  }
    78  
    79  func TestMultiLevelFieldReaderReadFieldMerge(t *testing.T) {
    80  	cases := map[string]struct {
    81  		Addr    []string
    82  		Readers []FieldReader
    83  		Result  FieldReadResult
    84  	}{
    85  		"stringInDiff": {
    86  			Addr: []string{"availability_zone"},
    87  
    88  			Readers: []FieldReader{
    89  				&DiffFieldReader{
    90  					Schema: map[string]*Schema{
    91  						"availability_zone": &Schema{Type: TypeString},
    92  					},
    93  
    94  					Source: &MapFieldReader{
    95  						Schema: map[string]*Schema{
    96  							"availability_zone": &Schema{Type: TypeString},
    97  						},
    98  						Map: BasicMapReader(map[string]string{
    99  							"availability_zone": "foo",
   100  						}),
   101  					},
   102  
   103  					Diff: &terraform.InstanceDiff{
   104  						Attributes: map[string]*terraform.ResourceAttrDiff{
   105  							"availability_zone": &terraform.ResourceAttrDiff{
   106  								Old:         "foo",
   107  								New:         "bar",
   108  								RequiresNew: true,
   109  							},
   110  						},
   111  					},
   112  				},
   113  			},
   114  
   115  			Result: FieldReadResult{
   116  				Value:  "bar",
   117  				Exists: true,
   118  			},
   119  		},
   120  
   121  		"lastLevelComputed": {
   122  			Addr: []string{"availability_zone"},
   123  
   124  			Readers: []FieldReader{
   125  				&MapFieldReader{
   126  					Schema: map[string]*Schema{
   127  						"availability_zone": &Schema{Type: TypeString},
   128  					},
   129  
   130  					Map: BasicMapReader(map[string]string{
   131  						"availability_zone": "foo",
   132  					}),
   133  				},
   134  
   135  				&DiffFieldReader{
   136  					Schema: map[string]*Schema{
   137  						"availability_zone": &Schema{Type: TypeString},
   138  					},
   139  
   140  					Source: &MapFieldReader{
   141  						Schema: map[string]*Schema{
   142  							"availability_zone": &Schema{Type: TypeString},
   143  						},
   144  
   145  						Map: BasicMapReader(map[string]string{
   146  							"availability_zone": "foo",
   147  						}),
   148  					},
   149  
   150  					Diff: &terraform.InstanceDiff{
   151  						Attributes: map[string]*terraform.ResourceAttrDiff{
   152  							"availability_zone": &terraform.ResourceAttrDiff{
   153  								Old:         "foo",
   154  								New:         "bar",
   155  								NewComputed: true,
   156  							},
   157  						},
   158  					},
   159  				},
   160  			},
   161  
   162  			Result: FieldReadResult{
   163  				Value:    "",
   164  				Exists:   true,
   165  				Computed: true,
   166  			},
   167  		},
   168  
   169  		"list of maps with removal in diff": {
   170  			Addr: []string{"config_vars"},
   171  
   172  			Readers: []FieldReader{
   173  				&DiffFieldReader{
   174  					Schema: map[string]*Schema{
   175  						"config_vars": &Schema{
   176  							Type: TypeList,
   177  							Elem: &Schema{Type: TypeMap},
   178  						},
   179  					},
   180  
   181  					Source: &MapFieldReader{
   182  						Schema: map[string]*Schema{
   183  							"config_vars": &Schema{
   184  								Type: TypeList,
   185  								Elem: &Schema{Type: TypeMap},
   186  							},
   187  						},
   188  
   189  						Map: BasicMapReader(map[string]string{
   190  							"config_vars.#":     "2",
   191  							"config_vars.0.foo": "bar",
   192  							"config_vars.0.bar": "bar",
   193  							"config_vars.1.bar": "baz",
   194  						}),
   195  					},
   196  
   197  					Diff: &terraform.InstanceDiff{
   198  						Attributes: map[string]*terraform.ResourceAttrDiff{
   199  							"config_vars.0.bar": &terraform.ResourceAttrDiff{
   200  								NewRemoved: true,
   201  							},
   202  						},
   203  					},
   204  				},
   205  			},
   206  
   207  			Result: FieldReadResult{
   208  				Value: []interface{}{
   209  					map[string]interface{}{
   210  						"foo": "bar",
   211  					},
   212  					map[string]interface{}{
   213  						"bar": "baz",
   214  					},
   215  				},
   216  				Exists: true,
   217  			},
   218  		},
   219  
   220  		"first level only": {
   221  			Addr: []string{"foo"},
   222  
   223  			Readers: []FieldReader{
   224  				&MapFieldReader{
   225  					Schema: map[string]*Schema{
   226  						"foo": &Schema{Type: TypeString},
   227  					},
   228  					Map: BasicMapReader(map[string]string{
   229  						"foo": "bar",
   230  					}),
   231  				},
   232  				&MapFieldReader{
   233  					Schema: map[string]*Schema{
   234  						"foo": &Schema{Type: TypeString},
   235  					},
   236  					Map: BasicMapReader(map[string]string{}),
   237  				},
   238  			},
   239  
   240  			Result: FieldReadResult{
   241  				Value:  "bar",
   242  				Exists: true,
   243  			},
   244  		},
   245  	}
   246  
   247  	for name, tc := range cases {
   248  		readers := make(map[string]FieldReader)
   249  		levels := make([]string, len(tc.Readers))
   250  		for i, r := range tc.Readers {
   251  			is := strconv.FormatInt(int64(i), 10)
   252  			readers[is] = r
   253  			levels[i] = is
   254  		}
   255  
   256  		r := &MultiLevelFieldReader{
   257  			Readers: readers,
   258  			Levels:  levels,
   259  		}
   260  
   261  		out, err := r.ReadFieldMerge(tc.Addr, levels[len(levels)-1])
   262  		if err != nil {
   263  			t.Fatalf("%s: err: %s", name, err)
   264  		}
   265  
   266  		if !reflect.DeepEqual(tc.Result, out) {
   267  			t.Fatalf("%s: bad: %#v", name, out)
   268  		}
   269  	}
   270  }