memory_test.go 1.5 KB

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