api: Return 404 when deregistering a non-existent check (#11950)

Update the `/agent/check/deregister/` API endpoint to return a 404
HTTP response code when an attempt is made to de-register a check ID
that does not exist on the agent.

This brings the behavior of /agent/check/deregister/ in line with the
behavior of /agent/service/deregister/ which was changed in #10632 to
similarly return a 404 when de-registering non-existent services.

Fixes #5821
pull/11963/head
Blake Covarrubias 3 years ago committed by GitHub
parent 1eac39ae9c
commit 4bd92921f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
```release-note:improvement
api: Return 404 when de-registering a non-existent check
```

@ -149,7 +149,9 @@ func (a *Agent) vetCheckUpdateWithAuthorizer(authz acl.Authorizer, checkID struc
} }
} }
} else { } else {
return fmt.Errorf("Unknown check ID %q. Ensure that the check ID is passed, not the check name.", checkID.String()) return NotFoundError{Reason: fmt.Sprintf(
"Unknown check ID %q. Ensure that the check ID is passed, not the check name.",
checkID.String())}
} }
return nil return nil

@ -2889,12 +2889,19 @@ func TestAgent_DeregisterCheck(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
t.Run("remove registered check", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil) req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req) a.srv.h.ServeHTTP(resp, req)
if http.StatusOK != resp.Code { require.Equal(t, http.StatusOK, resp.Code)
t.Fatalf("expected 200 but got %v", resp.Code) })
}
t.Run("remove non-existent check", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/test", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})
// Ensure we have a check mapping // Ensure we have a check mapping
requireCheckMissing(t, a, "test") requireCheckMissing(t, a, "test")
@ -2928,6 +2935,20 @@ func TestAgent_DeregisterCheckACLDeny(t *testing.T) {
a.srv.h.ServeHTTP(resp, req) a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusOK, resp.Code) require.Equal(t, http.StatusOK, resp.Code)
}) })
t.Run("non-existent check without token", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/_nope_", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})
t.Run("non-existent check with token", func(t *testing.T) {
req, _ := http.NewRequest("PUT", "/v1/agent/check/deregister/_nope_?token=root", nil)
resp := httptest.NewRecorder()
a.srv.h.ServeHTTP(resp, req)
require.Equal(t, http.StatusNotFound, resp.Code)
})
} }
func TestAgent_PassCheck(t *testing.T) { func TestAgent_PassCheck(t *testing.T) {

Loading…
Cancel
Save