Skip to content

Commit 48ecee1

Browse files
authored
chore(cli): address cli netcheck test flake (#13492)
* netcheck: removes check for healthy node report in test * coderd/healthcheck/derphealth: do not override parent context deadline
1 parent 7c3b8b6 commit 48ecee1

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

cli/netcheck_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"testing"
77

8-
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
109

1110
"github.com/coder/coder/v2/cli/clitest"
@@ -30,7 +29,8 @@ func TestNetcheck(t *testing.T) {
3029
var report healthsdk.DERPHealthReport
3130
require.NoError(t, json.Unmarshal(b, &report))
3231

33-
assert.True(t, report.Healthy)
32+
// We do not assert that the report is healthy, just that
33+
// it has the expected number of reports per region.
3434
require.Len(t, report.Regions, 1+1) // 1 built-in region + 1 test-managed STUN region
3535
for _, v := range report.Regions {
3636
require.Len(t, v.NodeReports, len(v.Region.Nodes))

coderd/healthcheck/derphealth/derp.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ func (r *NodeReport) derpURL() *url.URL {
236236
}
237237

238238
func (r *NodeReport) Run(ctx context.Context) {
239-
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
240-
defer cancel()
239+
// If there already is a deadline set on the context, do not override it.
240+
if _, ok := ctx.Deadline(); !ok {
241+
dCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
242+
defer cancel()
243+
ctx = dCtx
244+
}
241245

242246
r.Severity = health.SeverityOK
243247
r.ClientLogs = [][]string{}

coderd/healthcheck/derphealth/derp_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/httptest"
99
"net/url"
1010
"testing"
11+
"time"
1112

1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -84,6 +85,45 @@ func TestDERP(t *testing.T) {
8485
}
8586
})
8687

88+
t.Run("TimeoutCtx", func(t *testing.T) {
89+
t.Parallel()
90+
91+
derpSrv := derp.NewServer(key.NewNode(), func(format string, args ...any) { t.Logf(format, args...) })
92+
defer derpSrv.Close()
93+
srv := httptest.NewServer(derphttp.Handler(derpSrv))
94+
defer srv.Close()
95+
96+
var (
97+
// nolint:gocritic // testing a deadline exceeded
98+
ctx, cancel = context.WithTimeout(context.Background(), time.Nanosecond)
99+
report = derphealth.Report{}
100+
derpURL, _ = url.Parse(srv.URL)
101+
opts = &derphealth.ReportOptions{
102+
DERPMap: &tailcfg.DERPMap{Regions: map[int]*tailcfg.DERPRegion{
103+
1: {
104+
EmbeddedRelay: true,
105+
RegionID: 999,
106+
Nodes: []*tailcfg.DERPNode{{
107+
Name: "1a",
108+
RegionID: 999,
109+
HostName: derpURL.Host,
110+
IPv4: derpURL.Host,
111+
STUNPort: -1,
112+
InsecureForTests: true,
113+
ForceHTTP: true,
114+
}},
115+
},
116+
}},
117+
}
118+
)
119+
cancel()
120+
121+
report.Run(ctx, opts)
122+
123+
assert.False(t, report.Healthy)
124+
assert.Nil(t, report.Error)
125+
})
126+
87127
t.Run("HealthyWithNodeDegraded", func(t *testing.T) {
88128
t.Parallel()
89129

0 commit comments

Comments
 (0)