github.com/paybyphone/terraform@v0.9.5-0.20170613192930-9706042ddd51/terraform/eval_validate_selfref_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config"
     8  )
     9  
    10  func TestEvalValidateResourceSelfRef(t *testing.T) {
    11  	cases := []struct {
    12  		Name   string
    13  		Addr   string
    14  		Config map[string]interface{}
    15  		Err    bool
    16  	}{
    17  		{
    18  			"no interpolations",
    19  			"aws_instance.foo",
    20  			map[string]interface{}{
    21  				"foo": "bar",
    22  			},
    23  			false,
    24  		},
    25  
    26  		{
    27  			"non self reference",
    28  			"aws_instance.foo",
    29  			map[string]interface{}{
    30  				"foo": "${aws_instance.bar.id}",
    31  			},
    32  			false,
    33  		},
    34  
    35  		{
    36  			"self reference",
    37  			"aws_instance.foo",
    38  			map[string]interface{}{
    39  				"foo": "hello ${aws_instance.foo.id}",
    40  			},
    41  			true,
    42  		},
    43  
    44  		{
    45  			"self reference other index",
    46  			"aws_instance.foo",
    47  			map[string]interface{}{
    48  				"foo": "hello ${aws_instance.foo.4.id}",
    49  			},
    50  			false,
    51  		},
    52  
    53  		{
    54  			"self reference same index",
    55  			"aws_instance.foo[4]",
    56  			map[string]interface{}{
    57  				"foo": "hello ${aws_instance.foo.4.id}",
    58  			},
    59  			true,
    60  		},
    61  
    62  		{
    63  			"self reference multi",
    64  			"aws_instance.foo[4]",
    65  			map[string]interface{}{
    66  				"foo": "hello ${aws_instance.foo.*.id}",
    67  			},
    68  			true,
    69  		},
    70  
    71  		{
    72  			"self reference multi single",
    73  			"aws_instance.foo",
    74  			map[string]interface{}{
    75  				"foo": "hello ${aws_instance.foo.*.id}",
    76  			},
    77  			true,
    78  		},
    79  	}
    80  
    81  	for i, tc := range cases {
    82  		t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) {
    83  			addr, err := ParseResourceAddress(tc.Addr)
    84  			if err != nil {
    85  				t.Fatalf("err: %s", err)
    86  			}
    87  			conf := config.TestRawConfig(t, tc.Config)
    88  
    89  			n := &EvalValidateResourceSelfRef{Addr: &addr, Config: &conf}
    90  			result, err := n.Eval(nil)
    91  			if result != nil {
    92  				t.Fatal("result should always be nil")
    93  			}
    94  			if (err != nil) != tc.Err {
    95  				t.Fatalf("err: %s", err)
    96  			}
    97  		})
    98  	}
    99  }