mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 06:59:44 -08:00
collector: add bounds check and test for tcpstat collector (#1134)
Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
parent
3d798aa4a1
commit
778124a56c
|
@ -118,6 +118,10 @@ func parseTCPStats(r io.Reader) (map[tcpConnectionState]float64, error) {
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if len(parts) < 4 {
|
||||||
|
return nil, fmt.Errorf("invalid TCP stats line: %q", scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(parts[0], "sl") {
|
if strings.HasPrefix(parts[0], "sl") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,9 +15,30 @@ package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func Test_parseTCPStatsError(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
in string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "too few fields",
|
||||||
|
in: "hello world",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if _, err := parseTCPStats(strings.NewReader(tt.in)); err == nil {
|
||||||
|
t.Fatal("expected an error, but none occurred")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTCPStat(t *testing.T) {
|
func TestTCPStat(t *testing.T) {
|
||||||
file, err := os.Open("fixtures/proc/net/tcpstat")
|
file, err := os.Open("fixtures/proc/net/tcpstat")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue