mirror of
https://github.com/prometheus/node_exporter.git
synced 2025-03-05 21:00:12 -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 (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -139,11 +141,20 @@ func mountPointDetails() ([]filesystemLabels, error) {
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
filesystems := []filesystemLabels{}
|
return parseFilesystemLabels(file)
|
||||||
scanner := bufio.NewScanner(file)
|
}
|
||||||
|
|
||||||
|
func parseFilesystemLabels(r io.Reader) ([]filesystemLabels, error) {
|
||||||
|
var filesystems []filesystemLabels
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
parts := strings.Fields(scanner.Text())
|
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
|
// Ensure we handle the translation of \040 and \011
|
||||||
// as per fstab(5).
|
// as per fstab(5).
|
||||||
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
|
parts[1] = strings.Replace(parts[1], "\\040", " ", -1)
|
||||||
|
@ -156,5 +167,6 @@ func mountPointDetails() ([]filesystemLabels, error) {
|
||||||
options: parts[3],
|
options: parts[3],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return filesystems, scanner.Err()
|
return filesystems, scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,11 +16,32 @@
|
||||||
package collector
|
package collector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
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) {
|
func TestMountPointDetails(t *testing.T) {
|
||||||
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
|
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
Loading…
Reference in a new issue