forked from shirou/gopsutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_linux_test.go
165 lines (150 loc) · 3.83 KB
/
mem_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//go:build linux
// +build linux
package mem
import (
"path/filepath"
"reflect"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVirtualMemoryEx(t *testing.T) {
v, err := VirtualMemoryEx()
if err != nil {
t.Error(err)
}
t.Log(v)
}
var virtualMemoryTests = []struct {
mockedRootFS string
stat *VirtualMemoryStat
}{
{
"intelcorei5", &VirtualMemoryStat{
Total: 16502300672,
Available: 11495358464,
Used: 3437277184,
UsedPercent: 20.82907863769651,
Free: 8783491072,
Active: 4347392000,
Inactive: 2938834944,
Wired: 0,
Laundry: 0,
Buffers: 212496384,
Cached: 4069036032,
WriteBack: 0,
Dirty: 176128,
WriteBackTmp: 0,
Shared: 1222402048,
Slab: 253771776,
Sreclaimable: 186470400,
Sunreclaim: 67301376,
PageTables: 65241088,
SwapCached: 0,
CommitLimit: 16509730816,
CommittedAS: 12360818688,
HighTotal: 0,
HighFree: 0,
LowTotal: 0,
LowFree: 0,
SwapTotal: 8258580480,
SwapFree: 8258580480,
Mapped: 1172627456,
VmallocTotal: 35184372087808,
VmallocUsed: 0,
VmallocChunk: 0,
HugePagesTotal: 0,
HugePagesFree: 0,
HugePagesRsvd: 0,
HugePagesSurp: 0,
HugePageSize: 2097152,
},
},
{
"issue1002", &VirtualMemoryStat{
Total: 260579328,
Available: 215199744,
Used: 34328576,
UsedPercent: 13.173944481121694,
Free: 124506112,
Active: 108785664,
Inactive: 8581120,
Wired: 0,
Laundry: 0,
Buffers: 4915200,
Cached: 96829440,
WriteBack: 0,
Dirty: 0,
WriteBackTmp: 0,
Shared: 0,
Slab: 9293824,
Sreclaimable: 2764800,
Sunreclaim: 6529024,
PageTables: 405504,
SwapCached: 0,
CommitLimit: 130289664,
CommittedAS: 25567232,
HighTotal: 134217728,
HighFree: 67784704,
LowTotal: 126361600,
LowFree: 56721408,
SwapTotal: 0,
SwapFree: 0,
Mapped: 38793216,
VmallocTotal: 1996488704,
VmallocUsed: 0,
VmallocChunk: 0,
HugePagesTotal: 0,
HugePagesFree: 0,
HugePagesRsvd: 0,
HugePagesSurp: 0,
HugePageSize: 0,
},
},
}
func TestVirtualMemoryLinux(t *testing.T) {
for _, tt := range virtualMemoryTests {
t.Run(tt.mockedRootFS, func(t *testing.T) {
t.Setenv("HOST_PROC", filepath.Join("testdata/linux/virtualmemory/", tt.mockedRootFS, "proc"))
stat, err := VirtualMemory()
skipIfNotImplementedErr(t, err)
if err != nil {
t.Errorf("error %v", err)
}
if !reflect.DeepEqual(stat, tt.stat) {
t.Errorf("got: %+v\nwant: %+v", stat, tt.stat)
}
})
}
}
const validFile = `Filename Type Size Used Priority
/dev/dm-2 partition 67022844 490788 -2
/swapfile file 2 1 -3
`
const invalidFile = `INVALID Type Size Used Priority
/dev/dm-2 partition 67022844 490788 -2
/swapfile file 1048572 0 -3
`
func TestParseSwapsFile_ValidFile(t *testing.T) {
assert := assert.New(t)
stats, err := parseSwapsFile(strings.NewReader(validFile))
assert.NoError(err)
assert.Equal(*stats[0], SwapDevice{
Name: "/dev/dm-2",
UsedBytes: 502566912,
FreeBytes: 68128825344,
})
assert.Equal(*stats[1], SwapDevice{
Name: "/swapfile",
UsedBytes: 1024,
FreeBytes: 1024,
})
}
func TestParseSwapsFile_InvalidFile(t *testing.T) {
_, err := parseSwapsFile(strings.NewReader(invalidFile))
assert.Error(t, err)
}
func TestParseSwapsFile_EmptyFile(t *testing.T) {
_, err := parseSwapsFile(strings.NewReader(""))
assert.Error(t, err)
}