Debian Codegraph @

provider_test.go

raw
1 package tfaddr
2
3 import (
4 "fmt"
5 "log"
6 "testing"
7
8 "github.com/google/go-cmp/cmp"
9 svchost "github.com/hashicorp/terraform-svchost"
10 )
11
12 func TestProviderString(t *testing.T) {
13 tests := []struct {
14 Input Provider
15 Want string
16 }{
17 {
18 Provider{
19 Type: "test",
20 Hostname: DefaultProviderRegistryHost,
21 Namespace: "hashicorp",
22 },
23 NewProvider(DefaultProviderRegistryHost, "hashicorp", "test").String(),
24 },
25 {
26 Provider{
27 Type: "test-beta",
28 Hostname: DefaultProviderRegistryHost,
29 Namespace: "hashicorp",
30 },
31 NewProvider(DefaultProviderRegistryHost, "hashicorp", "test-beta").String(),
32 },
33 {
34 Provider{
35 Type: "test",
36 Hostname: "registry.terraform.com",
37 Namespace: "hashicorp",
38 },
39 "registry.terraform.com/hashicorp/test",
40 },
41 {
42 Provider{
43 Type: "test",
44 Hostname: DefaultProviderRegistryHost,
45 Namespace: "othercorp",
46 },
47 DefaultProviderRegistryHost.ForDisplay() + "/othercorp/test",
48 },
49 }
50
51 for _, test := range tests {
52 got := test.Input.String()
53 if got != test.Want {
54 t.Errorf("wrong result for %s\n", test.Input.String())
55 }
56 }
57 }
58
59 func TestProviderLegacyString(t *testing.T) {
60 tests := []struct {
61 Input Provider
62 Want string
63 }{
64 {
65 Provider{
66 Type: "test",
67 Hostname: DefaultProviderRegistryHost,
68 Namespace: LegacyProviderNamespace,
69 },
70 "test",
71 },
72 {
73 Provider{
74 Type: "terraform",
75 Hostname: BuiltInProviderHost,
76 Namespace: BuiltInProviderNamespace,
77 },
78 "terraform",
79 },
80 }
81
82 for _, test := range tests {
83 got := test.Input.LegacyString()
84 if got != test.Want {
85 t.Errorf("wrong result for %s\ngot: %s\nwant: %s", test.Input.String(), got, test.Want)
86 }
87 }
88 }
89
90 func TestProviderDisplay(t *testing.T) {
91 tests := []struct {
92 Input Provider
93 Want string
94 }{
95 {
96 Provider{
97 Type: "test",
98 Hostname: DefaultProviderRegistryHost,
99 Namespace: "hashicorp",
100 },
101 "hashicorp/test",
102 },
103 {
104 Provider{
105 Type: "test",
106 Hostname: "registry.terraform.com",
107 Namespace: "hashicorp",
108 },
109 "registry.terraform.com/hashicorp/test",
110 },
111 {
112 Provider{
113 Type: "test",
114 Hostname: DefaultProviderRegistryHost,
115 Namespace: "othercorp",
116 },
117 "othercorp/test",
118 },
119 {
120 Provider{
121 Type: "terraform",
122 Namespace: BuiltInProviderNamespace,
123 Hostname: BuiltInProviderHost,
124 },
125 "terraform.io/builtin/terraform",
126 },
127 }
128
129 for _, test := range tests {
130 got := test.Input.ForDisplay()
131 if got != test.Want {
132 t.Errorf("wrong result for %s: %q\n", test.Input.String(), got)
133 }
134 }
135 }
136
137 func TestProviderIsBuiltIn(t *testing.T) {
138 tests := []struct {
139 Input Provider
140 Want bool
141 }{
142 {
143 Provider{
144 Type: "test",
145 Hostname: BuiltInProviderHost,
146 Namespace: BuiltInProviderNamespace,
147 },
148 true,
149 },
150 {
151 Provider{
152 Type: "terraform",
153 Hostname: BuiltInProviderHost,
154 Namespace: BuiltInProviderNamespace,
155 },
156 true,
157 },
158 {
159 Provider{
160 Type: "test",
161 Hostname: BuiltInProviderHost,
162 Namespace: "boop",
163 },
164 false,
165 },
166 {
167 Provider{
168 Type: "test",
169 Hostname: DefaultProviderRegistryHost,
170 Namespace: BuiltInProviderNamespace,
171 },
172 false,
173 },
174 {
175 Provider{
176 Type: "test",
177 Hostname: DefaultProviderRegistryHost,
178 Namespace: "hashicorp",
179 },
180 false,
181 },
182 {
183 Provider{
184 Type: "test",
185 Hostname: "registry.terraform.com",
186 Namespace: "hashicorp",
187 },
188 false,
189 },
190 {
191 Provider{
192 Type: "test",
193 Hostname: DefaultProviderRegistryHost,
194 Namespace: "othercorp",
195 },
196 false,
197 },
198 }
199
200 for _, test := range tests {
201 got := test.Input.IsBuiltIn()
202 if got != test.Want {
203 t.Errorf("wrong result for %s\ngot: %#v\nwant: %#v", test.Input.String(), got, test.Want)
204 }
205 }
206 }
207
208 func TestProviderIsLegacy(t *testing.T) {
209 tests := []struct {
210 Input Provider
211 Want bool
212 }{
213 {
214 Provider{
215 Type: "test",
216 Hostname: DefaultProviderRegistryHost,
217 Namespace: LegacyProviderNamespace,
218 },
219 true,
220 },
221 {
222 Provider{
223 Type: "test",
224 Hostname: "registry.terraform.com",
225 Namespace: LegacyProviderNamespace,
226 },
227 false,
228 },
229 {
230 Provider{
231 Type: "test",
232 Hostname: DefaultProviderRegistryHost,
233 Namespace: "hashicorp",
234 },
235 false,
236 },
237 }
238
239 for _, test := range tests {
240 got := test.Input.IsLegacy()
241 if got != test.Want {
242 t.Errorf("wrong result for %s\n", test.Input.String())
243 }
244 }
245 }
246
247 func ExampleParseProviderSource() {
248 pAddr, err := ParseProviderSource("hashicorp/aws")
249 if err != nil {
250 log.Fatal(err)
251 }
252 fmt.Printf("%#v", pAddr)
253 // Output: tfaddr.Provider{Type:"aws", Namespace:"hashicorp", Hostname:svchost.Hostname("registry.terraform.io")}
254 }
255
256 func TestParseProviderSource(t *testing.T) {
257 tests := map[string]struct {
258 Want Provider
259 Err bool
260 }{
261 "registry.terraform.io/hashicorp/aws": {
262 Provider{
263 Type: "aws",
264 Namespace: "hashicorp",
265 Hostname: DefaultProviderRegistryHost,
266 },
267 false,
268 },
269 "registry.Terraform.io/HashiCorp/AWS": {
270 Provider{
271 Type: "aws",
272 Namespace: "hashicorp",
273 Hostname: DefaultProviderRegistryHost,
274 },
275 false,
276 },
277 "terraform.io/builtin/terraform": {
278 Provider{
279 Type: "terraform",
280 Namespace: BuiltInProviderNamespace,
281 Hostname: BuiltInProviderHost,
282 },
283 false,
284 },
285 // v0.12 representation
286 // In most cases this would *likely* be the same 'terraform' provider
287 // we otherwise represent as builtin, but we cannot be sure
288 // in the context of the source string alone.
289 "terraform": {
290 Provider{
291 Type: "terraform",
292 Namespace: UnknownProviderNamespace,
293 Hostname: DefaultProviderRegistryHost,
294 },
295 false,
296 },
297 "hashicorp/aws": {
298 Provider{
299 Type: "aws",
300 Namespace: "hashicorp",
301 Hostname: DefaultProviderRegistryHost,
302 },
303 false,
304 },
305 "HashiCorp/AWS": {
306 Provider{
307 Type: "aws",
308 Namespace: "hashicorp",
309 Hostname: DefaultProviderRegistryHost,
310 },
311 false,
312 },
313 "aws": {
314 Provider{
315 Type: "aws",
316 Namespace: UnknownProviderNamespace,
317 Hostname: DefaultProviderRegistryHost,
318 },
319 false,
320 },
321 "AWS": {
322 Provider{
323 Type: "aws",
324 Namespace: UnknownProviderNamespace,
325 Hostname: DefaultProviderRegistryHost,
326 },
327 false,
328 },
329 "example.com/foo-bar/baz-boop": {
330 Provider{
331 Type: "baz-boop",
332 Namespace: "foo-bar",
333 Hostname: svchost.Hostname("example.com"),
334 },
335 false,
336 },
337 "foo-bar/baz-boop": {
338 Provider{
339 Type: "baz-boop",
340 Namespace: "foo-bar",
341 Hostname: DefaultProviderRegistryHost,
342 },
343 false,
344 },
345 "localhost:8080/foo/bar": {
346 Provider{
347 Type: "bar",
348 Namespace: "foo",
349 Hostname: svchost.Hostname("localhost:8080"),
350 },
351 false,
352 },
353 "example.com/too/many/parts/here": {
354 Provider{},
355 true,
356 },
357 "/too///many//slashes": {
358 Provider{},
359 true,
360 },
361 "///": {
362 Provider{},
363 true,
364 },
365 "/ / /": { // empty strings
366 Provider{},
367 true,
368 },
369 "badhost!/hashicorp/aws": {
370 Provider{},
371 true,
372 },
373 "example.com/badnamespace!/aws": {
374 Provider{},
375 true,
376 },
377 "example.com/bad--namespace/aws": {
378 Provider{},
379 true,
380 },
381 "example.com/-badnamespace/aws": {
382 Provider{},
383 true,
384 },
385 "example.com/badnamespace-/aws": {
386 Provider{},
387 true,
388 },
389 "example.com/bad.namespace/aws": {
390 Provider{},
391 true,
392 },
393 "example.com/hashicorp/badtype!": {
394 Provider{},
395 true,
396 },
397 "example.com/hashicorp/bad--type": {
398 Provider{},
399 true,
400 },
401 "example.com/hashicorp/-badtype": {
402 Provider{},
403 true,
404 },
405 "example.com/hashicorp/badtype-": {
406 Provider{},
407 true,
408 },
409 "example.com/hashicorp/bad.type": {
410 Provider{},
411 true,
412 },
413
414 // We forbid the terraform- prefix both because it's redundant to
415 // include "terraform" in a Terraform provider name and because we use
416 // the longer prefix terraform-provider- to hint for users who might be
417 // accidentally using the git repository name or executable file name
418 // instead of the provider type.
419 "example.com/hashicorp/terraform-provider-bad": {
420 Provider{},
421 true,
422 },
423 "example.com/hashicorp/terraform-bad": {
424 Provider{},
425 true,
426 },
427 }
428
429 for name, test := range tests {
430 got, err := ParseProviderSource(name)
431 if diff := cmp.Diff(test.Want, got); diff != "" {
432 t.Errorf("mismatch (%q): %s", name, diff)
433 }
434 if err != nil {
435 if test.Err == false {
436 t.Errorf("got error: %s, expected success", err)
437 }
438 } else {
439 if test.Err {
440 t.Errorf("got success, expected error")
441 }
442 }
443 }
444 }
445
446 func TestParseProviderPart(t *testing.T) {
447 tests := map[string]struct {
448 Want string
449 Error string
450 }{
451 `foo`: {
452 `foo`,
453 ``,
454 },
455 `FOO`: {
456 `foo`,
457 ``,
458 },
459 `Foo`: {
460 `foo`,
461 ``,
462 },
463 `abc-123`: {
464 `abc-123`,
465 ``,
466 },
467 `Испытание`: {
468 `испытание`,
469 ``,
470 },
471 `münchen`: { // this is a precomposed u with diaeresis
472 `münchen`, // this is a precomposed u with diaeresis
473 ``,
474 },
475 `münchen`: { // this is a separate u and combining diaeresis
476 `münchen`, // this is a precomposed u with diaeresis
477 ``,
478 },
479 `abc--123`: {
480 ``,
481 `cannot use multiple consecutive dashes`,
482 },
483 `xn--80akhbyknj4f`: { // this is the punycode form of "испытание", but we don't accept punycode here
484 ``,
485 `cannot use multiple consecutive dashes`,
486 },
487 `abc.123`: {
488 ``,
489 `dots are not allowed`,
490 },
491 `-abc123`: {
492 ``,
493 `must contain only letters, digits, and dashes, and may not use leading or trailing dashes`,
494 },
495 `abc123-`: {
496 ``,
497 `must contain only letters, digits, and dashes, and may not use leading or trailing dashes`,
498 },
499 ``: {
500 ``,
501 `must have at least one character`,
502 },
503 }
504
505 for given, test := range tests {
506 t.Run(given, func(t *testing.T) {
507 got, err := ParseProviderPart(given)
508 if test.Error != "" {
509 if err == nil {
510 t.Errorf("unexpected success\ngot: %s\nwant: %s", err, test.Error)
511 } else if got := err.Error(); got != test.Error {
512 t.Errorf("wrong error\ngot: %s\nwant: %s", got, test.Error)
513 }
514 } else {
515 if err != nil {
516 t.Errorf("unexpected error\ngot: %s\nwant: <nil>", err)
517 } else if got != test.Want {
518 t.Errorf("wrong result\ngot: %s\nwant: %s", got, test.Want)
519 }
520 }
521 })
522 }
523 }
524
525 func TestProviderEquals(t *testing.T) {
526 tests := []struct {
527 InputP Provider
528 OtherP Provider
529 Want bool
530 }{
531 {
532 NewProvider(DefaultProviderRegistryHost, "foo", "test"),
533 NewProvider(DefaultProviderRegistryHost, "foo", "test"),
534 true,
535 },
536 {
537 NewProvider(DefaultProviderRegistryHost, "foo", "test"),
538 NewProvider(DefaultProviderRegistryHost, "bar", "test"),
539 false,
540 },
541 {
542 NewProvider(DefaultProviderRegistryHost, "foo", "test"),
543 NewProvider(DefaultProviderRegistryHost, "foo", "my-test"),
544 false,
545 },
546 {
547 NewProvider(DefaultProviderRegistryHost, "foo", "test"),
548 NewProvider("example.com", "foo", "test"),
549 false,
550 },
551 }
552 for _, test := range tests {
553 t.Run(test.InputP.String(), func(t *testing.T) {
554 got := test.InputP.Equals(test.OtherP)
555 if got != test.Want {
556 t.Errorf("wrong result\ngot: %v\nwant: %v", got, test.Want)
557 }
558 })
559 }
560 }
561
562 func TestValidateProviderAddress(t *testing.T) {
563 t.Skip("TODO")
564 }