Skip to content

Commit

Permalink
Add unit test for memory bandwidth exporter. (#411)
Browse files Browse the repository at this point in the history
* Add unit test for memory bandwidth exporter.

Signed-off-by: Yugar-1 <[email protected]>
  • Loading branch information
Yugar-1 authored Sep 9, 2024
1 parent 353f3a5 commit 43adcc6
Show file tree
Hide file tree
Showing 7 changed files with 905 additions and 11 deletions.
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)
}
})
}
}
20 changes: 12 additions & 8 deletions kubernetes-addons/memory-bandwidth-exporter/collector/collector.go
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

0 comments on commit 43adcc6

Please sign in to comment.