Skip to content

Commit ae9a1f3

Browse files
committed
test: make token exchange settable so it can be tested, add test
Signed-off-by: Chris Privitere <23177737+cprivitere@users.noreply.github.com>
1 parent 911648e commit ae9a1f3

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

internal/emlb/emlb.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151
loadBalancerOriginIDAnnotation = "equinix.com/loadbalanceroriginID"
5252
// EMLBVIPID is the stringused to refer to the EMLB load balancer and VIP Manager type.
5353
EMLBVIPID = "EMLB"
54+
// loadbalancerTokenExchangeURL is the default URL to use for Token Exchange to talk to the Equinix Metal Load Balancer API
55+
loadbalancerTokenExchnageURL = "https://iam.metalctrl.io/api-keys/exchange" //nolint:gosec
5456
)
5557

5658
var lbMetros = map[string]string{
@@ -87,8 +89,9 @@ func NewEMLB(metalAPIKey, projectID, metro string) *EMLB {
8789

8890
manager.client = lbaas.NewAPIClient(emlbConfig)
8991
manager.tokenExchanger = &TokenExchanger{
90-
metalAPIKey: metalAPIKey,
91-
client: manager.client.GetConfig().HTTPClient,
92+
metalAPIKey: metalAPIKey,
93+
tokenExchangeURL: loadbalancerTokenExchnageURL,
94+
client: manager.client.GetConfig().HTTPClient,
9295
}
9396
manager.projectID = projectID
9497
manager.metro = metro

internal/emlb/token-exchanger_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package emlb
18+
19+
import (
20+
"net/http"
21+
"net/http/httptest"
22+
"testing"
23+
"time"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestTokenExchanger_Token(t *testing.T) {
29+
// Create a mock server to handle the token exchange request
30+
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31+
// Return a sample token response
32+
response := `{"access_token": "sample_token", "expires_in": 3600}`
33+
w.WriteHeader(http.StatusOK)
34+
w.Write([]byte(response))
35+
}))
36+
defer mockServer.Close()
37+
38+
// Create a TokenExchanger instance with the mock server URL
39+
exchanger := &TokenExchanger{
40+
metalAPIKey: "sample_api_key",
41+
tokenExchangeURL: mockServer.URL,
42+
client: mockServer.Client(),
43+
}
44+
45+
// Call the Token method
46+
token, err := exchanger.Token()
47+
48+
// Assert that no error occurred
49+
assert.NoError(t, err)
50+
51+
// Assert that the token is not nil
52+
assert.NotNil(t, token)
53+
54+
// Assert the token values
55+
assert.Equal(t, "sample_token", token.AccessToken)
56+
assert.Equal(t, time.Now().Add(time.Hour).Round(time.Second), token.Expiry.Round(time.Second))
57+
}

internal/emlb/token_exchanger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import (
2828

2929
// TokenExchanger is an client for authenticating to the Load Balancer API.
3030
type TokenExchanger struct {
31-
metalAPIKey string
32-
client *http.Client
31+
metalAPIKey string
32+
tokenExchangeURL string
33+
client *http.Client
3334
}
3435

3536
// Token creates a Token object to authenticate with the Load Balancer API.
3637
func (m *TokenExchanger) Token() (*oauth2.Token, error) {
37-
tokenExchangeURL := "https://iam.metalctrl.io/api-keys/exchange" //nolint:gosec
38-
tokenExchangeRequest, err := http.NewRequest(http.MethodPost, tokenExchangeURL, http.NoBody) //nolint:noctx // we can't find a way to get the ctx into here yet and just using context.Background adds no value that we can tell
38+
tokenExchangeRequest, err := http.NewRequest(http.MethodPost, m.tokenExchangeURL, http.NoBody) //nolint:noctx // we can't find a way to get the ctx into here yet and just using context.Background adds no value that we can tell
3939
if err != nil {
4040
return nil, err
4141
}

0 commit comments

Comments
 (0)