2023-06-20 10:13:22 -05:00
|
|
|
package healthcheck_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-27 14:13:54 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-04 19:35:56 -05:00
|
|
|
"go.uber.org/mock/gomock"
|
2023-06-20 10:13:22 -05:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/database/dbmock"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck"
|
2023-11-23 16:08:41 +01:00
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck/health"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/testutil"
|
2023-06-20 10:13:22 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDatabase(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
|
report = healthcheck.DatabaseReport{}
|
|
|
|
|
db = dbmock.NewMockStore(gomock.NewController(t))
|
|
|
|
|
ping = 10 * time.Millisecond
|
|
|
|
|
)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(ping, nil).Times(5)
|
|
|
|
|
|
|
|
|
|
report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db})
|
|
|
|
|
|
|
|
|
|
assert.True(t, report.Healthy)
|
|
|
|
|
assert.True(t, report.Reachable)
|
2023-11-23 16:08:41 +01:00
|
|
|
assert.Equal(t, health.SeverityOK, report.Severity)
|
2023-08-03 16:20:46 -05:00
|
|
|
assert.Equal(t, ping.String(), report.Latency)
|
2023-11-13 14:14:43 +00:00
|
|
|
assert.Equal(t, ping.Milliseconds(), report.LatencyMS)
|
|
|
|
|
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
|
2023-06-27 14:13:54 -05:00
|
|
|
assert.Nil(t, report.Error)
|
2023-06-20 10:13:22 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Error", 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})
|
|
|
|
|
|
|
|
|
|
assert.False(t, report.Healthy)
|
|
|
|
|
assert.False(t, report.Reachable)
|
2023-11-23 16:08:41 +01:00
|
|
|
assert.Equal(t, health.SeverityError, report.Severity)
|
2023-06-20 10:13:22 -05:00
|
|
|
assert.Zero(t, report.Latency)
|
2023-06-27 14:13:54 -05:00
|
|
|
require.NotNil(t, report.Error)
|
2023-11-13 14:14:43 +00:00
|
|
|
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
|
2023-06-27 14:13:54 -05:00
|
|
|
assert.Contains(t, *report.Error, err.Error())
|
2023-11-30 12:15:40 +00:00
|
|
|
assert.Contains(t, *report.Error, health.CodeDatabasePingFailed)
|
2023-06-20 10:13:22 -05:00
|
|
|
})
|
|
|
|
|
|
2023-11-29 17:37:40 +01:00
|
|
|
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)
|
2023-11-30 12:15:40 +00:00
|
|
|
assert.Contains(t, *report.Error, health.CodeDatabasePingFailed)
|
2023-11-29 17:37:40 +01:00
|
|
|
})
|
|
|
|
|
|
2023-06-20 10:13:22 -05:00
|
|
|
t.Run("Median", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
|
report = healthcheck.DatabaseReport{}
|
|
|
|
|
db = dbmock.NewMockStore(gomock.NewController(t))
|
|
|
|
|
)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Microsecond, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Second, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Nanosecond, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Minute, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Millisecond, nil)
|
|
|
|
|
|
|
|
|
|
report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db})
|
|
|
|
|
|
|
|
|
|
assert.True(t, report.Healthy)
|
|
|
|
|
assert.True(t, report.Reachable)
|
2023-11-23 16:08:41 +01:00
|
|
|
assert.Equal(t, health.SeverityOK, report.Severity)
|
2023-08-03 16:20:46 -05:00
|
|
|
assert.Equal(t, time.Millisecond.String(), report.Latency)
|
2023-11-13 14:14:43 +00:00
|
|
|
assert.EqualValues(t, 1, report.LatencyMS)
|
|
|
|
|
assert.Equal(t, healthcheck.DatabaseDefaultThreshold.Milliseconds(), report.ThresholdMS)
|
|
|
|
|
assert.Nil(t, report.Error)
|
2023-11-30 12:15:40 +00:00
|
|
|
assert.Empty(t, report.Warnings)
|
2023-11-13 14:14:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Threshold", func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
|
report = healthcheck.DatabaseReport{}
|
|
|
|
|
db = dbmock.NewMockStore(gomock.NewController(t))
|
|
|
|
|
)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Second, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Millisecond, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Second, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Millisecond, nil)
|
|
|
|
|
db.EXPECT().Ping(gomock.Any()).Return(time.Second, nil)
|
|
|
|
|
|
|
|
|
|
report.Run(ctx, &healthcheck.DatabaseReportOptions{DB: db, Threshold: time.Second})
|
|
|
|
|
|
2023-11-23 16:08:41 +01:00
|
|
|
assert.True(t, report.Healthy)
|
2023-11-13 14:14:43 +00:00
|
|
|
assert.True(t, report.Reachable)
|
2023-11-23 16:08:41 +01:00
|
|
|
assert.Equal(t, health.SeverityWarning, report.Severity)
|
2023-11-13 14:14:43 +00:00
|
|
|
assert.Equal(t, time.Second.String(), report.Latency)
|
|
|
|
|
assert.EqualValues(t, 1000, report.LatencyMS)
|
|
|
|
|
assert.Equal(t, time.Second.Milliseconds(), report.ThresholdMS)
|
2023-06-27 14:13:54 -05:00
|
|
|
assert.Nil(t, report.Error)
|
2023-11-30 12:15:40 +00:00
|
|
|
if assert.NotEmpty(t, report.Warnings) {
|
2023-11-30 14:49:50 +00:00
|
|
|
assert.Equal(t, report.Warnings[0].Code, health.CodeDatabasePingSlow)
|
2023-11-30 12:15:40 +00:00
|
|
|
}
|
2023-06-20 10:13:22 -05:00
|
|
|
})
|
|
|
|
|
}
|