forked from kubernetes-sigs/cloud-provider-equinix-metal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_test.go
211 lines (184 loc) · 6.07 KB
/
cloud_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
210
211
package metal
import (
"fmt"
"net/http/httptest"
"net/url"
"testing"
metaltest "sigs.k8s.io/cloud-provider-equinix-metal/metal/testing"
metal "github.com/equinix/equinix-sdk-go/services/metalv1"
"github.com/google/uuid"
clientset "k8s.io/client-go/kubernetes"
k8sfake "k8s.io/client-go/kubernetes/fake"
restclient "k8s.io/client-go/rest"
cloudprovider "k8s.io/cloud-provider"
"k8s.io/component-base/version"
)
const (
token = "12345678"
nodeName = "ccm-test"
validRegionCode = "ME"
validRegionName = "Metro"
validZoneCode = "ewr1"
)
// mockControllerClientBuilder mock implementation of https://pkg.go.dev/k8s.io/cloud-provider#ControllerClientBuilder
// so we can pass it to cloud.Initialize()
type mockControllerClientBuilder struct{}
func (m mockControllerClientBuilder) Config(name string) (*restclient.Config, error) {
return &restclient.Config{}, nil
}
func (m mockControllerClientBuilder) ConfigOrDie(name string) *restclient.Config {
return &restclient.Config{}
}
func (m mockControllerClientBuilder) Client(name string) (clientset.Interface, error) {
return k8sfake.NewSimpleClientset(), nil
}
func (m mockControllerClientBuilder) ClientOrDie(name string) clientset.Interface {
return k8sfake.NewSimpleClientset()
}
// create a valid cloud with a client
func testGetValidCloud(t *testing.T, LoadBalancerSetting string) (*cloud, *metaltest.MockMetalServer) {
mockServer := metaltest.NewMockMetalServer(t)
// mock endpoint so our client can handle it
ts := httptest.NewServer(mockServer.CreateHandler())
url, _ := url.Parse(ts.URL)
urlString := url.String()
client := constructClient(token, &urlString)
// now just need to create a client
config := Config{
ProjectID: uuid.New().String(),
LoadBalancerSetting: LoadBalancerSetting,
}
c, _ := newCloud(config, client)
ccb := &mockControllerClientBuilder{}
c.Initialize(ccb, nil)
return c.(*cloud), mockServer
}
func TestLoadBalancerDefaultDisabled(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
response, supported := vc.LoadBalancer()
var (
expectedSupported = false
expectedResponse = response
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestLoadBalancerMetalLB(t *testing.T) {
t.Skip("Test needs a k8s client to work")
vc, _ := testGetValidCloud(t, "metallb:///metallb-system/config")
response, supported := vc.LoadBalancer()
var (
expectedSupported = true
expectedResponse = response
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestLoadBalancerEmpty(t *testing.T) {
t.Skip("Test needs a k8s client to work")
vc, _ := testGetValidCloud(t, "empty://")
response, supported := vc.LoadBalancer()
var (
expectedSupported = true
expectedResponse = response
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestLoadBalancerKubeVIP(t *testing.T) {
t.Skip("Test needs a k8s client to work")
vc, _ := testGetValidCloud(t, "kube-vip://")
response, supported := vc.LoadBalancer()
var (
expectedSupported = true
expectedResponse = response
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestInstances(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
response, supported := vc.Instances()
expectedSupported := false
expectedResponse := cloudprovider.Instances(nil)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestClusters(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
response, supported := vc.Clusters()
var (
expectedSupported = false
expectedResponse cloudprovider.Clusters // defaults to nil
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestRoutes(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
response, supported := vc.Routes()
var (
expectedSupported = false
expectedResponse cloudprovider.Routes // defaults to nil
)
if supported != expectedSupported {
t.Errorf("supported returned %v instead of expected %v", supported, expectedSupported)
}
if response != expectedResponse {
t.Errorf("value returned %v instead of expected %v", response, expectedResponse)
}
}
func TestProviderName(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
name := vc.ProviderName()
if name != ProviderName {
t.Errorf("returned %s instead of expected %s", name, ProviderName)
}
}
func TestHasClusterID(t *testing.T) {
vc, _ := testGetValidCloud(t, "")
cid := vc.HasClusterID()
expectedCid := true
if cid != expectedCid {
t.Errorf("returned %v instead of expected %v", cid, expectedCid)
}
}
// builds an Equinix Metal client
func constructClient(authToken string, baseUrl *string) *metal.APIClient {
configuration := metal.NewConfiguration()
configuration.AddDefaultHeader("X-Auth-Token", authToken)
configuration.UserAgent = fmt.Sprintf("cloud-provider-equinix-metal/%s %s", version.Get(), configuration.UserAgent)
if baseUrl != nil {
configuration.Servers = metal.ServerConfigurations{
{
URL: *baseUrl,
Description: "No description provided",
},
}
}
return metal.NewAPIClient(configuration)
}