-
Notifications
You must be signed in to change notification settings - Fork 22
test: unit test to document validation behavior of parameters #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package provider_test | |
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -686,6 +687,259 @@ data "coder_parameter" "region" { | |
} | ||
} | ||
|
||
// TestParameterValidationEnforcement tests various parameter states and the | ||
// validation enforcement that should be applied to them. The table is described | ||
// by a markdown table. This is done so that the test cases can be more easily | ||
// edited and read. | ||
// | ||
// Copy and paste the table to https://www.tablesgenerator.com/markdown_tables for easier editing | ||
// | ||
//nolint:paralleltest,tparallel // Parameters load values from env vars | ||
func TestParameterValidationEnforcement(t *testing.T) { | ||
// Some interesting observations: | ||
// - Validation logic does not apply to the value of 'options' | ||
// - [NumDefInvOpt] So an invalid option can be present and selected, but would fail | ||
// - Validation logic does not apply to the default if a value is given | ||
// - [NumIns/DefInv] So the default can be invalid if an input value is valid. | ||
// The value is therefore not really optional, but it is marked as such. | ||
// - [NumInsNotOptsVal | NumsInsNotOpts] values do not need to be in the option set? | ||
|
||
table := strings.TrimSpace(` | ||
| Name | Type | Input Value | Default | Options | Validation | -> | Output Value | Optional | Error | | ||
|---------------------|---------------|-------------|---------|-------------------|------------|----|--------------|----------|--------------| | ||
| | Empty Vals | | | | | | | | | | ||
| Emty | string,number | | | | | | "" | false | | | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| EmtyOpts | string,number | | | 1,2,3 | | | "" | false | | | ||
| EmtyRegex | string | | | | world | | | | regex error | | ||
| EmtyMin | number | | | | 1-10 | | | | 1 < < 10 | | ||
| EmtyMinOpt | number | | | 1,2,3 | 2-5 | | | | 2 < < 5 | | ||
| EmtyRegexOpt | string | | | "hello","goodbye" | goodbye | | | | regex error | | ||
| EmtyRegexOk | string | | | | .* | | "" | false | | | ||
| | | | | | | | | | | | ||
| | Default Set | No inputs | | | | | | | | | ||
| NumDef | number | | 5 | | | | 5 | true | | | ||
| NumDefVal | number | | 5 | | 3-7 | | 5 | true | | | ||
| NumDefInv | number | | 5 | | 10- | | | | 10 < 5 < 0 | | ||
| NumDefOpts | number | | 5 | 1,3,5,7 | 2-6 | | 5 | true | | | ||
| NumDefNotOpts | number | | 5 | 1,3,7,9 | 2-6 | | | | valid option | | ||
| NumDefInvOpt | number | | 5 | 1,3,5,7 | 6-10 | | | | 6 < 5 < 10 | | ||
| | | | | | | | | | | | ||
| StrDef | string | | hello | | | | hello | true | | | ||
| StrDefInv | string | | hello | | world | | | | regex error | | ||
| StrDefOpts | string | | a | a,b,c | | | a | true | | | ||
| StrDefNotOpts | string | | a | b,c,d | | | | | valid option | | ||
| StrDefOpts | string | | a | a,b,c,d,e,f | [a-c] | | a | true | | | ||
| StrDefInvOpt | string | | d | a,b,c,d,e,f | [a-c] | | | | regex error | | ||
| | | | | | | | | | | | ||
| | Input Vals | | | | | | | | | | ||
| NumIns | number | 3 | | | | | 3 | false | | | ||
| NumInsDef | number | 3 | 5 | | | | 3 | true | | | ||
| NumIns/DefInv | number | 3 | 5 | | 1-3 | | 3 | true | | | ||
| NumIns=DefInv | number | 5 | 5 | | 1-3 | | | | 1 < 5 < 3 | | ||
| NumInsOpts | number | 3 | 5 | 1,2,3,4,5 | 1-3 | | 3 | true | | | ||
| NumInsNotOptsVal | number | 3 | 5 | 1,2,4,5 | 1-3 | | 3 | true | | | ||
| NumInsNotOptsInv | number | 3 | 5 | 1,2,4,5 | 1-2 | | | true | 1 < 3 < 2 | | ||
| NumInsNotOpts | number | 3 | 5 | 1,2,4,5 | | | 3 | true | | | ||
| NumInsNotOpts/NoDef | number | 3 | | 1,2,4,5 | | | 3 | false | | | ||
| | | | | | | | | | | | ||
| StrIns | string | c | | | | | c | false | | | ||
| StrInsDef | string | c | e | | | | c | true | | | ||
| StrIns/DefInv | string | c | e | | [a-c] | | c | true | | | ||
| NumIns=DefInv | string | e | e | | [a-c] | | | | regex error | | ||
| StrInsOpts | string | c | e | a,b,c,d,e | [a-c] | | c | true | | | ||
| StrInsNotOptsVal | string | c | e | a,b,d,e | [a-c] | | c | true | | | ||
| StrInsNotOptsInv | string | c | e | a,b,d,e | [a-b] | | | | regex error | | ||
| StrInsNotOpts | string | c | e | a,b,d,e | | | c | true | | | ||
| StrInsNotOpts/NoDef | string | c | | a,b,d,e | | | c | false | | | ||
`) | ||
|
||
type row struct { | ||
Name string | ||
Types []string | ||
InputValue string | ||
Default string | ||
Options []string | ||
Validation *provider.Validation | ||
OutputValue string | ||
Optional bool | ||
Error *regexp.Regexp | ||
} | ||
|
||
rows := make([]row, 0) | ||
lines := strings.Split(table, "\n") | ||
validMinMax := regexp.MustCompile("^[0-9]*-[0-9]*$") | ||
for _, line := range lines[2:] { | ||
columns := strings.Split(line, "|") | ||
columns = columns[1 : len(columns)-1] | ||
for i := range columns { | ||
// Trim the whitespace from all columns | ||
columns[i] = strings.TrimSpace(columns[i]) | ||
} | ||
|
||
if columns[0] == "" { | ||
continue // Skip rows with empty names | ||
} | ||
|
||
optional, err := strconv.ParseBool(columns[8]) | ||
if columns[8] != "" { | ||
// Value does not matter if not specified | ||
require.NoError(t, err) | ||
} | ||
|
||
var rerr *regexp.Regexp | ||
if columns[9] != "" { | ||
rerr, err = regexp.Compile(columns[9]) | ||
if err != nil { | ||
t.Fatalf("failed to parse error column %q: %v", columns[9], err) | ||
} | ||
} | ||
var options []string | ||
if columns[4] != "" { | ||
options = strings.Split(columns[4], ",") | ||
} | ||
|
||
var validation *provider.Validation | ||
if columns[5] != "" { | ||
// Min-Max validation should look like: | ||
// 1-10 :: min=1, max=10 | ||
// -10 :: max=10 | ||
// 1- :: min=1 | ||
if validMinMax.MatchString(columns[5]) { | ||
parts := strings.Split(columns[5], "-") | ||
min, _ := strconv.ParseInt(parts[0], 10, 64) | ||
max, _ := strconv.ParseInt(parts[1], 10, 64) | ||
validation = &provider.Validation{ | ||
Min: int(min), | ||
MinDisabled: parts[0] == "", | ||
Max: int(max), | ||
MaxDisabled: parts[1] == "", | ||
Monotonic: "", | ||
Regex: "", | ||
Error: "{min} < {value} < {max}", | ||
} | ||
} else { | ||
validation = &provider.Validation{ | ||
Min: 0, | ||
MinDisabled: true, | ||
Max: 0, | ||
MaxDisabled: true, | ||
Monotonic: "", | ||
Regex: columns[5], | ||
Error: "regex error", | ||
} | ||
} | ||
} | ||
Comment on lines
+725
to
+785
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Extract this to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it cannot be reused outside the test, I am going to leave it here. |
||
|
||
rows = append(rows, row{ | ||
Name: columns[0], | ||
Types: strings.Split(columns[1], ","), | ||
InputValue: columns[2], | ||
Default: columns[3], | ||
Options: options, | ||
Validation: validation, | ||
OutputValue: columns[7], | ||
Optional: optional, | ||
Error: rerr, | ||
}) | ||
} | ||
|
||
stringLiteral := func(s string) string { | ||
if s == "" { | ||
return `""` | ||
} | ||
return fmt.Sprintf("%q", s) | ||
} | ||
|
||
for rowIndex, row := range rows { | ||
for _, rt := range row.Types { | ||
//nolint:paralleltest,tparallel // Parameters load values from env vars | ||
t.Run(fmt.Sprintf("%d|%s:%s", rowIndex, row.Name, rt), func(t *testing.T) { | ||
if row.InputValue != "" { | ||
t.Setenv(provider.ParameterEnvironmentVariable("parameter"), row.InputValue) | ||
} | ||
|
||
if row.Error != nil { | ||
if row.OutputValue != "" { | ||
t.Errorf("output value %q should not be set if error is set", row.OutputValue) | ||
} | ||
} | ||
|
||
var cfg strings.Builder | ||
cfg.WriteString("data \"coder_parameter\" \"parameter\" {\n") | ||
cfg.WriteString("\tname = \"parameter\"\n") | ||
cfg.WriteString(fmt.Sprintf("\ttype = \"%s\"\n", rt)) | ||
if row.Default != "" { | ||
cfg.WriteString(fmt.Sprintf("\tdefault = %s\n", stringLiteral(row.Default))) | ||
} | ||
|
||
for _, opt := range row.Options { | ||
cfg.WriteString("\toption {\n") | ||
cfg.WriteString(fmt.Sprintf("\t\tname = %s\n", stringLiteral(opt))) | ||
cfg.WriteString(fmt.Sprintf("\t\tvalue = %s\n", stringLiteral(opt))) | ||
cfg.WriteString("\t}\n") | ||
} | ||
|
||
if row.Validation != nil { | ||
cfg.WriteString("\tvalidation {\n") | ||
if !row.Validation.MinDisabled { | ||
cfg.WriteString(fmt.Sprintf("\t\tmin = %d\n", row.Validation.Min)) | ||
} | ||
if !row.Validation.MaxDisabled { | ||
cfg.WriteString(fmt.Sprintf("\t\tmax = %d\n", row.Validation.Max)) | ||
} | ||
if row.Validation.Monotonic != "" { | ||
cfg.WriteString(fmt.Sprintf("\t\tmonotonic = \"%s\"\n", row.Validation.Monotonic)) | ||
} | ||
if row.Validation.Regex != "" { | ||
cfg.WriteString(fmt.Sprintf("\t\tregex = %q\n", row.Validation.Regex)) | ||
} | ||
cfg.WriteString(fmt.Sprintf("\t\terror = %q\n", row.Validation.Error)) | ||
cfg.WriteString("\t}\n") | ||
} | ||
|
||
cfg.WriteString("}\n") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
ProviderFactories: coderFactory(), | ||
IsUnitTest: true, | ||
Steps: []resource.TestStep{{ | ||
Config: cfg.String(), | ||
ExpectError: row.Error, | ||
Check: func(state *terraform.State) error { | ||
require.Len(t, state.Modules, 1) | ||
require.Len(t, state.Modules[0].Resources, 1) | ||
param := state.Modules[0].Resources["data.coder_parameter.parameter"] | ||
require.NotNil(t, param) | ||
|
||
if row.Default == "" { | ||
_, ok := param.Primary.Attributes["default"] | ||
require.False(t, ok, "default should not be set") | ||
} else { | ||
require.Equal(t, strings.Trim(row.Default, `"`), param.Primary.Attributes["default"]) | ||
} | ||
|
||
if row.OutputValue == "" { | ||
_, ok := param.Primary.Attributes["value"] | ||
require.False(t, ok, "output value should not be set") | ||
} else { | ||
require.Equal(t, strings.Trim(row.OutputValue, `"`), param.Primary.Attributes["value"]) | ||
} | ||
|
||
for key, expected := range map[string]string{ | ||
"optional": strconv.FormatBool(row.Optional), | ||
} { | ||
require.Equal(t, expected, param.Primary.Attributes[key], "optional") | ||
} | ||
|
||
return nil | ||
}, | ||
}}, | ||
}) | ||
}) | ||
} | ||
} | ||
} | ||
|
||
func TestValueValidatesType(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range []struct { | ||
|
@@ -798,6 +1052,25 @@ func TestValueValidatesType(t *testing.T) { | |
Value: `[]`, | ||
MinDisabled: true, | ||
MaxDisabled: true, | ||
}, { | ||
Name: "ValidListOfStrings", | ||
Type: "list(string)", | ||
Value: `["first","second","third"]`, | ||
MinDisabled: true, | ||
MaxDisabled: true, | ||
}, { | ||
Name: "InvalidListOfStrings", | ||
Type: "list(string)", | ||
Value: `["first","second","third"`, | ||
MinDisabled: true, | ||
MaxDisabled: true, | ||
Error: regexp.MustCompile("is not valid list of strings"), | ||
}, { | ||
Name: "EmptyListOfStrings", | ||
Type: "list(string)", | ||
Value: `[]`, | ||
MinDisabled: true, | ||
MaxDisabled: true, | ||
}} { | ||
Comment on lines
+1014
to
1033
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added some extra test cases down here too |
||
tc := tc | ||
t.Run(tc.Name, func(t *testing.T) { | ||
|
Uh oh!
There was an error while loading. Please reload this page.