forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper_test.go
277 lines (253 loc) · 9.71 KB
/
scraper_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package bigipreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver"
import (
"context"
"encoding/json"
"errors"
"path/filepath"
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver/internal/mocks"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/bigipreceiver/internal/models"
)
func TestScraperStart(t *testing.T) {
testcases := []struct {
desc string
scraper *bigipScraper
expectError bool
}{
{
desc: "Bad Config",
scraper: &bigipScraper{
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
Endpoint: defaultEndpoint,
TLSSetting: configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
},
},
},
settings: componenttest.NewNopTelemetrySettings(),
},
expectError: true,
},
{
desc: "Valid Config",
scraper: &bigipScraper{
cfg: &Config{
ClientConfig: confighttp.ClientConfig{
TLSSetting: configtls.ClientConfig{},
Endpoint: defaultEndpoint,
},
},
settings: componenttest.NewNopTelemetrySettings(),
},
expectError: false,
},
}
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
err := tc.scraper.start(context.Background(), componenttest.NewNopHost())
if tc.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestScaperScrape(t *testing.T) {
testCases := []struct {
desc string
setupMockClient func(t *testing.T) client
expectedMetricGen func(t *testing.T) pmetric.Metrics
expectedErr error
}{
{
desc: "Nil client",
setupMockClient: func(*testing.T) client {
return nil
},
expectedMetricGen: func(*testing.T) pmetric.Metrics {
return pmetric.NewMetrics()
},
expectedErr: errClientNotInit,
},
{
desc: "Login API Call Failure",
setupMockClient: func(*testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(errors.New("some api error"))
return &mockClient
},
expectedMetricGen: func(*testing.T) pmetric.Metrics {
return pmetric.NewMetrics()
},
expectedErr: errors.New("some api error"),
},
{
desc: "Get API Calls All Failure",
setupMockClient: func(*testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(nil)
mockClient.On("GetVirtualServers", mock.Anything).Return(nil, errors.New("some virtual api error"))
mockClient.On("GetPools", mock.Anything).Return(nil, errors.New("some pool api error"))
mockClient.On("GetPoolMembers", mock.Anything, mock.Anything).Return(nil, errCollectedNoPoolMembers)
mockClient.On("GetNodes", mock.Anything).Return(nil, errors.New("some node api error"))
return &mockClient
},
expectedMetricGen: func(*testing.T) pmetric.Metrics {
return pmetric.NewMetrics()
},
expectedErr: errors.New("failed to scrape any metrics"),
},
{
desc: "Successful Full Empty Collection",
setupMockClient: func(*testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(nil)
mockClient.On("GetVirtualServers", mock.Anything).Return(&models.VirtualServers{}, nil)
mockClient.On("GetPools", mock.Anything).Return(&models.Pools{}, nil)
mockClient.On("GetPoolMembers", mock.Anything, mock.Anything).Return(&models.PoolMembers{}, nil)
mockClient.On("GetNodes", mock.Anything).Return(&models.Nodes{}, nil)
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "expected_metrics", "metrics_empty_golden.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
expectedErr: nil,
},
{
desc: "Successful Partial Collection",
setupMockClient: func(t *testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(nil)
// use helper function from client tests
data := loadAPIResponseData(t, virtualServersCombinedFile)
var virtualServers *models.VirtualServers
err := json.Unmarshal(data, &virtualServers)
require.NoError(t, err)
mockClient.On("GetVirtualServers", mock.Anything).Return(virtualServers, nil)
mockClient.On("GetPools", mock.Anything).Return(nil, errors.New("some pool api error"))
// with GetPools returning an error GetPoolMembers should not be called, so this error should no appear
mockClient.On("GetPoolMembers", mock.Anything, mock.Anything).Return(nil, errCollectedNoPoolMembers)
mockClient.On("GetNodes", mock.Anything).Return(nil, errors.New("some node api error"))
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "expected_metrics", "metrics_partial_golden.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
expectedErr: scrapererror.NewPartialScrapeError(errors.New("some pool api error; some node api error"), 0),
},
{
desc: "Successful Partial Collection With Partial Members",
setupMockClient: func(t *testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(nil)
// use helper function from client tests
data := loadAPIResponseData(t, virtualServersCombinedFile)
var virtualServers *models.VirtualServers
err := json.Unmarshal(data, &virtualServers)
require.NoError(t, err)
mockClient.On("GetVirtualServers", mock.Anything).Return(virtualServers, nil)
// use helper function from client tests
data = loadAPIResponseData(t, poolsStatsResponseFile)
var pools *models.Pools
err = json.Unmarshal(data, &pools)
require.NoError(t, err)
mockClient.On("GetPools", mock.Anything).Return(pools, nil)
// use helper function from client tests
data = loadAPIResponseData(t, poolMembersCombinedFile)
var poolMembers *models.PoolMembers
err = json.Unmarshal(data, &poolMembers)
require.NoError(t, err)
mockClient.On("GetPoolMembers", mock.Anything, mock.Anything).Return(poolMembers, errors.New("some member api error"))
mockClient.On("GetNodes", mock.Anything).Return(nil, errors.New("some node api error"))
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "expected_metrics", "metrics_partial_with_members_golden.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
expectedErr: scrapererror.NewPartialScrapeError(errors.New("some member api error; some node api error"), 0),
},
{
desc: "Successful Full Collection",
setupMockClient: func(t *testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetNewToken", mock.Anything).Return(nil)
// use helper function from client tests
data := loadAPIResponseData(t, virtualServersCombinedFile)
var virtualServers *models.VirtualServers
err := json.Unmarshal(data, &virtualServers)
require.NoError(t, err)
mockClient.On("GetVirtualServers", mock.Anything).Return(virtualServers, nil)
// use helper function from client tests
data = loadAPIResponseData(t, poolsStatsResponseFile)
var pools *models.Pools
err = json.Unmarshal(data, &pools)
require.NoError(t, err)
mockClient.On("GetPools", mock.Anything).Return(pools, nil)
// use helper function from client tests
data = loadAPIResponseData(t, poolMembersCombinedFile)
var poolMembers *models.PoolMembers
err = json.Unmarshal(data, &poolMembers)
require.NoError(t, err)
mockClient.On("GetPoolMembers", mock.Anything, mock.Anything).Return(poolMembers, nil)
// use helper function from client tests
data = loadAPIResponseData(t, nodesStatsResponseFile)
var nodes *models.Nodes
err = json.Unmarshal(data, &nodes)
require.NoError(t, err)
mockClient.On("GetNodes", mock.Anything).Return(nodes, nil)
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "expected_metrics", "metrics_golden.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
expectedErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
scraper := newScraper(zap.NewNop(), createDefaultConfig().(*Config), receivertest.NewNopCreateSettings())
scraper.client = tc.setupMockClient(t)
actualMetrics, err := scraper.scrape(context.Background())
if tc.expectedErr == nil {
require.NoError(t, err)
} else {
require.EqualError(t, err, tc.expectedErr.Error())
}
expectedMetrics := tc.expectedMetricGen(t)
err = pmetrictest.CompareMetrics(expectedMetrics, actualMetrics,
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp())
require.NoError(t, err)
})
}
}