-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_basic_test.go
More file actions
287 lines (244 loc) · 9.04 KB
/
create_basic_test.go
File metadata and controls
287 lines (244 loc) · 9.04 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
280
281
282
283
284
285
286
287
package provider
import (
"context"
"fmt"
"time"
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1"
"github.com/gardener/machine-controller-manager/pkg/util/provider/driver"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes"
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/client"
"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/client/mock"
api "github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/provider/apis"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var _ = Describe("CreateMachine", func() {
var (
ctx context.Context
provider *Provider
mockClient *mock.StackitClient
req *driver.CreateMachineRequest
secret *corev1.Secret
machineClass *v1alpha1.MachineClass
machine *v1alpha1.Machine
)
BeforeEach(func() {
ctx = context.Background()
mockClient = &mock.StackitClient{}
provider = &Provider{
client: mockClient,
pollingInterval: 10 * time.Millisecond,
pollingTimeout: 5 * time.Second,
}
secret = &corev1.Secret{
Data: map[string][]byte{
"project-id": []byte("11111111-2222-3333-4444-555555555555"),
"serviceaccount.json": []byte(`{"credentials":{"iss":"test"}}`),
},
}
// Create ProviderSpec
providerSpec := &api.ProviderSpec{
MachineType: "c2i.2",
ImageID: "12345678-1234-1234-1234-123456789abc",
Region: "eu01",
Networking: &api.NetworkingSpec{
NetworkID: "770e8400-e29b-41d4-a716-446655440000",
},
}
providerSpecRaw, _ := mock.EncodeProviderSpec(providerSpec)
// Create MachineClass
machineClass = &v1alpha1.MachineClass{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine-class",
},
Provider: "stackit",
ProviderSpec: runtime.RawExtension{
Raw: providerSpecRaw,
},
}
// Create Machine
machine = &v1alpha1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
Namespace: "default",
},
}
// Create request
req = &driver.CreateMachineRequest{
Machine: machine,
MachineClass: machineClass,
Secret: secret,
}
})
Context("with valid inputs", func() {
It("should successfully create a machine", func() {
resp, err := provider.CreateMachine(ctx, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp).NotTo(BeNil())
Expect(resp.ProviderID).To(Equal("stackit://11111111-2222-3333-4444-555555555555/550e8400-e29b-41d4-a716-446655440000"))
Expect(resp.NodeName).To(Equal("test-machine"))
})
It("should call STACKIT API with correct parameters", func() {
var capturedReq *client.CreateServerRequest
var capturedProjectID string
mockClient.CreateServerFunc = func(_ context.Context, projectID, _ string, req *client.CreateServerRequest) (*client.Server, error) {
capturedProjectID = projectID
capturedReq = req
return &client.Server{
ID: "test-server-id",
Name: req.Name,
Status: "CREATING",
}, nil
}
_, err := provider.CreateMachine(ctx, req)
Expect(err).NotTo(HaveOccurred())
Expect(capturedProjectID).To(Equal("11111111-2222-3333-4444-555555555555"))
Expect(capturedReq).NotTo(BeNil())
Expect(capturedReq.Name).To(Equal("test-machine"))
Expect(capturedReq.MachineType).To(Equal("c2i.2"))
Expect(capturedReq.ImageID).To(Equal("12345678-1234-1234-1234-123456789abc"))
})
It("should return internal IPs from NICs in Addresses", func() {
mockClient.GetNICsFunc = func(_ context.Context, _, _, _ string) ([]*client.NIC, error) {
return []*client.NIC{
{ID: "nic-1", NetworkID: "net-1", IPv4: "10.0.0.5", IPv6: "fd00::1"},
{ID: "nic-2", NetworkID: "net-2", IPv4: "10.0.1.5"},
}, nil
}
resp, err := provider.CreateMachine(ctx, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp.Addresses).To(ConsistOf(
corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: "10.0.0.5"},
corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: "fd00::1"},
corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: "10.0.1.5"},
))
})
It("should poll GetServer until server is ACTIVE", func() {
getServerCallCount := 0
mockClient.CreateServerFunc = func(_ context.Context, _, _ string, req *client.CreateServerRequest) (*client.Server, error) {
return &client.Server{
ID: "550e8400-e29b-41d4-a716-446655440000",
Name: req.Name,
Status: "CREATING",
}, nil
}
mockClient.GetServerFunc = func(_ context.Context, _, _, _ string) (*client.Server, error) {
getServerCallCount++
// First call returns CREATING, second call returns ACTIVE
if getServerCallCount == 1 {
return &client.Server{
ID: "550e8400-e29b-41d4-a716-446655440000",
Name: "test-machine",
Status: "CREATING",
}, nil
}
return &client.Server{
ID: "550e8400-e29b-41d4-a716-446655440000",
Name: "test-machine",
Status: "ACTIVE",
}, nil
}
resp, err := provider.CreateMachine(ctx, req)
Expect(err).NotTo(HaveOccurred())
Expect(resp).NotTo(BeNil())
Expect(resp.ProviderID).To(Equal("stackit://11111111-2222-3333-4444-555555555555/550e8400-e29b-41d4-a716-446655440000"))
Expect(getServerCallCount).To(BeNumerically(">=", 2))
})
})
Context("with invalid ProviderSpec", func() {
It("should fail when MachineType is missing", func() {
providerSpec := &api.ProviderSpec{
MachineType: "",
ImageID: "12345678-1234-1234-1234-123456789abc",
}
providerSpecRaw, _ := mock.EncodeProviderSpec(providerSpec)
req.MachineClass.ProviderSpec.Raw = providerSpecRaw
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.InvalidArgument))
})
It("should fail when ImageID is missing", func() {
providerSpec := &api.ProviderSpec{
MachineType: "c2i.2",
ImageID: "",
}
providerSpecRaw, _ := mock.EncodeProviderSpec(providerSpec)
req.MachineClass.ProviderSpec.Raw = providerSpecRaw
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.InvalidArgument))
})
It("should fail when Provider is wrong", func() {
req.MachineClass.Provider = "openstack"
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("requested for Provider 'openstack', we only support 'stackit'"))
})
})
Context("with invalid Secret", func() {
It("should fail when projectId is missing", func() {
req.Secret.Data = map[string][]byte{}
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.InvalidArgument))
})
})
Context("when server has no NICs", func() {
It("should return Unavailable error", func() {
mockClient.GetNICsFunc = func(_ context.Context, _, _, _ string) ([]*client.NIC, error) {
return []*client.NIC{}, nil
}
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.Unavailable))
Expect(err.Error()).To(ContainSubstring("no NICs found"))
})
})
Context("when STACKIT API fails", func() {
It("should return Internal error on API failure", func() {
mockClient.CreateServerFunc = func(_ context.Context, _, _ string, _ *client.CreateServerRequest) (*client.Server, error) {
return nil, fmt.Errorf("API connection failed")
}
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.Unavailable))
})
It("should return ResourceExhausted when server enters ERROR state with 'no valid host'", func() {
mockClient.CreateServerFunc = func(_ context.Context, _, _ string, req *client.CreateServerRequest) (*client.Server, error) {
return &client.Server{
ID: "550e8400-e29b-41d4-a716-446655440000",
Name: req.Name,
Status: "CREATING",
}, nil
}
mockClient.GetServerFunc = func(_ context.Context, _, _, _ string) (*client.Server, error) {
return &client.Server{
ID: "550e8400-e29b-41d4-a716-446655440000",
Name: "test-machine",
Status: "ERROR",
ErrorMessage: "No valid host was found. There are not enough hosts available.",
}, nil
}
_, err := provider.CreateMachine(ctx, req)
Expect(err).To(HaveOccurred())
statusErr, ok := status.FromError(err)
Expect(ok).To(BeTrue())
Expect(statusErr.Code()).To(Equal(codes.ResourceExhausted))
Expect(err.Error()).To(ContainSubstring("No valid host"))
})
})
})