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

chore: add tests for app ID copy in app healths#12088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
deansheather merged 1 commit intomainfromdean/app-id-copy-tests
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletionsagent/agenttest/client.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -197,9 +197,10 @@ type FakeAgentAPI struct {
t testing.TB
logger slog.Logger

manifest*agentproto.Manifest
startupChchan*agentproto.Startup
statsChchan*agentproto.Stats
manifest*agentproto.Manifest
startupChchan*agentproto.Startup
statsChchan*agentproto.Stats
appHealthChchan*agentproto.BatchUpdateAppHealthRequest

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

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

func (f*FakeAgentAPI)AppHealthCh()<-chan*agentproto.BatchUpdateAppHealthRequest {
returnf.appHealthCh
}

func (f*FakeAgentAPI)UpdateStartup(_ context.Context,req*agentproto.UpdateStartupRequest) (*agentproto.Startup,error) {
f.startupCh<-req.GetStartup()
returnreq.GetStartup(),nil
Expand All@@ -264,10 +270,11 @@ func (*FakeAgentAPI) BatchCreateLogs(context.Context, *agentproto.BatchCreateLog

funcNewFakeAgentAPI(t testing.TB,logger slog.Logger,manifest*agentproto.Manifest,statsChchan*agentproto.Stats)*FakeAgentAPI {
return&FakeAgentAPI{
t:t,
logger:logger.Named("FakeAgentAPI"),
manifest:manifest,
statsCh:statsCh,
startupCh:make(chan*agentproto.Startup,100),
t:t,
logger:logger.Named("FakeAgentAPI"),
manifest:manifest,
statsCh:statsCh,
startupCh:make(chan*agentproto.Startup,100),
appHealthCh:make(chan*agentproto.BatchUpdateAppHealthRequest,100),
}
}
70 changes: 56 additions & 14 deletionsagent/apphealth_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,16 +4,21 @@ import (
"context"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/v2/agent"
"github.com/coder/coder/v2/agent/agenttest"
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
Expand All@@ -40,12 +45,23 @@ func TestAppHealth_Healthy(t *testing.T) {
},
Health:codersdk.WorkspaceAppHealthInitializing,
},
{
Slug:"app3",
Healthcheck: codersdk.Healthcheck{
Interval:2,
Threshold:1,
},
Health:codersdk.WorkspaceAppHealthInitializing,
},
}
handlers:= []http.Handler{
nil,
http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
httpapi.Write(r.Context(),w,http.StatusOK,nil)
}),
http.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {
httpapi.Write(r.Context(),w,http.StatusOK,nil)
}),
}
getApps,closeFn:=setupAppReporter(ctx,t,apps,handlers)
defercloseFn()
Expand All@@ -58,7 +74,7 @@ func TestAppHealth_Healthy(t *testing.T) {
returnfalse
}

returnapps[1].Health==codersdk.WorkspaceAppHealthHealthy
returnapps[1].Health==codersdk.WorkspaceAppHealthHealthy&&apps[2].Health==codersdk.WorkspaceAppHealthHealthy
},testutil.WaitLong,testutil.IntervalSlow)
}

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

funcsetupAppReporter(ctx context.Context,t*testing.T,apps []codersdk.WorkspaceApp,handlers []http.Handler) (agent.WorkspaceAgentApps,func()) {
closers:= []func(){}
fori,app:=rangeapps {
ifapp.ID==uuid.Nil {
app.ID=uuid.New()
apps[i]=app
}
}
fori,handler:=rangehandlers {
ifhandler==nil {
continue
Expand All@@ -181,23 +203,43 @@ func setupAppReporter(ctx context.Context, t *testing.T, apps []codersdk.Workspa
varnewApps []codersdk.WorkspaceApp
returnappend(newApps,apps...),nil
}
postWorkspaceAgentAppHealth:=func(_ context.Context,req agentsdk.PostAppHealthsRequest)error {
mu.Lock()
forid,health:=rangereq.Healths {
fori,app:=rangeapps {
ifapp.ID!=id {
continue

// We don't care about manifest or stats in this test since it's not using
// a full agent and these RPCs won't get called.
//
// We use a proper fake agent API so we can test the conversion code and the
// request code as well. Before we were bypassing these by using a custom
// post function.
fakeAAPI:=agenttest.NewFakeAgentAPI(t,slogtest.Make(t,nil),nil,nil)

// Process events from the channel and update the health of the apps.
gofunc() {
appHealthCh:=fakeAAPI.AppHealthCh()
for {
select {
case<-ctx.Done():
return
casereq:=<-appHealthCh:
mu.Lock()
for_,update:=rangereq.Updates {
updateID,err:=uuid.FromBytes(update.Id)
assert.NoError(t,err)
updateHealth:=codersdk.WorkspaceAppHealth(strings.ToLower(proto.AppHealth_name[int32(update.Health)]))

fori,app:=rangeapps {
ifapp.ID!=updateID {
continue
}
app.Health=updateHealth
apps[i]=app
}
}
app.Health=health
apps[i]=app
mu.Unlock()
}
}
mu.Unlock()

returnnil
}
}()

goagent.NewWorkspaceAppHealthReporter(slogtest.Make(t,nil).Leveled(slog.LevelDebug),apps,postWorkspaceAgentAppHealth)(ctx)
goagent.NewWorkspaceAppHealthReporter(slogtest.Make(t,nil).Leveled(slog.LevelDebug),apps,agentsdk.AppHealthPoster(fakeAAPI))(ctx)

returnworkspaceAgentApps,func() {
for_,closeFn:=rangeclosers {
Expand Down
7 changes: 6 additions & 1 deletioncodersdk/agentsdk/agentsdk.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -226,7 +226,12 @@ type PostAppHealthsRequest struct {
Healthsmap[uuid.UUID]codersdk.WorkspaceAppHealth
}

funcAppHealthPoster(aAPI proto.DRPCAgentClient)func(ctx context.Context,reqPostAppHealthsRequest)error {
// BatchUpdateAppHealthsClient is a partial interface of proto.DRPCAgentClient.
typeBatchUpdateAppHealthsClientinterface {
BatchUpdateAppHealths(ctx context.Context,req*proto.BatchUpdateAppHealthRequest) (*proto.BatchUpdateAppHealthResponse,error)
}

funcAppHealthPoster(aAPIBatchUpdateAppHealthsClient)func(ctx context.Context,reqPostAppHealthsRequest)error {
returnfunc(ctx context.Context,reqPostAppHealthsRequest)error {
pReq,err:=ProtoFromAppHealthsRequest(req)
iferr!=nil {
Expand Down
2 changes: 2 additions & 0 deletionscodersdk/agentsdk/convert.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -287,6 +287,8 @@ func ProtoFromAppHealthsRequest(req PostAppHealthsRequest) (*proto.BatchUpdateAp
returnnil,xerrors.Errorf("unknown app health: %s",h)
}

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

[8]ページ先頭

©2009-2025 Movatter.jp