Skip to content

Commit

Permalink
Fix: don't use 0 in subject and condition enums
Browse files Browse the repository at this point in the history
Keep 0 as the "default" value. For some assertion types that means not
used, and for others it means a different value will be used
(RESPONSE_BODY for subject, CONTAINS for condition).

This is backwards-compatible: if a check is sent with the value 0, it
uses the corresponding value. Future checks should use the non-zero
values. At some point in the future, we can update the code to error out
if a 0 is used.

Signed-off-by: Marcelo E. Magallon <marcelo.magallon@grafana.com>
  • Loading branch information
mem committed Jul 14, 2023
1 parent fd37a3e commit 6f4fa04
Show file tree
Hide file tree
Showing 3 changed files with 297 additions and 287 deletions.
8 changes: 4 additions & 4 deletions internal/prober/multihttp/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c assertionCondition) Name(w *strings.Builder, subject, value string) {
case sm.MultiHttpEntryAssertionConditionVariant_NOT_CONTAINS:
w.WriteString(`does not contain`)

case sm.MultiHttpEntryAssertionConditionVariant_CONTAINS:
case sm.MultiHttpEntryAssertionConditionVariant_CONTAINS, sm.MultiHttpEntryAssertionConditionVariant_DEFAULT_CONDITION:
w.WriteString(`contains`)

case sm.MultiHttpEntryAssertionConditionVariant_EQUALS:
Expand All @@ -91,7 +91,7 @@ func (c assertionCondition) Render(w *strings.Builder, subject, value string) {
w.WriteRune('!')
fallthrough

case sm.MultiHttpEntryAssertionConditionVariant_CONTAINS:
case sm.MultiHttpEntryAssertionConditionVariant_CONTAINS, sm.MultiHttpEntryAssertionConditionVariant_DEFAULT_CONDITION:
w.WriteString(subject)
w.WriteString(`.includes('`)
w.WriteString(template.JSEscapeString(value))
Expand Down Expand Up @@ -131,7 +131,7 @@ func buildChecks(url, method string, assertion *sm.MultiHttpEntryAssertion) stri
cond := assertionCondition(assertion.Condition)

switch assertion.Subject {
case sm.MultiHttpEntryAssertionSubjectVariant_RESPONSE_BODY:
case sm.MultiHttpEntryAssertionSubjectVariant_RESPONSE_BODY, sm.MultiHttpEntryAssertionSubjectVariant_DEFAULT_SUBJECT:
cond.Name(&b, "body", assertion.Value)
b.WriteString(`': response => `)
cond.Render(&b, "response.body", assertion.Value)
Expand Down Expand Up @@ -167,7 +167,7 @@ func buildChecks(url, method string, assertion *sm.MultiHttpEntryAssertion) stri

case sm.MultiHttpEntryAssertionType_REGEX_ASSERTION:
switch assertion.Subject {
case sm.MultiHttpEntryAssertionSubjectVariant_RESPONSE_BODY:
case sm.MultiHttpEntryAssertionSubjectVariant_RESPONSE_BODY, sm.MultiHttpEntryAssertionSubjectVariant_DEFAULT_SUBJECT:
b.WriteString(`body matches /`)
b.WriteString(template.JSEscapeString(assertion.Expression))
b.WriteString(`/': response => { const expr = new RegExp('`)
Expand Down
Loading

0 comments on commit 6f4fa04

Please sign in to comment.