github.com/pulumi/terraform@v1.4.0/pkg/addrs/target_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestTargetContains(t *testing.T) {
     9  	for _, test := range []struct {
    10  		addr, other Targetable
    11  		expect      bool
    12  	}{
    13  		{
    14  			mustParseTarget("module.foo"),
    15  			mustParseTarget("module.bar"),
    16  			false,
    17  		},
    18  		{
    19  			mustParseTarget("module.foo"),
    20  			mustParseTarget("module.foo"),
    21  			true,
    22  		},
    23  		{
    24  			RootModuleInstance,
    25  			mustParseTarget("module.foo"),
    26  			true,
    27  		},
    28  		{
    29  			mustParseTarget("module.foo"),
    30  			RootModuleInstance,
    31  			false,
    32  		},
    33  		{
    34  			mustParseTarget("module.foo"),
    35  			mustParseTarget("module.foo.module.bar[0]"),
    36  			true,
    37  		},
    38  		{
    39  			mustParseTarget("module.foo"),
    40  			mustParseTarget("module.foo.module.bar[0]"),
    41  			true,
    42  		},
    43  		{
    44  			mustParseTarget("module.foo[2]"),
    45  			mustParseTarget("module.foo[2].module.bar[0]"),
    46  			true,
    47  		},
    48  		{
    49  			mustParseTarget("module.foo"),
    50  			mustParseTarget("module.foo.test_resource.bar"),
    51  			true,
    52  		},
    53  		{
    54  			mustParseTarget("module.foo"),
    55  			mustParseTarget("module.foo.test_resource.bar[0]"),
    56  			true,
    57  		},
    58  
    59  		// Resources
    60  		{
    61  			mustParseTarget("test_resource.foo"),
    62  			mustParseTarget("test_resource.foo[\"bar\"]"),
    63  			true,
    64  		},
    65  		{
    66  			mustParseTarget(`test_resource.foo["bar"]`),
    67  			mustParseTarget(`test_resource.foo["bar"]`),
    68  			true,
    69  		},
    70  		{
    71  			mustParseTarget("test_resource.foo"),
    72  			mustParseTarget("test_resource.foo[2]"),
    73  			true,
    74  		},
    75  		{
    76  			mustParseTarget("test_resource.foo"),
    77  			mustParseTarget("module.bar.test_resource.foo[2]"),
    78  			false,
    79  		},
    80  		{
    81  			mustParseTarget("module.bar.test_resource.foo"),
    82  			mustParseTarget("module.bar.test_resource.foo[2]"),
    83  			true,
    84  		},
    85  		{
    86  			mustParseTarget("module.bar.test_resource.foo"),
    87  			mustParseTarget("module.bar[0].test_resource.foo[2]"),
    88  			false,
    89  		},
    90  		{
    91  			mustParseTarget("module.bar.test_resource.foo"),
    92  			mustParseTarget("module.bar.test_resource.foo[0]"),
    93  			true,
    94  		},
    95  		{
    96  			mustParseTarget("module.bax"),
    97  			mustParseTarget("module.bax[0].test_resource.foo[0]"),
    98  			true,
    99  		},
   100  
   101  		// Config paths, while never returned from parsing a target, must still
   102  		// be targetable
   103  		{
   104  			ConfigResource{
   105  				Module: []string{"bar"},
   106  				Resource: Resource{
   107  					Mode: ManagedResourceMode,
   108  					Type: "test_resource",
   109  					Name: "foo",
   110  				},
   111  			},
   112  			mustParseTarget("module.bar.test_resource.foo[2]"),
   113  			true,
   114  		},
   115  		{
   116  			mustParseTarget("module.bar"),
   117  			ConfigResource{
   118  				Module: []string{"bar"},
   119  				Resource: Resource{
   120  					Mode: ManagedResourceMode,
   121  					Type: "test_resource",
   122  					Name: "foo",
   123  				},
   124  			},
   125  			true,
   126  		},
   127  		{
   128  			mustParseTarget("module.bar.test_resource.foo"),
   129  			ConfigResource{
   130  				Module: []string{"bar"},
   131  				Resource: Resource{
   132  					Mode: ManagedResourceMode,
   133  					Type: "test_resource",
   134  					Name: "foo",
   135  				},
   136  			},
   137  			true,
   138  		},
   139  		{
   140  			ConfigResource{
   141  				Resource: Resource{
   142  					Mode: ManagedResourceMode,
   143  					Type: "test_resource",
   144  					Name: "foo",
   145  				},
   146  			},
   147  			mustParseTarget("module.bar.test_resource.foo[2]"),
   148  			false,
   149  		},
   150  		{
   151  			ConfigResource{
   152  				Module: []string{"bar"},
   153  				Resource: Resource{
   154  					Mode: ManagedResourceMode,
   155  					Type: "test_resource",
   156  					Name: "foo",
   157  				},
   158  			},
   159  			mustParseTarget("module.bar[0].test_resource.foo"),
   160  			true,
   161  		},
   162  
   163  		// Modules are also never the result of parsing a target, but also need
   164  		// to be targetable
   165  		{
   166  			Module{"bar"},
   167  			Module{"bar", "baz"},
   168  			true,
   169  		},
   170  		{
   171  			Module{"bar"},
   172  			mustParseTarget("module.bar[0]"),
   173  			true,
   174  		},
   175  		{
   176  			// Parsing an ambiguous module path needs to ensure the
   177  			// ModuleInstance could contain the Module. This is safe because if
   178  			// the module could be expanded, it must have an index, meaning no
   179  			// index indicates that the module instance and module are
   180  			// functionally equivalent.
   181  			mustParseTarget("module.bar"),
   182  			Module{"bar"},
   183  			true,
   184  		},
   185  		{
   186  			// A specific ModuleInstance cannot contain a module
   187  			mustParseTarget("module.bar[0]"),
   188  			Module{"bar"},
   189  			false,
   190  		},
   191  		{
   192  			Module{"bar", "baz"},
   193  			mustParseTarget("module.bar[0].module.baz.test_resource.foo[1]"),
   194  			true,
   195  		},
   196  		{
   197  			mustParseTarget("module.bar[0].module.baz"),
   198  			Module{"bar", "baz"},
   199  			false,
   200  		},
   201  	} {
   202  		t.Run(fmt.Sprintf("%s-in-%s", test.other, test.addr), func(t *testing.T) {
   203  			got := test.addr.TargetContains(test.other)
   204  			if got != test.expect {
   205  				t.Fatalf("expected %q.TargetContains(%q) == %t", test.addr, test.other, test.expect)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func TestResourceContains(t *testing.T) {
   212  	for _, test := range []struct {
   213  		in, other Targetable
   214  		expect    bool
   215  	}{} {
   216  		t.Run(fmt.Sprintf("%s-in-%s", test.other, test.in), func(t *testing.T) {
   217  			got := test.in.TargetContains(test.other)
   218  			if got != test.expect {
   219  				t.Fatalf("expected %q.TargetContains(%q) == %t", test.in, test.other, test.expect)
   220  			}
   221  		})
   222  	}
   223  }
   224  
   225  func mustParseTarget(str string) Targetable {
   226  	t, diags := ParseTargetStr(str)
   227  	if diags != nil {
   228  		panic(fmt.Sprintf("%s: %s", str, diags.ErrWithWarnings()))
   229  	}
   230  	return t.Subject
   231  }