diff --git a/net_sockstat.go b/net_sockstat.go index a34c9b862..f91ef5523 100644 --- a/net_sockstat.go +++ b/net_sockstat.go @@ -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")) } @@ -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 } diff --git a/net_sockstat_test.go b/net_sockstat_test.go index 657d61e8a..08672d74f 100644 --- a/net_sockstat_test.go +++ b/net_sockstat_test.go @@ -14,6 +14,7 @@ package procfs import ( + "os" "strings" "testing" @@ -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