2023-04-03 06:28:42 +00:00
|
|
|
package coderd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"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"
|
|
|
|
"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-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-05-30 19:22:32 +00:00
|
|
|
assert.Equal(t, sessionToken, apiKey)
|
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()
|
|
|
|
|
2023-05-30 19:22:32 +00:00
|
|
|
sessionToken = client.SessionToken()
|
2023-04-03 06:28:42 +00:00
|
|
|
res, err := client.Request(ctx, "GET", "/debug/health", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer res.Body.Close()
|
|
|
|
_, _ = io.ReadAll(res.Body)
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDebugWebsocket(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
})
|
2023-04-03 06:28:42 +00:00
|
|
|
}
|