|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 14 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 15 | +) |
| 16 | + |
| 17 | +type getter struct { |
| 18 | + val types.String |
| 19 | +} |
| 20 | + |
| 21 | +func (g *getter) GetAttribute(_ context.Context, _ path.Path, target interface{}) diag.Diagnostics { |
| 22 | + *(target.(*basetypes.StringValue)) = g.val |
| 23 | + return diag.Diagnostics{} |
| 24 | +} |
| 25 | + |
| 26 | +func TestModifyPlanForDefaultOrganizationChange(t *testing.T) { |
| 27 | + // if configOrg.IsNull() && !plannedOrg.IsNull() && providerDefaultOrg != plannedOrg.ValueString() { |
| 28 | + testCases := map[string]struct { |
| 29 | + providerDefaultOrg string |
| 30 | + planValue types.String |
| 31 | + configValue types.String |
| 32 | + expectedPlanValue string |
| 33 | + expectedRequiresReplace bool |
| 34 | + }{ |
| 35 | + "No change in provider org": { |
| 36 | + providerDefaultOrg: "foo", |
| 37 | + planValue: types.StringValue("foo"), |
| 38 | + configValue: types.StringNull(), |
| 39 | + expectedPlanValue: "foo", |
| 40 | + expectedRequiresReplace: false, |
| 41 | + }, |
| 42 | + "Change in provider org": { |
| 43 | + providerDefaultOrg: "bar", |
| 44 | + planValue: types.StringValue("foo"), |
| 45 | + configValue: types.StringNull(), |
| 46 | + expectedPlanValue: "bar", |
| 47 | + expectedRequiresReplace: true, |
| 48 | + }, |
| 49 | + "Config org changed": { |
| 50 | + providerDefaultOrg: "foo", |
| 51 | + planValue: types.StringValue("bar"), |
| 52 | + configValue: types.StringValue("bar"), |
| 53 | + expectedPlanValue: "bar", |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + for name, tc := range testCases { |
| 58 | + t.Run(name, func(t *testing.T) { |
| 59 | + fakeState := tftypes.NewValue(tftypes.Object{}, make(map[string]tftypes.Value)) |
| 60 | + |
| 61 | + fakeSchema := schema.Schema{ |
| 62 | + Attributes: map[string]schema.Attribute{ |
| 63 | + "organization": schema.StringAttribute{ |
| 64 | + Computed: true, |
| 65 | + Optional: true, |
| 66 | + Description: "Test organization", |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + fakePlan := tftypes.NewValue( |
| 72 | + tftypes.Object{ |
| 73 | + AttributeTypes: map[string]tftypes.Type{ |
| 74 | + "organization": tftypes.String, |
| 75 | + }, |
| 76 | + }, |
| 77 | + map[string]tftypes.Value{ |
| 78 | + "organization": tftypes.NewValue(tftypes.String, tc.planValue.ValueString()), |
| 79 | + }, |
| 80 | + ) |
| 81 | + |
| 82 | + fakeResponse := &resource.ModifyPlanResponse{ |
| 83 | + Plan: tfsdk.Plan{Schema: fakeSchema, Raw: fakePlan}, |
| 84 | + RequiresReplace: make(path.Paths, 0), |
| 85 | + Diagnostics: diag.Diagnostics{}, |
| 86 | + } |
| 87 | + |
| 88 | + c := context.TODO() |
| 89 | + |
| 90 | + modifyPlanForDefaultOrganizationChange( |
| 91 | + c, |
| 92 | + tc.providerDefaultOrg, |
| 93 | + tfsdk.State{Raw: fakeState}, |
| 94 | + &getter{val: tc.configValue}, |
| 95 | + &getter{val: tc.planValue}, |
| 96 | + fakeResponse, |
| 97 | + ) |
| 98 | + |
| 99 | + orgPath := path.Root("organization") |
| 100 | + var value types.String |
| 101 | + fakeResponse.Plan.GetAttribute(c, orgPath, &value) |
| 102 | + if fakeResponse.Diagnostics.HasError() { |
| 103 | + t.Fatalf("Expected no errors, got %v", fakeResponse.Diagnostics) |
| 104 | + } |
| 105 | + |
| 106 | + if value.ValueString() != tc.expectedPlanValue { |
| 107 | + t.Fatalf("Expected plan value to be %q, got %q", tc.expectedPlanValue, value.ValueString()) |
| 108 | + } |
| 109 | + |
| 110 | + if tc.expectedRequiresReplace && len(fakeResponse.RequiresReplace) == 0 { |
| 111 | + t.Fatal("Expected RequiresReplace to be set, but it was not") |
| 112 | + } |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
0 commit comments