3
3
package cgroup
4
4
5
5
import (
6
+ "fmt"
6
7
"testing"
7
8
8
9
"github.com/influxdata/telegraf/testutil"
10
+ "github.com/stretchr/testify/assert"
9
11
"github.com/stretchr/testify/require"
12
+ "reflect"
10
13
)
11
14
12
15
var cg1 = & CGroup {
@@ -21,15 +24,32 @@ var cg1 = &CGroup{
21
24
},
22
25
}
23
26
27
+ func assertContainsFields (a * testutil.Accumulator , t * testing.T , measurement string , fieldSet []map [string ]interface {}) {
28
+ a .Lock ()
29
+ defer a .Unlock ()
30
+
31
+ numEquals := 0
32
+ for _ , p := range a .Metrics {
33
+ if p .Measurement == measurement {
34
+ for _ , fields := range fieldSet {
35
+ if reflect .DeepEqual (fields , p .Fields ) {
36
+ numEquals ++
37
+ }
38
+ }
39
+ }
40
+ }
41
+
42
+ if numEquals != len (fieldSet ) {
43
+ assert .Fail (t , fmt .Sprintf ("only %d of %d are equal" , numEquals , len (fieldSet )))
44
+ }
45
+ }
46
+
24
47
func TestCgroupStatistics_1 (t * testing.T ) {
25
48
var acc testutil.Accumulator
26
49
27
50
err := cg1 .Gather (& acc )
28
51
require .NoError (t , err )
29
52
30
- tags := map [string ]string {
31
- "path" : "testdata/memory" ,
32
- }
33
53
fields := map [string ]interface {}{
34
54
"memory.stat.cache" : 1739362304123123123 ,
35
55
"memory.stat.rss" : 1775325184 ,
@@ -42,8 +62,9 @@ func TestCgroupStatistics_1(t *testing.T) {
42
62
"memory.limit_in_bytes" : 223372036854771712 ,
43
63
"memory.use_hierarchy" : "12-781" ,
44
64
"notify_on_release" : 0 ,
65
+ "path" : "testdata/memory" ,
45
66
}
46
- acc . AssertContainsTaggedFields ( t , "cgroup" , fields , tags )
67
+ assertContainsFields ( & acc , t , "cgroup" , [] map [ string ] interface {}{ fields } )
47
68
}
48
69
49
70
// ======================================================================
@@ -59,16 +80,14 @@ func TestCgroupStatistics_2(t *testing.T) {
59
80
err := cg2 .Gather (& acc )
60
81
require .NoError (t , err )
61
82
62
- tags := map [string ]string {
63
- "path" : "testdata/cpu" ,
64
- }
65
83
fields := map [string ]interface {}{
66
84
"cpuacct.usage_percpu.0" : - 1452543795404 ,
67
85
"cpuacct.usage_percpu.1" : 1376681271659 ,
68
86
"cpuacct.usage_percpu.2" : 1450950799997 ,
69
87
"cpuacct.usage_percpu.3" : - 1473113374257 ,
88
+ "path" : "testdata/cpu" ,
70
89
}
71
- acc . AssertContainsTaggedFields ( t , "cgroup" , fields , tags )
90
+ assertContainsFields ( & acc , t , "cgroup" , [] map [ string ] interface {}{ fields } )
72
91
}
73
92
74
93
// ======================================================================
@@ -84,18 +103,16 @@ func TestCgroupStatistics_3(t *testing.T) {
84
103
err := cg3 .Gather (& acc )
85
104
require .NoError (t , err )
86
105
87
- tags := map [string ]string {
88
- "path" : "testdata/memory/group_1" ,
89
- }
90
106
fields := map [string ]interface {}{
91
107
"memory.limit_in_bytes" : 223372036854771712 ,
108
+ "path" : "testdata/memory/group_1" ,
92
109
}
93
- acc .AssertContainsTaggedFields (t , "cgroup" , fields , tags )
94
110
95
- tags = map [string ]string {
96
- "path" : "testdata/memory/group_2" ,
111
+ fieldsTwo := map [string ]interface {}{
112
+ "memory.limit_in_bytes" : 223372036854771712 ,
113
+ "path" : "testdata/memory/group_2" ,
97
114
}
98
- acc . AssertContainsTaggedFields ( t , "cgroup" , fields , tags )
115
+ assertContainsFields ( & acc , t , "cgroup" , [] map [ string ] interface {}{ fields , fieldsTwo } )
99
116
}
100
117
101
118
// ======================================================================
@@ -111,23 +128,22 @@ func TestCgroupStatistics_4(t *testing.T) {
111
128
err := cg4 .Gather (& acc )
112
129
require .NoError (t , err )
113
130
114
- tags := map [string ]string {
115
- "path" : "testdata/memory/group_1/group_1_1" ,
116
- }
117
131
fields := map [string ]interface {}{
118
132
"memory.limit_in_bytes" : 223372036854771712 ,
133
+ "path" : "testdata/memory/group_1/group_1_1" ,
119
134
}
120
- acc .AssertContainsTaggedFields (t , "cgroup" , fields , tags )
121
135
122
- tags = map [string ]string {
123
- "path" : "testdata/memory/group_1/group_1_2" ,
136
+ fieldsTwo := map [string ]interface {}{
137
+ "memory.limit_in_bytes" : 223372036854771712 ,
138
+ "path" : "testdata/memory/group_1/group_1_2" ,
124
139
}
125
- acc .AssertContainsTaggedFields (t , "cgroup" , fields , tags )
126
140
127
- tags = map [string ]string {
128
- "path" : "testdata/memory/group_2" ,
141
+ fieldsThree := map [string ]interface {}{
142
+ "memory.limit_in_bytes" : 223372036854771712 ,
143
+ "path" : "testdata/memory/group_2" ,
129
144
}
130
- acc .AssertContainsTaggedFields (t , "cgroup" , fields , tags )
145
+
146
+ assertContainsFields (& acc , t , "cgroup" , []map [string ]interface {}{fields , fieldsTwo , fieldsThree })
131
147
}
132
148
133
149
// ======================================================================
@@ -143,18 +159,16 @@ func TestCgroupStatistics_5(t *testing.T) {
143
159
err := cg5 .Gather (& acc )
144
160
require .NoError (t , err )
145
161
146
- tags := map [string ]string {
147
- "path" : "testdata/memory/group_1/group_1_1" ,
148
- }
149
162
fields := map [string ]interface {}{
150
163
"memory.limit_in_bytes" : 223372036854771712 ,
164
+ "path" : "testdata/memory/group_1/group_1_1" ,
151
165
}
152
- acc .AssertContainsTaggedFields (t , "cgroup" , fields , tags )
153
166
154
- tags = map [string ]string {
155
- "path" : "testdata/memory/group_2/group_1_1" ,
167
+ fieldsTwo := map [string ]interface {}{
168
+ "memory.limit_in_bytes" : 223372036854771712 ,
169
+ "path" : "testdata/memory/group_2/group_1_1" ,
156
170
}
157
- acc . AssertContainsTaggedFields ( t , "cgroup" , fields , tags )
171
+ assertContainsFields ( & acc , t , "cgroup" , [] map [ string ] interface {}{ fields , fieldsTwo } )
158
172
}
159
173
160
174
// ======================================================================
@@ -170,13 +184,11 @@ func TestCgroupStatistics_6(t *testing.T) {
170
184
err := cg6 .Gather (& acc )
171
185
require .NoError (t , err )
172
186
173
- tags := map [string ]string {
174
- "path" : "testdata/memory" ,
175
- }
176
187
fields := map [string ]interface {}{
177
188
"memory.usage_in_bytes" : 3513667584 ,
178
189
"memory.use_hierarchy" : "12-781" ,
179
190
"memory.kmem.limit_in_bytes" : 9223372036854771712 ,
191
+ "path" : "testdata/memory" ,
180
192
}
181
- acc . AssertContainsTaggedFields ( t , "cgroup" , fields , tags )
193
+ assertContainsFields ( & acc , t , "cgroup" , [] map [ string ] interface {}{ fields } )
182
194
}
0 commit comments