sigar_interface_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package gosigar_test
  2. import (
  3. "os"
  4. "os/user"
  5. "path/filepath"
  6. "runtime"
  7. "strings"
  8. "testing"
  9. "time"
  10. . "notabug.org/themusicgod1/gosigar"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. const invalidPid = 666666
  14. func TestCpu(t *testing.T) {
  15. cpu := Cpu{}
  16. assert.NoError(t, cpu.Get())
  17. }
  18. func TestLoadAverage(t *testing.T) {
  19. avg := LoadAverage{}
  20. assert.NoError(t, skipNotImplemented(t, avg.Get(), "windows"))
  21. }
  22. func TestUptime(t *testing.T) {
  23. uptime := Uptime{}
  24. if assert.NoError(t, uptime.Get()) {
  25. assert.True(t, uptime.Length > 0, "Uptime (%f) must be positive", uptime.Length)
  26. }
  27. }
  28. func TestMem(t *testing.T) {
  29. mem := Mem{}
  30. if assert.NoError(t, mem.Get()) {
  31. assert.True(t, mem.Total > 0, "mem.Total (%d) must be positive", mem.Total)
  32. assert.True(t, (mem.Used+mem.Free) <= mem.Total,
  33. "mem.Used (%d) + mem.Free (%d) must <= mem.Total (%d)",
  34. mem.Used, mem.Free, mem.Total)
  35. }
  36. }
  37. func TestSwap(t *testing.T) {
  38. swap := Swap{}
  39. if assert.NoError(t, swap.Get()) {
  40. assert.True(t, (swap.Used+swap.Free) <= swap.Total,
  41. "swap.Used (%d) + swap.Free (%d) must <= swap.Total (%d)",
  42. swap.Used, swap.Free, swap.Total)
  43. }
  44. }
  45. func TestCpuList(t *testing.T) {
  46. cpuList := CpuList{}
  47. if assert.NoError(t, cpuList.Get()) {
  48. numCore := len(cpuList.List)
  49. numCpu := runtime.NumCPU()
  50. assert.True(t, numCore >= numCpu, "Number of cores (%d) >= number of logical CPUs (%d)",
  51. numCore, numCpu)
  52. }
  53. }
  54. func TestFileSystemList(t *testing.T) {
  55. fsList := FileSystemList{}
  56. if assert.NoError(t, fsList.Get()) {
  57. assert.True(t, len(fsList.List) > 0)
  58. }
  59. }
  60. func TestFileSystemUsage(t *testing.T) {
  61. root := "/"
  62. if runtime.GOOS == "windows" {
  63. root = `C:\`
  64. }
  65. fsusage := FileSystemUsage{}
  66. if assert.NoError(t, fsusage.Get(root)) {
  67. assert.True(t, fsusage.Total > 0)
  68. }
  69. assert.Error(t, fsusage.Get("T O T A L L Y B O G U S"))
  70. }
  71. func TestProcList(t *testing.T) {
  72. pids := ProcList{}
  73. if assert.NoError(t, pids.Get()) {
  74. assert.True(t, len(pids.List) > 2)
  75. }
  76. }
  77. func TestProcState(t *testing.T) {
  78. u, err := user.Current()
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. state := ProcState{}
  83. if assert.NoError(t, state.Get(os.Getppid())) {
  84. assert.Contains(t, []RunState{RunStateRun, RunStateSleep}, state.State)
  85. assert.Regexp(t, "go(.exe)?", state.Name)
  86. assert.Equal(t, u.Username, state.Username)
  87. assert.True(t, state.Ppid > 0, "ppid=%v is non-positive", state.Ppid)
  88. }
  89. assert.Error(t, state.Get(invalidPid))
  90. }
  91. func TestProcMem(t *testing.T) {
  92. mem := ProcMem{}
  93. assert.NoError(t, mem.Get(os.Getppid()))
  94. assert.Error(t, mem.Get(invalidPid))
  95. }
  96. func TestProcTime(t *testing.T) {
  97. procTime := ProcTime{}
  98. if assert.NoError(t, procTime.Get(os.Getppid())) {
  99. // Sanity check the start time of the "go test" process.
  100. delta := time.Now().Sub(time.Unix(0, int64(procTime.StartTime*uint64(time.Millisecond))))
  101. assert.True(t, delta > 0 && delta < 2*time.Minute, "ProcTime.StartTime differs by %v", delta)
  102. }
  103. assert.Error(t, procTime.Get(invalidPid))
  104. }
  105. func TestProcArgs(t *testing.T) {
  106. args := ProcArgs{}
  107. if assert.NoError(t, args.Get(os.Getppid())) {
  108. assert.NotEmpty(t, args.List)
  109. }
  110. }
  111. func TestProcEnv(t *testing.T) {
  112. env := &ProcEnv{}
  113. if assert.NoError(t, skipNotImplemented(t, env.Get(os.Getpid()), "windows", "openbsd")) {
  114. assert.True(t, len(env.Vars) > 0, "env is empty")
  115. for k, v := range env.Vars {
  116. assert.Equal(t, strings.TrimSpace(os.Getenv(k)), v)
  117. }
  118. }
  119. }
  120. func TestProcExe(t *testing.T) {
  121. exe := ProcExe{}
  122. if assert.NoError(t, skipNotImplemented(t, exe.Get(os.Getppid()), "windows")) {
  123. assert.Regexp(t, "go(.exe)?", filepath.Base(exe.Name))
  124. }
  125. }
  126. func skipNotImplemented(t testing.TB, err error, goos ...string) error {
  127. for _, os := range goos {
  128. if runtime.GOOS == os {
  129. if err == nil {
  130. t.Fatal("expected ErrNotImplemented")
  131. } else if IsNotImplemented(err) {
  132. t.Skipf("Skipping test on %s", runtime.GOOS)
  133. }
  134. break
  135. }
  136. }
  137. return err
  138. }