|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | +) |
| 10 | + |
| 11 | +func TestUseStateForUnknownIf_PlanModifyString(t *testing.T) { |
| 12 | + ctx := context.Background() |
| 13 | + |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + stateValue types.String |
| 17 | + planValue types.String |
| 18 | + configValue types.String |
| 19 | + ifFunc UseStateForUnknownIfFunc |
| 20 | + expectedPlanValue types.String |
| 21 | + expectedError bool |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "State is Null (Creation)", |
| 25 | + stateValue: types.StringNull(), |
| 26 | + planValue: types.StringUnknown(), |
| 27 | + configValue: types.StringValue("some-config"), |
| 28 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 29 | + // This should not be reached because the state is null |
| 30 | + resp.UseStateForUnknown = true |
| 31 | + }, |
| 32 | + expectedPlanValue: types.StringUnknown(), |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "Plan is already known - (User updated the value)", |
| 36 | + stateValue: types.StringValue("old-state"), |
| 37 | + planValue: types.StringValue("new-plan"), |
| 38 | + configValue: types.StringValue("new-plan"), |
| 39 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 40 | + // This should not be reached because the plan is known |
| 41 | + resp.UseStateForUnknown = true |
| 42 | + }, |
| 43 | + expectedPlanValue: types.StringValue("new-plan"), |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Config is Unknown (Interpolation)", |
| 47 | + stateValue: types.StringValue("old-state"), |
| 48 | + planValue: types.StringUnknown(), |
| 49 | + configValue: types.StringUnknown(), |
| 50 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 51 | + // This should not be reached |
| 52 | + resp.UseStateForUnknown = true |
| 53 | + }, |
| 54 | + expectedPlanValue: types.StringUnknown(), |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Condition returns False (Do not use state)", |
| 58 | + stateValue: types.StringValue("old-state"), |
| 59 | + planValue: types.StringUnknown(), |
| 60 | + configValue: types.StringNull(), // Simulating computed only |
| 61 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 62 | + resp.UseStateForUnknown = false |
| 63 | + }, |
| 64 | + expectedPlanValue: types.StringUnknown(), |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "Condition returns True (Use state)", |
| 68 | + stateValue: types.StringValue("old-state"), |
| 69 | + planValue: types.StringUnknown(), |
| 70 | + configValue: types.StringNull(), |
| 71 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 72 | + resp.UseStateForUnknown = true |
| 73 | + }, |
| 74 | + expectedPlanValue: types.StringValue("old-state"), |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "Func returns Error", |
| 78 | + stateValue: types.StringValue("old-state"), |
| 79 | + planValue: types.StringUnknown(), |
| 80 | + configValue: types.StringNull(), |
| 81 | + ifFunc: func(_ context.Context, _ planmodifier.StringRequest, resp *UseStateForUnknownFuncResponse) { |
| 82 | + resp.Diagnostics.AddError("Test Error", "Something went wrong") |
| 83 | + }, |
| 84 | + expectedPlanValue: types.StringUnknown(), |
| 85 | + expectedError: true, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + // Initialize the modifier |
| 92 | + modifier := UseStateForUnknownIf(tt.ifFunc, "test description") |
| 93 | + |
| 94 | + // Construct request |
| 95 | + req := planmodifier.StringRequest{ |
| 96 | + StateValue: tt.stateValue, |
| 97 | + PlanValue: tt.planValue, |
| 98 | + ConfigValue: tt.configValue, |
| 99 | + } |
| 100 | + |
| 101 | + // Construct response |
| 102 | + // Note: In the framework, resp.PlanValue is initialized to req.PlanValue |
| 103 | + // before the modifier is called. We must simulate this. |
| 104 | + resp := &planmodifier.StringResponse{ |
| 105 | + PlanValue: tt.planValue, |
| 106 | + } |
| 107 | + |
| 108 | + // Run the modifier |
| 109 | + modifier.PlanModifyString(ctx, req, resp) |
| 110 | + |
| 111 | + // Check Errors |
| 112 | + if tt.expectedError { |
| 113 | + if !resp.Diagnostics.HasError() { |
| 114 | + t.Error("Expected error, got none") |
| 115 | + } |
| 116 | + } else { |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + t.Errorf("Unexpected error: %s", resp.Diagnostics) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Check Plan Value |
| 123 | + if !resp.PlanValue.Equal(tt.expectedPlanValue) { |
| 124 | + t.Errorf("PlanValue mismatch.\nExpected: %s\nGot: %s", tt.expectedPlanValue, resp.PlanValue) |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
0 commit comments