2023-04-03 06:28:42 +00:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-11-15 15:54:15 +00:00
|
|
|
"sync/atomic"
|
2023-04-03 06:28:42 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-04-03 06:28:42 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck"
|
2023-09-25 22:55:50 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
|
2023-11-28 17:15:17 +00:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/testutil"
|
2023-04-03 06:28:42 +00:00
|
|
|
)
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
func TestDebugHealth(t *testing.T) {
|
2023-04-03 06:28:42 +00:00
|
|
|
t.Parallel()
|
2023-05-30 19:22:32 +00:00
|
|
|
t.Run("OK", func(t *testing.T) {
|
2023-04-03 06:28:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
2023-11-15 15:54:15 +00:00
|
|
|
calls = atomic.Int64{}
|
2023-05-30 19:22:32 +00:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
sessionToken string
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
2023-06-02 00:21:24 +00:00
|
|
|
HealthcheckFunc: func(_ context.Context, apiKey string) *healthcheck.Report {
|
2023-11-15 15:54:15 +00:00
|
|
|
calls.Add(1)
|
2023-05-30 19:22:32 +00:00
|
|
|
assert.Equal(t, sessionToken, apiKey)
|
2023-11-15 15:54:15 +00:00
|
|
|
return &healthcheck.Report{
|
|
|
|
Time: time.Now(),
|
|
|
|
}
|
2023-04-03 06:28:42 +00:00
|
|
|
},
|
2023-11-15 15:54:15 +00:00
|
|
|
HealthcheckRefresh: time.Hour, // Avoid flakes.
|
2023-04-03 06:28:42 +00:00
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
sessionToken = client.SessionToken()
|
2023-11-15 15:54:15 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|
|
|
|
// The healthcheck should only have been called once.
|
|
|
|
require.EqualValues(t, 1, calls.Load())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Forced", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
calls = atomic.Int64{}
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
sessionToken string
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
|
|
|
HealthcheckFunc: func(_ context.Context, apiKey string) *healthcheck.Report {
|
|
|
|
calls.Add(1)
|
|
|
|
assert.Equal(t, sessionToken, apiKey)
|
|
|
|
return &healthcheck.Report{
|
|
|
|
Time: time.Now(),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
HealthcheckRefresh: time.Hour, // Avoid flakes.
|
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
sessionToken = client.SessionToken()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health?force=true", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
}
|
|
|
|
// The healthcheck func should have been called each time.
|
|
|
|
require.EqualValues(t, 10, calls.Load())
|
2023-04-03 06:28:42 +00:00
|
|
|
})
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
t.Run("Timeout", func(t *testing.T) {
|
2023-04-03 06:28:42 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
|
|
|
HealthcheckTimeout: time.Microsecond,
|
2023-06-02 00:21:24 +00:00
|
|
|
HealthcheckFunc: func(context.Context, string) *healthcheck.Report {
|
2023-04-03 06:28:42 +00:00
|
|
|
t := time.NewTimer(time.Second)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-06-02 00:21:24 +00:00
|
|
|
return &healthcheck.Report{}
|
2023-04-03 06:28:42 +00:00
|
|
|
case <-t.C:
|
2023-06-02 00:21:24 +00:00
|
|
|
return &healthcheck.Report{}
|
2023-04-03 06:28:42 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
require.Equal(t, http.StatusNotFound, res.StatusCode)
|
|
|
|
})
|
2023-05-30 19:22:32 +00:00
|
|
|
|
2023-11-13 14:14:43 +00:00
|
|
|
t.Run("Refresh", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
calls = make(chan struct{})
|
|
|
|
callsDone = make(chan struct{})
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
|
|
|
HealthcheckRefresh: time.Microsecond,
|
|
|
|
HealthcheckFunc: func(context.Context, string) *healthcheck.Report {
|
|
|
|
calls <- struct{}{}
|
|
|
|
return &healthcheck.Report{}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(callsDone)
|
|
|
|
<-calls
|
|
|
|
<-time.After(testutil.IntervalFast)
|
|
|
|
<-calls
|
|
|
|
}()
|
|
|
|
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
res, err = client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-callsDone:
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Fatal("timed out waiting for calls to finish")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
t.Run("Deduplicated", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
calls int
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
|
|
|
HealthcheckRefresh: time.Hour,
|
|
|
|
HealthcheckTimeout: time.Hour,
|
2023-06-02 00:21:24 +00:00
|
|
|
HealthcheckFunc: func(context.Context, string) *healthcheck.Report {
|
2023-05-30 19:22:32 +00:00
|
|
|
calls++
|
|
|
|
return &healthcheck.Report{
|
|
|
|
Time: time.Now(),
|
2023-06-02 00:21:24 +00:00
|
|
|
}
|
2023-05-30 19:22:32 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
res, err = client.Request(ctx, "GET", "/api/v2/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
require.Equal(t, 1, calls)
|
|
|
|
})
|
2023-09-25 22:55:50 +00:00
|
|
|
|
|
|
|
t.Run("Text", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
sessionToken string
|
|
|
|
client = coderdtest.New(t, &coderdtest.Options{
|
|
|
|
HealthcheckFunc: func(_ context.Context, apiKey string) *healthcheck.Report {
|
|
|
|
assert.Equal(t, sessionToken, apiKey)
|
|
|
|
return &healthcheck.Report{
|
|
|
|
Time: time.Now(),
|
|
|
|
Healthy: true,
|
|
|
|
DERP: derphealth.Report{Healthy: true},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
_ = coderdtest.CreateFirstUser(t, client)
|
|
|
|
)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
sessionToken = client.SessionToken()
|
|
|
|
res, err := client.Request(ctx, "GET", "/api/v2/debug/health?format=text", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
resB, _ := io.ReadAll(res.Body)
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
resStr := string(resB)
|
|
|
|
assert.Contains(t, resStr, "healthy: true")
|
|
|
|
assert.Contains(t, resStr, "derp: true")
|
|
|
|
assert.Contains(t, resStr, "access_url: false")
|
|
|
|
assert.Contains(t, resStr, "websocket: false")
|
|
|
|
assert.Contains(t, resStr, "database: false")
|
|
|
|
})
|
2023-05-30 19:22:32 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 17:15:17 +00:00
|
|
|
func TestHealthSettings(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("InitialState", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// given
|
|
|
|
adminClient := coderdtest.New(t, nil)
|
|
|
|
_ = coderdtest.CreateFirstUser(t, adminClient)
|
|
|
|
|
|
|
|
// when
|
|
|
|
settings, err := adminClient.HealthSettings(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// then
|
2023-12-05 20:00:27 +00:00
|
|
|
require.Equal(t, codersdk.HealthSettings{DismissedHealthchecks: []codersdk.HealthSection{}}, settings)
|
2023-11-28 17:15:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DismissSection", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// given
|
|
|
|
adminClient := coderdtest.New(t, nil)
|
|
|
|
_ = coderdtest.CreateFirstUser(t, adminClient)
|
|
|
|
|
|
|
|
expected := codersdk.HealthSettings{
|
2023-12-05 20:00:27 +00:00
|
|
|
DismissedHealthchecks: []codersdk.HealthSection{codersdk.HealthSectionDERP, codersdk.HealthSectionWebsocket},
|
2023-11-28 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// when: dismiss "derp" and "websocket"
|
|
|
|
err := adminClient.PutHealthSettings(ctx, expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// then
|
|
|
|
settings, err := adminClient.HealthSettings(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, settings)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("UnDismissSection", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// given
|
|
|
|
adminClient := coderdtest.New(t, nil)
|
|
|
|
_ = coderdtest.CreateFirstUser(t, adminClient)
|
|
|
|
|
|
|
|
initial := codersdk.HealthSettings{
|
2023-12-05 20:00:27 +00:00
|
|
|
DismissedHealthchecks: []codersdk.HealthSection{codersdk.HealthSectionDERP, codersdk.HealthSectionWebsocket},
|
2023-11-28 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := adminClient.PutHealthSettings(ctx, initial)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := codersdk.HealthSettings{
|
2023-12-05 20:00:27 +00:00
|
|
|
DismissedHealthchecks: []codersdk.HealthSection{codersdk.HealthSectionDERP},
|
2023-11-28 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// when: undismiss "websocket"
|
|
|
|
err = adminClient.PutHealthSettings(ctx, expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// then
|
|
|
|
settings, err := adminClient.HealthSettings(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, settings)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NotModified", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// given
|
|
|
|
adminClient := coderdtest.New(t, nil)
|
|
|
|
_ = coderdtest.CreateFirstUser(t, adminClient)
|
|
|
|
|
|
|
|
expected := codersdk.HealthSettings{
|
2023-12-05 20:00:27 +00:00
|
|
|
DismissedHealthchecks: []codersdk.HealthSection{codersdk.HealthSectionDERP, codersdk.HealthSectionWebsocket},
|
2023-11-28 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := adminClient.PutHealthSettings(ctx, expected)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// when
|
|
|
|
err = adminClient.PutHealthSettings(ctx, expected)
|
|
|
|
|
|
|
|
// then
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "health settings not modified")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
func TestDebugWebsocket(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
})
|
2023-04-03 06:28:42 +00:00
|
|
|
}
|