|
4 | 4 | "reflect" |
5 | 5 | "sort" |
6 | 6 | "strings" |
| 7 | + "sync" |
7 | 8 | "testing" |
8 | 9 | ) |
9 | 10 |
|
@@ -685,6 +686,100 @@ func TestContext2Refresh_vars(t *testing.T) { |
685 | 686 | } |
686 | 687 | } |
687 | 688 |
|
| 689 | +func TestContext2Refresh_orphanModule(t *testing.T) { |
| 690 | + p := testProvider("aws") |
| 691 | + m := testModule(t, "refresh-module-orphan") |
| 692 | + |
| 693 | + // Create a custom refresh function to track the order they were visited |
| 694 | + var order []string |
| 695 | + var orderLock sync.Mutex |
| 696 | + p.RefreshFn = func( |
| 697 | + info *InstanceInfo, |
| 698 | + is *InstanceState) (*InstanceState, error) { |
| 699 | + orderLock.Lock() |
| 700 | + defer orderLock.Unlock() |
| 701 | + |
| 702 | + order = append(order, is.ID) |
| 703 | + return is, nil |
| 704 | + } |
| 705 | + |
| 706 | + state := &State{ |
| 707 | + Modules: []*ModuleState{ |
| 708 | + &ModuleState{ |
| 709 | + Path: rootModulePath, |
| 710 | + Resources: map[string]*ResourceState{ |
| 711 | + "aws_instance.foo": &ResourceState{ |
| 712 | + Primary: &InstanceState{ |
| 713 | + ID: "i-abc123", |
| 714 | + Attributes: map[string]string{ |
| 715 | + "childid": "i-bcd234", |
| 716 | + "grandchildid": "i-cde345", |
| 717 | + }, |
| 718 | + }, |
| 719 | + Dependencies: []string{ |
| 720 | + "module.child", |
| 721 | + "module.child", |
| 722 | + }, |
| 723 | + }, |
| 724 | + }, |
| 725 | + }, |
| 726 | + &ModuleState{ |
| 727 | + Path: append(rootModulePath, "child"), |
| 728 | + Resources: map[string]*ResourceState{ |
| 729 | + "aws_instance.bar": &ResourceState{ |
| 730 | + Primary: &InstanceState{ |
| 731 | + ID: "i-bcd234", |
| 732 | + Attributes: map[string]string{ |
| 733 | + "grandchildid": "i-cde345", |
| 734 | + }, |
| 735 | + }, |
| 736 | + Dependencies: []string{ |
| 737 | + "module.grandchild", |
| 738 | + }, |
| 739 | + }, |
| 740 | + }, |
| 741 | + Outputs: map[string]string{ |
| 742 | + "id": "i-bcd234", |
| 743 | + "grandchild_id": "i-cde345", |
| 744 | + }, |
| 745 | + }, |
| 746 | + &ModuleState{ |
| 747 | + Path: append(rootModulePath, "child", "grandchild"), |
| 748 | + Resources: map[string]*ResourceState{ |
| 749 | + "aws_instance.baz": &ResourceState{ |
| 750 | + Primary: &InstanceState{ |
| 751 | + ID: "i-cde345", |
| 752 | + }, |
| 753 | + }, |
| 754 | + }, |
| 755 | + Outputs: map[string]string{ |
| 756 | + "id": "i-cde345", |
| 757 | + }, |
| 758 | + }, |
| 759 | + }, |
| 760 | + } |
| 761 | + ctx := testContext2(t, &ContextOpts{ |
| 762 | + Module: m, |
| 763 | + Providers: map[string]ResourceProviderFactory{ |
| 764 | + "aws": testProviderFuncFixed(p), |
| 765 | + }, |
| 766 | + State: state, |
| 767 | + }) |
| 768 | + |
| 769 | + testCheckDeadlock(t, func() { |
| 770 | + _, err := ctx.Refresh() |
| 771 | + if err != nil { |
| 772 | + t.Fatalf("err: %s", err) |
| 773 | + } |
| 774 | + |
| 775 | + // TODO: handle order properly for orphaned modules / resources |
| 776 | + // expected := []string{"i-abc123", "i-bcd234", "i-cde345"} |
| 777 | + // if !reflect.DeepEqual(order, expected) { |
| 778 | + // t.Fatalf("expected: %#v, got: %#v", expected, order) |
| 779 | + // } |
| 780 | + }) |
| 781 | +} |
| 782 | + |
688 | 783 | func TestContext2Validate(t *testing.T) { |
689 | 784 | p := testProvider("aws") |
690 | 785 | m := testModule(t, "validate-good") |
|
0 commit comments