gupta_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. cpustat := NewCPUStat(procstatLine)
  34. t.Run("Test that gupta hast Report", func(t *testing.T) {
  35. // test we can get some report struct from gupta
  36. report := GuptaReport{
  37. cpuLoad: 0.2,
  38. cpuUsage: 0.2,
  39. memoryUsage: 70.0,
  40. }
  41. got := report.GetCPULoad()
  42. want := "0.20"
  43. name := "CPULoad"
  44. if got != want {
  45. t.Errorf("got %s %s want %s", name, got, want)
  46. }
  47. })
  48. t.Run("Test the CPUStat can read info", func(t *testing.T) {
  49. cpustat := CPUStat{}
  50. cpustat.ReadInfo([]string{
  51. "4705",
  52. "356",
  53. "584",
  54. "3699",
  55. "23",
  56. "23",
  57. "0",
  58. "0",
  59. "0",
  60. "0"})
  61. got := cpustat.user
  62. want := uint64(4705)
  63. name := "cpustat.user"
  64. assert(t, name, got, want)
  65. })
  66. t.Run("Test the NewCPUStat can create an instance", func(t *testing.T) {
  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. got := cpustat.TotalTime()
  74. want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
  75. cpustat.irq + cpustat.softirq + cpustat.steal
  76. name := "total time"
  77. assert(t, name, got, want)
  78. })
  79. t.Run("Test that IdleTime calculates properly", func(t *testing.T) {
  80. got := cpustat.IdleTime()
  81. want := uint64(3722)
  82. name := "idle time"
  83. assert(t, name, got, want)
  84. })
  85. t.Run("Test that CPUUsage calculates properly", func(t *testing.T) {
  86. got := cpustat.CPUUsage()
  87. want := uint64(5668)
  88. assert(t, "CPUUsage", got, want)
  89. })
  90. }
  91. func assert(t *testing.T, name string, got, want uint64) {
  92. if got != want {
  93. t.Errorf("got %s %v want %v", name, got, want)
  94. }
  95. }