Skip to content

Commit 36b8c1c

Browse files
committed
API tests
1 parent a01878e commit 36b8c1c

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

coderd/debug.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"net/http"
88
"time"
99

10+
"golang.org/x/exp/slices"
11+
"golang.org/x/xerrors"
12+
1013
"github.com/coder/coder/v2/coderd/healthcheck"
1114
"github.com/coder/coder/v2/coderd/httpapi"
1215
"github.com/coder/coder/v2/coderd/httpmw"
1316
"github.com/coder/coder/v2/coderd/rbac"
1417
"github.com/coder/coder/v2/codersdk"
15-
"golang.org/x/exp/slices"
16-
"golang.org/x/xerrors"
1718
)
1819

1920
// @Summary Debug Info Wireguard Coordinator

coderd/debug_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/coder/coder/v2/coderd/coderdtest"
1515
"github.com/coder/coder/v2/coderd/healthcheck"
1616
"github.com/coder/coder/v2/coderd/healthcheck/derphealth"
17+
"github.com/coder/coder/v2/codersdk"
1718
"github.com/coder/coder/v2/testutil"
1819
)
1920

@@ -232,6 +233,52 @@ func TestDebugHealth(t *testing.T) {
232233
})
233234
}
234235

236+
func TestHealthSettings(t *testing.T) {
237+
t.Parallel()
238+
239+
t.Run("InitialState", func(t *testing.T) {
240+
t.Parallel()
241+
242+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
243+
defer cancel()
244+
245+
// given
246+
adminClient := coderdtest.New(t, nil)
247+
_ = coderdtest.CreateFirstUser(t, adminClient)
248+
249+
// when
250+
settings, err := adminClient.HealthSettings(ctx)
251+
require.NoError(t, err)
252+
253+
// then
254+
require.Equal(t, codersdk.HealthSettings{DismissedHealthchecks: []string{}}, settings)
255+
})
256+
257+
t.Run("Updated", func(t *testing.T) {
258+
t.Parallel()
259+
260+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
261+
defer cancel()
262+
263+
// given
264+
adminClient := coderdtest.New(t, nil)
265+
_ = coderdtest.CreateFirstUser(t, adminClient)
266+
267+
expected := codersdk.HealthSettings{
268+
DismissedHealthchecks: []string{healthcheck.SectionDERP, healthcheck.SectionWebsocket},
269+
}
270+
271+
// when
272+
err := adminClient.UpdateHealthSettings(ctx, expected)
273+
require.NoError(t, err)
274+
275+
// then
276+
settings, err := adminClient.HealthSettings(ctx)
277+
require.NoError(t, err)
278+
require.Equal(t, expected, settings)
279+
})
280+
}
281+
235282
func TestDebugWebsocket(t *testing.T) {
236283
t.Parallel()
237284

codersdk/health.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
package codersdk
22

3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
39
type HealthSettings struct {
410
DismissedHealthchecks []string `json:"dismissed_healthchecks"`
511
}
612

713
type UpdateHealthSettings struct {
814
DismissedHealthchecks []string `json:"dismissed_healthchecks"`
915
}
16+
17+
func (c *Client) HealthSettings(ctx context.Context) (HealthSettings, error) {
18+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/debug/health/settings", nil)
19+
if err != nil {
20+
return HealthSettings{}, err
21+
}
22+
defer res.Body.Close()
23+
if res.StatusCode != http.StatusOK {
24+
return HealthSettings{}, ReadBodyAsError(res)
25+
}
26+
var settings HealthSettings
27+
return settings, json.NewDecoder(res.Body).Decode(&settings)
28+
}
29+
30+
func (c *Client) UpdateHealthSettings(ctx context.Context, settings HealthSettings) error {
31+
res, err := c.Request(ctx, http.MethodPut, "/api/v2/debug/health/settings", settings)
32+
if err != nil {
33+
return err
34+
}
35+
defer res.Body.Close()
36+
if res.StatusCode != http.StatusOK {
37+
return ReadBodyAsError(res)
38+
}
39+
return nil
40+
}

0 commit comments

Comments
 (0)