|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package actionvalidator_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 16 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-framework-validators/actionvalidator" |
| 19 | +) |
| 20 | + |
| 21 | +func TestAllValidatorValidateAction(t *testing.T) { |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + testCases := map[string]struct { |
| 25 | + validators []action.ConfigValidator |
| 26 | + req action.ValidateConfigRequest |
| 27 | + expected *action.ValidateConfigResponse |
| 28 | + }{ |
| 29 | + "no-diagnostics": { |
| 30 | + validators: []action.ConfigValidator{ |
| 31 | + actionvalidator.ExactlyOneOf( |
| 32 | + path.MatchRoot("test1"), |
| 33 | + path.MatchRoot("test2"), |
| 34 | + ), |
| 35 | + actionvalidator.All( |
| 36 | + actionvalidator.AtLeastOneOf( |
| 37 | + path.MatchRoot("test3"), |
| 38 | + path.MatchRoot("test4"), |
| 39 | + ), |
| 40 | + actionvalidator.Conflicting( |
| 41 | + path.MatchRoot("test3"), |
| 42 | + path.MatchRoot("test5"), |
| 43 | + ), |
| 44 | + ), |
| 45 | + }, |
| 46 | + req: action.ValidateConfigRequest{ |
| 47 | + Config: tfsdk.Config{ |
| 48 | + Schema: schema.UnlinkedSchema{ |
| 49 | + Attributes: map[string]schema.Attribute{ |
| 50 | + "test1": schema.StringAttribute{ |
| 51 | + Optional: true, |
| 52 | + }, |
| 53 | + "test2": schema.StringAttribute{ |
| 54 | + Optional: true, |
| 55 | + }, |
| 56 | + "test3": schema.StringAttribute{ |
| 57 | + Optional: true, |
| 58 | + }, |
| 59 | + "test4": schema.StringAttribute{ |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + "test5": schema.StringAttribute{ |
| 63 | + Optional: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + Raw: tftypes.NewValue( |
| 68 | + tftypes.Object{ |
| 69 | + AttributeTypes: map[string]tftypes.Type{ |
| 70 | + "test1": tftypes.String, |
| 71 | + "test2": tftypes.String, |
| 72 | + "test3": tftypes.String, |
| 73 | + "test4": tftypes.String, |
| 74 | + "test5": tftypes.String, |
| 75 | + }, |
| 76 | + }, |
| 77 | + map[string]tftypes.Value{ |
| 78 | + "test1": tftypes.NewValue(tftypes.String, nil), |
| 79 | + "test2": tftypes.NewValue(tftypes.String, nil), |
| 80 | + "test3": tftypes.NewValue(tftypes.String, "test-value"), |
| 81 | + "test4": tftypes.NewValue(tftypes.String, nil), |
| 82 | + "test5": tftypes.NewValue(tftypes.String, nil), |
| 83 | + }, |
| 84 | + ), |
| 85 | + }, |
| 86 | + }, |
| 87 | + expected: &action.ValidateConfigResponse{}, |
| 88 | + }, |
| 89 | + "diagnostics": { |
| 90 | + validators: []action.ConfigValidator{ |
| 91 | + actionvalidator.ExactlyOneOf( |
| 92 | + path.MatchRoot("test1"), |
| 93 | + path.MatchRoot("test2"), |
| 94 | + ), |
| 95 | + actionvalidator.All( |
| 96 | + actionvalidator.AtLeastOneOf( |
| 97 | + path.MatchRoot("test3"), |
| 98 | + path.MatchRoot("test4"), |
| 99 | + ), |
| 100 | + actionvalidator.Conflicting( |
| 101 | + path.MatchRoot("test3"), |
| 102 | + path.MatchRoot("test5"), |
| 103 | + ), |
| 104 | + ), |
| 105 | + }, |
| 106 | + req: action.ValidateConfigRequest{ |
| 107 | + Config: tfsdk.Config{ |
| 108 | + Schema: schema.UnlinkedSchema{ |
| 109 | + Attributes: map[string]schema.Attribute{ |
| 110 | + "test1": schema.StringAttribute{ |
| 111 | + Optional: true, |
| 112 | + }, |
| 113 | + "test2": schema.StringAttribute{ |
| 114 | + Optional: true, |
| 115 | + }, |
| 116 | + "test3": schema.StringAttribute{ |
| 117 | + Optional: true, |
| 118 | + }, |
| 119 | + "test4": schema.StringAttribute{ |
| 120 | + Optional: true, |
| 121 | + }, |
| 122 | + "test5": schema.StringAttribute{ |
| 123 | + Optional: true, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + Raw: tftypes.NewValue( |
| 128 | + tftypes.Object{ |
| 129 | + AttributeTypes: map[string]tftypes.Type{ |
| 130 | + "test1": tftypes.String, |
| 131 | + "test2": tftypes.String, |
| 132 | + "test3": tftypes.String, |
| 133 | + "test4": tftypes.String, |
| 134 | + "test5": tftypes.String, |
| 135 | + }, |
| 136 | + }, |
| 137 | + map[string]tftypes.Value{ |
| 138 | + "test1": tftypes.NewValue(tftypes.String, nil), |
| 139 | + "test2": tftypes.NewValue(tftypes.String, nil), |
| 140 | + "test3": tftypes.NewValue(tftypes.String, "test-value"), |
| 141 | + "test4": tftypes.NewValue(tftypes.String, nil), |
| 142 | + "test5": tftypes.NewValue(tftypes.String, "test-value"), |
| 143 | + }, |
| 144 | + ), |
| 145 | + }, |
| 146 | + }, |
| 147 | + expected: &action.ValidateConfigResponse{ |
| 148 | + Diagnostics: diag.Diagnostics{ |
| 149 | + diag.NewErrorDiagnostic( |
| 150 | + "Missing Attribute Configuration", |
| 151 | + "Exactly one of these attributes must be configured: [test1,test2]", |
| 152 | + ), |
| 153 | + diag.WithPath(path.Root("test3"), |
| 154 | + diag.NewErrorDiagnostic( |
| 155 | + "Invalid Attribute Combination", |
| 156 | + "These attributes cannot be configured together: [test3,test5]", |
| 157 | + )), |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + for name, testCase := range testCases { |
| 164 | + |
| 165 | + t.Run(name, func(t *testing.T) { |
| 166 | + t.Parallel() |
| 167 | + |
| 168 | + got := &action.ValidateConfigResponse{} |
| 169 | + |
| 170 | + actionvalidator.Any(testCase.validators...).ValidateAction(context.Background(), testCase.req, got) |
| 171 | + |
| 172 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 173 | + t.Errorf("unexpected difference: %s", diff) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments