1package mem
2
3import (
4	"os"
5	"path/filepath"
6	"reflect"
7	"testing"
8)
9
10func TestVirtualMemoryEx(t *testing.T) {
11	v, err := VirtualMemoryEx()
12	if err != nil {
13		t.Error(err)
14	}
15
16	t.Log(v)
17}
18
19var virtualMemoryTests = []struct {
20	mockedRootFS string
21	stat         *VirtualMemoryStat
22}{
23	{"intelcorei5", &VirtualMemoryStat{
24		Total:          16502300672,
25		Available:      11495358464,
26		Used:           3437277184,
27		UsedPercent:    20.82907863769651,
28		Free:           8783491072,
29		Active:         4347392000,
30		Inactive:       2938834944,
31		Wired:          0,
32		Laundry:        0,
33		Buffers:        212496384,
34		Cached:         4069036032,
35		Writeback:      0,
36		Dirty:          176128,
37		WritebackTmp:   0,
38		Shared:         1222402048,
39		Slab:           253771776,
40		SReclaimable:   186470400,
41		SUnreclaim:     67301376,
42		PageTables:     65241088,
43		SwapCached:     0,
44		CommitLimit:    16509730816,
45		CommittedAS:    12360818688,
46		HighTotal:      0,
47		HighFree:       0,
48		LowTotal:       0,
49		LowFree:        0,
50		SwapTotal:      8258580480,
51		SwapFree:       8258580480,
52		Mapped:         1172627456,
53		VMallocTotal:   35184372087808,
54		VMallocUsed:    0,
55		VMallocChunk:   0,
56		HugePagesTotal: 0,
57		HugePagesFree:  0,
58		HugePageSize:   2097152},
59	},
60	{"issue1002", &VirtualMemoryStat{
61		Total:          260579328,
62		Available:      215199744,
63		Used:           34328576,
64		UsedPercent:    13.173944481121694,
65		Free:           124506112,
66		Active:         108785664,
67		Inactive:       8581120,
68		Wired:          0,
69		Laundry:        0,
70		Buffers:        4915200,
71		Cached:         96829440,
72		Writeback:      0,
73		Dirty:          0,
74		WritebackTmp:   0,
75		Shared:         0,
76		Slab:           9293824,
77		SReclaimable:   2764800,
78		SUnreclaim:     6529024,
79		PageTables:     405504,
80		SwapCached:     0,
81		CommitLimit:    130289664,
82		CommittedAS:    25567232,
83		HighTotal:      134217728,
84		HighFree:       67784704,
85		LowTotal:       126361600,
86		LowFree:        56721408,
87		SwapTotal:      0,
88		SwapFree:       0,
89		Mapped:         38793216,
90		VMallocTotal:   1996488704,
91		VMallocUsed:    0,
92		VMallocChunk:   0,
93		HugePagesTotal: 0,
94		HugePagesFree:  0,
95		HugePageSize:   0},
96	},
97}
98
99func TestVirtualMemoryLinux(t *testing.T) {
100	origProc := os.Getenv("HOST_PROC")
101	defer os.Setenv("HOST_PROC", origProc)
102
103	for _, tt := range virtualMemoryTests {
104		t.Run(tt.mockedRootFS, func(t *testing.T) {
105			os.Setenv("HOST_PROC", filepath.Join("testdata/linux/virtualmemory/", tt.mockedRootFS, "proc"))
106
107			stat, err := VirtualMemory()
108			skipIfNotImplementedErr(t, err)
109			if err != nil {
110				t.Errorf("error %v", err)
111			}
112			if !reflect.DeepEqual(stat, tt.stat) {
113				t.Errorf("got: %+v\nwant: %+v", stat, tt.stat)
114			}
115		})
116	}
117}
118