gupta_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 TestCPUStat(t *testing.T) {
  15. // 0 1 2 3 4 5 6 7 8 9 10
  16. // cpu user nice system idle iowait irq softirq steal guest guest_nice
  17. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  18. procstatLineNew := "cpu 4875 356 584 3779 23 23 0 0 0 0"
  19. cpustat := NewCPUStat(procstatLine, procstatLineNew)
  20. t.Run("Test that gupta hast Report", func(t *testing.T) {
  21. // test we can get some report struct from gupta
  22. report := GuptaReport{
  23. LoadAvarage: 0.2,
  24. CPUUsage: &cpustat,
  25. MemoryUsage: &Memory{},
  26. }
  27. got := report.CPUUsage.CPUUsage()
  28. want := float64(68.00)
  29. name := "CPULoad"
  30. if got != want {
  31. t.Errorf("got %s %.2f want %.2f", name, got, want)
  32. }
  33. })
  34. t.Run("Test that CPUUsage calculates properly", func(t *testing.T) {
  35. cpustat = NewCPUStat("cpu 657046 84 232824 4025519 2874 0 40555 0 0 0",
  36. "cpu 657136 84 232952 4025695 2874 0 40555 0 0 0")
  37. got := fmt.Sprintf("%.2f", cpustat.CPUUsage())
  38. want := "55.33"
  39. name := "CPUUsate"
  40. if got != want {
  41. t.Errorf("got %s %s want %s", name, got, want)
  42. }
  43. })
  44. }
  45. func assert(t *testing.T, name string, got, want uint64) {
  46. if got != want {
  47. t.Errorf("got %s %v want %v", name, got, want)
  48. }
  49. }