Skip to content

Commit 94876c1

Browse files
fix: fixed failure of test_case
1 parent 09876d9 commit 94876c1

File tree

1 file changed

+51
-17
lines changed
  • metrics-operator/controllers/common/providers/prometheus

1 file changed

+51
-17
lines changed

metrics-operator/controllers/common/providers/prometheus/common_test.go

+51-17
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package prometheus
22

33
import (
44
"context"
5-
"crypto/tls"
5+
"encoding/base64"
66
"net/http"
77
"net/http/httptest"
8-
"reflect"
98
"strings"
109
"testing"
1110

1211
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1"
1312
"github.com/keptn/lifecycle-toolkit/metrics-operator/controllers/common/fake"
14-
promapi "github.com/prometheus/client_golang/api"
13+
1514
"github.com/prometheus/common/config"
1615
"github.com/stretchr/testify/require"
1716
corev1 "k8s.io/api/core/v1"
@@ -120,7 +119,8 @@ func Test_GetRoundtripper(t *testing.T) {
120119
name string
121120
provider metricsapi.KeptnMetricsProvider
122121
k8sClient client.Client
123-
want http.RoundTripper
122+
wantUser string
123+
wantPass string
124124
wantErr bool
125125
errorStr string
126126
}{
@@ -138,24 +138,19 @@ func Test_GetRoundtripper(t *testing.T) {
138138
Key: "",
139139
Optional: nil,
140140
},
141-
InsecureSkipTlsVerify: true,
142141
},
143142
},
144143
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,
153147
},
154148
{
155149
name: "TestSecretNotDefined",
156150
provider: metricsapi.KeptnMetricsProvider{},
157151
k8sClient: fake.NewClient(),
158-
want: promapi.DefaultRoundTripper,
152+
wantUser: "",
153+
wantPass: "",
159154
wantErr: false,
160155
},
161156
{
@@ -175,7 +170,8 @@ func Test_GetRoundtripper(t *testing.T) {
175170
},
176171
},
177172
k8sClient: fake.NewClient(),
178-
want: nil,
173+
wantUser: "",
174+
wantPass: "",
179175
wantErr: true,
180176
errorStr: "not found",
181177
},
@@ -193,8 +189,46 @@ func Test_GetRoundtripper(t *testing.T) {
193189
t.Errorf("getRoundtripper() error = %s, wantErr %s", err.Error(), tt.errorStr)
194190
return
195191
}
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)
198232
}
199233
})
200234
}

0 commit comments

Comments
 (0)