Skip to content

Commit

Permalink
command: fixup parsing of stale query parameter (#15654)
Browse files Browse the repository at this point in the history
In #15605 we fixed the bug where the presense of "stale" query parameter
was mean to imply stale, even if the value of the parameter was "false"
or malformed. In parsing, we missed the case where the slice of values
would be nil which lead to a failing test case that was missed because
CI didn't run against the original PR.

Co-authored-by: Seth Hoenig <shoenig@duck.com>
  • Loading branch information
hc-github-team-nomad-core and shoenig committed Jan 3, 2023
1 parent 6c59a6e commit d146bb2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,17 @@ func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOpti
func parseConsistency(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) {
query := req.URL.Query()
if staleVal, ok := query["stale"]; ok {
if len(staleVal) == 0 || staleVal[0] == "" {
b.AllowStale = true
return
}
staleQuery, err := strconv.ParseBool(staleVal[0])
if err != nil {
resp.WriteHeader(400)
resp.Write([]byte(fmt.Sprintf("Expect `true` or `false` for `stale` query string parameter, got %s", staleVal[0])))
_, _ = resp.Write([]byte(fmt.Sprintf("Expect `true` or `false` for `stale` query string parameter, got %s", staleVal[0])))
return
}

b.AllowStale = staleQuery || staleVal[0] == ""
b.AllowStale = staleQuery
}
}

Expand Down
1 change: 1 addition & 0 deletions command/operator_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func runTestCases(t *testing.T, cases testCases) {
require.Equalf(t, code, c.expectedCode, "expected exit code %d, got: %d: %s", c.expectedCode, code, outerr)
for _, expectedOutput := range c.expectedOutputs {
require.Contains(t, out, expectedOutput, "expected output %q, got %q", expectedOutput, out)

}
require.Containsf(t, outerr, c.expectedError, "expected error %q, got %q", c.expectedError, outerr)
})
Expand Down

0 comments on commit d146bb2

Please sign in to comment.