-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcloud_test.go
More file actions
259 lines (226 loc) · 7.05 KB
/
Copy pathcloud_test.go
File metadata and controls
259 lines (226 loc) · 7.05 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
/*
Copyright 2018 Hetzner Cloud GmbH.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hcloud
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
hrobot "github.com/syself/hrobot-go"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/cache"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/config"
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/testsupport"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)
type testEnv struct {
Server *httptest.Server
Mux *http.ServeMux
Client *hcloud.Client
RobotClient hrobot.RobotClient
ServerCache *cache.Cache[hcloud.Server]
Recorder record.EventRecorder
Cfg config.HCCMConfiguration
}
func (env *testEnv) Teardown() {
env.Server.Close()
env.Server = nil
env.Mux = nil
env.Client = nil
env.RobotClient = nil
env.ServerCache = nil
env.Recorder = nil
}
func newTestEnv() testEnv {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
client := hcloud.NewClient(
hcloud.WithEndpoint(server.URL),
hcloud.WithToken("jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jNZXCeTYQ4uArypFM3nh75"),
hcloud.WithPollOpts(hcloud.PollOpts{BackoffFunc: hcloud.ConstantBackoff(0)}),
hcloud.WithRetryOpts(hcloud.RetryOpts{BackoffFunc: hcloud.ConstantBackoff(0)}),
hcloud.WithDebugWriter(os.Stdout),
)
robotClient := hrobot.NewBasicAuthClient("", "")
robotClient.SetBaseURL(server.URL + "/robot")
serverCache := cache.NewServerCache(client, cache.ModeOne, 10*time.Second)
recorder := record.NewBroadcaster().NewRecorder(scheme.Scheme, corev1.EventSource{Component: "hcloud-cloud-controller-manager"})
cfg := config.HCCMConfiguration{}
cfg.Instance.AddressFamily = config.AddressFamilyIPv4
cfg.Instance.ZoneLabelEnabled = true
return testEnv{
Server: server,
Mux: mux,
Client: client,
RobotClient: robotClient,
ServerCache: serverCache,
Recorder: recorder,
Cfg: cfg,
}
}
func TestNewCloud(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
resetEnv := testsupport.Setenv(t,
"HCLOUD_ENDPOINT", env.Server.URL,
"HCLOUD_TOKEN", "jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jN_NOT_VALID_dzhepnahq",
"HCLOUD_METRICS_ENABLED", "false",
)
defer resetEnv()
env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(schema.LocationListResponse{Locations: []schema.Location{}})
})
_, err := NewCloud(DefaultClusterCIDR, nil)
assert.NoError(t, err)
}
func TestNewCloudConnectionNotPossible(t *testing.T) {
resetEnv := testsupport.Setenv(t,
"HCLOUD_ENDPOINT", "http://127.0.0.1:4711/v1",
"HCLOUD_TOKEN", "jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jN_NOT_VALID_dzhepnahq",
"HCLOUD_METRICS_ENABLED", "false",
)
defer resetEnv()
_, err := NewCloud(DefaultClusterCIDR, nil)
assert.EqualError(t, err,
`hcloud/newCloud: Get "http://127.0.0.1:4711/v1/locations?": dial tcp 127.0.0.1:4711: connect: connection refused`)
}
func TestNewCloudInvalidToken(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
resetEnv := testsupport.Setenv(t,
"HCLOUD_ENDPOINT", env.Server.URL,
"HCLOUD_TOKEN", "jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jN_NOT_VALID_dzhepnahq",
"HCLOUD_METRICS_ENABLED", "false",
)
defer resetEnv()
env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(
schema.ErrorResponse{
Error: schema.Error{
Code: "unauthorized",
Message: "unable to authenticate",
},
},
)
})
_, err := NewCloud(DefaultClusterCIDR, nil)
assert.EqualError(t, err, "hcloud/newCloud: unable to authenticate (unauthorized)")
}
func TestCloud(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
resetEnv := testsupport.Setenv(t,
"HCLOUD_ENDPOINT", env.Server.URL,
"HCLOUD_TOKEN", "jr5g7ZHpPptyhJzZyHw2Pqu4g9gTqDvEceYpngPf79jN_NOT_VALID_dzhepnahq",
"HCLOUD_METRICS_ENABLED", "false",
"ROBOT_USER", "user",
"ROBOT_PASSWORD", "pass123",
)
defer resetEnv()
env.Mux.HandleFunc("/locations", func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(schema.LocationListResponse{Locations: []schema.Location{}})
})
env.Mux.HandleFunc("/networks/1", func(w http.ResponseWriter, _ *http.Request) {
json.NewEncoder(w).Encode(
schema.NetworkGetResponse{
Network: schema.Network{
ID: 1,
Name: "test",
Created: time.Time{},
IPRange: "10.0.0.8",
Subnets: nil,
Routes: nil,
Servers: nil,
Protection: schema.NetworkProtection{},
Labels: nil,
},
},
)
})
cloud, err := NewCloud(DefaultClusterCIDR, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Run("Instances", func(t *testing.T) {
_, supported := cloud.Instances()
if supported {
t.Error("Instances interface should not be supported")
}
})
t.Run("Zones", func(t *testing.T) {
_, supported := cloud.Zones()
if supported {
t.Error("Zones interface should not be supported")
}
})
t.Run("InstancesV2", func(t *testing.T) {
_, supported := cloud.InstancesV2()
if !supported {
t.Error("InstancesV2 interface should be supported")
}
})
t.Run("LoadBalancer", func(t *testing.T) {
_, supported := cloud.LoadBalancer()
if !supported {
t.Error("LoadBalancer interface should be supported")
}
})
t.Run("Clusters", func(t *testing.T) {
_, supported := cloud.Clusters()
if supported {
t.Error("Clusters interface should not be supported")
}
})
t.Run("Routes", func(t *testing.T) {
_, supported := cloud.Routes()
if supported {
t.Error("Routes interface should not be supported")
}
})
t.Run("RoutesWithNetworks", func(t *testing.T) {
resetEnv := testsupport.Setenv(t,
"HCLOUD_NETWORK", "1",
"HCLOUD_NETWORK_DISABLE_ATTACHED_CHECK", "true",
"HCLOUD_METRICS_ENABLED", "false",
"ROBOT_USER", "",
"ROBOT_PASSWORD", "",
)
defer resetEnv()
c, err := NewCloud(DefaultClusterCIDR, nil)
if err != nil {
t.Errorf("%s", err)
}
_, supported := c.Routes()
if !supported {
t.Error("Routes interface should be supported")
}
})
t.Run("HasClusterID", func(t *testing.T) {
if cloud.HasClusterID() {
t.Error("HasClusterID should be false")
}
})
t.Run("ProviderName", func(t *testing.T) {
if cloud.ProviderName() != "hcloud" {
t.Error("ProviderName should be hcloud")
}
})
}