|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/elastic/elastic-agent/internal/pkg/fleetapi" |
| 9 | + "github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker" |
| 10 | + "github.com/elastic/elastic-agent/internal/pkg/fleetapi/client" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/mock" |
| 14 | +) |
| 15 | + |
| 16 | +type mockDispatcher struct { |
| 17 | + mock.Mock |
| 18 | +} |
| 19 | + |
| 20 | +func (m *mockDispatcher) Dispatch(ctx context.Context, ack acker.Acker, actions ...fleetapi.Action) { |
| 21 | + m.Called(ctx, ack, actions) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockDispatcher) Errors() <-chan error { |
| 25 | + args := m.Called() |
| 26 | + return args.Get(0).(<-chan error) |
| 27 | +} |
| 28 | + |
| 29 | +type mockGateway struct { |
| 30 | + mock.Mock |
| 31 | +} |
| 32 | + |
| 33 | +func (m *mockGateway) Run(ctx context.Context) error { |
| 34 | + args := m.Called(ctx) |
| 35 | + return args.Error(0) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockGateway) Errors() <-chan error { |
| 39 | + args := m.Called() |
| 40 | + return args.Get(0).(<-chan error) |
| 41 | +} |
| 42 | + |
| 43 | +func (m *mockGateway) Actions() <-chan []fleetapi.Action { |
| 44 | + args := m.Called() |
| 45 | + return args.Get(0).(<-chan []fleetapi.Action) |
| 46 | +} |
| 47 | + |
| 48 | +func (m *mockGateway) SetClient(c client.Sender) { |
| 49 | + m.Called(c) |
| 50 | +} |
| 51 | + |
| 52 | +type mockAcker struct { |
| 53 | + mock.Mock |
| 54 | +} |
| 55 | + |
| 56 | +func (m *mockAcker) Ack(ctx context.Context, action fleetapi.Action) error { |
| 57 | + args := m.Called(ctx, action) |
| 58 | + return args.Error(0) |
| 59 | +} |
| 60 | + |
| 61 | +func (m *mockAcker) Commit(ctx context.Context) error { |
| 62 | + args := m.Called(ctx) |
| 63 | + return args.Error(0) |
| 64 | +} |
| 65 | + |
| 66 | +func Test_runDispatcher(t *testing.T) { |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + mockGateway func(chan []fleetapi.Action) *mockGateway |
| 70 | + mockDispatcher func() *mockDispatcher |
| 71 | + interval time.Duration |
| 72 | + }{{ |
| 73 | + name: "dispatcher not called", |
| 74 | + mockGateway: func(ch chan []fleetapi.Action) *mockGateway { |
| 75 | + gateway := &mockGateway{} |
| 76 | + gateway.On("Actions").Return((<-chan []fleetapi.Action)(ch)) |
| 77 | + return gateway |
| 78 | + }, |
| 79 | + mockDispatcher: func() *mockDispatcher { |
| 80 | + dispatcher := &mockDispatcher{} |
| 81 | + return dispatcher |
| 82 | + }, |
| 83 | + interval: time.Second, |
| 84 | + }, { |
| 85 | + name: "gateway actions passed", |
| 86 | + mockGateway: func(ch chan []fleetapi.Action) *mockGateway { |
| 87 | + ch <- []fleetapi.Action{&fleetapi.ActionUnknown{ActionID: "test"}} |
| 88 | + gateway := &mockGateway{} |
| 89 | + gateway.On("Actions").Return((<-chan []fleetapi.Action)(ch)) |
| 90 | + return gateway |
| 91 | + }, |
| 92 | + mockDispatcher: func() *mockDispatcher { |
| 93 | + dispatcher := &mockDispatcher{} |
| 94 | + dispatcher.On("Dispatch", mock.Anything, mock.Anything, mock.Anything).Once() |
| 95 | + return dispatcher |
| 96 | + }, |
| 97 | + interval: time.Second, |
| 98 | + }, { |
| 99 | + name: "no gateway actions, dispatcher is flushed", |
| 100 | + mockGateway: func(ch chan []fleetapi.Action) *mockGateway { |
| 101 | + gateway := &mockGateway{} |
| 102 | + gateway.On("Actions").Return((<-chan []fleetapi.Action)(ch)) |
| 103 | + return gateway |
| 104 | + }, |
| 105 | + mockDispatcher: func() *mockDispatcher { |
| 106 | + dispatcher := &mockDispatcher{} |
| 107 | + dispatcher.On("Dispatch", mock.Anything, mock.Anything, mock.Anything).Once() |
| 108 | + return dispatcher |
| 109 | + }, |
| 110 | + interval: time.Millisecond * 60, |
| 111 | + }} |
| 112 | + |
| 113 | + for _, tc := range tests { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + ch := make(chan []fleetapi.Action, 1) |
| 116 | + gateway := tc.mockGateway(ch) |
| 117 | + dispatcher := tc.mockDispatcher() |
| 118 | + acker := &mockAcker{} |
| 119 | + |
| 120 | + ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100) |
| 121 | + defer cancel() |
| 122 | + runDispatcher(ctx, dispatcher, gateway, acker, tc.interval) |
| 123 | + assert.Empty(t, ch) |
| 124 | + |
| 125 | + gateway.AssertExpectations(t) |
| 126 | + dispatcher.AssertExpectations(t) |
| 127 | + acker.AssertExpectations(t) |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments