Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for memory bandwidth exporter. #411

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/pr-go-unittests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ jobs:

- name: Run tests and generate coverage
run: |
if [ "${{ matrix.gopath }}" == "${MBE_DIR}" ]; then
exit 0
fi
cd ${{ matrix.gopath }}
go test -coverprofile=coverage.out $(go list ./... | grep -v /e2e)
${{ github.workspace }}/.github/workflows/scripts/go-coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package collector

import (
"reflect"
"testing"
"time"

"github.com/go-kit/log"
)

func TestNewClassCollector(t *testing.T) {
nn := "node1"
metrics1 := noMetrics
metrics2 := allMetrics
metrics3 := "mb,llc,cpu"
type fields struct {
classCollectorMetrics *string
nodeName *string
}
type args struct {
logger log.Logger
interval time.Duration
}
tests := []struct {
name string
fields fields
args args
want Collector
wantErr bool
}{
{
name: "TestNewNodeCollector 1",
fields: fields{
classCollectorMetrics: &metrics1,
nodeName: &nn,
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
},
want: &classCollector{
statsCache: make(map[string]*stats),
interval: 3 * time.Second,
logger: log.NewNopLogger(),
nodeName: "node1",
metrics: make(map[string]struct{}),
},
wantErr: false,
},
{
name: "TestNewNodeCollector 2",
fields: fields{
classCollectorMetrics: &metrics2,
nodeName: &nn,
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
},
want: &classCollector{
statsCache: make(map[string]*stats),
interval: 3 * time.Second,
logger: log.NewNopLogger(),
nodeName: "node1",
metrics: map[string]struct{}{
"mb": {},
"llc": {},
},
},
wantErr: false,
},
{
name: "TestNewNodeCollector 3",
fields: fields{
classCollectorMetrics: &metrics3,
nodeName: &nn,
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
},
want: &classCollector{
statsCache: make(map[string]*stats),
interval: 3 * time.Second,
logger: log.NewNopLogger(),
nodeName: "node1",
metrics: map[string]struct{}{
"mb": {},
"llc": {},
"cpu": {},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
classCollectorMetrics = tt.fields.classCollectorMetrics
nodeName = tt.fields.nodeName
got, err := NewClassCollector(tt.args.logger, tt.args.interval)
if (err != nil) != tt.wantErr {
t.Errorf("NewClassCollector() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewClassCollector() = %v, want %v", got, tt.want)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,41 @@ func ParseCollectorMetrics() bool {
for collector := range collectorState {
if collector == containerCollectorSubsystem {
var isDefaultEnabled bool
if *containerCollectorMetrics == noMetrics {
if containerCollectorMetrics == nil || *containerCollectorMetrics == noMetrics {
isDefaultEnabled = false
collectorMetrics[collector] = noMetrics
} else {
isDefaultEnabled = true
collectorMetrics[collector] = *containerCollectorMetrics
}
collectorState[collector] = &isDefaultEnabled
collectorMetrics[collector] = *containerCollectorMetrics
if *containerCollectorMetrics == allMetrics || strings.Contains(*containerCollectorMetrics, "mb") ||
strings.Contains(*containerCollectorMetrics, "llc") {
if containerCollectorMetrics != nil && (*containerCollectorMetrics == allMetrics ||
strings.Contains(*containerCollectorMetrics, "mb") ||
strings.Contains(*containerCollectorMetrics, "llc")) {
isNeedNRIPlugin = true
}
}
if collector == classCollectorSubsystem {
var isDefaultEnabled bool
if *classCollectorMetrics == "none" {
if classCollectorMetrics == nil || *classCollectorMetrics == noMetrics {
isDefaultEnabled = false
collectorMetrics[collector] = noMetrics
} else {
isDefaultEnabled = true
collectorMetrics[collector] = *classCollectorMetrics
}
collectorState[collector] = &isDefaultEnabled
collectorMetrics[collector] = *classCollectorMetrics
}
if collector == nodeCollectorSubsystem {
var isDefaultEnabled bool
if *nodeCollectorMetrics == "none" {
if nodeCollectorMetrics == nil || *nodeCollectorMetrics == noMetrics {
isDefaultEnabled = false
collectorMetrics[collector] = noMetrics
} else {
isDefaultEnabled = true
collectorMetrics[collector] = *nodeCollectorMetrics
}
collectorState[collector] = &isDefaultEnabled
collectorMetrics[collector] = *nodeCollectorMetrics
}
}
return isNeedNRIPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package collector

import (
"sync"
"testing"
"time"

"github.com/go-kit/log"
)

func TestParseCollectorMetrics(t *testing.T) {
type fields struct {
containerCollectorMetrics *string
classCollectorMetrics *string
nodeCollectorMetrics *string
}
metrics1 := noMetrics
metrics2 := allMetrics
metrics3 := "mb"
tests := []struct {
name string
fields fields
want bool
}{
{
name: "TestParseCollectorMetrics 1",
fields: fields{
containerCollectorMetrics: nil,
classCollectorMetrics: nil,
nodeCollectorMetrics: nil,
},
want: false,
},
{
name: "TestParseCollectorMetrics 2",
fields: fields{
containerCollectorMetrics: &metrics1,
classCollectorMetrics: nil,
nodeCollectorMetrics: nil,
},
want: false,
},
{
name: "TestParseCollectorMetrics 3",
fields: fields{
containerCollectorMetrics: &metrics2,
classCollectorMetrics: nil,
nodeCollectorMetrics: nil,
},
want: true,
},
{
name: "TestParseCollectorMetrics 4",
fields: fields{
containerCollectorMetrics: &metrics3,
classCollectorMetrics: nil,
nodeCollectorMetrics: nil,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
containerCollectorMetrics = tt.fields.containerCollectorMetrics
classCollectorMetrics = tt.fields.classCollectorMetrics
nodeCollectorMetrics = tt.fields.nodeCollectorMetrics
if got := ParseCollectorMetrics(); got != tt.want {
t.Errorf("ParseCollectorMetrics() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewCollector(t *testing.T) {
isDefaultEnabled := true
isDefaultDisabled := false
type fields struct {
collectorState map[string]*bool
}
type args struct {
logger log.Logger
interval time.Duration
filters []string
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "TestNewCollector 1",
fields: fields{
collectorState: map[string]*bool{
containerCollectorSubsystem: &isDefaultEnabled,
classCollectorSubsystem: &isDefaultDisabled,
nodeCollectorSubsystem: &isDefaultDisabled,
},
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
filters: []string{},
},
wantErr: false,
},
{
name: "TestNewCollector 2",
fields: fields{
collectorState: map[string]*bool{
// containerCollectorSubsystem: &isDefaultDisabled,
classCollectorSubsystem: &isDefaultDisabled,
nodeCollectorSubsystem: &isDefaultDisabled,
},
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
filters: []string{
containerCollectorSubsystem,
},
},
wantErr: true,
},
{
name: "TestNewCollector32",
fields: fields{
collectorState: map[string]*bool{
containerCollectorSubsystem: &isDefaultDisabled,
classCollectorSubsystem: &isDefaultDisabled,
nodeCollectorSubsystem: &isDefaultDisabled,
},
},
args: args{
logger: log.NewNopLogger(),
interval: 3 * time.Second,
filters: []string{
containerCollectorSubsystem,
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(
tt.name,
func(t *testing.T) {
collectorState = tt.fields.collectorState
initiatedCollectorsMtx = sync.Mutex{}
initiatedCollectors = make(map[string]Collector)
_, err := NewCollector(tt.args.logger, tt.args.interval, tt.args.filters...)
if (err != nil) != tt.wantErr {
t.Errorf("NewCollector() error = %v, wantErr %v", err, tt.wantErr)
return
}
},
)
}
}

func TestIsNoDataError(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want bool
}{
{
name: "TestIsNoDataError 1",
args: args{
err: nil,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsNoDataError(tt.args.err); got != tt.want {
t.Errorf("IsNoDataError() = %v, want %v", got, tt.want)
}
})
}
}
Loading
Loading