github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/addrs/provider_config_test.go (about) 1 package addrs 2 3 import ( 4 "testing" 5 6 "github.com/go-test/deep" 7 8 "github.com/hashicorp/hcl/v2" 9 "github.com/hashicorp/hcl/v2/hclsyntax" 10 ) 11 12 func TestParseProviderConfigCompact(t *testing.T) { 13 tests := []struct { 14 Input string 15 Want ProviderConfig 16 WantDiag string 17 }{ 18 { 19 `aws`, 20 ProviderConfig{ 21 Type: "aws", 22 }, 23 ``, 24 }, 25 { 26 `aws.foo`, 27 ProviderConfig{ 28 Type: "aws", 29 Alias: "foo", 30 }, 31 ``, 32 }, 33 { 34 `aws["foo"]`, 35 ProviderConfig{}, 36 `The provider type name must either stand alone or be followed by an alias name separated with a dot.`, 37 }, 38 } 39 40 for _, test := range tests { 41 t.Run(test.Input, func(t *testing.T) { 42 traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{}) 43 if len(parseDiags) != 0 { 44 t.Errorf("unexpected diagnostics during parse") 45 for _, diag := range parseDiags { 46 t.Logf("- %s", diag) 47 } 48 return 49 } 50 51 got, diags := ParseProviderConfigCompact(traversal) 52 53 if test.WantDiag != "" { 54 if len(diags) != 1 { 55 t.Fatalf("got %d diagnostics; want 1", len(diags)) 56 } 57 gotDetail := diags[0].Description().Detail 58 if gotDetail != test.WantDiag { 59 t.Fatalf("wrong diagnostic detail\ngot: %s\nwant: %s", gotDetail, test.WantDiag) 60 } 61 return 62 } else { 63 if len(diags) != 0 { 64 t.Fatalf("got %d diagnostics; want 0", len(diags)) 65 } 66 } 67 68 for _, problem := range deep.Equal(got, test.Want) { 69 t.Error(problem) 70 } 71 }) 72 } 73 } 74 func TestParseAbsProviderConfig(t *testing.T) { 75 tests := []struct { 76 Input string 77 Want AbsProviderConfig 78 WantDiag string 79 }{ 80 { 81 `provider.aws`, 82 AbsProviderConfig{ 83 Module: RootModuleInstance, 84 ProviderConfig: ProviderConfig{ 85 Type: "aws", 86 }, 87 }, 88 ``, 89 }, 90 { 91 `provider.aws.foo`, 92 AbsProviderConfig{ 93 Module: RootModuleInstance, 94 ProviderConfig: ProviderConfig{ 95 Type: "aws", 96 Alias: "foo", 97 }, 98 }, 99 ``, 100 }, 101 { 102 `module.baz.provider.aws`, 103 AbsProviderConfig{ 104 Module: ModuleInstance{ 105 { 106 Name: "baz", 107 }, 108 }, 109 ProviderConfig: ProviderConfig{ 110 Type: "aws", 111 }, 112 }, 113 ``, 114 }, 115 { 116 `module.baz.provider.aws.foo`, 117 AbsProviderConfig{ 118 Module: ModuleInstance{ 119 { 120 Name: "baz", 121 }, 122 }, 123 ProviderConfig: ProviderConfig{ 124 Type: "aws", 125 Alias: "foo", 126 }, 127 }, 128 ``, 129 }, 130 { 131 `module.baz["foo"].provider.aws`, 132 AbsProviderConfig{ 133 Module: ModuleInstance{ 134 { 135 Name: "baz", 136 InstanceKey: StringKey("foo"), 137 }, 138 }, 139 ProviderConfig: ProviderConfig{ 140 Type: "aws", 141 }, 142 }, 143 ``, 144 }, 145 { 146 `module.baz[1].provider.aws`, 147 AbsProviderConfig{ 148 Module: ModuleInstance{ 149 { 150 Name: "baz", 151 InstanceKey: IntKey(1), 152 }, 153 }, 154 ProviderConfig: ProviderConfig{ 155 Type: "aws", 156 }, 157 }, 158 ``, 159 }, 160 { 161 `module.baz[1].module.bar.provider.aws`, 162 AbsProviderConfig{ 163 Module: ModuleInstance{ 164 { 165 Name: "baz", 166 InstanceKey: IntKey(1), 167 }, 168 { 169 Name: "bar", 170 }, 171 }, 172 ProviderConfig: ProviderConfig{ 173 Type: "aws", 174 }, 175 }, 176 ``, 177 }, 178 { 179 `aws`, 180 AbsProviderConfig{}, 181 `Provider address must begin with "provider.", followed by a provider type name.`, 182 }, 183 { 184 `aws.foo`, 185 AbsProviderConfig{}, 186 `Provider address must begin with "provider.", followed by a provider type name.`, 187 }, 188 { 189 `provider`, 190 AbsProviderConfig{}, 191 `Provider address must begin with "provider.", followed by a provider type name.`, 192 }, 193 { 194 `provider.aws.foo.bar`, 195 AbsProviderConfig{}, 196 `Extraneous operators after provider configuration alias.`, 197 }, 198 { 199 `provider["aws"]`, 200 AbsProviderConfig{}, 201 `The prefix "provider." must be followed by a provider type name.`, 202 }, 203 { 204 `provider.aws["foo"]`, 205 AbsProviderConfig{}, 206 `Provider type name must be followed by a configuration alias name.`, 207 }, 208 { 209 `module.foo`, 210 AbsProviderConfig{}, 211 `Provider address must begin with "provider.", followed by a provider type name.`, 212 }, 213 { 214 `module.foo["provider"]`, 215 AbsProviderConfig{}, 216 `Provider address must begin with "provider.", followed by a provider type name.`, 217 }, 218 } 219 220 for _, test := range tests { 221 t.Run(test.Input, func(t *testing.T) { 222 traversal, parseDiags := hclsyntax.ParseTraversalAbs([]byte(test.Input), "", hcl.Pos{}) 223 if len(parseDiags) != 0 { 224 t.Errorf("unexpected diagnostics during parse") 225 for _, diag := range parseDiags { 226 t.Logf("- %s", diag) 227 } 228 return 229 } 230 231 got, diags := ParseAbsProviderConfig(traversal) 232 233 if test.WantDiag != "" { 234 if len(diags) != 1 { 235 t.Fatalf("got %d diagnostics; want 1", len(diags)) 236 } 237 gotDetail := diags[0].Description().Detail 238 if gotDetail != test.WantDiag { 239 t.Fatalf("wrong diagnostic detail\ngot: %s\nwant: %s", gotDetail, test.WantDiag) 240 } 241 return 242 } else { 243 if len(diags) != 0 { 244 t.Fatalf("got %d diagnostics; want 0", len(diags)) 245 } 246 } 247 248 for _, problem := range deep.Equal(got, test.Want) { 249 t.Error(problem) 250 } 251 }) 252 } 253 }