gupta_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // 0 1 2 3 4 5 6 7 8 9 10
  31. // cpu user nice system idle iowait irq softirq steal guest guest_nice
  32. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  33. procstatLineNew := "cpu 4875 356 584 3779 23 23 0 0 0 0"
  34. cpustat := NewCPUStat(procstatLine, procstatLineNew)
  35. t.Run("Test that gupta hast Report", func(t *testing.T) {
  36. // test we can get some report struct from gupta
  37. report := GuptaReport{
  38. cpuLoad: 0.2,
  39. cpuUsage: 0.2,
  40. memoryUsage: 70.0,
  41. }
  42. got := report.GetCPULoad()
  43. want := "0.20"
  44. name := "CPULoad"
  45. if got != want {
  46. t.Errorf("got %s %s want %s", name, got, want)
  47. }
  48. })
  49. t.Run("Test the CPUStat can read info", func(t *testing.T) {
  50. cpustat := CPUStat{}
  51. cpustat.ReadInfo([]string{
  52. "4705",
  53. "356",
  54. "584",
  55. "3699",
  56. "23",
  57. "23",
  58. "0",
  59. "0",
  60. "0",
  61. "0"})
  62. got := cpustat.user
  63. want := uint64(4705)
  64. name := "cpustat.user"
  65. assert(t, name, got, want)
  66. })
  67. t.Run("Test the NewCPUStat can create an instance", func(t *testing.T) {
  68. got := cpustat.user
  69. want := uint64(4705)
  70. name := "cpustat.user"
  71. assert(t, name, got, want)
  72. })
  73. t.Run("Test that Total CPUTime calculates properly", func(t *testing.T) {
  74. got := cpustat.TotalTime()
  75. want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
  76. cpustat.irq + cpustat.softirq + cpustat.steal
  77. name := "total time"
  78. assert(t, name, got, want)
  79. })
  80. t.Run("Test that IdleTime calculates properly", func(t *testing.T) {
  81. got := cpustat.IdleTime()
  82. want := uint64(3722)
  83. name := "idle time"
  84. assert(t, name, got, want)
  85. })
  86. t.Run("Test that CPUUsage calculates properly", func(t *testing.T) {
  87. cpustat = NewCPUStat("cpu 657046 84 232824 4025519 2874 0 40555 0 0 0",
  88. "cpu 657136 84 232952 4025695 2874 0 40555 0 0 0")
  89. got := fmt.Sprintf("%.2f", cpustat.CPUUsage())
  90. want := "55.33"
  91. name := "CPUUsate"
  92. if got != want {
  93. t.Errorf("got %s %s want %s", name, got, want)
  94. }
  95. })
  96. }
  97. func assert(t *testing.T, name string, got, want uint64) {
  98. if got != want {
  99. t.Errorf("got %s %v want %v", name, got, want)
  100. }
  101. }