Fix tests

This commit is contained in:
Tobias Schmidt 2014-11-24 18:30:07 -05:00
parent d06281286d
commit 974f6fc762
16 changed files with 206 additions and 151 deletions

View File

@ -13,10 +13,10 @@ func TestBonding(t *testing.T) {
t.Fatal("bond0 in unexpected state") t.Fatal("bond0 in unexpected state")
} }
t.Logf("int: %v", bondingStats["int"])
if bondingStats["int"][0] != 2 || bondingStats["int"][1] != 1 { if bondingStats["int"][0] != 2 || bondingStats["int"][1] != 1 {
t.Fatal("int in unexpected state") t.Fatal("int in unexpected state")
} }
if bondingStats["dmz"][0] != 2 || bondingStats["dmz"][1] != 2 { if bondingStats["dmz"][0] != 2 || bondingStats["dmz"][1] != 2 {
t.Fatal("dmz in unexpected state") t.Fatal("dmz in unexpected state")
} }

View File

@ -184,13 +184,17 @@ func getDiskStats() (map[string]map[int]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
return parseDiskStats(file) return parseDiskStats(file)
} }
func parseDiskStats(r io.ReadCloser) (map[string]map[int]string, error) { func parseDiskStats(r io.Reader) (map[string]map[int]string, error) {
defer r.Close() var (
diskStats := map[string]map[int]string{} diskStats = map[string]map[int]string{}
scanner := bufio.NewScanner(r) scanner = bufio.NewScanner(r)
)
for scanner.Scan() { for scanner.Scan() {
parts := strings.Fields(string(scanner.Text())) parts := strings.Fields(string(scanner.Text()))
if len(parts) != len(diskStatsMetrics)+3 { // we strip major, minor and dev if len(parts) != len(diskStatsMetrics)+3 { // we strip major, minor and dev
@ -202,5 +206,6 @@ func parseDiskStats(r io.ReadCloser) (map[string]map[int]string, error) {
diskStats[dev][i] = v diskStats[dev][i] = v
} }
} }
return diskStats, nil return diskStats, nil
} }

View File

@ -0,0 +1,27 @@
package collector
import (
"os"
"testing"
)
func TestDiskStats(t *testing.T) {
file, err := os.Open("fixtures/diskstats")
if err != nil {
t.Fatal(err)
}
defer file.Close()
diskStats, err := parseDiskStats(file)
if err != nil {
t.Fatal(err)
}
if want, got := "25353629", diskStats["sda4"][0]; want != got {
t.Errorf("want diskstats sda4 %s, got %s", want, got)
}
if want, got := "68", diskStats["mmcblk0p2"][10]; want != got {
t.Errorf("want diskstats mmcblk0p2 %s, got %s", want, got)
}
}

View File

@ -1 +0,0 @@
0.21 0.37 0.39 1/719 19737

View File

@ -0,0 +1,4 @@
TcpExt: SyncookiesSent SyncookiesRecv SyncookiesFailed EmbryonicRsts PruneCalled RcvPruned OfoPruned OutOfWindowIcmps LockDroppedIcmps ArpFilter TW TWRecycled TWKilled PAWSPassive PAWSActive PAWSEstab DelayedACKs DelayedACKLocked DelayedACKLost ListenOverflows ListenDrops TCPPrequeued TCPDirectCopyFromBacklog TCPDirectCopyFromPrequeue TCPPrequeueDropped TCPHPHits TCPHPHitsToUser TCPPureAcks TCPHPAcks TCPRenoRecovery TCPSackRecovery TCPSACKReneging TCPFACKReorder TCPSACKReorder TCPRenoReorder TCPTSReorder TCPFullUndo TCPPartialUndo TCPDSACKUndo TCPLossUndo TCPLoss TCPLostRetransmit TCPRenoFailures TCPSackFailures TCPLossFailures TCPFastRetrans TCPForwardRetrans TCPSlowStartRetrans TCPTimeouts TCPRenoRecoveryFail TCPSackRecoveryFail TCPSchedulerFailed TCPRcvCollapsed TCPDSACKOldSent TCPDSACKOfoSent TCPDSACKRecv TCPDSACKOfoRecv TCPAbortOnData TCPAbortOnClose TCPAbortOnMemory TCPAbortOnTimeout TCPAbortOnLinger TCPAbortFailed TCPMemoryPressures TCPSACKDiscard TCPDSACKIgnoredOld TCPDSACKIgnoredNoUndo TCPSpuriousRTOs TCPMD5NotFound TCPMD5Unexpected TCPSackShifted TCPSackMerged TCPSackShiftFallback TCPBacklogDrop TCPMinTTLDrop TCPDeferAcceptDrop IPReversePathFilter TCPTimeWaitOverflow TCPReqQFullDoCookies TCPReqQFullDrop TCPChallengeACK TCPSYNChallenge
TcpExt: 0 0 2 0 0 0 0 0 0 0 388812 0 0 0 0 6 102471 17 9 0 0 80568 0 168808 0 4471289 26 1433940 3744565 0 1 0 0 0 0 0 0 0 0 48 0 0 0 1 0 1 0 1 115 0 0 0 0 9 0 5 0 41 4 0 0 0 0 0 0 0 1 0 0 0 0 2 5 0 0 0 0 0 0 0 2 2
IpExt: InNoRoutes InTruncatedPkts InMcastPkts OutMcastPkts InBcastPkts OutBcastPkts InOctets OutOctets InMcastOctets OutMcastOctets InBcastOctets OutBcastOctets
IpExt: 0 0 0 0 0 0 6286396970 2786264347 0 0 0 0

View File

@ -80,13 +80,17 @@ func getInterrupts() (map[string]interrupt, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
return parseInterrupts(file) return parseInterrupts(file)
} }
func parseInterrupts(r io.ReadCloser) (map[string]interrupt, error) { func parseInterrupts(r io.Reader) (map[string]interrupt, error) {
defer r.Close() var (
interrupts := map[string]interrupt{} interrupts = map[string]interrupt{}
scanner := bufio.NewScanner(r) scanner = bufio.NewScanner(r)
)
if !scanner.Scan() { if !scanner.Scan() {
return nil, fmt.Errorf("%s empty", procInterrupts) return nil, fmt.Errorf("%s empty", procInterrupts)
} }
@ -111,5 +115,6 @@ func parseInterrupts(r io.ReadCloser) (map[string]interrupt, error) {
} }
interrupts[intName] = intr interrupts[intName] = intr
} }
return interrupts, nil return interrupts, nil
} }

View File

@ -0,0 +1,23 @@
package collector
import (
"os"
"testing"
)
func TestInterrupts(t *testing.T) {
file, err := os.Open("fixtures/interrupts")
if err != nil {
t.Fatal(err)
}
defer file.Close()
interrupts, err := parseInterrupts(file)
if err != nil {
t.Fatal(err)
}
if want, got := "5031", interrupts["NMI"].values[1]; want != got {
t.Errorf("want interrupts %s, got %s", want, got)
}
}

14
collector/loadavg_test.go Normal file
View File

@ -0,0 +1,14 @@
package collector
import "testing"
func TestLoad(t *testing.T) {
load, err := parseLoad("0.21 0.37 0.39 1/719 19737")
if err != nil {
t.Fatal(err)
}
if want := 0.21; want != load {
t.Fatalf("want load %f, got %f", want, load)
}
}

View File

@ -43,13 +43,14 @@ func init() {
Factories["megacli"] = NewMegaCliCollector Factories["megacli"] = NewMegaCliCollector
} }
func parseMegaCliDisks(r io.ReadCloser) (map[int]map[int]map[string]string, error) { func parseMegaCliDisks(r io.Reader) (map[int]map[int]map[string]string, error) {
defer r.Close() var (
stats := map[int]map[int]map[string]string{} stats = map[int]map[int]map[string]string{}
scanner := bufio.NewScanner(r) scanner = bufio.NewScanner(r)
curEnc = -1
curSlot = -1
)
curEnc := -1
curSlot := -1
for scanner.Scan() { for scanner.Scan() {
var err error var err error
text := strings.TrimSpace(scanner.Text()) text := strings.TrimSpace(scanner.Text())
@ -80,15 +81,18 @@ func parseMegaCliDisks(r io.ReadCloser) (map[int]map[int]map[string]string, erro
stats[curEnc][curSlot][key] = value stats[curEnc][curSlot][key] = value
} }
} }
return stats, nil return stats, nil
} }
func parseMegaCliAdapter(r io.ReadCloser) (map[string]map[string]string, error) { func parseMegaCliAdapter(r io.Reader) (map[string]map[string]string, error) {
defer r.Close() var (
raidStats := map[string]map[string]string{} raidStats = map[string]map[string]string{}
scanner := bufio.NewScanner(r) scanner = bufio.NewScanner(r)
header := "" header = ""
last := "" last = ""
)
for scanner.Scan() { for scanner.Scan() {
text := strings.TrimSpace(scanner.Text()) text := strings.TrimSpace(scanner.Text())
if text == adapterHeaderSep { if text == adapterHeaderSep {
@ -110,6 +114,7 @@ func parseMegaCliAdapter(r io.ReadCloser) (map[string]map[string]string, error)
raidStats[header][key] = value raidStats[header][key] = value
} }
return raidStats, nil return raidStats, nil
} }

View File

@ -67,14 +67,18 @@ func getMemInfo() (map[string]float64, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
return parseMemInfo(file) return parseMemInfo(file)
} }
func parseMemInfo(r io.ReadCloser) (map[string]float64, error) { func parseMemInfo(r io.Reader) (map[string]float64, error) {
defer r.Close() var (
memInfo := map[string]float64{} memInfo = map[string]float64{}
scanner := bufio.NewScanner(r) scanner = bufio.NewScanner(r)
re := regexp.MustCompile("\\((.*)\\)") re = regexp.MustCompile("\\((.*)\\)")
)
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
parts := strings.Fields(string(line)) parts := strings.Fields(string(line))
@ -94,6 +98,6 @@ func parseMemInfo(r io.ReadCloser) (map[string]float64, error) {
key = re.ReplaceAllString(key, "_${1}") key = re.ReplaceAllString(key, "_${1}")
memInfo[key] = fv memInfo[key] = fv
} }
return memInfo, nil
return memInfo, nil
} }

27
collector/meminfo_test.go Normal file
View File

@ -0,0 +1,27 @@
package collector
import (
"os"
"testing"
)
func TestMemInfo(t *testing.T) {
file, err := os.Open("fixtures/meminfo")
if err != nil {
t.Fatal(err)
}
defer file.Close()
memInfo, err := parseMemInfo(file)
if err != nil {
t.Fatal(err)
}
if want, got := 3831959552.0, memInfo["MemTotal"]; want != got {
t.Errorf("want memory total %f, got %f", want, got)
}
if want, got := 3787456512.0, memInfo["DirectMap2M"]; want != got {
t.Errorf("want memory directMap2M %f, got %f", want, got)
}
}

View File

@ -1,117 +0,0 @@
package collector
import (
"io/ioutil"
"os"
"testing"
)
const (
loadExpected = 0.21
memTotalExpected = 3831959552
memDirectMap2MExpected = 3787456512
interruptsNmi1Expected = "5031"
netReceiveWlan0Bytes = "10437182923"
netTransmitTun0Packages = "934"
diskSda4ReadsCompleted = "25353629"
diskMmcIoTimeWeighted = "68"
testProcLoad = "fixtures/loadavg"
testProcMemInfo = "fixtures/meminfo"
testProcInterrupts = "fixtures/interrupts"
testProcNetDev = "fixtures/net-dev"
testProcDiskStats = "fixtures/diskstats"
)
func TestLoad(t *testing.T) {
data, err := ioutil.ReadFile(testProcLoad)
if err != nil {
t.Fatal(err)
}
load, err := parseLoad(string(data))
if err != nil {
t.Fatal(err)
}
if load != loadExpected {
t.Fatalf("Unexpected load: %f != %f", load, loadExpected)
}
}
func TestMemInfo(t *testing.T) {
file, err := os.Open(testProcMemInfo)
if err != nil {
t.Fatal(err)
}
memInfo, err := parseMemInfo(file)
if err != nil {
t.Fatal(err)
}
if memInfo["MemTotal"] != memTotalExpected {
t.Fatalf("Unexpected memory: %f != %d", memInfo["MemTotal"], memTotalExpected)
}
if memInfo["DirectMap2M"] != memDirectMap2MExpected {
t.Fatalf("Unexpected memory: %f != %d", memInfo["MemTotal"], memTotalExpected)
}
}
func TestInterrupts(t *testing.T) {
file, err := os.Open(testProcInterrupts)
if err != nil {
t.Fatal(err)
}
interrupts, err := parseInterrupts(file)
if err != nil {
t.Fatal(err)
}
if interrupts["NMI"].values[1] != interruptsNmi1Expected {
t.Fatalf("Unexpected interrupts: %s != %s", interrupts["NMI"].values[1],
interruptsNmi1Expected)
}
}
func TestNetStats(t *testing.T) {
file, err := os.Open(testProcNetDev)
if err != nil {
t.Fatal(err)
}
netStats, err := parseNetStats(file)
if err != nil {
t.Fatal(err)
}
if netStats["receive"]["wlan0"]["bytes"] != netReceiveWlan0Bytes {
t.Fatalf("Unexpected netstats: %s != %s", netStats["receive"]["wlan0"]["bytes"],
netReceiveWlan0Bytes)
}
if netStats["transmit"]["tun0"]["packets"] != netTransmitTun0Packages {
t.Fatalf("Unexpected netstats: %s != %s", netStats["transmit"]["tun0"]["packets"],
netTransmitTun0Packages)
}
}
func TestDiskStats(t *testing.T) {
file, err := os.Open(testProcDiskStats)
if err != nil {
t.Fatal(err)
}
diskStats, err := parseDiskStats(file)
if err != nil {
t.Fatal(err)
}
if diskStats["sda4"][0] != diskSda4ReadsCompleted {
t.Fatalf("Unexpected diskstats: %s != %s", diskStats["sda4"][0],
diskSda4ReadsCompleted)
}
if diskStats["mmcblk0p2"][10] != diskMmcIoTimeWeighted {
t.Fatalf("Unexpected diskstats: %s != %s",
diskStats["mmcblk0p2"][10], diskMmcIoTimeWeighted)
}
}

View File

@ -78,11 +78,12 @@ func getNetDevStats() (map[string]map[string]map[string]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
return parseNetDevStats(file) return parseNetDevStats(file)
} }
func parseNetDevStats(r io.ReadCloser) (map[string]map[string]map[string]string, error) { func parseNetDevStats(r io.Reader) (map[string]map[string]map[string]string, error) {
defer r.Close()
netDev := map[string]map[string]map[string]string{} netDev := map[string]map[string]map[string]string{}
netDev["transmit"] = map[string]map[string]string{} netDev["transmit"] = map[string]map[string]string{}
netDev["receive"] = map[string]map[string]string{} netDev["receive"] = map[string]map[string]string{}

27
collector/netdev_test.go Normal file
View File

@ -0,0 +1,27 @@
package collector
import (
"os"
"testing"
)
func TestNetDevStats(t *testing.T) {
file, err := os.Open("fixtures/net-dev")
if err != nil {
t.Fatal(err)
}
defer file.Close()
netStats, err := parseNetDevStats(file)
if err != nil {
t.Fatal(err)
}
if want, got := "10437182923", netStats["receive"]["wlan0"]["bytes"]; want != got {
t.Errorf("want netstat wlan0 bytes %s, got %s", want, got)
}
if want, got := "934", netStats["transmit"]["tun0"]["packets"]; want != got {
t.Errorf("want netstat tun0 packets %s, got %s", want, got)
}
}

View File

@ -75,14 +75,17 @@ func getNetStats() (map[string]map[string]string, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer file.Close()
return parseNetStats(file) return parseNetStats(file)
} }
func parseNetStats(r io.ReadCloser) (map[string]map[string]string, error) { func parseNetStats(r io.Reader) (map[string]map[string]string, error) {
defer r.Close() var (
netStats := map[string]map[string]string{} netStats = map[string]map[string]string{}
scanner = bufio.NewScanner(r)
)
scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
nameParts := strings.Split(string(scanner.Text()), " ") nameParts := strings.Split(string(scanner.Text()), " ")
scanner.Scan() scanner.Scan()
@ -98,5 +101,6 @@ func parseNetStats(r io.ReadCloser) (map[string]map[string]string, error) {
netStats[protocol][nameParts[i]] = valueParts[i] netStats[protocol][nameParts[i]] = valueParts[i]
} }
} }
return netStats, nil return netStats, nil
} }

27
collector/netstat_test.go Normal file
View File

@ -0,0 +1,27 @@
package collector
import (
"os"
"testing"
)
func TestNetStats(t *testing.T) {
file, err := os.Open("fixtures/netstat")
if err != nil {
t.Fatal(err)
}
defer file.Close()
netStats, err := parseNetStats(file)
if err != nil {
t.Fatal(err)
}
if want, got := "102471", netStats["TcpExt"]["DelayedACKs"]; want != got {
t.Errorf("want netstat TCP DelayedACKs %s, got %s", want, got)
}
if want, got := "2786264347", netStats["IpExt"]["OutOctets"]; want != got {
t.Errorf("want netstat IP OutOctets %s, got %s", want, got)
}
}