1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package gupta
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "strings"
- "testing"
- )
- func TestFreeMem(t *testing.T) {
- m := Memory{}
- m.Free = uint64(1200)
- got := m.Free
- want := uint64(1200)
- name := "free"
- if got != want {
- t.Errorf("got %s %d want %d", name, got, want)
- }
- }
- func TestTotalMem(t *testing.T) {
- m := Memory{Total: uint64(2400)}
- got := m.Total
- want := uint64(2400)
- name := "total"
- if got != want {
- t.Errorf("got %s %d want %d", name, got, want)
- }
- }
- func TestUsedMem(t *testing.T) {
- m := Memory{Total: uint64(2400), Free: uint64(1200)}
- got := m.Used()
- fmt.Println(got)
- want := uint64(1200)
- name := "used"
- if got != want {
- t.Errorf("got %s %d want %d", name, got, want)
- }
- }
- func TestNewMemory(t *testing.T) {
- //r, _ := os.open("/proc/meminfo")
- r := strings.NewReader("MemTotal: 16080192 kB\nMemFree: 3617004 kB\nMemAvailable: 11258504 kB\nBuffers: 1814108 kB\n")
- mem := NewMemory(r)
- t.Run("Test that we can get total memory", func(t *testing.T) {
- got := mem.Total
- want := uint64(16080192)
- name := "memory total"
- if got != want {
- t.Errorf("got %s %d want %d", name, got, want)
- }
- })
- t.Run("Test that we can properly report Memory usage", func(t *testing.T) {
- var buff bytes.Buffer
- enc := json.NewEncoder(&buff)
- enc.Encode(&mem)
- report := buff.String()
- fmt.Println(report)
- reportFields := strings.Split(report, ":")
- fmt.Println(reportFields[1])
- if reportFields[1] != "\"16080192KB\",\"used\"" {
- t.Errorf("Encoding report failed")
- }
- })
- }
|