memory_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package gupta
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. )
  8. func TestFreeMem(t *testing.T) {
  9. m := Memory{}
  10. m.Free = uint64(1200)
  11. got := m.Free
  12. want := uint64(1200)
  13. name := "free"
  14. if got != want {
  15. t.Errorf("got %s %d want %d", name, got, want)
  16. }
  17. }
  18. func TestTotalMem(t *testing.T) {
  19. m := Memory{Total: uint64(2400)}
  20. got := m.Total
  21. want := uint64(2400)
  22. name := "total"
  23. if got != want {
  24. t.Errorf("got %s %d want %d", name, got, want)
  25. }
  26. }
  27. func TestUsedMem(t *testing.T) {
  28. m := Memory{Total: uint64(2400), Free: uint64(1200)}
  29. got := m.Used()
  30. want := uint64(1200)
  31. name := "used"
  32. if got != want {
  33. t.Errorf("got %s %d want %d", name, got, want)
  34. }
  35. }
  36. func TestNewMemory(t *testing.T) {
  37. //r, _ := os.open("/proc/meminfo")
  38. r := strings.NewReader("MemTotal: 16080192 kB\nMemFree: 3617004 kB\nMemAvailable: 11258504 kB\nBuffers: 1814108 kB\n")
  39. mem := NewMemory(r)
  40. t.Run("Test that we can get total memory", func(t *testing.T) {
  41. got := mem.Total
  42. want := uint64(16080192)
  43. name := "memory total"
  44. if got != want {
  45. t.Errorf("got %s %d want %d", name, got, want)
  46. }
  47. })
  48. t.Run("Test that we can properly report Memory usage", func(t *testing.T) {
  49. var buff bytes.Buffer
  50. enc := json.NewEncoder(&buff)
  51. enc.Encode(&mem)
  52. report := buff.String()
  53. reportFields := strings.Split(report, ":")
  54. if reportFields[2] != "\"16080192KB\",\"used\"" {
  55. t.Errorf("Encoding report failed")
  56. }
  57. })
  58. }