Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Jan 7, 2025
1 parent b05d676 commit f5fbb2e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
4 changes: 4 additions & 0 deletions components/accelerator/nvidia/peermem/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (c *component) Events(ctx context.Context, since time.Time) ([]components.E
return nil, err
}

return c.getEvents(ctx, since, dmesgTailResults)
}

func (c *component) getEvents(ctx context.Context, since time.Time, dmesgTailResults *dmesg.State) ([]components.Event, error) {
// dedup by minute level
seenMinute := make(map[int64]struct{})
events := make([]components.Event, 0)
Expand Down
92 changes: 92 additions & 0 deletions components/accelerator/nvidia/peermem/component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package peermem

import (
"context"
"testing"
"time"

"github.com/leptonai/gpud/components/dmesg"
query_log "github.com/leptonai/gpud/components/query/log"
query_log_common "github.com/leptonai/gpud/components/query/log/common"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetEvents(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input *dmesg.State
expected int
}{
{
name: "should skip non-peermem invalid context events",
input: &dmesg.State{
TailScanMatched: []query_log.Item{
{
Time: metav1.Time{Time: time.Now()},
Line: "some other event",
Matched: &query_log_common.Filter{
Name: "other_event",
},
},
},
},
expected: 0,
},
{
name: "should skip peermem invalid context events due to driver fix",
input: &dmesg.State{
TailScanMatched: []query_log.Item{
{
Time: metav1.Time{Time: time.Now()},
Line: "nvidia-peermem invalid context",
Matched: &query_log_common.Filter{
Name: dmesg.EventNvidiaPeermemInvalidContext,
},
},
},
},
expected: 0,
},
{
name: "should deduplicate events in same minute",
input: &dmesg.State{
TailScanMatched: []query_log.Item{
{
Time: metav1.Time{Time: time.Now()},
Line: "nvidia-peermem invalid context 1",
Matched: &query_log_common.Filter{
Name: dmesg.EventNvidiaPeermemInvalidContext,
},
},
{
Time: metav1.Time{Time: time.Now()},
Line: "nvidia-peermem invalid context 2",
Matched: &query_log_common.Filter{
Name: dmesg.EventNvidiaPeermemInvalidContext,
},
},
},
},
expected: 0,
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

c := &component{}
events, err := c.getEvents(context.Background(), time.Now(), tc.input)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if len(events) != tc.expected {
t.Errorf("expected %d events, got %d", tc.expected, len(events))
}
})
}
}

0 comments on commit f5fbb2e

Please sign in to comment.