forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskio_test.go
127 lines (120 loc) · 2.77 KB
/
diskio_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
package diskio
import (
"testing"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/influxdata/telegraf/testutil"
"github.com/shirou/gopsutil/disk"
"github.com/stretchr/testify/require"
)
func TestDiskIO(t *testing.T) {
type Result struct {
stats map[string]disk.IOCountersStat
err error
}
type Metric struct {
tags map[string]string
fields map[string]interface{}
}
tests := []struct {
name string
devices []string
result Result
err error
metrics []Metric
}{
{
name: "minimal",
result: Result{
stats: map[string]disk.IOCountersStat{
"sda": {
ReadCount: 888,
WriteCount: 5341,
ReadBytes: 100000,
WriteBytes: 200000,
ReadTime: 7123,
WriteTime: 9087,
MergedReadCount: 11,
MergedWriteCount: 12,
Name: "sda",
IoTime: 123552,
SerialNumber: "ab-123-ad",
},
},
err: nil,
},
err: nil,
metrics: []Metric{
{
tags: map[string]string{
"name": "sda",
"serial": "ab-123-ad",
},
fields: map[string]interface{}{
"reads": uint64(888),
"writes": uint64(5341),
"read_bytes": uint64(100000),
"write_bytes": uint64(200000),
"read_time": uint64(7123),
"write_time": uint64(9087),
"io_time": uint64(123552),
"weighted_io_time": uint64(0),
"iops_in_progress": uint64(0),
"merged_reads": uint64(11),
"merged_writes": uint64(12),
},
},
},
},
{
name: "glob device",
devices: []string{"sd*"},
result: Result{
stats: map[string]disk.IOCountersStat{
"sda": {
Name: "sda",
ReadCount: 42,
},
"vda": {
Name: "vda",
ReadCount: 42,
},
},
err: nil,
},
err: nil,
metrics: []Metric{
{
tags: map[string]string{
"name": "sda",
"serial": "unknown",
},
fields: map[string]interface{}{
"reads": uint64(42),
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var mps system.MockPS
mps.On("DiskIO").Return(tt.result.stats, tt.result.err)
var acc testutil.Accumulator
diskio := &DiskIO{
Log: testutil.Logger{},
ps: &mps,
Devices: tt.devices,
}
err := diskio.Gather(&acc)
require.Equal(t, tt.err, err)
for _, metric := range tt.metrics {
for k, v := range metric.fields {
require.True(t, acc.HasPoint("diskio", metric.tags, k, v),
"missing point: diskio %v %q: %v", metric.tags, k, v)
}
}
require.Equal(t, len(tt.metrics), int(acc.NMetrics()), "unexpected number of metrics")
require.True(t, mps.AssertExpectations(t))
})
}
}