Skip to content

Commit

Permalink
handlers: check DB access from heartbeat but don't fail when inaccess…
Browse files Browse the repository at this point in the history
…ible
  • Loading branch information
Greg Guthe committed Jul 23, 2019
1 parent 066a7fe commit 76f64b9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,19 @@ func (a *autographer) handleHeartbeat(w http.ResponseWriter, r *http.Request) {
}
}

// check the database connection and return its status, but
// don't fail the heartbeat since we only care about DB
// connectivity on server start
if a.db != nil {
err := a.db.CheckConnection()
if err == nil {
result["dbAccessible"] = true
} else {
log.Errorf("error checking DB connection: %s", err)
result["dbAccessible"] = false
}
}

respdata, err := json.Marshal(result)
if err != nil {
log.Errorf("heartbeat failed to marshal JSON with error: %s", err)
Expand Down
37 changes: 36 additions & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"testing"

"go.mozilla.org/autograph/database"
"go.mozilla.org/autograph/signer/apk"
"go.mozilla.org/autograph/signer/contentsignature"
"go.mozilla.org/autograph/signer/mar"
Expand Down Expand Up @@ -416,7 +417,7 @@ func TestHeartbeat(t *testing.T) {
}

func TestHeartbeatChecksHSMStatusFails(t *testing.T) {
// NB: do not run in parallel with TestHeartbeat
// NB: do not run in parallel with TestHeartbeat*
ag.hsmHeartbeatSignerConf = &ag.signers[0].(*contentsignature.ContentSigner).Configuration

expectedStatus := http.StatusInternalServerError
Expand All @@ -439,6 +440,40 @@ func TestHeartbeatChecksHSMStatusFails(t *testing.T) {
ag.hsmHeartbeatSignerConf = nil
}

func TestHeartbeatChecksDBStatusOK(t *testing.T) {
// NB: do not run in parallel with TestHeartbeat* or DB tests
db, err := database.Connect(database.Config{
Name: "autograph",
User: "myautographdbuser",
Password: "myautographdbpassword",
Host: "127.0.0.1:5432",
})
if err != nil {
t.Fatal(err)
}
ag.db = db

expectedStatus := http.StatusOK
expectedBody := []byte("{\"dbAccessible\":true}")

req, err := http.NewRequest(`GET`, "http://foo.bar/__heartbeat__", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
ag.handleHeartbeat(w, req)

if w.Code != expectedStatus {
t.Fatalf("failed with code %d but %d was expected", w.Code, expectedStatus)
}
if !bytes.Equal(w.Body.Bytes(), expectedBody) {
t.Fatalf("got unexpected heartbeat body %s expected %s", w.Body.Bytes(), expectedBody)
}

db.Close()
ag.db = nil
}

func TestVersion(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 76f64b9

Please sign in to comment.