Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Fix lint and race condition in test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joerger committed Nov 28, 2023
1 parent 416bb57 commit ff7c61d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion event-handler/teleport_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"encoding/json"
"time"

"github.com/gravitational/trace"

Check failure on line 23 in event-handler/teleport_event.go

View workflow job for this annotation

GitHub Actions / Plugins Lint (Go)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/gravitational/teleport-plugins) --custom-order (gci)

auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
"github.com/gravitational/teleport/api/types/events"

Check failure on line 26 in event-handler/teleport_event.go

View workflow job for this annotation

GitHub Actions / Plugins Lint (Go)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/gravitational/teleport-plugins) --custom-order (gci)
"github.com/gravitational/trace"
)

const (
Expand Down
29 changes: 24 additions & 5 deletions event-handler/teleport_events_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"strconv"
"sync"
"testing"
"time"

Expand All @@ -33,13 +34,31 @@ import (

// mockTeleportEventWatcher is Teleport client mock
type mockTeleportEventWatcher struct {
mu sync.Mutex
// events is the mock list of events
events []events.AuditEvent
// mockSearchErr is an error to return
mockSearchErr error
}

func (c *mockTeleportEventWatcher) setEvents(events []events.AuditEvent) {
c.mu.Lock()
defer c.mu.Unlock()

c.events = events
}

func (c *mockTeleportEventWatcher) setSearchEventsError(err error) {
c.mu.Lock()
defer c.mu.Unlock()

c.mockSearchErr = err
}

func (c *mockTeleportEventWatcher) SearchEvents(ctx context.Context, fromUTC, toUTC time.Time, namespace string, eventTypes []string, limit int, order types.EventOrder, startKey string) ([]events.AuditEvent, string, error) {
c.mu.Lock()
defer c.mu.Unlock()

if c.mockSearchErr != nil {
return nil, "", c.mockSearchErr
}
Expand Down Expand Up @@ -173,7 +192,7 @@ func TestEvents(t *testing.T) {

// Events goroutine should return next page errors
mockErr := trace.Errorf("error")
mockEventWatcher.mockSearchErr = mockErr
mockEventWatcher.setSearchEventsError(mockErr)

select {
case err := <-chErr:
Expand Down Expand Up @@ -221,7 +240,7 @@ func TestUpdatePage(t *testing.T) {
chEvt, chErr := client.Events(ctx)

// Add an incomplete page of 3 events and collect them.
mockEventWatcher.events = testAuditEvents[:3]
mockEventWatcher.setEvents(testAuditEvents[:3])
var i int
for ; i < 3; i++ {
select {
Expand Down Expand Up @@ -249,7 +268,7 @@ func TestUpdatePage(t *testing.T) {
}

// Update the event watcher with the full page of events an collect.
mockEventWatcher.events = testAuditEvents[:5]
mockEventWatcher.setEvents(testAuditEvents[:5])
for ; i < 5; i++ {
select {
case event, ok := <-chEvt:
Expand All @@ -276,7 +295,7 @@ func TestUpdatePage(t *testing.T) {
}

// Add another partial page and collect the events
mockEventWatcher.events = testAuditEvents[:7]
mockEventWatcher.setEvents(testAuditEvents[:7])
for ; i < 7; i++ {
select {
case event, ok := <-chEvt:
Expand All @@ -295,7 +314,7 @@ func TestUpdatePage(t *testing.T) {

// Events goroutine should return update page errors
mockErr := trace.Errorf("error")
mockEventWatcher.mockSearchErr = mockErr
mockEventWatcher.setSearchEventsError(mockErr)

select {
case err := <-chErr:
Expand Down

0 comments on commit ff7c61d

Please sign in to comment.