-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathexporter_test.go
More file actions
271 lines (250 loc) · 7.26 KB
/
exporter_test.go
File metadata and controls
271 lines (250 loc) · 7.26 KB
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
package main
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"slices"
"testing"
"time"
"github.com/grafana/cloudcost-exporter/cmd/exporter/config"
"github.com/grafana/cloudcost-exporter/pkg/aws"
"github.com/grafana/cloudcost-exporter/pkg/azure"
"github.com/grafana/cloudcost-exporter/pkg/google"
"github.com/grafana/cloudcost-exporter/pkg/provider"
mock_provider "github.com/grafana/cloudcost-exporter/pkg/provider/mocks"
"go.uber.org/mock/gomock"
)
func Test_selectProvider(t *testing.T) {
tests := map[string]struct {
providerName string
collectorTimeout time.Duration
awsCalled bool
azureCalled bool
gcpCalled bool
constructorErr error
wantErr bool
wantCollectorTimeout time.Duration // if non-zero, asserts the timeout forwarded to the constructor
}{
"aws provider": {
providerName: "aws",
awsCalled: true,
},
"azure provider": {
providerName: "azure",
azureCalled: true,
},
"gcp provider": {
providerName: "gcp",
gcpCalled: true,
},
"aws constructor error is propagated": {
providerName: "aws",
constructorErr: errors.New("constructor failed"),
wantErr: true,
},
"azure constructor error is propagated": {
providerName: "azure",
constructorErr: errors.New("constructor failed"),
wantErr: true,
},
"gcp constructor error is propagated": {
providerName: "gcp",
constructorErr: errors.New("constructor failed"),
wantErr: true,
},
"unknown provider returns error": {
providerName: "unknown",
wantErr: true,
},
"zero timeout defaults to one minute": {
providerName: "aws",
collectorTimeout: 0,
awsCalled: true,
wantCollectorTimeout: time.Minute,
},
"explicit timeout is passed through": {
providerName: "aws",
collectorTimeout: 5 * time.Minute,
awsCalled: true,
wantCollectorTimeout: 5 * time.Minute,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var awsCalled, azureCalled, gcpCalled bool
var capturedTimeout time.Duration
mockProv := mock_provider.NewMockProvider(ctrl)
stubAWS := func(_ context.Context, cfg *aws.Config) (provider.Provider, error) {
awsCalled = true
capturedTimeout = cfg.CollectorTimeout
if tc.constructorErr != nil {
return nil, tc.constructorErr
}
return mockProv, nil
}
stubAzure := func(_ context.Context, cfg *azure.Config) (provider.Provider, error) {
azureCalled = true
capturedTimeout = cfg.CollectorTimeout
if tc.constructorErr != nil {
return nil, tc.constructorErr
}
return mockProv, nil
}
stubGCP := func(_ context.Context, cfg *google.Config) (provider.Provider, error) {
gcpCalled = true
capturedTimeout = cfg.CollectorTimeout
if tc.constructorErr != nil {
return nil, tc.constructorErr
}
return mockProv, nil
}
cfg := &config.Config{Provider: tc.providerName}
cfg.Collector.Timeout = tc.collectorTimeout
got, err := selectProviderWith(context.Background(), cfg, stubAWS, stubAzure, stubGCP)
if tc.wantErr {
if err == nil {
t.Error("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got == nil {
t.Fatal("expected non-nil provider")
}
if awsCalled != tc.awsCalled {
t.Errorf("awsCalled = %v, want %v", awsCalled, tc.awsCalled)
}
if azureCalled != tc.azureCalled {
t.Errorf("azureCalled = %v, want %v", azureCalled, tc.azureCalled)
}
if gcpCalled != tc.gcpCalled {
t.Errorf("gcpCalled = %v, want %v", gcpCalled, tc.gcpCalled)
}
if tc.wantCollectorTimeout != 0 && capturedTimeout != tc.wantCollectorTimeout {
t.Errorf("collectorTimeout = %v, want %v", capturedTimeout, tc.wantCollectorTimeout)
}
})
}
}
func Test_expandAzureServices(t *testing.T) {
tests := map[string]struct {
in config.StringSliceFlag
want []string
}{
"empty": {in: nil, want: nil},
"single token": {in: config.StringSliceFlag{"blob"}, want: []string{"blob"}},
"comma separated in one flag": {in: config.StringSliceFlag{"AKS, blob"}, want: []string{"AKS", "blob"}},
"repeat flag": {in: config.StringSliceFlag{"AKS", "blob"}, want: []string{"AKS", "blob"}},
"trims and skips empty": {in: config.StringSliceFlag{" AKS , ", ""}, want: []string{"AKS"}},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := expandAzureServices(tt.in)
if !slices.Equal(got, tt.want) {
t.Fatalf("expandAzureServices(%#v) = %#v, want %#v", tt.in, got, tt.want)
}
})
}
}
func Test_regionFromConfig(t *testing.T) {
tests := map[string]struct {
provider string
awsRegion string
gcpRegion string
want string
}{
"aws returns aws region": {
provider: "aws",
awsRegion: "me-central-1",
want: "me-central-1",
},
"gcp returns gcp region": {
provider: "gcp",
gcpRegion: "us-central1",
want: "us-central1",
},
"azure returns empty string": {
provider: "azure",
want: "",
},
"unknown provider returns empty string": {
provider: "unknown",
want: "",
},
"aws with empty region returns empty string": {
provider: "aws",
awsRegion: "",
want: "",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
cfg := &config.Config{Provider: tc.provider}
cfg.Providers.AWS.Region = tc.awsRegion
cfg.Providers.GCP.Region = tc.gcpRegion
got := regionFromConfig(cfg)
if got != tc.want {
t.Errorf("regionFromConfig() = %q, want %q", got, tc.want)
}
})
}
}
func Test_createPromRegistryHandler(t *testing.T) {
tests := map[string]struct {
setupMock func(m *mock_provider.MockProvider)
wantErr bool
wantHTTPStatus int
}{
"returns error when RegisterCollectors fails": {
setupMock: func(m *mock_provider.MockProvider) {
m.EXPECT().Describe(gomock.Any()).AnyTimes()
m.EXPECT().RegisterCollectors(gomock.Any()).Return(errors.New("collector registration failed"))
},
wantErr: true,
},
"returns working handler on success": {
setupMock: func(m *mock_provider.MockProvider) {
m.EXPECT().Describe(gomock.Any()).AnyTimes()
m.EXPECT().RegisterCollectors(gomock.Any()).Return(nil)
m.EXPECT().Collect(gomock.Any()).AnyTimes()
},
wantErr: false,
wantHTTPStatus: http.StatusOK,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockProv := mock_provider.NewMockProvider(ctrl)
tc.setupMock(mockProv)
handler, err := createPromRegistryHandler(mockProv, "us-east-1")
if tc.wantErr {
if err == nil {
t.Error("expected error, got nil")
}
if handler != nil {
t.Error("expected nil handler on error")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if handler == nil {
t.Fatal("expected non-nil handler")
}
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != tc.wantHTTPStatus {
t.Errorf("expected HTTP status %d, got %d", tc.wantHTTPStatus, rec.Code)
}
})
}
}