Skip to content

Commit

Permalink
Add more validation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kate-osborn committed Sep 6, 2024
1 parent 61dffdf commit ef188b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
16 changes: 16 additions & 0 deletions internal/mode/static/state/graph/snippets_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,25 @@ func validateSnippetsFilter(filter *ngfAPI.SnippetsFilter) *conditions.Condition
var allErrs field.ErrorList
snippetsPath := field.NewPath("spec.snippets")

if len(filter.Spec.Snippets) == 0 {
cond := staticConds.NewSnippetsFilterInvalid(
field.Required(snippetsPath, "at least one snippet must be provided").Error(),
)
return &cond
}

usedContexts := make(map[ngfAPI.NginxContext]struct{})

for i, snippet := range filter.Spec.Snippets {
valuePath := snippetsPath.Index(i).Child("value")
if snippet.Value == "" {
cond := staticConds.NewSnippetsFilterInvalid(
field.Required(valuePath, "value cannot be empty").Error(),
)

return &cond
}

ctxPath := snippetsPath.Index(i).Child("context")

switch snippet.Context {
Expand Down
31 changes: 29 additions & 2 deletions internal/mode/static/state/graph/snippets_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func TestValidateSnippetsFilter(t *testing.T) {
},
expCond: conditions.Condition{},
},
{
msg: "empty filter",
filter: &ngfAPI.SnippetsFilter{},
expCond: staticConds.NewSnippetsFilterInvalid(
"spec.snippets: Required value: at least one snippet must be provided",
),
},
{
msg: "invalid filter; invalid snippet context",
filter: &ngfAPI.SnippetsFilter{
Expand Down Expand Up @@ -170,7 +177,7 @@ func TestValidateSnippetsFilter(t *testing.T) {
Value: "invalid",
},
{
Context: "also invalid context",
Context: "", // empty context
Value: "invalid too",
},
},
Expand All @@ -179,7 +186,7 @@ func TestValidateSnippetsFilter(t *testing.T) {
expCond: staticConds.NewSnippetsFilterInvalid(
"[spec.snippets[1].context: Unsupported value: \"invalid context\": supported values: " +
"\"main\", \"http\", \"http.server\", \"http.server.location\", spec.snippets[2].context: " +
"Unsupported value: \"also invalid context\": supported values: \"main\", \"http\", " +
"Unsupported value: \"\": supported values: \"main\", \"http\", " +
"\"http.server\", \"http.server.location\"]",
),
},
Expand Down Expand Up @@ -237,6 +244,26 @@ func TestValidateSnippetsFilter(t *testing.T) {
"\"http\", \"http.server\", \"http.server.location\"]",
),
},
{
msg: "invalid filter; empty value",
filter: &ngfAPI.SnippetsFilter{
Spec: ngfAPI.SnippetsFilterSpec{
Snippets: []ngfAPI.Snippet{
{
Context: ngfAPI.NginxContextMain,
Value: "main snippet",
},
{
Context: ngfAPI.NginxContextMain,
Value: "", // empty value
},
},
},
},
expCond: staticConds.NewSnippetsFilterInvalid(
"spec.snippets[1].value: Required value: value cannot be empty",
),
},
}

for _, test := range tests {
Expand Down

0 comments on commit ef188b4

Please sign in to comment.