|
@@ -1,6 +1,8 @@
|
|
|
package gupta
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
"testing"
|
|
@@ -8,8 +10,8 @@ import (
|
|
|
|
|
|
func TestFreeMem(t *testing.T) {
|
|
|
m := Memory{}
|
|
|
- m.free = uint64(1200)
|
|
|
- got := m.Free()
|
|
|
+ m.Free = uint64(1200)
|
|
|
+ got := m.Free
|
|
|
want := uint64(1200)
|
|
|
name := "free"
|
|
|
if got != want {
|
|
@@ -18,8 +20,8 @@ func TestFreeMem(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestTotalMem(t *testing.T) {
|
|
|
- m := Memory{total: uint64(2400)}
|
|
|
- got := m.Total()
|
|
|
+ m := Memory{Total: uint64(2400)}
|
|
|
+ got := m.Total
|
|
|
want := uint64(2400)
|
|
|
name := "total"
|
|
|
if got != want {
|
|
@@ -28,7 +30,7 @@ func TestTotalMem(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestUsedMem(t *testing.T) {
|
|
|
- m := Memory{total: uint64(2400), free: uint64(1200)}
|
|
|
+ m := Memory{Total: uint64(2400), Free: uint64(1200)}
|
|
|
got := m.Used()
|
|
|
fmt.Println(got)
|
|
|
want := uint64(1200)
|
|
@@ -42,10 +44,25 @@ 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)
|
|
|
- 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 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")
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|