Skip to content

Commit

Permalink
procstats/linux: skip tests if /sys not available
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinburkesegment committed Dec 7, 2023
1 parent 9934686 commit cf67f29
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
16 changes: 16 additions & 0 deletions procstats/linux/cgroup_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package linux

import (
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -51,6 +52,12 @@ func TestParseProcCGroup(t *testing.T) {
}
}

func sysGone(t *testing.T) bool {
t.Helper()
_, err := os.Stat("/sys/fs/cgroup/cpu/cpu.cfs_period_us")
return os.IsNotExist(err)
}

func TestProcCGroupLookup(t *testing.T) {
tests := []struct {
proc ProcCGroup
Expand Down Expand Up @@ -81,6 +88,9 @@ func TestProcCGroupLookup(t *testing.T) {
}

func TestReadCPUPeriod(t *testing.T) {
if sysGone(t) {
t.Skip("/sys files not available on this filesystem; skipping test")
}
period, err := ReadCPUPeriod("")
if err != nil {
t.Fatal(err)
Expand All @@ -91,6 +101,9 @@ func TestReadCPUPeriod(t *testing.T) {
}

func TestReadCPUQuota(t *testing.T) {
if sysGone(t) {
t.Skip("/sys files not available on this filesystem; skipping test")
}
quota, err := ReadCPUQuota("")
if err != nil {
t.Fatal(err)
Expand All @@ -101,6 +114,9 @@ func TestReadCPUQuota(t *testing.T) {
}

func TestReadCPUShares(t *testing.T) {
if sysGone(t) {
t.Skip("/sys files not available on this filesystem; skipping test")
}
shares, err := ReadCPUShares("")
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions procstats/linux/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package linux

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)

func readFile(path string) string {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
check(err)
return string(b)
}
Expand Down
4 changes: 2 additions & 2 deletions procstats/linux/memory_linux.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package linux

import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -32,7 +32,7 @@ func readProcCGroupMemoryLimit(cgroups ProcCGroup) (limit uint64) {
func readMemoryCGroupMemoryLimit(cgroup CGroup) (limit uint64) {
limit = unlimitedMemoryLimit // default value if something doesn't work

if b, err := ioutil.ReadFile(readMemoryCGroupMemoryLimitFilePath(cgroup.Path)); err == nil {
if b, err := os.ReadFile(readMemoryCGroupMemoryLimitFilePath(cgroup.Path)); err == nil {
if v, err := strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64); err == nil {
limit = v
}
Expand Down
3 changes: 3 additions & 0 deletions procstats/linux/memory_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
)

func TestReadMemoryLimit(t *testing.T) {
if sysGone(t) {
t.Skip("/sys files not available on this filesystem; skipping test")
}
if limit, err := ReadMemoryLimit(os.Getpid()); err != nil {
t.Error(err)

Expand Down

0 comments on commit cf67f29

Please sign in to comment.