5
5
% % Copyright (c) 2007-2023 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries.
6
6
% %
7
7
8
- -module (seshat_test ).
8
+ -module (seshat_SUITE ).
9
9
-include_lib (" eunit/include/eunit.hrl" ).
10
10
11
- setup () ->
12
- {ok , _ } = application :ensure_all_started (seshat ).
11
+ -compile (nowarn_export_all ).
12
+ -compile (export_all ).
13
+
14
+ all () ->
15
+ [overview ,
16
+ counters_with_persistent_term_field_spec ,
17
+ format_group ,
18
+ format_one ,
19
+ format_with_many_labels ,
20
+ format_ratio ,
21
+ format_time_metrics ,
22
+ format_selected_metrics ,
23
+ text_format_selected_metrics ,
24
+ invalid_fields ].
25
+
26
+ init_per_suite (Config ) ->
27
+ {ok , _ } = application :ensure_all_started (seshat ),
28
+ Config .
29
+
30
+ end_per_suite (_Config ) ->
31
+ ok = application :stop (seshat ),
32
+ ok .
13
33
14
- cleanup (_ ) ->
15
- ok = application :stop (seshat ).
34
+ init_per_testcase (TestCaseName , Config ) ->
35
+ Group = TestCaseName ,
36
+ seshat :new_group (Group ),
37
+ [{group , Group } | Config ].
16
38
17
- test_suite_test_ () ->
18
- {foreach ,
19
- fun setup /0 ,
20
- fun cleanup /1 ,
21
- [ fun overview /0 ,
22
- fun counters_with_persistent_term_field_spec /0 ,
23
- fun format_group /0 ,
24
- fun format_one /0 ,
25
- fun format_with_many_labels /0 ,
26
- fun format_ratio /0 ,
27
- fun format_time_metrics /0 ,
28
- fun format_selected_metrics /0 ,
29
- fun text_format_selected_metrics /0 ,
30
- fun invalid_fields /0 ]}.
39
+ end_per_testcase (TestCaseName , _Config ) ->
40
+ seshat :delete_group (TestCaseName ),
41
+ ok .
31
42
32
- overview () ->
43
+ % % Test cases
44
+ overview (_Config ) ->
33
45
Group = ? FUNCTION_NAME ,
34
46
Counters = [
35
- {
36
- carrots_eaten_total , 1 , counter ,
37
- " Total number of carrots eaten on a meal"
38
- },
39
- {
40
- holes_dug_total , 2 , counter ,
41
- " Total number of holes dug in an afternoon"
42
- }
47
+ {carrots_eaten_total , 1 , counter , " Total number of carrots eaten on a meal" },
48
+ {holes_dug_total , 2 , counter , " Total number of holes dug in an afternoon" }
43
49
],
44
- seshat :new_group (Group ),
45
50
seshat :new (Group , " rabbit" , Counters ),
46
51
set_value (Group , " rabbit" , carrots_eaten_total , 3 ),
47
52
set_value (Group , " rabbit" , holes_dug_total , 1 ),
48
53
Overview = seshat :overview (Group ),
49
54
? assertEqual (
50
- #{" rabbit" => #{carrots_eaten_total => 3 ,
51
- holes_dug_total => 1 }},
55
+ #{" rabbit" => #{carrots_eaten_total => 3 , holes_dug_total => 1 }},
52
56
Overview ),
53
-
54
- ? assertMatch (#{carrots_eaten_total := 3 ,
55
- holes_dug_total := 1 },
57
+ ? assertMatch (#{carrots_eaten_total := 3 , holes_dug_total := 1 },
56
58
seshat :overview (Group , " rabbit" )),
57
-
58
59
? assertMatch (#{holes_dug_total := 1 },
59
60
seshat :counters (Group , " rabbit" , [holes_dug_total ])),
60
61
ok .
61
62
62
- counters_with_persistent_term_field_spec () ->
63
+ counters_with_persistent_term_field_spec (_Config ) ->
63
64
Group = ? FUNCTION_NAME ,
64
65
Counters = [
65
- {
66
- carrots_eaten_total , 1 , counter ,
67
- " Total number of carrots eaten on a meal"
68
- },
69
- {
70
- holes_dug_total , 2 , counter ,
71
- " Total number of holes dug in an afternoon"
72
- }
66
+ {carrots_eaten_total , 1 , counter , " Total number of carrots eaten on a meal" },
67
+ {holes_dug_total , 2 , counter , " Total number of holes dug in an afternoon" }
73
68
],
74
-
75
69
persistent_term :put (pets_field_spec , Counters ),
76
- seshat :new_group (Group ),
77
70
seshat :new (Group , " rabbit" , {persistent_term , pets_field_spec }),
78
71
set_value (Group , " rabbit" , carrots_eaten_total , 3 ),
79
72
set_value (Group , " rabbit" , holes_dug_total , 1 ),
80
73
Overview = seshat :overview (Group ),
81
74
? assertEqual (
82
- #{" rabbit" => #{carrots_eaten_total => 3 ,
83
- holes_dug_total => 1 }},
75
+ #{" rabbit" => #{carrots_eaten_total => 3 , holes_dug_total => 1 }},
84
76
Overview ),
85
-
86
- ? assertMatch (#{carrots_eaten_total := 3 ,
87
- holes_dug_total := 1 },
77
+ ? assertMatch (#{carrots_eaten_total := 3 , holes_dug_total := 1 },
88
78
seshat :overview (Group , " rabbit" )),
89
-
90
79
? assertMatch (#{holes_dug_total := 1 },
91
80
seshat :counters (Group , " rabbit" , [holes_dug_total ])),
92
-
81
+ persistent_term : erase ( pets_field_spec ),
93
82
ok .
94
83
95
- format_group () ->
84
+ format_group (_Config ) ->
96
85
Group = ? FUNCTION_NAME ,
97
86
Counters = [{reads , 1 , counter , " Total reads" }],
98
- seshat :new_group (Group ),
99
87
seshat :new (Group , widget1 , Counters , #{component => widget1 }),
100
88
seshat :new (Group , widget2 , Counters , #{component => widget2 }),
101
89
seshat :new (Group , screw , Counters ), % no labels, will be omitted
@@ -107,10 +95,9 @@ format_group() ->
107
95
? assertEqual (ExpectedMap , Result ),
108
96
ok .
109
97
110
- format_one () ->
98
+ format_one (_Config ) ->
111
99
Group = ? FUNCTION_NAME ,
112
100
Counters = [{reads , 1 , counter , " Total reads" }],
113
- seshat :new_group (Group ),
114
101
seshat :new (Group , widget1 , Counters , #{component => widget1 }),
115
102
seshat :new (Group , widget2 , Counters , #{component => widget2 }),
116
103
Result = seshat :format_one (Group , widget2 ),
@@ -120,10 +107,9 @@ format_one() ->
120
107
? assertEqual (ExpectedMap , Result ),
121
108
ok .
122
109
123
- format_with_many_labels () ->
110
+ format_with_many_labels (_Config ) ->
124
111
Group = ? FUNCTION_NAME ,
125
112
Counters = [{reads , 1 , counter , " Total reads" }],
126
- seshat :new_group (Group ),
127
113
seshat :new (Group , widget1 , Counters , #{component => " widget1" , status => up }),
128
114
seshat :new (Group , widget2 , Counters , #{component => " widget2" , status => down }),
129
115
set_value (Group , widget1 , reads , 1 ),
@@ -136,14 +122,13 @@ format_with_many_labels() ->
136
122
? assertEqual (ExpectedMap , Result ),
137
123
ok .
138
124
139
- format_selected_metrics () ->
125
+ format_selected_metrics (_Config ) ->
140
126
Group = ? FUNCTION_NAME ,
141
127
Counters = [
142
128
{reads , 1 , counter , " Total reads" },
143
129
{writes , 2 , counter , " Total writes" },
144
130
{lookups , 3 , counter , " Total lookups" }
145
131
],
146
- seshat :new_group (Group ),
147
132
seshat :new (Group , thing1 , Counters , #{component => " thing1" }),
148
133
seshat :new (Group , thing2 , Counters , #{component => " thing2" }),
149
134
Result = seshat :format (Group , [reads , writes ]),
@@ -158,29 +143,25 @@ format_selected_metrics() ->
158
143
? assertEqual (ExpectedMap , Result ),
159
144
ok .
160
145
161
- invalid_fields () ->
162
- Group = widgets ,
146
+ invalid_fields (_Config ) ->
147
+ Group = ? FUNCTION_NAME ,
163
148
Fields = [{reads , 1 , counter , " Total reads" },
164
- {writes , 3 , counter , " Total writes" }],
165
- seshat :new_group (Group ),
149
+ {writes , 3 , counter , " Total writes" }], % Index 2 is missing
166
150
? assertError (invalid_field_specification ,
167
151
seshat :new (Group , invalid_fields , Fields )),
168
-
169
152
ok .
170
153
171
- format_ratio () ->
154
+ format_ratio (_Config ) ->
172
155
Group = ? FUNCTION_NAME ,
173
156
Counters = [{pings , 1 , ratio , " Some ratio that happens to be 0%" },
174
157
{pongs , 2 , ratio , " Some ratio that happens to be 17%" },
175
158
{pangs , 3 , ratio , " Some ratio that happens to be 33%" },
176
159
{rings , 4 , ratio , " Some ratio that happens to be 100%" }],
177
- seshat :new_group (Group ),
178
160
seshat :new (Group , test_component , Counters , #{component => test }),
179
161
set_value (Group , test_component , pings , 0 ),
180
162
set_value (Group , test_component , pongs , 17 ),
181
163
set_value (Group , test_component , pangs , 33 ),
182
164
set_value (Group , test_component , rings , 100 ),
183
-
184
165
Result = seshat :format (Group ),
185
166
ExpectedMap = #{pings => #{type => gauge ,
186
167
help => " Some ratio that happens to be 0%" ,
@@ -197,58 +178,46 @@ format_ratio() ->
197
178
? assertEqual (ExpectedMap , Result ),
198
179
ok .
199
180
200
- format_time_metrics () ->
201
- Group = ? FUNCTION_NAME ,
202
- Counters = [
203
- {job_duration , 2 , time_s , " Job duration" },
204
- {short_latency , 3 , time_ms , " Short latency" },
205
- {long_latency , 1 , time_ms , " Request latency" }
206
- ],
207
- seshat :new_group (Group ),
208
- Labels = #{component => test },
209
- seshat :new (Group , test_component , Counters , Labels ),
210
-
211
- % Set values (1500 ms, 30 s, 5 ms)
212
- set_value (Group , test_component , job_duration , 30 ),
213
- set_value (Group , test_component , short_latency , 5 ),
214
- set_value (Group , test_component , long_latency , 1500 ),
215
-
216
- MapResult = seshat :format (Group ),
217
- ExpectedMap = #{
218
- job_duration => #{type => gauge ,
219
- help => " Job duration" ,
220
- values => #{Labels => 30.0 }},
221
- short_latency => #{type => gauge ,
222
- help => " Short latency" ,
223
- values => #{Labels => 0.005 }},
224
- long_latency => #{type => gauge ,
225
- help => " Request latency" ,
226
- values => #{Labels => 1.5 }}
227
- },
228
- ? assertEqual (ExpectedMap , MapResult ),
229
-
230
- Prefix = " myapp" ,
231
- MetricNames = [job_duration , short_latency , long_latency ], % Added new metric name
232
- TextResult = seshat :text_format (Group , Prefix , MetricNames ),
233
-
234
- ExpectedLines = [
235
- " # HELP myapp_job_duration_seconds Job duration" ,
236
- " # TYPE myapp_job_duration_seconds gauge" ,
237
- " myapp_job_duration_seconds{component=\" test\" } 30.0" ,
238
- " # HELP myapp_short_latency_seconds Short latency" ,
239
- " # TYPE myapp_short_latency_seconds gauge" ,
240
- " myapp_short_latency_seconds{component=\" test\" } 0.005" ,
241
- " # HELP myapp_long_latency_seconds Request latency" ,
242
- " # TYPE myapp_long_latency_seconds gauge" ,
243
- " myapp_long_latency_seconds{component=\" test\" } 1.5"
244
- ],
245
- ExpectedResult = list_to_binary (string :join (ExpectedLines , " \n " ) ++ " \n " ),
181
+ format_time_metrics (_Config ) ->
182
+ Group = ? FUNCTION_NAME ,
183
+ Counters = [
184
+ {job_duration , 2 , time_s , " Job duration" },
185
+ {short_latency , 3 , time_ms , " Short latency" },
186
+ {long_latency , 1 , time_ms , " Request latency" }
187
+ ],
188
+ Labels = #{component => test },
189
+ seshat :new (Group , test_component , Counters , Labels ),
190
+ set_value (Group , test_component , job_duration , 30 ),
191
+ set_value (Group , test_component , short_latency , 5 ),
192
+ set_value (Group , test_component , long_latency , 1500 ),
193
+ MapResult = seshat :format (Group ),
194
+ ExpectedMap = #{
195
+ job_duration => #{type => gauge , help => " Job duration" , values => #{Labels => 30.0 }},
196
+ short_latency => #{type => gauge , help => " Short latency" , values => #{Labels => 0.005 }},
197
+ long_latency => #{type => gauge , help => " Request latency" , values => #{Labels => 1.5 }}
198
+ },
199
+ ? assertEqual (ExpectedMap , MapResult ),
200
+ Prefix = " myapp" ,
201
+ MetricNames = [job_duration , short_latency , long_latency ],
202
+ TextResult = seshat :text_format (Group , Prefix , MetricNames ),
203
+ ExpectedLines = [
204
+ " # HELP myapp_job_duration_seconds Job duration" ,
205
+ " # TYPE myapp_job_duration_seconds gauge" ,
206
+ " myapp_job_duration_seconds{component=\" test\" } 30.0" ,
207
+ " # HELP myapp_short_latency_seconds Short latency" ,
208
+ " # TYPE myapp_short_latency_seconds gauge" ,
209
+ " myapp_short_latency_seconds{component=\" test\" } 0.005" ,
210
+ " # HELP myapp_long_latency_seconds Request latency" ,
211
+ " # TYPE myapp_long_latency_seconds gauge" ,
212
+ " myapp_long_latency_seconds{component=\" test\" } 1.5"
213
+ ],
214
+ ExpectedResult = list_to_binary (string :join (ExpectedLines , " \n " ) ++ " \n " ),
246
215
247
- ? assertEqual (ExpectedResult , TextResult ),
248
- ok .
216
+ assertEqualIgnoringOrder (ExpectedResult , TextResult ),
217
+ ok .
249
218
250
- text_format_selected_metrics () ->
251
- Group = widgets ,
219
+ text_format_selected_metrics (_Config ) ->
220
+ Group = ? FUNCTION_NAME ,
252
221
Counters = [
253
222
{reads , 1 , counter , " Total reads" },
254
223
{writes , 2 , counter , " Total writes" },
@@ -257,7 +226,6 @@ text_format_selected_metrics() ->
257
226
{duration , 5 , time_s , " Duration" },
258
227
{npc , 6 , gauge , " A metric we don't request in a call to text_format/3" }
259
228
],
260
- seshat :new_group (Group ),
261
229
seshat :new (Group , thing1 , Counters , #{component => " thing1" , version => " 1.2.3" }),
262
230
seshat :new (Group , thing2 , Counters , #{component => " thing2" , some_atom => atom_value }),
263
231
seshat :new (Group , thing3 , Counters , #{component => " thing3" , some_binary => <<" binary_value" >>}),
@@ -266,19 +234,19 @@ text_format_selected_metrics() ->
266
234
set_value (Group , thing1 , cached , 10 ),
267
235
set_value (Group , thing1 , latency , 5 ),
268
236
set_value (Group , thing1 , duration , 123 ),
269
- set_value (Group , thing1 , npc , 1 ), % to be ignored
237
+ set_value (Group , thing1 , npc , 1 ), % to be ignored
270
238
set_value (Group , thing2 , reads , 3 ),
271
239
set_value (Group , thing2 , writes , 4 ),
272
240
set_value (Group , thing2 , cached , 100 ),
273
241
set_value (Group , thing2 , latency , 6 ),
274
242
set_value (Group , thing2 , duration , 234 ),
275
- set_value (Group , thing2 , npc , 1 ), % to be ignored
243
+ set_value (Group , thing2 , npc , 1 ), % to be ignored
276
244
set_value (Group , thing3 , reads , 1234 ),
277
245
set_value (Group , thing3 , writes , 4321 ),
278
246
set_value (Group , thing3 , cached , 17 ),
279
247
set_value (Group , thing3 , latency , 7 ),
280
248
set_value (Group , thing3 , duration , 345 ),
281
- set_value (Group , thing3 , npc , 1 ), % to be ignored
249
+ set_value (Group , thing3 , npc , 1 ), % to be ignored
282
250
283
251
Result = seshat :text_format (Group , " acme" , [reads , writes , cached , latency , duration ]),
284
252
ExpectedLines = [
@@ -310,18 +278,18 @@ text_format_selected_metrics() ->
310
278
],
311
279
ExpectedResult = list_to_binary (string :join (ExpectedLines , " \n " ) ++ " \n " ),
312
280
313
- ? assertEqual (ExpectedResult , Result ),
281
+ assertEqualIgnoringOrder (ExpectedResult , Result ),
314
282
ok .
315
283
316
- % % test helpers
317
-
284
+ % % Helpers
318
285
set_value (Group , Id , Name , Value ) ->
319
- [{Id , CRef , FieldSpec , _Labels }] = ets :lookup (seshat_counters_server :get_table (Group ), Id ),
320
- Fields = resolve_fieldspec (FieldSpec ),
286
+ Table = seshat_counters_server :get_table (Group ),
287
+ [{Id , CRef , FieldSpec , _Labels }] = ets :lookup (Table , Id ),
288
+ Fields = seshat :resolve_fields_spec (FieldSpec ),
321
289
{Name , Index , _Type , _Help } = lists :keyfind (Name , 1 , Fields ),
322
- counters :put (CRef , Index , Value ).
290
+ ok = counters :put (CRef , Index , Value ).
323
291
324
- resolve_fieldspec ( Fields = FieldSpec ) when is_list ( FieldSpec ) ->
325
- Fields ;
326
- resolve_fieldspec ({ persistent_term , PTerm }) ->
327
- persistent_term : get ( PTerm ).
292
+ assertEqualIgnoringOrder ( Expected , Actual ) ->
293
+ ExpectedSorted = lists : sort ( binary : split ( Expected , << " \n " >>, [ global , trim ])),
294
+ ActualSorted = lists : sort ( binary : split ( Actual , << " \n " >>, [ global , trim ])),
295
+ ? assertEqual ( ExpectedSorted , ActualSorted ).
0 commit comments