gupta_test.go 1.6 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. 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{
  44. "cpu",
  45. "4705",
  46. "356",
  47. "584",
  48. "3699",
  49. "23",
  50. "23",
  51. "0",
  52. "0",
  53. "0",
  54. "0"})
  55. if cpustat.user != 4705 {
  56. t.Errorf("got %v want %v", cpustat.user, 4705)
  57. }
  58. }
  59. func TestCalculateCPUUsage(t *testing.T) {
  60. // 0 1 2 3 4 5 6 7 8 9 10
  61. // cpu user nice system idle iowait irq softirq steal guest guest_nice
  62. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  63. cpustat := NewCPUStat(procstatLine)
  64. if cpustat.user != 4705 {
  65. t.Errorf("got %v want %v", cpustat.user, 4705)
  66. }
  67. }