2023-05-30 19:22:32 +00:00
|
|
|
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"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck"
|
2023-11-23 15:08:41 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/healthcheck/health"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/testutil"
|
2023-05-30 19:22:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWebsocket(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
srv := httptest.NewServer(&healthcheck.WebsocketEchoServer{})
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
u, err := url.Parse(srv.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wsReport := healthcheck.WebsocketReport{}
|
|
|
|
wsReport.Run(ctx, &healthcheck.WebsocketReportOptions{
|
|
|
|
AccessURL: u,
|
|
|
|
HTTPClient: srv.Client(),
|
|
|
|
APIKey: "test",
|
|
|
|
})
|
|
|
|
|
2023-06-27 19:13:54 +00:00
|
|
|
require.Nil(t, wsReport.Error)
|
2023-05-30 19:22:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Error", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
srv := httptest.NewServer(&healthcheck.WebsocketEchoServer{
|
|
|
|
Error: xerrors.New("test error"),
|
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
})
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
u, err := url.Parse(srv.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
wsReport := healthcheck.WebsocketReport{}
|
|
|
|
wsReport.Run(ctx, &healthcheck.WebsocketReportOptions{
|
|
|
|
AccessURL: u,
|
|
|
|
HTTPClient: srv.Client(),
|
|
|
|
APIKey: "test",
|
|
|
|
})
|
|
|
|
|
2023-11-30 12:15:40 +00:00
|
|
|
if assert.NotNil(t, wsReport.Error) {
|
|
|
|
assert.Contains(t, *wsReport.Error, health.CodeWebsocketDial)
|
|
|
|
}
|
2023-11-23 15:08:41 +00:00
|
|
|
require.Equal(t, health.SeverityError, wsReport.Severity)
|
2023-08-03 21:20:46 +00:00
|
|
|
assert.Equal(t, wsReport.Body, "test error")
|
|
|
|
assert.Equal(t, wsReport.Code, http.StatusBadRequest)
|
2023-05-30 19:22:32 +00:00
|
|
|
})
|
2023-11-29 16:37:40 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
2023-05-30 19:22:32 +00:00
|
|
|
}
|