gupta_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 TestGetCPUUsage(t *testing.T) {
  42. cpustat := CPUStat{}
  43. cpustat.ReadInfo([]string{
  44. "4705",
  45. "356",
  46. "584",
  47. "3699",
  48. "23",
  49. "23",
  50. "0",
  51. "0",
  52. "0",
  53. "0"})
  54. if cpustat.User != 4705 {
  55. t.Errorf("Got cpustat.User %v want %v", cpustat.User, 4705)
  56. }
  57. }
  58. func TestNewCPUUsage(t *testing.T) {
  59. // 0 1 2 3 4 5 6 7 8 9 10
  60. // cpu user nice system idle iowait irq softirq steal guest guest_nice
  61. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  62. cpustat := NewCPUStat(procstatLine)
  63. if cpustat.User != 4705 {
  64. t.Errorf("got %v want %v", cpustat.User, 4705)
  65. }
  66. }
  67. func TestTotalCPUTime(t *testing.T) {
  68. procstatLine := "cpu 4705 356 584 3699 23 23 0 0 0 0"
  69. cpustat := NewCPUStat(procstatLine)
  70. got := cpustat.TotalTime()
  71. want := cpustat.User + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
  72. cpustat.irq + cpustat.softirq + cpustat.steal
  73. if got != want {
  74. t.Errorf("got total time %v want %v", got, want)
  75. }
  76. }