gupta_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if got != want {
  10. t.Errorf("got %q want %q", got, want)
  11. }
  12. }
  13. func TestRunGupta(t *testing.T) {
  14. // test that we get CPU load
  15. got := make([]float64, 3)
  16. got[0], got[1], got[2] = Run(true, true, true, "", "")
  17. want := []float64{0.2, 0.2, 70.0}
  18. for i, v := range got {
  19. if v != want[i] {
  20. t.Errorf("got %v want %v", v, want[i])
  21. }
  22. }
  23. // here it makes sense to return some kind of data structure
  24. // with properties filled.
  25. // having a data structure it would be possible to format
  26. // it to json ...
  27. }
  28. func TestGuptaHasReport(t *testing.T) {
  29. // test we can get some report struct from gupta
  30. report := GuptaReport{
  31. cpuLoad: 0.2,
  32. cpuUsage: 0.2,
  33. memoryUsage: 70.0,
  34. }
  35. got := report.GetCPULoad()
  36. want := "0.20"
  37. if got != want {
  38. t.Errorf("got %v want %v", got, want)
  39. }
  40. }
  41. func TestGetCPULoad(t *testing.T) {
  42. cpustat := CPUStat{}
  43. cpustat.ReadInfo([]string{"4705",
  44. "356",
  45. "584",
  46. "3699",
  47. "23",
  48. "23",
  49. "0",
  50. "0",
  51. "0",
  52. "0"})
  53. if cpustat.user != 4705 {
  54. t.Errorf("got %v want %v", cpustat.user, 4705)
  55. }
  56. }
  57. func TestCalculateCPUUsage(t *testing.T) {
  58. // 0 1 2 3 4 5 6 7 8 9
  59. // user nice system idle iowait irq softirq steal guest guest_nice
  60. procstatLine := "4705 356 584 3699 23 23 0 0 0 0"
  61. cpustat := NewCPUStat(procstatLine)
  62. if cpustat.user != 4705 {
  63. t.Errorf("got %v want %v", cpustat.user, 4705)
  64. }
  65. }