Repository navigation
Expand file tree
/
Copy pathcloudstack_zone_test.go
More file actions
279 lines (243 loc) · 8.89 KB
/
Copy pathcloudstack_zone_test.go
File metadata and controls
279 lines (243 loc) · 8.89 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 cloudstack
import (
"context"
"errors"
"strings"
"testing"
"github.com/apache/cloudstack-go/v2/cloudstack"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
restclient "k8s.io/client-go/rest"
cloudprovider "k8s.io/cloud-provider"
)
type fakeClientBuilder struct {
client kubernetes.Interface
err error
}
func (b *fakeClientBuilder) Config(string) (*restclient.Config, error) { return nil, b.err }
func (b *fakeClientBuilder) ConfigOrDie(string) *restclient.Config { return nil }
func (b *fakeClientBuilder) Client(string) (kubernetes.Interface, error) {
return b.client, b.err
}
func (b *fakeClientBuilder) ClientOrDie(string) kubernetes.Interface { return b.client }
var _ cloudprovider.ControllerClientBuilder = &fakeClientBuilder{}
type zoneLookup struct {
name string
call func(cs *CSCloud) (cloudprovider.Zone, error)
// expect registers the one CloudStack lookup the call makes.
expect func(e *vmLookupEnv, t *testing.T, result *cloudstack.VirtualMachine, count int, err error)
// notFound is the text of the error for an instance CloudStack does not know.
notFound string
}
var zoneLookups = []zoneLookup{
{
name: "GetZoneByNodeName",
call: func(cs *CSCloud) (cloudprovider.Zone, error) {
return cs.GetZoneByNodeName(context.TODO(), "node-1")
},
expect: func(e *vmLookupEnv, t *testing.T, result *cloudstack.VirtualMachine, count int, err error) {
e.expectByName(t, "node-1", result, count, err)
},
notFound: "could not find node: node-1",
},
{
name: "GetZoneByProviderID",
call: func(cs *CSCloud) (cloudprovider.Zone, error) {
return cs.GetZoneByProviderID(context.TODO(), "external-cloudstack://vm-1")
},
expect: func(e *vmLookupEnv, t *testing.T, result *cloudstack.VirtualMachine, count int, err error) {
e.expectByID(t, "vm-1", result, count, err)
},
notFound: "could not find node by ID: external-cloudstack://vm-1",
},
}
// TestZoneLookups checks that a zone is resolved from the instance's zone name in the configured
// project, that the region is the configured one or else the zone name, and that failures read
// differently for a missing instance and an API error.
func TestZoneLookups(t *testing.T) {
for _, lookup := range zoneLookups {
t.Run(lookup.name+"/region falls back to the zone", func(t *testing.T) {
env := newVMLookupEnv(t, "")
lookup.expect(env, t, testInstance(), 1, nil)
zone, err := lookup.call(env.cs)
if err != nil || zone.FailureDomain != "zone-a" || zone.Region != "zone-a" {
t.Errorf("%s() = %+v, %v, want zone-a in region zone-a", lookup.name, zone, err)
}
})
t.Run(lookup.name+"/configured region in a project", func(t *testing.T) {
env := newVMLookupEnv(t, testProjectID)
env.cs.region = "region-1"
lookup.expect(env, t, testInstance(), 1, nil)
zone, err := lookup.call(env.cs)
if err != nil || zone.FailureDomain != "zone-a" || zone.Region != "region-1" {
t.Errorf("%s() = %+v, %v, want zone-a in region region-1", lookup.name, zone, err)
}
})
t.Run(lookup.name+"/instance not found", func(t *testing.T) {
env := newVMLookupEnv(t, "")
lookup.expect(env, t, nil, 0, errors.New("no match"))
_, err := lookup.call(env.cs)
if err == nil || err.Error() != lookup.notFound {
t.Errorf("%s() error = %v, want %q", lookup.name, err, lookup.notFound)
}
})
t.Run(lookup.name+"/api error", func(t *testing.T) {
env := newVMLookupEnv(t, "")
lookup.expect(env, t, nil, 1, errors.New("boom"))
_, err := lookup.call(env.cs)
if err == nil || err.Error() != "error retrieving zone: boom" {
t.Errorf("%s() error = %v, want %q", lookup.name, err, "error retrieving zone: boom")
}
})
}
}
func podNamed(name, namespace, nodeName string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: corev1.PodSpec{NodeName: nodeName},
}
}
func setPodEnvironment(t *testing.T, podName, namespace string) {
t.Helper()
t.Setenv("HOSTNAME", podName)
t.Setenv("POD_NAMESPACE", namespace)
}
// TestGetNodeNameFromPod checks that the node is read from the CCM's own pod and that every way
// that can fail is reported.
func TestGetNodeNameFromPod(t *testing.T) {
setPodEnvironment(t, "ccm-pod", "ccm-ns")
tests := []struct {
name string
builder *fakeClientBuilder
want string
errPrefix string
}{
{
name: "no client builder",
errPrefix: "clientBuilder not initialized",
},
{
name: "client cannot be built",
builder: &fakeClientBuilder{err: errors.New("no config")},
errPrefix: "failed to get Kubernetes client: no config",
},
{
name: "pod does not exist",
builder: &fakeClientBuilder{client: fake.NewSimpleClientset()},
errPrefix: "failed to get pod ccm-ns/ccm-pod from Kubernetes API",
},
{
name: "pod not scheduled yet",
builder: &fakeClientBuilder{client: fake.NewSimpleClientset(podNamed("ccm-pod", "ccm-ns", ""))},
errPrefix: "pod ccm-ns/ccm-pod does not have a nodeName assigned yet",
},
{
name: "pod scheduled",
builder: &fakeClientBuilder{client: fake.NewSimpleClientset(podNamed("ccm-pod", "ccm-ns", "node-1"))},
want: "node-1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cs := &CSCloud{}
if tt.builder != nil {
cs.clientBuilder = tt.builder
}
got, err := cs.getNodeNameFromPod(context.TODO())
if tt.errPrefix != "" {
if err == nil || !strings.HasPrefix(err.Error(), tt.errPrefix) {
t.Errorf("getNodeNameFromPod() error = %v, want prefix %q", err, tt.errPrefix)
}
return
}
if err != nil || got != tt.want {
t.Errorf("getNodeNameFromPod() = %q, %v, want %q", got, err, tt.want)
}
})
}
}
// TestGetZone checks that the zone is looked up once through the pod's node and then served from the cache.
func TestGetZone(t *testing.T) {
t.Run("cached zone makes no lookup", func(t *testing.T) {
env := newVMLookupEnv(t, "")
env.cs.zone = "zone-a"
env.cs.region = "region-1"
zone, err := env.cs.GetZone(context.TODO())
if err != nil || zone.FailureDomain != "zone-a" || zone.Region != "region-1" {
t.Errorf("GetZone() = %+v, %v, want zone-a in region region-1", zone, err)
}
})
t.Run("zone of the pod's node is looked up once", func(t *testing.T) {
setPodEnvironment(t, "ccm-pod", "ccm-ns")
env := newVMLookupEnv(t, testProjectID)
env.cs.clientBuilder = &fakeClientBuilder{client: fake.NewSimpleClientset(podNamed("ccm-pod", "ccm-ns", "node-1"))}
env.expectByName(t, "node-1", testInstance(), 1, nil).Times(1)
for i := 0; i < 2; i++ {
zone, err := env.cs.GetZone(context.TODO())
if err != nil || zone.FailureDomain != "zone-a" || zone.Region != "zone-a" {
t.Fatalf("GetZone() call %d = %+v, %v, want zone-a in region zone-a", i+1, zone, err)
}
}
})
failures := []struct {
name string
builder cloudprovider.ControllerClientBuilder
result *cloudstack.VirtualMachine
count int
err error
errPrefix string
}{
{
name: "node of the pod unknown",
errPrefix: "failed to get node name for retrieving the zone",
},
{
name: "instance not found",
builder: &fakeClientBuilder{client: fake.NewSimpleClientset(podNamed("ccm-pod", "ccm-ns", "node-1"))},
count: 0,
err: errors.New("no match"),
errPrefix: "could not find CloudStack instance with name node-1 for retrieving the zone",
},
{
name: "api error",
builder: &fakeClientBuilder{client: fake.NewSimpleClientset(podNamed("ccm-pod", "ccm-ns", "node-1"))},
count: 1,
err: errors.New("boom"),
errPrefix: "error getting instance for retrieving the zone",
},
}
for _, tt := range failures {
t.Run(tt.name, func(t *testing.T) {
setPodEnvironment(t, "ccm-pod", "ccm-ns")
env := newVMLookupEnv(t, "")
env.cs.clientBuilder = tt.builder
if tt.err != nil {
env.expectByName(t, "node-1", tt.result, tt.count, tt.err)
}
_, err := env.cs.GetZone(context.TODO())
if err == nil || !strings.HasPrefix(err.Error(), tt.errPrefix) {
t.Errorf("GetZone() error = %v, want prefix %q", err, tt.errPrefix)
}
})
}
}