-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for memory bandwidth exporter. (#411)
* Add unit test for memory bandwidth exporter. Signed-off-by: Yugar-1 <[email protected]>
- Loading branch information
Showing
7 changed files
with
905 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
kubernetes-addons/memory-bandwidth-exporter/collector/class_collector_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
kubernetes-addons/memory-bandwidth-exporter/collector/collector_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.