memory_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. assertInt64(t, "free", got, uint64(1200))
  13. }
  14. func TestTotalMem(t *testing.T) {
  15. m := Memory{Total: uint64(2400)}
  16. got := m.Total
  17. assertInt64(t, "total", got, uint64(2400))
  18. }
  19. func TestUsedMem(t *testing.T) {
  20. m := Memory{Total: uint64(2400), Free: uint64(1200)}
  21. got := m.Used()
  22. assertInt64(t, "used", got, uint64(1200))
  23. }
  24. func TestNewMemory(t *testing.T) {
  25. //r, _ := os.open("/proc/meminfo")
  26. r := strings.NewReader("MemTotal: 16080192 kB\nMemFree: 3617004 kB\nMemAvailable: 11258504 kB\nBuffers: 1814108 kB\n")
  27. mem := NewMemory(r)
  28. t.Run("Test that we can get total memory", func(t *testing.T) {
  29. got := mem.Total
  30. assertInt64(t, "memory total", got, uint64(16080192))
  31. })
  32. t.Run("Test that we can properly report Memory usage", func(t *testing.T) {
  33. var buff bytes.Buffer
  34. enc := json.NewEncoder(&buff)
  35. enc.Encode(&mem)
  36. report := buff.String()
  37. reportFields := strings.Split(report, ":")
  38. if reportFields[2] != "\"16080192KB\",\"used\"" {
  39. t.Errorf("Encoding report failed")
  40. }
  41. })
  42. }