@@ -30,7 +30,6 @@ import (
3030
3131 "github.com/stretchr/testify/assert"
3232
33- "github.com/elastic/beats/v7/libbeat/common"
3433 "github.com/elastic/beats/v7/metricbeat/mb"
3534 mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing"
3635 "github.com/elastic/beats/v7/metricbeat/mb/testing/flags"
@@ -45,87 +44,6 @@ type TestCases []struct {
4544 ExpectedFile string
4645}
4746
48- // TestMetricSetEventsFetcher goes over the given TestCases and ensures that source Prometheus metrics gets converted
49- // into the expected events when passed by the given metricset.
50- // If -data flag is passed, the expected JSON file will be updated with the result
51- func TestMetricSetEventsFetcher (t * testing.T , module , metricset string , cases TestCases ) {
52- for _ , test := range cases {
53- t .Logf ("Testing %s file\n " , test .MetricsFile )
54-
55- file , err := os .Open (test .MetricsFile )
56- assert .NoError (t , err , "cannot open test file " + test .MetricsFile )
57-
58- body , err := ioutil .ReadAll (file )
59- assert .NoError (t , err , "cannot read test file " + test .MetricsFile )
60-
61- server := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
62- w .WriteHeader (200 )
63- w .Header ().Set ("Content-Type" , "text/plain; charset=ISO-8859-1" )
64- w .Write ([]byte (body ))
65- }))
66-
67- server .Start ()
68- defer server .Close ()
69-
70- config := map [string ]interface {}{
71- "module" : module ,
72- "metricsets" : []string {metricset },
73- "hosts" : []string {server .URL },
74- }
75-
76- f := mbtest .NewEventsFetcher (t , config )
77- events , err := f .Fetch ()
78- assert .Nil (t , err , "Errors while fetching metrics" )
79-
80- if * flags .DataFlag {
81- sort .SliceStable (events , func (i , j int ) bool {
82- h1 , _ := hashstructure .Hash (events [i ], nil )
83- h2 , _ := hashstructure .Hash (events [j ], nil )
84- return h1 < h2
85- })
86- eventsJSON , _ := json .MarshalIndent (events , "" , "\t " )
87- err = ioutil .WriteFile (test .ExpectedFile , eventsJSON , 0644 )
88- assert .NoError (t , err )
89- }
90-
91- // Read expected events from reference file
92- expected , err := ioutil .ReadFile (test .ExpectedFile )
93- if err != nil {
94- t .Fatal (err )
95- }
96-
97- var expectedEvents []common.MapStr
98- err = json .Unmarshal (expected , & expectedEvents )
99- if err != nil {
100- t .Fatal (err )
101- }
102-
103- for _ , event := range events {
104- // ensure the event is in expected list
105- found := - 1
106- for i , expectedEvent := range expectedEvents {
107- if event .String () == expectedEvent .String () {
108- found = i
109- break
110- }
111- }
112- if found > - 1 {
113- expectedEvents = append (expectedEvents [:found ], expectedEvents [found + 1 :]... )
114- } else {
115- t .Errorf ("Event was not expected: %+v" , event )
116- }
117- }
118-
119- if len (expectedEvents ) > 0 {
120- t .Error ("Some events were missing:" )
121- for _ , e := range expectedEvents {
122- t .Error (e )
123- }
124- t .Fatal ()
125- }
126- }
127- }
128-
12947// TestMetricSet goes over the given TestCases and ensures that source Prometheus metrics gets converted into the expected
13048// events when passed by the given metricset.
13149// If -data flag is passed, the expected JSON file will be updated with the result
0 commit comments