- Notifications
You must be signed in to change notification settings - Fork1.1k
feat(healthcheck): add accessurl check#7193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package healthcheck | ||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
| "golang.org/x/xerrors" | ||
| ) | ||
| type AccessURLReport struct { | ||
| Healthy bool | ||
| Reachable bool | ||
| StatusCode int | ||
| HealthzResponse string | ||
| Err error | ||
| } | ||
| type AccessURLOptions struct { | ||
| AccessURL *url.URL | ||
| Client *http.Client | ||
| } | ||
| func (r *AccessURLReport) Run(ctx context.Context, opts *AccessURLOptions) { | ||
| ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
| defer cancel() | ||
| if opts.Client == nil { | ||
| opts.Client = http.DefaultClient | ||
| } | ||
| accessURL, err := opts.AccessURL.Parse("/healthz") | ||
| if err != nil { | ||
| r.Err = xerrors.Errorf("parse healthz endpoint: %w", err) | ||
| return | ||
| } | ||
| req, err := http.NewRequestWithContext(ctx, "GET", accessURL.String(), nil) | ||
| if err != nil { | ||
| r.Err = xerrors.Errorf("create healthz request: %w", err) | ||
| return | ||
| } | ||
| res, err := opts.Client.Do(req) | ||
| if err != nil { | ||
| r.Err = xerrors.Errorf("get healthz endpoint: %w", err) | ||
| return | ||
| } | ||
| defer res.Body.Close() | ||
| body, err := io.ReadAll(res.Body) | ||
| if err != nil { | ||
| r.Err = xerrors.Errorf("read healthz response: %w", err) | ||
| return | ||
| } | ||
| r.Reachable = true | ||
| r.Healthy = res.StatusCode == http.StatusOK | ||
| r.StatusCode = res.StatusCode | ||
| r.HealthzResponse = string(body) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package healthcheck_test | ||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/xerrors" | ||
| "github.com/coder/coder/coderd/coderdtest" | ||
| "github.com/coder/coder/coderd/healthcheck" | ||
| ) | ||
| func TestAccessURL(t *testing.T) { | ||
| t.Parallel() | ||
| t.Run("OK", func(t *testing.T) { | ||
| t.Parallel() | ||
| var ( | ||
| ctx, cancel = context.WithCancel(context.Background()) | ||
| report healthcheck.AccessURLReport | ||
| client = coderdtest.New(t, nil) | ||
| ) | ||
| defer cancel() | ||
| report.Run(ctx, &healthcheck.AccessURLOptions{ | ||
| AccessURL: client.URL, | ||
| }) | ||
| assert.True(t, report.Healthy) | ||
| assert.True(t, report.Reachable) | ||
| assert.Equal(t, http.StatusOK, report.StatusCode) | ||
| assert.Equal(t, "OK", report.HealthzResponse) | ||
| assert.NoError(t, report.Err) | ||
| }) | ||
| t.Run("404", func(t *testing.T) { | ||
| t.Parallel() | ||
| var ( | ||
| ctx, cancel = context.WithCancel(context.Background()) | ||
| report healthcheck.AccessURLReport | ||
| resp = []byte("NOT OK") | ||
| srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusNotFound) | ||
| w.Write(resp) | ||
| })) | ||
| ) | ||
| defer cancel() | ||
| defer srv.Close() | ||
| u, err := url.Parse(srv.URL) | ||
| require.NoError(t, err) | ||
| report.Run(ctx, &healthcheck.AccessURLOptions{ | ||
| Client: srv.Client(), | ||
| AccessURL: u, | ||
| }) | ||
| assert.False(t, report.Healthy) | ||
| assert.True(t, report.Reachable) | ||
| assert.Equal(t, http.StatusNotFound, report.StatusCode) | ||
| assert.Equal(t, string(resp), report.HealthzResponse) | ||
| assert.NoError(t, report.Err) | ||
| }) | ||
| t.Run("ClientErr", func(t *testing.T) { | ||
| t.Parallel() | ||
| var ( | ||
| ctx, cancel = context.WithCancel(context.Background()) | ||
| report healthcheck.AccessURLReport | ||
| resp = []byte("OK") | ||
| srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| w.Write(resp) | ||
| })) | ||
| client = srv.Client() | ||
| ) | ||
| defer cancel() | ||
| defer srv.Close() | ||
| expErr := xerrors.New("client error") | ||
| client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
| return nil, expErr | ||
| }) | ||
| u, err := url.Parse(srv.URL) | ||
| require.NoError(t, err) | ||
| report.Run(ctx, &healthcheck.AccessURLOptions{ | ||
| Client: client, | ||
| AccessURL: u, | ||
| }) | ||
| assert.False(t, report.Healthy) | ||
| assert.False(t, report.Reachable) | ||
| assert.Equal(t, 0, report.StatusCode) | ||
| assert.Equal(t, "", report.HealthzResponse) | ||
| assert.ErrorIs(t, report.Err, expErr) | ||
| }) | ||
| } | ||
| type roundTripFunc func(r *http.Request) (*http.Response, error) | ||
| func (rt roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { | ||
| return rt(r) | ||
| } |
Uh oh!
There was an error while loading.Please reload this page.