Skip to content

Commit b3709a7

Browse files
committed
add agentendpoint.go tests
1 parent f7905f4 commit b3709a7

1 file changed

Lines changed: 128 additions & 5 deletions

File tree

agentendpoint/agentendpoint_test.go

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ type agentEndpointServiceTestServer struct {
126126
streamClose chan struct{}
127127
streamSend chan struct{}
128128
permissionError chan struct{}
129+
resourceExhaustedError chan struct{}
129130
taskStart bool
130131
execTaskProgress bool
131132
patchTaskProgress bool
@@ -138,21 +139,35 @@ type agentEndpointServiceTestServer struct {
138139

139140
func newAgentEndpointServiceTestServer() *agentEndpointServiceTestServer {
140141
return &agentEndpointServiceTestServer{
141-
streamClose: make(chan struct{}, 1),
142-
streamSend: make(chan struct{}, 1),
143-
permissionError: make(chan struct{}, 1),
142+
streamClose: make(chan struct{}, 1),
143+
streamSend: make(chan struct{}, 1),
144+
permissionError: make(chan struct{}, 1),
145+
resourceExhaustedError: make(chan struct{}, 1),
144146
}
145147
}
148+
func (s *agentEndpointServiceTestServer) causePermissionError() {
149+
s.permissionError <- struct{}{}
150+
}
151+
152+
func (s *agentEndpointServiceTestServer) causeResourceExhaustedError() {
153+
// ResourceExhausted is handled in waitForTask which calls receiveTaskNotification.
154+
// We'll need a way to trigger this. For now let's add a channel.
155+
s.resourceExhaustedError <- struct{}{}
156+
}
146157

147158
func (s *agentEndpointServiceTestServer) ReceiveTaskNotification(req *agentendpointpb.ReceiveTaskNotificationRequest, srv agentendpointpb.AgentEndpointService_ReceiveTaskNotificationServer) error {
148159
for {
149160
select {
150161
case <-s.streamClose:
151162
return nil
163+
case <-srv.Context().Done():
164+
return srv.Context().Err()
152165
case <-s.streamSend:
153166
srv.Send(&agentendpointpb.ReceiveTaskNotificationResponse{})
154167
case <-s.permissionError:
155168
return status.Errorf(codes.PermissionDenied, "")
169+
case <-s.resourceExhaustedError:
170+
return status.Errorf(codes.ResourceExhausted, "")
156171
}
157172
}
158173
}
@@ -209,8 +224,8 @@ func (s *agentEndpointServiceTestServer) ReportTaskComplete(ctx context.Context,
209224
return &agentendpointpb.ReportTaskCompleteResponse{}, nil
210225
}
211226

212-
func (*agentEndpointServiceTestServer) RegisterAgent(ctx context.Context, req *agentendpointpb.RegisterAgentRequest) (*agentendpointpb.RegisterAgentResponse, error) {
213-
return nil, status.Errorf(codes.Unimplemented, "method RegisterAgent not implemented")
227+
func (s *agentEndpointServiceTestServer) RegisterAgent(ctx context.Context, req *agentendpointpb.RegisterAgentRequest) (*agentendpointpb.RegisterAgentResponse, error) {
228+
return &agentendpointpb.RegisterAgentResponse{}, nil
214229
}
215230

216231
func (*agentEndpointServiceTestServer) ReportInventory(ctx context.Context, req *agentendpointpb.ReportInventoryRequest) (*agentendpointpb.ReportInventoryResponse, error) {
@@ -345,3 +360,111 @@ func TestLoadPatchTaskFromState(t *testing.T) {
345360
t.Errorf("first entry in runTaskIDs does not match taskID, %q, %q", srv.runTaskIDs, taskID)
346361
}
347362
}
363+
364+
func TestClose(t *testing.T) {
365+
ctx := context.Background()
366+
tc, err := newTestClient(ctx, newAgentEndpointServiceTestServer())
367+
if err != nil {
368+
t.Fatal(err)
369+
}
370+
defer tc.s.Stop()
371+
372+
if tc.client.Closed() {
373+
t.Fatal("Expected Closed() to be false initially")
374+
}
375+
tc.client.Close()
376+
}
377+
378+
func TestWaitForTaskNotification(t *testing.T) {
379+
ctx := context.Background()
380+
381+
tests := []struct {
382+
name string
383+
setup func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc)
384+
check func(t *testing.T, tc *testClient)
385+
}{
386+
{
387+
name: "Success",
388+
setup: func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc) {
389+
},
390+
check: func(t *testing.T, tc *testClient) {
391+
time.Sleep(50 * time.Millisecond)
392+
},
393+
},
394+
{
395+
name: "ServiceDisabled",
396+
setup: func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc) {
397+
srv.causePermissionError()
398+
},
399+
check: func(t *testing.T, tc *testClient) {
400+
for i := 0; i < 20; i++ {
401+
if tc.client.closed {
402+
return
403+
}
404+
time.Sleep(20 * time.Millisecond)
405+
}
406+
t.Error("Expected client to be closed after service disabled error")
407+
},
408+
},
409+
{
410+
name: "MultipleCalls",
411+
setup: func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc) {
412+
},
413+
check: func(t *testing.T, tc *testClient) {
414+
tc.client.WaitForTaskNotification(context.Background())
415+
},
416+
},
417+
{
418+
name: "ContextCancel",
419+
setup: func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc) {
420+
cancel()
421+
},
422+
check: func(t *testing.T, tc *testClient) {
423+
time.Sleep(50 * time.Millisecond)
424+
},
425+
},
426+
{
427+
name: "ResourceExhausted",
428+
setup: func(srv *agentEndpointServiceTestServer, cancel context.CancelFunc) {
429+
srv.causeResourceExhaustedError()
430+
},
431+
check: func(t *testing.T, tc *testClient) {
432+
time.Sleep(100 * time.Millisecond)
433+
},
434+
},
435+
}
436+
437+
for _, tt := range tests {
438+
t.Run(tt.name, func(t *testing.T) {
439+
tCtx, tCancel := context.WithCancel(ctx)
440+
defer tCancel()
441+
srv := newAgentEndpointServiceTestServer()
442+
tc, err := newTestClient(tCtx, srv)
443+
if err != nil {
444+
t.Fatal(err)
445+
}
446+
defer tc.s.Stop()
447+
448+
tt.setup(srv, tCancel)
449+
tc.client.WaitForTaskNotification(tCtx)
450+
451+
if tt.check != nil {
452+
tt.check(t, tc)
453+
}
454+
})
455+
}
456+
}
457+
458+
func TestRegisterAgent(t *testing.T) {
459+
ctx := context.Background()
460+
srv := newAgentEndpointServiceTestServer()
461+
tc, err := newTestClient(ctx, srv)
462+
if err != nil {
463+
t.Fatal(err)
464+
}
465+
defer tc.s.Stop()
466+
467+
if err := tc.client.RegisterAgent(ctx); err != nil {
468+
t.Errorf("RegisterAgent() error: %v", err)
469+
}
470+
}

0 commit comments

Comments
 (0)