@@ -8,11 +8,11 @@ import (
88 "testing"
99
1010 "github.com/google/go-cmp/cmp"
11- "github.com/hashicorp/terraform-plugin-framework/attr"
12- "github.com/hashicorp/terraform-plugin-framework/path"
1311 "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
1412 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
13+ "github.com/hashicorp/terraform-plugin-framework/tfsdk"
1514 "github.com/hashicorp/terraform-plugin-framework/types"
15+ "github.com/hashicorp/terraform-plugin-go/tftypes"
1616)
1717
1818func TestUseStateForUnknownModifierPlanModifyBool (t * testing.T ) {
@@ -26,6 +26,16 @@ func TestUseStateForUnknownModifierPlanModifyBool(t *testing.T) {
2626 // when we first create the resource, use the unknown
2727 // value
2828 request : planmodifier.BoolRequest {
29+ State : tfsdk.State {
30+ Raw : tftypes .NewValue (
31+ tftypes.Object {
32+ AttributeTypes : map [string ]tftypes.Type {
33+ "attr" : tftypes .Bool ,
34+ },
35+ },
36+ nil ,
37+ ),
38+ },
2939 StateValue : types .BoolNull (),
3040 PlanValue : types .BoolUnknown (),
3141 ConfigValue : types .BoolNull (),
@@ -42,6 +52,18 @@ func TestUseStateForUnknownModifierPlanModifyBool(t *testing.T) {
4252 // but we still want to preserve that value, in this
4353 // case
4454 request : planmodifier.BoolRequest {
55+ State : tfsdk.State {
56+ Raw : tftypes .NewValue (
57+ tftypes.Object {
58+ AttributeTypes : map [string ]tftypes.Type {
59+ "attr" : tftypes .Bool ,
60+ },
61+ },
62+ map [string ]tftypes.Value {
63+ "attr" : tftypes .NewValue (tftypes .Bool , false ),
64+ },
65+ ),
66+ },
4567 StateValue : types .BoolValue (false ),
4668 PlanValue : types .BoolValue (true ),
4769 ConfigValue : types .BoolNull (),
@@ -50,10 +72,22 @@ func TestUseStateForUnknownModifierPlanModifyBool(t *testing.T) {
5072 PlanValue : types .BoolValue (true ),
5173 },
5274 },
53- "non-null-state-unknown-plan" : {
75+ "non-null-state-value- unknown-plan" : {
5476 // this is the situation we want to preserve the state
5577 // in
5678 request : planmodifier.BoolRequest {
79+ State : tfsdk.State {
80+ Raw : tftypes .NewValue (
81+ tftypes.Object {
82+ AttributeTypes : map [string ]tftypes.Type {
83+ "attr" : tftypes .Bool ,
84+ },
85+ },
86+ map [string ]tftypes.Value {
87+ "attr" : tftypes .NewValue (tftypes .Bool , true ),
88+ },
89+ ),
90+ },
5791 StateValue : types .BoolValue (true ),
5892 PlanValue : types .BoolUnknown (),
5993 ConfigValue : types .BoolNull (),
@@ -62,6 +96,29 @@ func TestUseStateForUnknownModifierPlanModifyBool(t *testing.T) {
6296 PlanValue : types .BoolValue (true ),
6397 },
6498 },
99+ "null-state-value-unknown-plan" : {
100+ // Null state values are still known, so we should preserve this as well.
101+ request : planmodifier.BoolRequest {
102+ State : tfsdk.State {
103+ Raw : tftypes .NewValue (
104+ tftypes.Object {
105+ AttributeTypes : map [string ]tftypes.Type {
106+ "attr" : tftypes .Bool ,
107+ },
108+ },
109+ map [string ]tftypes.Value {
110+ "attr" : tftypes .NewValue (tftypes .Bool , nil ),
111+ },
112+ ),
113+ },
114+ StateValue : types .BoolNull (),
115+ PlanValue : types .BoolUnknown (),
116+ ConfigValue : types .BoolNull (),
117+ },
118+ expected : & planmodifier.BoolResponse {
119+ PlanValue : types .BoolNull (),
120+ },
121+ },
65122 "unknown-config" : {
66123 // this is the situation in which a user is
67124 // interpolating into a field. We want that to still
@@ -78,46 +135,6 @@ func TestUseStateForUnknownModifierPlanModifyBool(t *testing.T) {
78135 PlanValue : types .BoolUnknown (),
79136 },
80137 },
81- "under-list" : {
82- request : planmodifier.BoolRequest {
83- ConfigValue : types .BoolNull (),
84- Path : path .Root ("test" ).AtListIndex (0 ).AtName ("nested_test" ),
85- PlanValue : types .BoolUnknown (),
86- StateValue : types .BoolNull (),
87- },
88- expected : & planmodifier.BoolResponse {
89- PlanValue : types .BoolUnknown (),
90- },
91- },
92- "under-set" : {
93- request : planmodifier.BoolRequest {
94- ConfigValue : types .BoolNull (),
95- Path : path .Root ("test" ).AtSetValue (
96- types .SetValueMust (
97- types.ObjectType {
98- AttrTypes : map [string ]attr.Type {
99- "nested_test" : types .BoolType ,
100- },
101- },
102- []attr.Value {
103- types .ObjectValueMust (
104- map [string ]attr.Type {
105- "nested_test" : types .BoolType ,
106- },
107- map [string ]attr.Value {
108- "nested_test" : types .BoolUnknown (),
109- },
110- ),
111- },
112- ),
113- ).AtName ("nested_test" ),
114- PlanValue : types .BoolUnknown (),
115- StateValue : types .BoolNull (),
116- },
117- expected : & planmodifier.BoolResponse {
118- PlanValue : types .BoolUnknown (),
119- },
120- },
121138 }
122139
123140 for name , testCase := range testCases {
0 commit comments