@@ -2,16 +2,15 @@ package prometheus
2
2
3
3
import (
4
4
"context"
5
- "crypto/tls "
5
+ "encoding/base64 "
6
6
"net/http"
7
7
"net/http/httptest"
8
- "reflect"
9
8
"strings"
10
9
"testing"
11
10
12
11
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1"
13
12
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake"
14
- promapi "github.com/prometheus/client_golang/api"
13
+
15
14
"github.com/prometheus/common/config"
16
15
"github.com/stretchr/testify/require"
17
16
corev1 "k8s.io/api/core/v1"
@@ -120,7 +119,8 @@ func Test_GetRoundtripper(t *testing.T) {
120
119
name string
121
120
provider metricsapi.KeptnMetricsProvider
122
121
k8sClient client.Client
123
- want http.RoundTripper
122
+ wantUser string
123
+ wantPass string
124
124
wantErr bool
125
125
errorStr string
126
126
}{
@@ -138,24 +138,19 @@ func Test_GetRoundtripper(t *testing.T) {
138
138
Key : "" ,
139
139
Optional : nil ,
140
140
},
141
- InsecureSkipTlsVerify : true ,
142
141
},
143
142
},
144
143
k8sClient : fake .NewClient (goodsecret ),
145
- want : func () http.RoundTripper {
146
- transport := promapi .DefaultRoundTripper .(* http.Transport ).Clone ()
147
- transport .TLSClientConfig = & tls.Config {
148
- InsecureSkipVerify : true ,
149
- }
150
- return config .NewBasicAuthRoundTripper ("myuser" , "mytoken" , "" , "" , transport )
151
- }(),
152
- wantErr : false ,
144
+ wantUser : "myuser" ,
145
+ wantPass : "mytoken" ,
146
+ wantErr : false ,
153
147
},
154
148
{
155
149
name : "TestSecretNotDefined" ,
156
150
provider : metricsapi.KeptnMetricsProvider {},
157
151
k8sClient : fake .NewClient (),
158
- want : promapi .DefaultRoundTripper ,
152
+ wantUser : "" ,
153
+ wantPass : "" ,
159
154
wantErr : false ,
160
155
},
161
156
{
@@ -175,7 +170,8 @@ func Test_GetRoundtripper(t *testing.T) {
175
170
},
176
171
},
177
172
k8sClient : fake .NewClient (),
178
- want : nil ,
173
+ wantUser : "" ,
174
+ wantPass : "" ,
179
175
wantErr : true ,
180
176
errorStr : "not found" ,
181
177
},
@@ -193,8 +189,46 @@ func Test_GetRoundtripper(t *testing.T) {
193
189
t .Errorf ("getRoundtripper() error = %s, wantErr %s" , err .Error (), tt .errorStr )
194
190
return
195
191
}
196
- if ! reflect .DeepEqual (got , tt .want ) {
197
- t .Errorf ("getRoundtripper() got = %v, want %v" , got , tt .want )
192
+
193
+ if tt .wantErr {
194
+ return
195
+ }
196
+
197
+ if _ , ok := got .(* http.Transport ); ok {
198
+ if tt .wantUser != "" || tt .wantPass != "" {
199
+ t .Errorf ("getRoundtripper() got default RoundTripper, want BasicAuth" )
200
+ }
201
+ return
202
+ }
203
+
204
+ testServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
205
+ authHeader := r .Header .Get ("Authorization" )
206
+ if authHeader == "" {
207
+ t .Errorf ("Authorization header not set" )
208
+ return
209
+ }
210
+
211
+ auth := strings .SplitN (authHeader , " " , 2 )
212
+ if len (auth ) != 2 || auth [0 ] != "Basic" {
213
+ t .Errorf ("Invalid Authorization header format" )
214
+ return
215
+ }
216
+ payload , _ := base64 .StdEncoding .DecodeString (auth [1 ])
217
+ pair := strings .SplitN (string (payload ), ":" , 2 )
218
+
219
+ if pair [0 ] != tt .wantUser {
220
+ t .Errorf ("got user = %v, want %v" , pair [0 ], tt .wantUser )
221
+ }
222
+ if pair [1 ] != tt .wantPass {
223
+ t .Errorf ("got password = %v, want %v" , pair [1 ], tt .wantPass )
224
+ }
225
+ }))
226
+ defer testServer .Close ()
227
+
228
+ req , _ := http .NewRequest ("GET" , testServer .URL , nil )
229
+ _ , err = got .RoundTrip (req )
230
+ if err != nil {
231
+ t .Errorf ("RoundTrip error: %v" , err )
198
232
}
199
233
})
200
234
}
0 commit comments