gupta_test.go 1.6 KB

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