gupta_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. LoadAvarage: 0.2,
  39. CPUUsage: &cpustat,
  40. MemoryUsage: &Memory{},
  41. }
  42. got := report.CPUUsage.CPUUsage()
  43. want := float64(68.00)
  44. name := "CPULoad"
  45. if got != want {
  46. t.Errorf("got %s %.2f want %.2f", name, got, want)
  47. }
  48. })
  49. t.Run("Test that CPUUsage calculates properly", func(t *testing.T) {
  50. cpustat = NewCPUStat("cpu 657046 84 232824 4025519 2874 0 40555 0 0 0",
  51. "cpu 657136 84 232952 4025695 2874 0 40555 0 0 0")
  52. got := fmt.Sprintf("%.2f", cpustat.CPUUsage())
  53. want := "55.33"
  54. name := "CPUUsate"
  55. if got != want {
  56. t.Errorf("got %s %s want %s", name, got, want)
  57. }
  58. })
  59. }
  60. func assert(t *testing.T, name string, got, want uint64) {
  61. if got != want {
  62. t.Errorf("got %s %v want %v", name, got, want)
  63. }
  64. }