Skip to content

Commit

Permalink
feat: Healthz api check appId support
Browse files Browse the repository at this point in the history
related: dapr/components-contrib#2489

Signed-off-by: AlbertHuang <[email protected]>
  • Loading branch information
alberthuang24 committed Apr 23, 2023
1 parent c769460 commit a355aee
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,13 @@ func (a *api) onGetHealthz(reqCtx *fasthttp.RequestCtx) {
respond(reqCtx, withError(fasthttp.StatusInternalServerError, msg))
log.Debug(msg)
} else {
respond(reqCtx, withEmpty())
matchAppID := string(reqCtx.QueryArgs().Peek("app-id"))
if matchAppID != "" && matchAppID != a.id {
msg := NewErrorResponse("ERR_HEALTH_APPID_NOT_MATCH", messages.ErrHealthAppIDNotMatch)
respond(reqCtx, withError(fasthttp.StatusInternalServerError, msg))
} else {
respond(reqCtx, withEmpty())
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/http/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4998,8 +4998,10 @@ func (l *fakeWorkflowComponent) GetComponentMetadata() map[string]string {
func TestV1HealthzEndpoint(t *testing.T) {
fakeServer := newFakeHTTPServer()

appId := "test"
testAPI := &api{
actor: nil,
id: appId,
}

fakeServer.StartServer(testAPI.constructHealthzEndpoints())
Expand All @@ -5019,6 +5021,20 @@ func TestV1HealthzEndpoint(t *testing.T) {
assert.Equal(t, 204, resp.StatusCode)
})

t.Run("Healthz - 500 No AppId Match", func(t *testing.T) {
apiPath := "v1.0/healthz"
testAPI.MarkStatusAsReady()
resp := fakeServer.DoRequest("GET", apiPath, nil, map[string]string{"app-id": "not-test"})
assert.Equal(t, 500, resp.StatusCode)
})

t.Run("Healthz - 204 AppId Match", func(t *testing.T) {
apiPath := "v1.0/healthz"
testAPI.MarkStatusAsReady()
resp := fakeServer.DoRequest("GET", apiPath, nil, map[string]string{"app-id": appId})
assert.Equal(t, 204, resp.StatusCode)
})

fakeServer.Shutdown()
}

Expand Down
1 change: 1 addition & 0 deletions pkg/messages/predefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const (
// Healthz.
ErrHealthNotReady = "dapr is not ready"
ErrOutboundHealthNotReady = "dapr outbound is not ready"
ErrHealthAppIDNotMatch = "dapr appId is not match"

// Configuration.
ErrConfigurationStoresNotConfigured = "configuration stores not configured"
Expand Down

0 comments on commit a355aee

Please sign in to comment.