-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper_test.go
209 lines (194 loc) · 5.94 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package riakreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver"
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.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/riakreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/mocks"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/riakreceiver/internal/model"
)
func TestScraperStart(t *testing.T) {
clientConfigNonExistandCA := confighttp.NewDefaultClientConfig()
clientConfigNonExistandCA.Endpoint = defaultEndpoint
clientConfigNonExistandCA.TLSSetting = configtls.ClientConfig{
Config: configtls.Config{
CAFile: "/non/existent",
},
}
clientConfig := confighttp.NewDefaultClientConfig()
clientConfig.Endpoint = defaultEndpoint
testcases := []struct {
desc string
scraper *riakScraper
expectError bool
}{
{
desc: "Bad Config",
scraper: &riakScraper{
cfg: &Config{
ClientConfig: clientConfigNonExistandCA,
},
settings: componenttest.NewNopTelemetrySettings(),
},
expectError: true,
},
{
desc: "Valid Config",
scraper: &riakScraper{
cfg: &Config{
ClientConfig: clientConfig,
},
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
setupCfg func() *Config
expectedErr error
}{
{
desc: "Nil client",
setupMockClient: func(*testing.T) client {
return nil
},
expectedMetricGen: func(*testing.T) pmetric.Metrics {
return pmetric.NewMetrics()
},
setupCfg: func() *Config {
return createDefaultConfig().(*Config)
},
expectedErr: errClientNotInit,
},
{
desc: "API Call Failure",
setupMockClient: func(*testing.T) client {
mockClient := mocks.MockClient{}
mockClient.On("GetStats", mock.Anything).Return(nil, errors.New("some api error"))
return &mockClient
},
expectedMetricGen: func(*testing.T) pmetric.Metrics {
return pmetric.NewMetrics()
},
setupCfg: func() *Config {
return createDefaultConfig().(*Config)
},
expectedErr: errors.New("some api error"),
},
{
desc: "Metrics Disabled",
setupMockClient: func(t *testing.T) client {
mockClient := mocks.MockClient{}
// use helper function from client tests
data := loadAPIResponseData(t, statsAPIResponseFile)
var stats *model.Stats
err := json.Unmarshal(data, &stats)
require.NoError(t, err)
mockClient.On("GetStats", mock.Anything).Return(stats, nil)
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "scraper", "expected_disabled.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
setupCfg: func() *Config {
cfg := createDefaultConfig().(*Config)
cfg.MetricsBuilderConfig.Metrics = metadata.MetricsConfig{
RiakMemoryLimit: metadata.MetricConfig{
Enabled: false,
},
RiakNodeOperationCount: metadata.MetricConfig{
Enabled: false,
},
RiakNodeOperationTimeMean: metadata.MetricConfig{
Enabled: true,
},
RiakNodeReadRepairCount: metadata.MetricConfig{
Enabled: true,
},
RiakVnodeIndexOperationCount: metadata.MetricConfig{
Enabled: true,
},
RiakVnodeOperationCount: metadata.MetricConfig{
Enabled: true,
},
}
return cfg
},
expectedErr: nil,
},
{
desc: "Successful Collection",
setupMockClient: func(t *testing.T) client {
mockClient := mocks.MockClient{}
// use helper function from client tests
data := loadAPIResponseData(t, statsAPIResponseFile)
var stats *model.Stats
err := json.Unmarshal(data, &stats)
require.NoError(t, err)
mockClient.On("GetStats", mock.Anything).Return(stats, nil)
return &mockClient
},
expectedMetricGen: func(t *testing.T) pmetric.Metrics {
goldenPath := filepath.Join("testdata", "scraper", "expected.yaml")
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
return expectedMetrics
},
setupCfg: func() *Config {
return createDefaultConfig().(*Config)
},
expectedErr: nil,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
scraper := newScraper(zap.NewNop(), tc.setupCfg(), receivertest.NewNopSettings())
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.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
)
require.NoError(t, err)
})
}
}