Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
unit tests
  • Loading branch information
mtojek committed Nov 29, 2023
commit 6ce2d00b264d39fc7460ea2d772df9ab0c819f89
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func New(options *Options) *API {

if options.HealthcheckFunc == nil {
options.HealthcheckFunc = func(ctx context.Context, apiKey string) *healthcheck.Report {
dismissedHealthchecks := loadDismissedHealthcheck(ctx, options.Database, options.Logger)
dismissedHealthchecks := loadDismissedHealthchecks(ctx, options.Database, options.Logger)
return healthcheck.Run(ctx, &healthcheck.ReportOptions{
Database: healthcheck.DatabaseReportOptions{
DB: options.Database,
Expand Down
5 changes: 2 additions & 3 deletions coderd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"net/http"
"time"

"github.com/google/uuid"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

"cdr.dev/slog"

"github.com/google/uuid"

"github.com/coder/coder/v2/coderd/audit"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/healthcheck"
Expand Down Expand Up @@ -256,7 +255,7 @@ func validateHealthSettings(settings codersdk.HealthSettings) error {
// @x-apidocgen {"skip": true}
func _debugws(http.ResponseWriter, *http.Request) {} //nolint:unused

func loadDismissedHealthcheck(ctx context.Context, db database.Store, logger slog.Logger) []string {
func loadDismissedHealthchecks(ctx context.Context, db database.Store, logger slog.Logger) []string {
dismissedHealthchecks := []string{}
settingsJSON, err := db.GetHealthSettings(ctx)
if err == nil {
Expand Down
17 changes: 17 additions & 0 deletions coderd/healthcheck/accessurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ func TestAccessURL(t *testing.T) {
require.NotNil(t, report.Error)
assert.Contains(t, *report.Error, expErr.Error())
})

t.Run("DismissedError", func(t *testing.T) {
t.Parallel()

var (
ctx, cancel = context.WithCancel(context.Background())
report healthcheck.AccessURLReport
)
defer cancel()

report.Run(ctx, &healthcheck.AccessURLReportOptions{
Dismissed: true,
})

assert.True(t, report.Dismissed)
assert.Equal(t, health.SeverityError, report.Severity)
})
}

type roundTripFunc func(r *http.Request) (*http.Response, error)
Expand Down
20 changes: 20 additions & 0 deletions coderd/healthcheck/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ func TestDatabase(t *testing.T) {
assert.Contains(t, *report.Error, err.Error())
})

t.Run("DismissedError", func(t *testing.T) {
t.Parallel()

var (
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
report = healthcheck.DatabaseReport{}
db = dbmock.NewMockStore(gomock.NewController(t))
err = xerrors.New("ping error")
)
defer cancel()

db.EXPECT().Ping(gomock.Any()).Return(time.Duration(0), err)

report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db, Dismissed: true})

assert.Equal(t, health.SeverityError, report.Severity)
assert.True(t, report.Dismissed)
require.NotNil(t, report.Error)
})

t.Run("Median", func(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 2 additions & 0 deletions coderd/healthcheck/derphealth/derp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ func TestDERP(t *testing.T) {
}},
},
}},
Dismissed: true, // Let's sneak an extra unit test
}
)

report.Run(ctx, opts)

assert.True(t, report.Healthy)
assert.Equal(t, health.SeverityWarning, report.Severity)
assert.True(t, report.Dismissed)
for _, region := range report.Regions {
assert.True(t, region.Healthy)
assert.True(t, region.NodeReports[0].Healthy)
Expand Down
18 changes: 18 additions & 0 deletions coderd/healthcheck/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,22 @@ func TestWebsocket(t *testing.T) {
assert.Equal(t, wsReport.Body, "test error")
assert.Equal(t, wsReport.Code, http.StatusBadRequest)
})

t.Run("DismissedError", func(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

wsReport := healthcheck.WebsocketReport{}
wsReport.Run(ctx, &healthcheck.WebsocketReportOptions{
AccessURL: &url.URL{Host: "fake"},
Dismissed: true,
})

require.True(t, wsReport.Dismissed)
require.Equal(t, health.SeverityError, wsReport.Severity)
require.NotNil(t, wsReport.Error)
require.Equal(t, health.SeverityError, wsReport.Severity)
})
}
12 changes: 12 additions & 0 deletions coderd/healthcheck/workspaceproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,15 @@ func fakeUpdateProxyHealth(err error) func(context.Context) error {
return err
}
}

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

var report healthcheck.WorkspaceProxyReport
report.Run(context.Background(), &healthcheck.WorkspaceProxyReportOptions{
Dismissed: true,
})

assert.True(t, report.Dismissed)
assert.Equal(t, health.SeverityOK, report.Severity)
}