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