Skip to content

Commit

Permalink
Merge pull request #241 from prometheus/mdl-sockstat-ipv6
Browse files Browse the repository at this point in the history
procfs: ensure NetSockstat6 compatibility with os.IsNotExist
  • Loading branch information
mdlayher authored Nov 25, 2019
2 parents 4711af0 + 39b6b91 commit 6d489fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions net_sockstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func (fs FS) NetSockstat() (*NetSockstat, error) {
}

// NetSockstat6 retrieves IPv6 socket statistics.
//
// If IPv6 is disabled on this kernel, the returned error can be checked with
// os.IsNotExist.
func (fs FS) NetSockstat6() (*NetSockstat, error) {
return readSockstat(fs.proc.Path("net", "sockstat6"))
}
Expand All @@ -60,6 +63,8 @@ func readSockstat(name string) (*NetSockstat, error) {
// This file is small and can be read with one syscall.
b, err := util.ReadFileNoStat(name)
if err != nil {
// Do not wrap this error so the caller can detect os.IsNotExist and
// similar conditions.
return nil, err
}

Expand Down
12 changes: 12 additions & 0 deletions net_sockstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package procfs

import (
"os"
"strings"
"testing"

Expand Down Expand Up @@ -76,6 +77,17 @@ func TestNetSockstat6(t *testing.T) {
}
}

func Test_readSockstatIsNotExist(t *testing.T) {
// On a machine with IPv6 disabled for example, we want to ensure that
// readSockstat returns an error that is compatible with os.IsNotExist.
//
// We can use a synthetic file path here to verify this behavior.
_, err := readSockstat("/does/not/exist")
if err == nil || !os.IsNotExist(err) {
t.Fatalf("error is not compatible with os.IsNotExist: %#v", err)
}
}

func Test_parseSockstat(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 6d489fc

Please sign in to comment.