Skip to content

Commit

Permalink
Merge pull request #2 from treydock/tests
Browse files Browse the repository at this point in the history
Improved testing
  • Loading branch information
treydock authored Apr 26, 2021
2 parents a431d8e + 3f58a32 commit 278cba2
Show file tree
Hide file tree
Showing 22 changed files with 895 additions and 415 deletions.
27 changes: 15 additions & 12 deletions collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
package collectors

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
Expand All @@ -41,18 +45,17 @@ var (
[]string{"collector"}, nil)
)

func getDeviceGUIDs(devices map[string]InfinibandDevice) []string {
keys := make([]string, 0, len(devices))
for key := range devices {
keys = append(keys, key)
func ReadFixture(outputType string, name string) (string, error) {
_, filename, _, _ := runtime.Caller(0)
dir := filepath.Dir(filename)
if filepath.Base(dir) != "collectors" {
dir = filepath.Join(dir, "collectors")
}
return keys
}

func getDevicePorts(uplinks map[string]InfinibandUplink) []string {
keys := make([]string, 0, len(uplinks))
for key := range uplinks {
keys = append(keys, key)
fixtureDir := filepath.Join(dir, "fixtures", outputType)
fixture := filepath.Join(fixtureDir, fmt.Sprintf("%s.out", name))
buffer, err := os.ReadFile(fixture)
if err != nil {
return "", err
}
return keys
return string(buffer), nil
}
44 changes: 44 additions & 0 deletions collectors/collectors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,50 @@ var (
_, cancel = context.WithTimeout(context.Background(), 5*time.Second)
)

func SetIbnetdiscoverExec(t *testing.T, setErr bool, timeout bool) {
IbnetdiscoverExec = func(ctx context.Context) (string, error) {
if setErr {
return "", fmt.Errorf("Error")
}
if timeout {
return "", context.DeadlineExceeded
}
out, err := ReadFixture("ibnetdiscover", "test")
if err != nil {
t.Fatal(err.Error())
return "", err
}
return out, nil
}
}

func SetPerfqueryExecs(t *testing.T, setErr bool, timeout bool) {
PerfqueryExec = func(guid string, port string, extraArgs []string, ctx context.Context) (string, error) {
if setErr {
return "", fmt.Errorf("Error")
}
if timeout {
return "", context.DeadlineExceeded
}
var out string
var err error
if len(extraArgs) == 2 {
out, err = ReadFixture("perfquery", guid)
if err != nil {
t.Fatal(err.Error())
return "", err
}
} else {
out, err = ReadFixture("perfquery-rcv-error", fmt.Sprintf("%s-%s", guid, port))
if err != nil {
t.Fatal(err.Error())
return "", err
}
}
return out, nil
}
}

func fakeExecCommand(ctx context.Context, command string, args ...string) *exec.Cmd {
cs := []string{"-test.run=TestExecCommandHelper", "--", command}
cs = append(cs, args...)
Expand Down
8 changes: 8 additions & 0 deletions collectors/fixtures/ibnetdiscover/test.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CA 134 1 0x7cfe9003003b4bde 4x EDR - SW 1719 10 0x7cfe9003009ce5b0 ( 'o0001 HCA-1' - 'ib-i1l1s01' )
CA 133 1 0x7cfe9003003b4b96 4x EDR - SW 1719 11 0x7cfe9003009ce5b0 ( 'o0002 HCA-1' - 'ib-i1l1s01' )
CA 1432 1 0x506b4b0300cc02a6 4x EDR - SW 2052 35 0x506b4b03005c2740 ( 'p0001 HCA-1' - 'ib-i4l1s01' )
SW 1719 10 0x7cfe9003009ce5b0 4x EDR - CA 134 1 0x7cfe9003003b4bde ( 'ib-i1l1s01' - 'o0001 HCA-1' )
SW 1719 11 0x7cfe9003009ce5b0 4x EDR - CA 133 1 0x7cfe9003003b4b96 ( 'ib-i1l1s01' - 'o0002 HCA-1' )
SW 1719 1 0x7cfe9003009ce5b0 4x EDR - SW 1516 1 0x7cfe900300b07320 ( 'ib-i1l1s01' - 'ib-i1l2s01' )
SW 2052 35 0x506b4b03005c2740 4x EDR - CA 1432 1 0x506b4b0300cc02a6 ( 'ib-i4l1s01' - 'p0001 HCA-1' )
SW 2052 37 0x506b4b03005c2740 4x ??? 'ib-i4l1s01'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PortRcvErrorDetails counters: Lid 2052 port 1
PortSelect:......................1
CounterSelect:...................0x0000
PortLocalPhysicalErrors:.........0
PortMalformedPktErrors:..........0
PortBufferOverrunErrors:.........0
PortDLIDMappingErrors:...........0
PortVLMappingErrors:.............0
PortLoopingErrors:...............0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PortRcvErrorDetails counters: Lid 133 port 1
PortSelect:......................1
CounterSelect:...................0x0000
PortLocalPhysicalErrors:.........0
PortMalformedPktErrors:..........0
PortBufferOverrunErrors:.........0
PortDLIDMappingErrors:...........0
PortVLMappingErrors:.............0
PortLoopingErrors:...............0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PortRcvErrorDetails counters: Lid 134 port 1
PortSelect:......................1
CounterSelect:...................0x0000
PortLocalPhysicalErrors:.........0
PortMalformedPktErrors:..........0
PortBufferOverrunErrors:.........0
PortDLIDMappingErrors:...........0
PortVLMappingErrors:.............0
PortLoopingErrors:...............0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PortRcvErrorDetails counters: Lid 1719 port 1
PortSelect:......................1
CounterSelect:...................0x0000
PortLocalPhysicalErrors:.........0
PortMalformedPktErrors:..........0
PortBufferOverrunErrors:.........0
PortDLIDMappingErrors:...........0
PortVLMappingErrors:.............0
PortLoopingErrors:...............0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PortRcvErrorDetails counters: Lid 1719 port 2
PortSelect:......................2
CounterSelect:...................0x0000
PortLocalPhysicalErrors:.........0
PortMalformedPktErrors:..........0
PortBufferOverrunErrors:.........0
PortDLIDMappingErrors:...........0
PortVLMappingErrors:.............0
PortLoopingErrors:...............0
27 changes: 27 additions & 0 deletions collectors/fixtures/perfquery/0x506b4b03005c2740.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Port extended counters: Lid 2052 port 1 (CapMask: 0x5300 CapMask2: 0x0000002)
PortSelect:......................1
CounterSelect:...................0x0000
PortXmitData:....................178791657177235
PortRcvData:.....................178762341961629
PortXmitPkts:....................393094651266
PortRcvPkts:.....................387654829341
PortUnicastXmitPkts:.............387471005571
PortUnicastRcvPkts:..............387648134400
PortMulticastXmitPkts:...........5623645694
PortMulticastRcvPkts:............6694940
CounterSelect2:..................0x00000000
SymbolErrorCounter:..............0
LinkErrorRecoveryCounter:........0
LinkDownedCounter:...............1
PortRcvErrors:...................0
PortRcvRemotePhysicalErrors:.....0
PortRcvSwitchRelayErrors:........7
PortXmitDiscards:................20046
PortXmitConstraintErrors:........0
PortRcvConstraintErrors:.........0
LocalLinkIntegrityErrors:........0
ExcessiveBufferOverrunErrors:....0
VL15Dropped:.....................0
PortXmitWait:....................41864608
QP1Dropped:......................0

Empty file.
27 changes: 27 additions & 0 deletions collectors/fixtures/perfquery/0x7cfe9003003b4b96.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Port extended counters: Lid 133 port 1 (CapMask: 0x5A00 CapMask2: 0x0000000)
PortSelect:......................1
CounterSelect:...................0x0000
PortXmitData:....................37108676853855
PortRcvData:.....................37225401952885
PortXmitPkts:....................96917117320
PortRcvPkts:.....................100583719365
PortUnicastXmitPkts:.............96916572630
PortUnicastRcvPkts:..............96851346228
PortMulticastXmitPkts:...........544690
PortMulticastRcvPkts:............3732373137
CounterSelect2:..................0x00000000
SymbolErrorCounter:..............0
LinkErrorRecoveryCounter:........0
LinkDownedCounter:...............0
PortRcvErrors:...................0
PortRcvRemotePhysicalErrors:.....0
PortRcvSwitchRelayErrors:........0
PortXmitDiscards:................0
PortXmitConstraintErrors:........0
PortRcvConstraintErrors:.........0
LocalLinkIntegrityErrors:........0
ExcessiveBufferOverrunErrors:....0
VL15Dropped:.....................0
PortXmitWait:....................0
QP1Dropped:......................0

27 changes: 27 additions & 0 deletions collectors/fixtures/perfquery/0x7cfe9003003b4bde.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Port extended counters: Lid 134 port 1 (CapMask: 0x5A00 CapMask2: 0x0000000)
PortSelect:......................1
CounterSelect:...................0x0000
PortXmitData:....................9049592493976
PortRcvData:.....................9752484588300
PortXmitPkts:....................28825338611
PortRcvPkts:.....................33038722564
PortUnicastXmitPkts:.............28824617123
PortUnicastRcvPkts:..............29306563974
PortMulticastXmitPkts:...........721488
PortMulticastRcvPkts:............3732158589
CounterSelect2:..................0x00000000
SymbolErrorCounter:..............0
LinkErrorRecoveryCounter:........0
LinkDownedCounter:...............0
PortRcvErrors:...................0
PortRcvRemotePhysicalErrors:.....0
PortRcvSwitchRelayErrors:........0
PortXmitDiscards:................0
PortXmitConstraintErrors:........0
PortRcvConstraintErrors:.........0
LocalLinkIntegrityErrors:........0
ExcessiveBufferOverrunErrors:....0
VL15Dropped:.....................0
PortXmitWait:....................0
QP1Dropped:......................0

53 changes: 53 additions & 0 deletions collectors/fixtures/perfquery/0x7cfe9003009ce5b0.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Port extended counters: Lid 1719 port 1 (CapMask: 0x5300 CapMask2: 0x0000002)
PortSelect:......................1
CounterSelect:...................0x0000
PortXmitData:....................36298026860928
PortRcvData:.....................12279028775751
PortXmitPkts:....................101733204203
PortRcvPkts:.....................32262508468
PortUnicastXmitPkts:.............101708165289
PortUnicastRcvPkts:..............26677661727
PortMulticastXmitPkts:...........25038914
PortMulticastRcvPkts:............5584846741
CounterSelect2:..................0x00000000
SymbolErrorCounter:..............0
LinkErrorRecoveryCounter:........0
LinkDownedCounter:...............0
PortRcvErrors:...................0
PortRcvRemotePhysicalErrors:.....0
PortRcvSwitchRelayErrors:........0
PortXmitDiscards:................0
PortXmitConstraintErrors:........0
PortRcvConstraintErrors:.........0
LocalLinkIntegrityErrors:........0
ExcessiveBufferOverrunErrors:....0
VL15Dropped:.....................0
PortXmitWait:....................22730501
QP1Dropped:......................0
# Port extended counters: Lid 1719 port 2 (CapMask: 0x5300 CapMask2: 0x0000002)
PortSelect:......................2
CounterSelect:...................0x0000
PortXmitData:....................26006570014026
PortRcvData:.....................39078804993378
PortXmitPkts:....................122978948297
PortRcvPkts:.....................93660802641
PortUnicastXmitPkts:.............122978948297
PortUnicastRcvPkts:..............93660802641
PortMulticastXmitPkts:...........0
PortMulticastRcvPkts:............0
CounterSelect2:..................0x00000000
SymbolErrorCounter:..............0
LinkErrorRecoveryCounter:........0
LinkDownedCounter:...............0
PortRcvErrors:...................0
PortRcvRemotePhysicalErrors:.....0
PortRcvSwitchRelayErrors:........0
PortXmitDiscards:................0
PortXmitConstraintErrors:........0
PortRcvConstraintErrors:.........0
LocalLinkIntegrityErrors:........0
ExcessiveBufferOverrunErrors:....0
VL15Dropped:.....................0
PortXmitWait:....................36510964
QP1Dropped:......................0

15 changes: 4 additions & 11 deletions collectors/hca.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,8 @@ func (h *HCACollector) collect() ([]PerfQueryCounters, float64, float64) {
<-limit
return
}
deviceCounters, err := perfqueryParse(device, extendedOut, h.logger)
if err != nil {
errors++
<-limit
return
}
deviceCounters, errs := perfqueryParse(device, extendedOut, h.logger)
errors = errors + errs
level.Debug(h.logger).Log("msg", "Adding parsed counters", "count", len(deviceCounters), "guid", device.GUID, "name", device.Name)
countersLock.Lock()
counters = append(counters, deviceCounters...)
Expand All @@ -304,11 +300,8 @@ func (h *HCACollector) collect() ([]PerfQueryCounters, float64, float64) {
errors++
continue
}
rcvErrCounters, err := perfqueryParse(device, rcvErrOut, h.logger)
if err != nil {
errors++
continue
}
rcvErrCounters, errs := perfqueryParse(device, rcvErrOut, h.logger)
errors = errors + errs
countersLock.Lock()
counters = append(counters, rcvErrCounters...)
countersLock.Unlock()
Expand Down
Loading

0 comments on commit 278cba2

Please sign in to comment.