github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/node_resource_abstract_instance_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/internal/addrs"
     8  	"github.com/hashicorp/terraform/internal/configs"
     9  	"github.com/hashicorp/terraform/internal/configs/configschema"
    10  	"github.com/hashicorp/terraform/internal/states"
    11  	"github.com/zclconf/go-cty/cty"
    12  )
    13  
    14  func TestNodeAbstractResourceInstanceProvider(t *testing.T) {
    15  	tests := []struct {
    16  		Addr                 addrs.AbsResourceInstance
    17  		Config               *configs.Resource
    18  		StoredProviderConfig addrs.AbsProviderConfig
    19  		Want                 addrs.Provider
    20  	}{
    21  		{
    22  			Addr: addrs.Resource{
    23  				Mode: addrs.ManagedResourceMode,
    24  				Type: "null_resource",
    25  				Name: "baz",
    26  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    27  			Want: addrs.Provider{
    28  				Hostname:  addrs.DefaultProviderRegistryHost,
    29  				Namespace: "hashicorp",
    30  				Type:      "null",
    31  			},
    32  		},
    33  		{
    34  			Addr: addrs.Resource{
    35  				Mode: addrs.DataResourceMode,
    36  				Type: "terraform_remote_state",
    37  				Name: "baz",
    38  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    39  			Want: addrs.Provider{
    40  				// As a special case, the type prefix "terraform_" maps to
    41  				// the builtin provider, not the default one.
    42  				Hostname:  addrs.BuiltInProviderHost,
    43  				Namespace: addrs.BuiltInProviderNamespace,
    44  				Type:      "terraform",
    45  			},
    46  		},
    47  		{
    48  			Addr: addrs.Resource{
    49  				Mode: addrs.ManagedResourceMode,
    50  				Type: "null_resource",
    51  				Name: "baz",
    52  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    53  			Config: &configs.Resource{
    54  				// Just enough configs.Resource for the Provider method. Not
    55  				// actually valid for general use.
    56  				Provider: addrs.Provider{
    57  					Hostname:  addrs.DefaultProviderRegistryHost,
    58  					Namespace: "awesomecorp",
    59  					Type:      "happycloud",
    60  				},
    61  			},
    62  			// The config overrides the default behavior.
    63  			Want: addrs.Provider{
    64  				Hostname:  addrs.DefaultProviderRegistryHost,
    65  				Namespace: "awesomecorp",
    66  				Type:      "happycloud",
    67  			},
    68  		},
    69  		{
    70  			Addr: addrs.Resource{
    71  				Mode: addrs.DataResourceMode,
    72  				Type: "terraform_remote_state",
    73  				Name: "baz",
    74  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    75  			Config: &configs.Resource{
    76  				// Just enough configs.Resource for the Provider method. Not
    77  				// actually valid for general use.
    78  				Provider: addrs.Provider{
    79  					Hostname:  addrs.DefaultProviderRegistryHost,
    80  					Namespace: "awesomecorp",
    81  					Type:      "happycloud",
    82  				},
    83  			},
    84  			// The config overrides the default behavior.
    85  			Want: addrs.Provider{
    86  				Hostname:  addrs.DefaultProviderRegistryHost,
    87  				Namespace: "awesomecorp",
    88  				Type:      "happycloud",
    89  			},
    90  		},
    91  		{
    92  			Addr: addrs.Resource{
    93  				Mode: addrs.DataResourceMode,
    94  				Type: "null_resource",
    95  				Name: "baz",
    96  			}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
    97  			Config: nil,
    98  			StoredProviderConfig: addrs.AbsProviderConfig{
    99  				Module: addrs.RootModule,
   100  				Provider: addrs.Provider{
   101  					Hostname:  addrs.DefaultProviderRegistryHost,
   102  					Namespace: "awesomecorp",
   103  					Type:      "null",
   104  				},
   105  			},
   106  			// The stored provider config overrides the default behavior.
   107  			Want: addrs.Provider{
   108  				Hostname:  addrs.DefaultProviderRegistryHost,
   109  				Namespace: "awesomecorp",
   110  				Type:      "null",
   111  			},
   112  		},
   113  	}
   114  
   115  	for _, test := range tests {
   116  		var name string
   117  		if test.Config != nil {
   118  			name = fmt.Sprintf("%s with configured %s", test.Addr, test.Config.Provider)
   119  		} else {
   120  			name = fmt.Sprintf("%s with no configuration", test.Addr)
   121  		}
   122  		t.Run(name, func(t *testing.T) {
   123  			node := &NodeAbstractResourceInstance{
   124  				// Just enough NodeAbstractResourceInstance for the Provider
   125  				// function. (This would not be valid for some other functions.)
   126  				Addr: test.Addr,
   127  				NodeAbstractResource: NodeAbstractResource{
   128  					Config: test.Config,
   129  				},
   130  				storedProviderConfig: test.StoredProviderConfig,
   131  			}
   132  			got := node.Provider()
   133  			if got != test.Want {
   134  				t.Errorf("wrong result\naddr:  %s\nconfig: %#v\ngot:   %s\nwant:  %s", test.Addr, test.Config, got, test.Want)
   135  			}
   136  		})
   137  	}
   138  }
   139  
   140  func TestNodeAbstractResourceInstance_WriteResourceInstanceState(t *testing.T) {
   141  	state := states.NewState()
   142  	ctx := new(MockEvalContext)
   143  	ctx.StateState = state.SyncWrapper()
   144  	ctx.PathPath = addrs.RootModuleInstance
   145  
   146  	mockProvider := mockProviderWithResourceTypeSchema("aws_instance", &configschema.Block{
   147  		Attributes: map[string]*configschema.Attribute{
   148  			"id": {
   149  				Type:     cty.String,
   150  				Optional: true,
   151  			},
   152  		},
   153  	})
   154  
   155  	obj := &states.ResourceInstanceObject{
   156  		Value: cty.ObjectVal(map[string]cty.Value{
   157  			"id": cty.StringVal("i-abc123"),
   158  		}),
   159  		Status: states.ObjectReady,
   160  	}
   161  
   162  	node := &NodeAbstractResourceInstance{
   163  		Addr: mustResourceInstanceAddr("aws_instance.foo"),
   164  		// instanceState:        obj,
   165  		NodeAbstractResource: NodeAbstractResource{
   166  			ResolvedProvider: mustProviderConfig(`provider["registry.terraform.io/hashicorp/aws"]`),
   167  		},
   168  	}
   169  	ctx.ProviderProvider = mockProvider
   170  	ctx.ProviderSchemaSchema = mockProvider.ProviderSchema()
   171  
   172  	err := node.writeResourceInstanceState(ctx, obj, workingState)
   173  	if err != nil {
   174  		t.Fatalf("unexpected error: %s", err.Error())
   175  	}
   176  
   177  	checkStateString(t, state, `
   178  aws_instance.foo:
   179    ID = i-abc123
   180    provider = provider["registry.terraform.io/hashicorp/aws"]
   181  	`)
   182  }