1
2
3
4
5
6
7
8
9
10
11
12
13
14 package blockdevice
15
16 import (
17 "os"
18 "reflect"
19 "testing"
20 )
21
22 const (
23 failMsgFormat = "%v, expected %v, actual %v"
24 procfsFixtures = "testdata/fixtures/proc"
25 sysfsFixtures = "testdata/fixtures/sys"
26 )
27
28 func TestDiskstats(t *testing.T) {
29 blockdevice, err := NewFS(procfsFixtures, sysfsFixtures)
30 if err != nil {
31 t.Fatalf("failed to access blockdevice fs: %v", err)
32 }
33 diskstats, err := blockdevice.ProcDiskstats()
34 if err != nil {
35 t.Fatal(err)
36 }
37 expectedNumOfDevices := 52
38 if len(diskstats) != expectedNumOfDevices {
39 t.Errorf(failMsgFormat, "Incorrect number of devices", expectedNumOfDevices, len(diskstats))
40 }
41 if diskstats[0].DeviceName != "ram0" {
42 t.Errorf(failMsgFormat, "Incorrect device name", "ram0", diskstats[0].DeviceName)
43 }
44 if diskstats[1].IoStatsCount != 14 {
45 t.Errorf(failMsgFormat, "Incorrect number of stats read", 14, diskstats[0].IoStatsCount)
46 }
47 if diskstats[24].WriteIOs != 28444756 {
48 t.Errorf(failMsgFormat, "Incorrect writes completed", 28444756, diskstats[24].WriteIOs)
49 }
50 if diskstats[48].DiscardTicks != 11130 {
51 t.Errorf(failMsgFormat, "Incorrect discard time", 11130, diskstats[48].DiscardTicks)
52 }
53 if diskstats[48].IoStatsCount != 18 {
54 t.Errorf(failMsgFormat, "Incorrect number of stats read", 18, diskstats[48].IoStatsCount)
55 }
56 if diskstats[49].IoStatsCount != 20 {
57 t.Errorf(failMsgFormat, "Incorrect number of stats read", 20, diskstats[50].IoStatsCount)
58 }
59 if diskstats[49].FlushRequestsCompleted != 127 {
60 t.Errorf(failMsgFormat, "Incorrect number of flash requests completed", 127, diskstats[50].FlushRequestsCompleted)
61 }
62 if diskstats[49].TimeSpentFlushing != 182 {
63 t.Errorf(failMsgFormat, "Incorrect time spend flushing", 182, diskstats[50].TimeSpentFlushing)
64 }
65 }
66
67 func TestBlockDevice(t *testing.T) {
68 blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
69 if err != nil {
70 t.Fatalf("failed to access blockdevice fs: %v", err)
71 }
72 devices, err := blockdevice.SysBlockDevices()
73 if err != nil {
74 t.Fatal(err)
75 }
76 expectedNumOfDevices := 8
77 if len(devices) != expectedNumOfDevices {
78 t.Fatalf(failMsgFormat, "Incorrect number of devices", expectedNumOfDevices, len(devices))
79 }
80 if devices[0] != "dm-0" {
81 t.Errorf(failMsgFormat, "Incorrect device name", "dm-0", devices[0])
82 }
83 device0stats, count, err := blockdevice.SysBlockDeviceStat(devices[0])
84 if err != nil {
85 t.Fatal(err)
86 }
87 if count != 11 {
88 t.Errorf(failMsgFormat, "Incorrect number of stats read", 11, count)
89 }
90 if device0stats.ReadIOs != 6447303 {
91 t.Errorf(failMsgFormat, "Incorrect read I/Os", 6447303, device0stats.ReadIOs)
92 }
93 if device0stats.WeightedIOTicks != 6088971 {
94 t.Errorf(failMsgFormat, "Incorrect time in queue", 6088971, device0stats.WeightedIOTicks)
95 }
96 device7stats, count, err := blockdevice.SysBlockDeviceStat(devices[7])
97 if count != 15 {
98 t.Errorf(failMsgFormat, "Incorrect number of stats read", 15, count)
99 }
100 if err != nil {
101 t.Fatal(err)
102 }
103 if device7stats.WriteSectors != 286915323 {
104 t.Errorf(failMsgFormat, "Incorrect write merges", 286915323, device7stats.WriteSectors)
105 }
106 if device7stats.DiscardTicks != 12 {
107 t.Errorf(failMsgFormat, "Incorrect discard ticks", 12, device7stats.DiscardTicks)
108 }
109 blockQueueStatExpected := BlockQueueStats{
110 AddRandom: 1,
111 DAX: 0,
112 DiscardGranularity: 0,
113 DiscardMaxHWBytes: 0,
114 DiscardMaxBytes: 0,
115 HWSectorSize: 512,
116 IOPoll: 0,
117 IOPollDelay: -1,
118 IOTimeout: 30000,
119 IOStats: 1,
120 LogicalBlockSize: 512,
121 MaxHWSectorsKB: 32767,
122 MaxIntegritySegments: 0,
123 MaxSectorsKB: 1280,
124 MaxSegments: 168,
125 MaxSegmentSize: 65536,
126 MinimumIOSize: 512,
127 NoMerges: 0,
128 NRRequests: 64,
129 OptimalIOSize: 0,
130 PhysicalBlockSize: 512,
131 ReadAHeadKB: 128,
132 Rotational: 1,
133 RQAffinity: 1,
134 SchedulerList: []string{"mq-deadline", "kyber", "bfq", "none"},
135 SchedulerCurrent: "bfq",
136 WriteCache: "write back",
137 WriteSameMaxBytes: 0,
138 WBTLatUSec: 75000,
139 ThrottleSampleTime: nil,
140 Zoned: "none",
141 NRZones: 0,
142 ChunkSectors: 0,
143 FUA: 0,
144 MaxDiscardSegments: 1,
145 WriteZeroesMaxBytes: 0,
146 }
147
148 blockQueueStat, err := blockdevice.SysBlockDeviceQueueStats(devices[7])
149 if err != nil {
150 t.Fatal(err)
151 }
152 if !reflect.DeepEqual(blockQueueStat, blockQueueStatExpected) {
153 t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", blockQueueStatExpected, blockQueueStat)
154 }
155 }
156
157 func TestBlockDmInfo(t *testing.T) {
158 blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
159 if err != nil {
160 t.Fatalf("failed to access blockdevice fs: %v", err)
161 }
162 devices, err := blockdevice.SysBlockDevices()
163 if err != nil {
164 t.Fatal(err)
165 }
166 dm0Info, err := blockdevice.SysBlockDeviceMapperInfo(devices[0])
167 if err != nil {
168 t.Fatal(err)
169 }
170
171 dm0InfoExpected := DeviceMapperInfo{
172 Name: "vg0--lv_root",
173 RqBasedSeqIOMergeDeadline: 0,
174 Suspended: 0,
175 UseBlkMQ: 0,
176 UUID: "LVM-3zSHSR5Nbf4j7g6auAAefWY2CMaX01theZYEvQyecVsm2WtX3iY5q51qq5dWWOq7",
177 }
178 if !reflect.DeepEqual(dm0Info, dm0InfoExpected) {
179 t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", dm0InfoExpected, dm0Info)
180 }
181
182 dm1Info, err := blockdevice.SysBlockDeviceMapperInfo(devices[1])
183 if err != nil {
184 if _, ok := err.(*os.PathError); ok {
185
186 if !os.IsNotExist(err) {
187 t.Fatal(err)
188 }
189 } else {
190 t.Fatal(err)
191 }
192 } else {
193 t.Fatal("SysBlockDeviceMapperInfo on sda was supposed to fail.")
194 }
195 dm1InfoExpected := DeviceMapperInfo{}
196 if !reflect.DeepEqual(dm1Info, dm1InfoExpected) {
197 t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", dm0InfoExpected, dm0Info)
198 }
199 }
200
201 func TestSysBlockDeviceUnderlyingDevices(t *testing.T) {
202 blockdevice, err := NewFS("testdata/fixtures/proc", "testdata/fixtures/sys")
203 if err != nil {
204 t.Fatalf("failed to access blockdevice fs: %v", err)
205 }
206 devices, err := blockdevice.SysBlockDevices()
207 if err != nil {
208 t.Fatal(err)
209 }
210
211 underlying0, err := blockdevice.SysBlockDeviceUnderlyingDevices(devices[0])
212 if err != nil {
213 t.Fatal(err)
214 }
215 underlying0Expected := UnderlyingDeviceInfo{
216 DeviceNames: []string{"sda"},
217 }
218 if !reflect.DeepEqual(underlying0, underlying0Expected) {
219 t.Errorf("Incorrect BlockQueueStat, expected: \n%+v, got: \n%+v", underlying0Expected, underlying0)
220 }
221 }
222
View as plain text