Skip to content

Commit 56c7ceb

Browse files
committed
implement fake agent api sub agent methods
1 parent 934a222 commit 56c7ceb

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

agent/agenttest/client.go

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (c *Client) GetConnectionReports() []*agentproto.ReportConnectionRequest {
163163
return c.fakeAgentAPI.GetConnectionReports()
164164
}
165165

166+
func (c *Client) GetSubAgents() []*agentproto.SubAgent {
167+
return c.fakeAgentAPI.GetSubAgents()
168+
}
169+
166170
type FakeAgentAPI struct {
167171
sync.Mutex
168172
t testing.TB
@@ -177,6 +181,7 @@ type FakeAgentAPI struct {
177181
metadata map[string]agentsdk.Metadata
178182
timings []*agentproto.Timing
179183
connectionReports []*agentproto.ReportConnectionRequest
184+
subAgents map[uuid.UUID]*agentproto.SubAgent
180185

181186
getAnnouncementBannersFunc func() ([]codersdk.BannerConfig, error)
182187
getResourcesMonitoringConfigurationFunc func() (*agentproto.GetResourcesMonitoringConfigurationResponse, error)
@@ -365,16 +370,86 @@ func (f *FakeAgentAPI) GetConnectionReports() []*agentproto.ReportConnectionRequ
365370
return slices.Clone(f.connectionReports)
366371
}
367372

368-
func (*FakeAgentAPI) CreateSubAgent(_ context.Context, _ *agentproto.CreateSubAgentRequest) (*agentproto.CreateSubAgentResponse, error) {
369-
panic("unimplemented")
373+
func (f *FakeAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.CreateSubAgentRequest) (*agentproto.CreateSubAgentResponse, error) {
374+
f.Lock()
375+
defer f.Unlock()
376+
377+
f.logger.Debug(ctx, "create sub agent called", slog.F("req", req))
378+
379+
// Generate IDs for the new sub-agent.
380+
subAgentID := uuid.New()
381+
authToken := uuid.New()
382+
383+
// Create the sub-agent proto object.
384+
subAgent := &agentproto.SubAgent{
385+
Id: subAgentID[:],
386+
Name: req.Name,
387+
AuthToken: authToken[:],
388+
}
389+
390+
// Store the sub-agent in our map.
391+
if f.subAgents == nil {
392+
f.subAgents = make(map[uuid.UUID]*agentproto.SubAgent)
393+
}
394+
f.subAgents[subAgentID] = subAgent
395+
396+
// For a fake implementation, we don't create workspace apps.
397+
// Real implementations would handle req.Apps here.
398+
return &agentproto.CreateSubAgentResponse{
399+
Agent: subAgent,
400+
AppCreationErrors: nil,
401+
}, nil
402+
}
403+
404+
func (f *FakeAgentAPI) DeleteSubAgent(ctx context.Context, req *agentproto.DeleteSubAgentRequest) (*agentproto.DeleteSubAgentResponse, error) {
405+
f.Lock()
406+
defer f.Unlock()
407+
408+
f.logger.Debug(ctx, "delete sub agent called", slog.F("req", req))
409+
410+
subAgentID, err := uuid.FromBytes(req.Id)
411+
if err != nil {
412+
return nil, err
413+
}
414+
415+
// Remove the sub-agent from our map.
416+
if f.subAgents != nil {
417+
delete(f.subAgents, subAgentID)
418+
}
419+
420+
return &agentproto.DeleteSubAgentResponse{}, nil
370421
}
371422

372-
func (*FakeAgentAPI) DeleteSubAgent(_ context.Context, _ *agentproto.DeleteSubAgentRequest) (*agentproto.DeleteSubAgentResponse, error) {
373-
panic("unimplemented")
423+
func (f *FakeAgentAPI) ListSubAgents(ctx context.Context, req *agentproto.ListSubAgentsRequest) (*agentproto.ListSubAgentsResponse, error) {
424+
f.Lock()
425+
defer f.Unlock()
426+
427+
f.logger.Debug(ctx, "list sub agents called", slog.F("req", req))
428+
429+
var agents []*agentproto.SubAgent
430+
if f.subAgents != nil {
431+
agents = make([]*agentproto.SubAgent, 0, len(f.subAgents))
432+
for _, agent := range f.subAgents {
433+
agents = append(agents, agent)
434+
}
435+
}
436+
437+
return &agentproto.ListSubAgentsResponse{
438+
Agents: agents,
439+
}, nil
374440
}
375441

376-
func (*FakeAgentAPI) ListSubAgents(_ context.Context, _ *agentproto.ListSubAgentsRequest) (*agentproto.ListSubAgentsResponse, error) {
377-
panic("unimplemented")
442+
func (f *FakeAgentAPI) GetSubAgents() []*agentproto.SubAgent {
443+
f.Lock()
444+
defer f.Unlock()
445+
var agents []*agentproto.SubAgent
446+
if f.subAgents != nil {
447+
agents = make([]*agentproto.SubAgent, 0, len(f.subAgents))
448+
for _, agent := range f.subAgents {
449+
agents = append(agents, agent)
450+
}
451+
}
452+
return agents
378453
}
379454

380455
func NewFakeAgentAPI(t testing.TB, logger slog.Logger, manifest *agentproto.Manifest, statsCh chan *agentproto.Stats) *FakeAgentAPI {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy