|
7 | 7 | "context" |
8 | 8 | "fmt" |
9 | 9 | "math/big" |
10 | | - "sort" |
11 | 10 | "testing" |
12 | 11 |
|
13 | 12 | "github.com/google/go-cmp/cmp" |
@@ -364,305 +363,6 @@ func TestMarkComputedNilsAsUnknown(t *testing.T) { |
364 | 363 | } |
365 | 364 | } |
366 | 365 |
|
367 | | -func TestRequiredWriteOnlyNilsAttributePath(t *testing.T) { |
368 | | - t.Parallel() |
369 | | - |
370 | | - s := schema.Schema{ |
371 | | - Attributes: map[string]schema.Attribute{ |
372 | | - "string-value": schema.StringAttribute{ |
373 | | - Required: true, |
374 | | - }, |
375 | | - "string-nil-optional-writeonly": schema.StringAttribute{ |
376 | | - Optional: true, |
377 | | - WriteOnly: true, |
378 | | - }, |
379 | | - "string-value-optional-writeonly": schema.StringAttribute{ |
380 | | - Optional: true, |
381 | | - WriteOnly: true, |
382 | | - }, |
383 | | - "string-nil-required-writeonly": schema.StringAttribute{ |
384 | | - Required: true, |
385 | | - WriteOnly: true, |
386 | | - }, |
387 | | - "string-value-required-writeonly": schema.StringAttribute{ |
388 | | - Required: true, |
389 | | - WriteOnly: true, |
390 | | - }, |
391 | | - "list-value": schema.ListAttribute{ |
392 | | - ElementType: types.StringType, |
393 | | - Required: true, |
394 | | - }, |
395 | | - "list-nil-optional-writeonly": schema.ListAttribute{ |
396 | | - ElementType: types.StringType, |
397 | | - Optional: true, |
398 | | - WriteOnly: true, |
399 | | - }, |
400 | | - "list-value-optional-writeonly": schema.ListAttribute{ |
401 | | - ElementType: types.StringType, |
402 | | - Optional: true, |
403 | | - WriteOnly: true, |
404 | | - }, |
405 | | - "list-nil-required-writeonly": schema.ListAttribute{ |
406 | | - ElementType: types.StringType, |
407 | | - Required: true, |
408 | | - WriteOnly: true, |
409 | | - }, |
410 | | - "list-value-required-writeonly": schema.ListAttribute{ |
411 | | - ElementType: types.StringType, |
412 | | - Required: true, |
413 | | - WriteOnly: true, |
414 | | - }, |
415 | | - "dynamic-value": schema.DynamicAttribute{ |
416 | | - Required: true, |
417 | | - }, |
418 | | - "dynamic-nil-optional-writeonly": schema.DynamicAttribute{ |
419 | | - Optional: true, |
420 | | - WriteOnly: true, |
421 | | - }, |
422 | | - "dynamic-value-optional-writeonly": schema.DynamicAttribute{ |
423 | | - Optional: true, |
424 | | - WriteOnly: true, |
425 | | - }, |
426 | | - "dynamic-nil-required-writeonly": schema.DynamicAttribute{ |
427 | | - Required: true, |
428 | | - WriteOnly: true, |
429 | | - }, |
430 | | - "dynamic-value-required-writeonly": schema.DynamicAttribute{ |
431 | | - Required: true, |
432 | | - WriteOnly: true, |
433 | | - }, |
434 | | - // underlying values of dynamic attributes should be left alone |
435 | | - "dynamic-value-with-underlying-list-required-writeonly": schema.DynamicAttribute{ |
436 | | - Required: true, |
437 | | - WriteOnly: true, |
438 | | - }, |
439 | | - "object-nil-required-writeonly": schema.ObjectAttribute{ |
440 | | - AttributeTypes: map[string]attr.Type{ |
441 | | - "string-nil": types.StringType, |
442 | | - "string-set": types.StringType, |
443 | | - }, |
444 | | - Required: true, |
445 | | - WriteOnly: true, |
446 | | - }, |
447 | | - "object-value-required-writeonly": schema.ObjectAttribute{ |
448 | | - AttributeTypes: map[string]attr.Type{ |
449 | | - "string-nil": types.StringType, |
450 | | - "string-set": types.StringType, |
451 | | - }, |
452 | | - Required: true, |
453 | | - WriteOnly: true, |
454 | | - }, |
455 | | - "nested-nil-required-writeonly": schema.SingleNestedAttribute{ |
456 | | - Attributes: map[string]schema.Attribute{ |
457 | | - "string-nil": schema.StringAttribute{ |
458 | | - Required: true, |
459 | | - WriteOnly: true, |
460 | | - }, |
461 | | - "string-set": schema.StringAttribute{ |
462 | | - Required: true, |
463 | | - WriteOnly: true, |
464 | | - }, |
465 | | - }, |
466 | | - Required: true, |
467 | | - WriteOnly: true, |
468 | | - }, |
469 | | - "nested-value-required-writeonly": schema.SingleNestedAttribute{ |
470 | | - Attributes: map[string]schema.Attribute{ |
471 | | - "string-nil": schema.StringAttribute{ |
472 | | - Required: true, |
473 | | - WriteOnly: true, |
474 | | - }, |
475 | | - "string-set": schema.StringAttribute{ |
476 | | - Required: true, |
477 | | - WriteOnly: true, |
478 | | - }, |
479 | | - }, |
480 | | - Required: true, |
481 | | - WriteOnly: true, |
482 | | - }, |
483 | | - "optional-nested-value-required-writeonly-attributes": schema.SingleNestedAttribute{ |
484 | | - Attributes: map[string]schema.Attribute{ |
485 | | - "string-nil": schema.StringAttribute{ |
486 | | - Required: true, |
487 | | - WriteOnly: true, |
488 | | - }, |
489 | | - "string-set": schema.StringAttribute{ |
490 | | - Required: true, |
491 | | - WriteOnly: true, |
492 | | - }, |
493 | | - }, |
494 | | - Optional: true, |
495 | | - WriteOnly: true, |
496 | | - }, |
497 | | - }, |
498 | | - Blocks: map[string]schema.Block{ |
499 | | - "block-nil-required-writeonly-attributes": schema.SetNestedBlock{ |
500 | | - NestedObject: schema.NestedBlockObject{ |
501 | | - Attributes: map[string]schema.Attribute{ |
502 | | - "string-nil": schema.StringAttribute{ |
503 | | - Required: true, |
504 | | - WriteOnly: true, |
505 | | - }, |
506 | | - "string-set": schema.StringAttribute{ |
507 | | - Required: true, |
508 | | - WriteOnly: true, |
509 | | - }, |
510 | | - }, |
511 | | - }, |
512 | | - }, |
513 | | - "block-value-required-writeonly-attributes": schema.SetNestedBlock{ |
514 | | - NestedObject: schema.NestedBlockObject{ |
515 | | - Attributes: map[string]schema.Attribute{ |
516 | | - "string-nil": schema.StringAttribute{ |
517 | | - Required: true, |
518 | | - WriteOnly: true, |
519 | | - }, |
520 | | - "string-set": schema.StringAttribute{ |
521 | | - Required: true, |
522 | | - WriteOnly: true, |
523 | | - }, |
524 | | - }, |
525 | | - }, |
526 | | - }, |
527 | | - }, |
528 | | - } |
529 | | - input := tftypes.NewValue(s.Type().TerraformType(context.Background()), map[string]tftypes.Value{ |
530 | | - "string-value": tftypes.NewValue(tftypes.String, "hello, world"), |
531 | | - "string-nil-optional-writeonly": tftypes.NewValue(tftypes.String, nil), |
532 | | - "string-value-optional-writeonly": tftypes.NewValue(tftypes.String, "hello, world"), |
533 | | - "string-nil-required-writeonly": tftypes.NewValue(tftypes.String, nil), |
534 | | - "string-value-required-writeonly": tftypes.NewValue(tftypes.String, "hello, world"), |
535 | | - "list-value": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{tftypes.NewValue(tftypes.String, "hello, world")}), |
536 | | - "list-nil-optional-writeonly": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, nil), |
537 | | - "list-value-optional-writeonly": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{tftypes.NewValue(tftypes.String, "hello, world")}), |
538 | | - "list-nil-required-writeonly": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, nil), |
539 | | - "list-value-required-writeonly": tftypes.NewValue(tftypes.List{ElementType: tftypes.String}, []tftypes.Value{ |
540 | | - tftypes.NewValue(tftypes.String, "hello, world"), |
541 | | - tftypes.NewValue(tftypes.String, nil), |
542 | | - }), |
543 | | - "dynamic-value": tftypes.NewValue(tftypes.String, "hello, world"), |
544 | | - "dynamic-nil-optional-writeonly": tftypes.NewValue(tftypes.DynamicPseudoType, nil), |
545 | | - "dynamic-value-optional-writeonly": tftypes.NewValue(tftypes.String, "hello, world"), |
546 | | - "dynamic-nil-required-writeonly": tftypes.NewValue(tftypes.DynamicPseudoType, nil), |
547 | | - "dynamic-value-required-writeonly": tftypes.NewValue(tftypes.String, "hello, world"), |
548 | | - "dynamic-value-with-underlying-list-required-writeonly": tftypes.NewValue( |
549 | | - tftypes.List{ |
550 | | - ElementType: tftypes.Bool, |
551 | | - }, |
552 | | - []tftypes.Value{ |
553 | | - tftypes.NewValue(tftypes.Bool, true), |
554 | | - tftypes.NewValue(tftypes.Bool, nil), |
555 | | - }, |
556 | | - ), |
557 | | - "object-nil-required-writeonly": tftypes.NewValue(tftypes.Object{ |
558 | | - AttributeTypes: map[string]tftypes.Type{ |
559 | | - "string-nil": tftypes.String, |
560 | | - "string-set": tftypes.String, |
561 | | - }, |
562 | | - }, nil), |
563 | | - "object-value-required-writeonly": tftypes.NewValue(tftypes.Object{ |
564 | | - AttributeTypes: map[string]tftypes.Type{ |
565 | | - "string-nil": tftypes.String, |
566 | | - "string-set": tftypes.String, |
567 | | - }, |
568 | | - }, map[string]tftypes.Value{ |
569 | | - "string-nil": tftypes.NewValue(tftypes.String, nil), |
570 | | - "string-set": tftypes.NewValue(tftypes.String, "foo"), |
571 | | - }), |
572 | | - "nested-nil-required-writeonly": tftypes.NewValue(tftypes.Object{ |
573 | | - AttributeTypes: map[string]tftypes.Type{ |
574 | | - "string-nil": tftypes.String, |
575 | | - "string-set": tftypes.String, |
576 | | - }, |
577 | | - }, nil), |
578 | | - "nested-value-required-writeonly": tftypes.NewValue(tftypes.Object{ |
579 | | - AttributeTypes: map[string]tftypes.Type{ |
580 | | - "string-nil": tftypes.String, |
581 | | - "string-set": tftypes.String, |
582 | | - }, |
583 | | - }, map[string]tftypes.Value{ |
584 | | - "string-nil": tftypes.NewValue(tftypes.String, nil), |
585 | | - "string-set": tftypes.NewValue(tftypes.String, "bar"), |
586 | | - }), |
587 | | - "optional-nested-value-required-writeonly-attributes": tftypes.NewValue(tftypes.Object{ |
588 | | - AttributeTypes: map[string]tftypes.Type{ |
589 | | - "string-nil": tftypes.String, |
590 | | - "string-set": tftypes.String, |
591 | | - }, |
592 | | - }, map[string]tftypes.Value{ |
593 | | - "string-nil": tftypes.NewValue(tftypes.String, nil), |
594 | | - "string-set": tftypes.NewValue(tftypes.String, "bar"), |
595 | | - }), |
596 | | - "block-nil-required-writeonly-attributes": tftypes.NewValue(tftypes.Set{ |
597 | | - ElementType: tftypes.Object{ |
598 | | - AttributeTypes: map[string]tftypes.Type{ |
599 | | - "string-nil": tftypes.String, |
600 | | - "string-set": tftypes.String, |
601 | | - }, |
602 | | - }, |
603 | | - }, nil), |
604 | | - "block-value-required-writeonly-attributes": tftypes.NewValue(tftypes.Set{ |
605 | | - ElementType: tftypes.Object{ |
606 | | - AttributeTypes: map[string]tftypes.Type{ |
607 | | - "string-nil": tftypes.String, |
608 | | - "string-set": tftypes.String, |
609 | | - }, |
610 | | - }, |
611 | | - }, []tftypes.Value{ |
612 | | - tftypes.NewValue(tftypes.Object{ |
613 | | - AttributeTypes: map[string]tftypes.Type{ |
614 | | - "string-nil": tftypes.String, |
615 | | - "string-set": tftypes.String, |
616 | | - }, |
617 | | - }, map[string]tftypes.Value{ |
618 | | - "string-nil": tftypes.NewValue(tftypes.String, nil), |
619 | | - "string-set": tftypes.NewValue(tftypes.String, "bar"), |
620 | | - }), |
621 | | - }), |
622 | | - }) |
623 | | - expected := path.Paths{ |
624 | | - path.Root("block-value-required-writeonly-attributes"). |
625 | | - AtSetValue(types.ObjectValueMust( |
626 | | - map[string]attr.Type{ |
627 | | - "string-nil": types.StringType, |
628 | | - "string-set": types.StringType, |
629 | | - }, |
630 | | - map[string]attr.Value{ |
631 | | - "string-nil": types.StringNull(), |
632 | | - "string-set": types.StringValue("bar"), |
633 | | - }, |
634 | | - )). |
635 | | - AtName("string-nil"), |
636 | | - path.Root("dynamic-nil-required-writeonly"), |
637 | | - path.Root("list-nil-required-writeonly"), |
638 | | - path.Root("nested-value-required-writeonly").AtName("string-nil"), |
639 | | - path.Root("object-nil-required-writeonly"), |
640 | | - path.Root("optional-nested-value-required-writeonly-attributes").AtName("string-nil"), |
641 | | - path.Root("string-nil-required-writeonly"), |
642 | | - path.Root("nested-nil-required-writeonly"), |
643 | | - } |
644 | | - |
645 | | - var got path.Paths |
646 | | - err := tftypes.Walk(input, fwserver.RequiredWriteOnlyNilsAttributePaths(context.Background(), s, &got)) |
647 | | - if err != nil { |
648 | | - t.Errorf("Unexpected error: %s", err) |
649 | | - return |
650 | | - } |
651 | | - |
652 | | - sort.Slice(got, func(i, j int) bool { |
653 | | - return got[i].String() < got[j].String() |
654 | | - }) |
655 | | - |
656 | | - sort.Slice(expected, func(i, j int) bool { |
657 | | - return expected[i].String() < expected[j].String() |
658 | | - }) |
659 | | - |
660 | | - if diff := cmp.Diff(got, expected, cmpopts.EquateEmpty()); diff != "" { |
661 | | - t.Errorf("Unexpected diff (+wanted, -got): %s", diff) |
662 | | - return |
663 | | - } |
664 | | -} |
665 | | - |
666 | 366 | func TestNormaliseRequiresReplace(t *testing.T) { |
667 | 367 | t.Parallel() |
668 | 368 |
|
|
0 commit comments