gupta_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package gupta
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestHello(t *testing.T) {
  7. got := Hello()
  8. want := fmt.Sprintf("Welcome to gupta version %s", version)
  9. name := "welcome string"
  10. if got != want {
  11. t.Errorf("got %s %s want %s", name, got, want)
  12. }
  13. }
  14. func TestRunGupta(t *testing.T) {
  15. // test that we get CPU load
  16. got := make([]float64, 3)
  17. got[0], got[1], got[2] = Run(true, true, true, "", "")
  18. want := []float64{0.2, 0.2, 70.0}
  19. for i, v := range got {
  20. if v != want[i] {
  21. t.Errorf("got %v want %v", v, want[i])
  22. }
  23. }
  24. // here it makes sense to return some kind of data structure
  25. // with properties filled.
  26. // having a data structure it would be possible to format
  27. // it to json ...
  28. }
  29. func TestCPUStat(t *testing.T) {
  30. t.Run("Test that gupta hast Report", func(t *testing.T) {
  31. // test we can get some report struct from gupta
  32. report := GuptaReport{
  33. cpuLoad: 0.2,
  34. cpuUsage: 0.2,
  35. memoryUsage: 70.0,
  36. }
  37. got := report.GetCPULoad()
  38. want := "0.20"
  39. name := "CPULoad"
  40. if got != want {
  41. t.Errorf("got %s %s want %s", name, got, want)
  42. }
  43. })
  44. t.Run("Test the CPUStat can read info", func(t *testing.T) {
  45. cpustat := CPUStat{}
  46. cpustat.ReadInfo([]string{
  47. "4705",
  48. "356",
  49. "584",
  50. "3699",
  51. "23",
  52. "23",
  53. "0",
  54. "0",
  55. "0",
  56. "0"})
  57. got := cpustat.user
  58. want := uint64(4705)
  59. name := "cpustat.user"
  60. assert(t, name, got, want)
  61. })
  62. t.Run("Test the NewCPUStat can create an instance", func(t *testing.T) {
  63. // 0 1 2 3 4 5 6 7 8 9 10
  64. // cpu user nice system idle iowait irq softirq steal guest guest_nice
  65. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  66. cpustat := NewCPUStat(procstatLine)
  67. got := cpustat.user
  68. want := uint64(4705)
  69. name := "cpustat.user"
  70. assert(t, name, got, want)
  71. })
  72. t.Run("Test that Total CPUTime calculates properly", func(t *testing.T) {
  73. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  74. cpustat := NewCPUStat(procstatLine)
  75. got := cpustat.TotalTime()
  76. want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
  77. cpustat.irq + cpustat.softirq + cpustat.steal
  78. name := "total time"
  79. assert(t, name, got, want)
  80. })
  81. t.Run("Test that IdleTime calculates properly", func(t *testing.T) {
  82. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  83. cpustat := NewCPUStat(procstatLine)
  84. got := cpustat.IdleTime()
  85. want := uint64(3722)
  86. name := "idle time"
  87. assert(t, name, got, want)
  88. })
  89. }
  90. func assert(t *testing.T, name string, got, want uint64) {
  91. if got != want {
  92. t.Errorf("got %s %v want %v", name, got, want)
  93. }
  94. }