Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2fc3064

Browse files
authored
chore: add tests for app ID copy in app healths (#12088)
1 parent06254a1 commit2fc3064

File tree

4 files changed

+79
-23
lines changed

4 files changed

+79
-23
lines changed

‎agent/agenttest/client.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ type FakeAgentAPI struct {
197197
t testing.TB
198198
logger slog.Logger
199199

200-
manifest*agentproto.Manifest
201-
startupChchan*agentproto.Startup
202-
statsChchan*agentproto.Stats
200+
manifest*agentproto.Manifest
201+
startupChchan*agentproto.Startup
202+
statsChchan*agentproto.Stats
203+
appHealthChchan*agentproto.BatchUpdateAppHealthRequest
203204

204205
getServiceBannerFuncfunc() (codersdk.ServiceBannerConfig,error)
205206
}
@@ -244,9 +245,14 @@ func (*FakeAgentAPI) UpdateLifecycle(context.Context, *agentproto.UpdateLifecycl
244245

245246
func (f*FakeAgentAPI)BatchUpdateAppHealths(ctx context.Context,req*agentproto.BatchUpdateAppHealthRequest) (*agentproto.BatchUpdateAppHealthResponse,error) {
246247
f.logger.Debug(ctx,"batch update app health",slog.F("req",req))
248+
f.appHealthCh<-req
247249
return&agentproto.BatchUpdateAppHealthResponse{},nil
248250
}
249251

252+
func (f*FakeAgentAPI)AppHealthCh()<-chan*agentproto.BatchUpdateAppHealthRequest {
253+
returnf.appHealthCh
254+
}
255+
250256
func (f*FakeAgentAPI)UpdateStartup(_ context.Context,req*agentproto.UpdateStartupRequest) (*agentproto.Startup,error) {
251257
f.startupCh<-req.GetStartup()
252258
returnreq.GetStartup(),nil
@@ -264,10 +270,11 @@ func (*FakeAgentAPI) BatchCreateLogs(context.Context, *agentproto.BatchCreateLog
264270

265271
funcNewFakeAgentAPI(t testing.TB,logger slog.Logger,manifest*agentproto.Manifest,statsChchan*agentproto.Stats)*FakeAgentAPI {
266272
return&FakeAgentAPI{
267-
t:t,
268-
logger:logger.Named("FakeAgentAPI"),
269-
manifest:manifest,
270-
statsCh:statsCh,
271-
startupCh:make(chan*agentproto.Startup,100),
273+
t:t,
274+
logger:logger.Named("FakeAgentAPI"),
275+
manifest:manifest,
276+
statsCh:statsCh,
277+
startupCh:make(chan*agentproto.Startup,100),
278+
appHealthCh:make(chan*agentproto.BatchUpdateAppHealthRequest,100),
272279
}
273280
}

‎agent/apphealth_test.go

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import (
44
"context"
55
"net/http"
66
"net/http/httptest"
7+
"strings"
78
"sync"
89
"sync/atomic"
910
"testing"
1011
"time"
1112

13+
"github.com/google/uuid"
14+
"github.com/stretchr/testify/assert"
1215
"github.com/stretchr/testify/require"
1316

1417
"cdr.dev/slog"
1518
"cdr.dev/slog/sloggers/slogtest"
1619
"github.com/coder/coder/v2/agent"
20+
"github.com/coder/coder/v2/agent/agenttest"
21+
"github.com/coder/coder/v2/agent/proto"
1722
"github.com/coder/coder/v2/coderd/httpapi"
1823
"github.com/coder/coder/v2/codersdk"
1924
"github.com/coder/coder/v2/codersdk/agentsdk"
@@ -40,12 +45,23 @@ func TestAppHealth_Healthy(t *testing.T) {
4045
},
4146
Health:codersdk.WorkspaceAppHealthInitializing,
4247
},
48+
{
49+
Slug:"app3",
50+
Healthcheck: codersdk.Healthcheck{
51+
Interval:2,
52+
Threshold:1,
53+
},
54+
Health:codersdk.WorkspaceAppHealthInitializing,
55+
},
4356
}
4457
handlers:= []http.Handler{
4558
nil,
4659
http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
4760
httpapi.Write(r.Context(),w,http.StatusOK,nil)
4861
}),
62+
http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
63+
httpapi.Write(r.Context(),w,http.StatusOK,nil)
64+
}),
4965
}
5066
getApps,closeFn:=setupAppReporter(ctx,t,apps,handlers)
5167
defercloseFn()
@@ -58,7 +74,7 @@ func TestAppHealth_Healthy(t *testing.T) {
5874
returnfalse
5975
}
6076

61-
returnapps[1].Health==codersdk.WorkspaceAppHealthHealthy
77+
returnapps[1].Health==codersdk.WorkspaceAppHealthHealthy&&apps[2].Health==codersdk.WorkspaceAppHealthHealthy
6278
},testutil.WaitLong,testutil.IntervalSlow)
6379
}
6480

@@ -163,6 +179,12 @@ func TestAppHealth_NotSpamming(t *testing.T) {
163179

164180
funcsetupAppReporter(ctx context.Context,t*testing.T,apps []codersdk.WorkspaceApp,handlers []http.Handler) (agent.WorkspaceAgentApps,func()) {
165181
closers:= []func(){}
182+
fori,app:=rangeapps {
183+
ifapp.ID==uuid.Nil {
184+
app.ID=uuid.New()
185+
apps[i]=app
186+
}
187+
}
166188
fori,handler:=rangehandlers {
167189
ifhandler==nil {
168190
continue
@@ -181,23 +203,43 @@ func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.Workspa
181203
varnewApps []codersdk.WorkspaceApp
182204
returnappend(newApps,apps...),nil
183205
}
184-
postWorkspaceAgentAppHealth:=func(_ context.Context,req agentsdk.PostAppHealthsRequest)error {
185-
mu.Lock()
186-
forid,health:=rangereq.Healths {
187-
fori,app:=rangeapps {
188-
ifapp.ID!=id {
189-
continue
206+
207+
// We don't care about manifest or stats in this test since it's not using
208+
// a full agent and these RPCs won't get called.
209+
//
210+
// We use a proper fake agent API so we can test the conversion code and the
211+
// request code as well. Before we were bypassing these by using a custom
212+
// post function.
213+
fakeAAPI:=agenttest.NewFakeAgentAPI(t,slogtest.Make(t,nil),nil,nil)
214+
215+
// Process events from the channel and update the health of the apps.
216+
gofunc() {
217+
appHealthCh:=fakeAAPI.AppHealthCh()
218+
for {
219+
select {
220+
case<-ctx.Done():
221+
return
222+
casereq:=<-appHealthCh:
223+
mu.Lock()
224+
for_,update:=rangereq.Updates {
225+
updateID,err:=uuid.FromBytes(update.Id)
226+
assert.NoError(t,err)
227+
updateHealth:=codersdk.WorkspaceAppHealth(strings.ToLower(proto.AppHealth_name[int32(update.Health)]))
228+
229+
fori,app:=rangeapps {
230+
ifapp.ID!=updateID {
231+
continue
232+
}
233+
app.Health=updateHealth
234+
apps[i]=app
235+
}
190236
}
191-
app.Health=health
192-
apps[i]=app
237+
mu.Unlock()
193238
}
194239
}
195-
mu.Unlock()
196-
197-
returnnil
198-
}
240+
}()
199241

200-
goagent.NewWorkspaceAppHealthReporter(slogtest.Make(t,nil).Leveled(slog.LevelDebug),apps,postWorkspaceAgentAppHealth)(ctx)
242+
goagent.NewWorkspaceAppHealthReporter(slogtest.Make(t,nil).Leveled(slog.LevelDebug),apps,agentsdk.AppHealthPoster(fakeAAPI))(ctx)
201243

202244
returnworkspaceAgentApps,func() {
203245
for_,closeFn:=rangeclosers {

‎codersdk/agentsdk/agentsdk.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,12 @@ type PostAppHealthsRequest struct {
226226
Healthsmap[uuid.UUID]codersdk.WorkspaceAppHealth
227227
}
228228

229-
funcAppHealthPoster(aAPI proto.DRPCAgentClient)func(ctx context.Context,reqPostAppHealthsRequest)error {
229+
// BatchUpdateAppHealthsClient is a partial interface of proto.DRPCAgentClient.
230+
typeBatchUpdateAppHealthsClientinterface {
231+
BatchUpdateAppHealths(ctx context.Context,req*proto.BatchUpdateAppHealthRequest) (*proto.BatchUpdateAppHealthResponse,error)
232+
}
233+
234+
funcAppHealthPoster(aAPIBatchUpdateAppHealthsClient)func(ctx context.Context,reqPostAppHealthsRequest)error {
230235
returnfunc(ctx context.Context,reqPostAppHealthsRequest)error {
231236
pReq,err:=ProtoFromAppHealthsRequest(req)
232237
iferr!=nil {

‎codersdk/agentsdk/convert.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ func ProtoFromAppHealthsRequest(req PostAppHealthsRequest) (*proto.BatchUpdateAp
287287
returnnil,xerrors.Errorf("unknown app health: %s",h)
288288
}
289289

290+
// Copy the ID, otherwise all updates will have the same ID (the last
291+
// one in the list).
290292
varidCopy uuid.UUID
291293
copy(idCopy[:],id[:])
292294
pReq.Updates=append(pReq.Updates,&proto.BatchUpdateAppHealthRequest_HealthUpdate{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp