From d146bb2d05043150827eaf591a2300f2229cae4c Mon Sep 17 00:00:00 2001 From: hc-github-team-nomad-core <82989552+hc-github-team-nomad-core@users.noreply.github.com> Date: Tue, 3 Jan 2023 10:37:04 -0500 Subject: [PATCH] command: fixup parsing of stale query parameter (#15654) 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 --- command/agent/http.go | 10 +++++++--- command/operator_debug_test.go | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index ac76dedc7bc..37371f9350b 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -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 } } diff --git a/command/operator_debug_test.go b/command/operator_debug_test.go index 945c0abf59e..9409c08b665 100644 --- a/command/operator_debug_test.go +++ b/command/operator_debug_test.go @@ -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) })