mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 23:19:46 -08:00
collector: add bounds check and test for filesystem collector (#1133)
Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
parent
bdc0e7e678
commit
c0a55e3f80
|
@ -17,6 +17,8 @@ package collector
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -139,11 +141,20 @@ func mountPointDetails() ([]filesystemLabels, error) {
|
|||
}
|
||||
defer file.Close()
|
||||
|
||||
filesystems := []filesystemLabels{}
|
||||
scanner := bufio.NewScanner(file)
|
||||
return parseFilesystemLabels(file)
|
||||
}
|
||||
|
||||
func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
|
||||
var filesystems []filesystemLabels
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
parts := strings.Fields(scanner.Text())
|
||||
|
||||
if len(parts) < 4 {
|
||||
return nil, fmt.Errorf("malformed mount point information: %q", scanner.Text())
|
||||
}
|
||||
|
||||
// Ensure we handle the translation of \040 and \011
|
||||
// as per fstab(5).
|
||||
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
|
||||
|
@ -156,5 +167,6 @@ func mountPointDetails() ([]filesystemLabels, error) {
|
|||
options: parts[3],
|
||||
})
|
||||
}
|
||||
|
||||
return filesystems, scanner.Err()
|
||||
}
|
||||
|
|
|
@ -16,11 +16,32 @@
|
|||
package collector
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||
)
|
||||
|
||||
func Test_parseFilesystemLabelsError(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 := parseFilesystemLabels(strings.NewReader(tt.in)); err == nil {
|
||||
t.Fatal("expected an error, but none occurred")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMountPointDetails(t *testing.T) {
|
||||
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
Loading…
Reference in a new issue