-
Notifications
You must be signed in to change notification settings - Fork 257
/
openstackmachine_test.go
281 lines (231 loc) · 10.1 KB
/
openstackmachine_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
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
/*
Copyright 2024 The Kubernetes Authors.
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 apivalidations
import (
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
)
var _ = Describe("OpenStackMachine API validations", func() {
var namespace *corev1.Namespace
defaultMachine := func() *infrav1.OpenStackMachine {
// Initialise a basic machine object in the correct namespace
machine := &infrav1.OpenStackMachine{
Spec: infrav1.OpenStackMachineSpec{
Image: infrav1.ImageParam{Filter: &infrav1.ImageFilter{Name: ptr.To("test-image")}},
},
}
machine.Namespace = namespace.Name
machine.GenerateName = "machine-"
return machine
}
BeforeEach(func() {
namespace = createNamespace()
})
It("should allow the smallest permissible machine spec", func() {
Expect(k8sClient.Create(ctx, defaultMachine())).To(Succeed(), "OpenStackMachine creation should succeed")
})
It("should only allow the providerID to be set once", func() {
machine := defaultMachine()
By("Creating a bare machine")
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "OpenStackMachine creation should succeed")
By("Setting the providerID")
machine.Spec.ProviderID = ptr.To("foo")
Expect(k8sClient.Update(ctx, machine)).To(Succeed(), "Setting providerID should succeed")
By("Modifying the providerID")
machine.Spec.ProviderID = ptr.To("bar")
Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Updating providerID should fail")
})
It("should not allow server metadata to exceed 255 characters", func() {
machine := defaultMachine()
By("Creating a machine with a metadata key that is too long")
machine.Spec.ServerMetadata = []infrav1.ServerMetadata{
{
Key: strings.Repeat("a", 256),
Value: "value",
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a long metadata key should fail")
By("Creating a machine with a metadata value that is too long")
machine.Spec.ServerMetadata = []infrav1.ServerMetadata{
{
Key: "key",
Value: strings.Repeat("a", 256),
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a long metadata value should fail")
By("Creating a machine with a metadata key and value of 255 characters should succeed")
machine.Spec.ServerMetadata = []infrav1.ServerMetadata{
{
Key: strings.Repeat("a", 255),
Value: strings.Repeat("b", 255),
},
}
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with max metadata key and value should succeed")
})
Context("volumes", func() {
It("should not allow volume with zero size", func() {
machine := defaultMachine()
machine.Spec.RootVolume = &infrav1.RootVolume{
SizeGiB: 0,
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a zero size root volume should fail")
machine = defaultMachine()
machine.Spec.AdditionalBlockDevices = []infrav1.AdditionalBlockDevice{
{
Name: "test-volume",
SizeGiB: 0,
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a zero size additional block device should fail")
})
/* FIXME: These tests are failing
It("should not allow additional volume with empty name", func() {
machine.Spec.AdditionalBlockDevices = []infrav1.AdditionalBlockDevice{
{
Name: "",
SizeGiB: 1,
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an empty name additional block device should fail")
})
It("should not allow additional volume with name root", func() {
machine.Spec.AdditionalBlockDevices = []infrav1.AdditionalBlockDevice{
{
Name: "root",
SizeGiB: 1,
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root named additional block device should fail")
})
*/
It("should not allow additional volume with duplicate name", func() {
machine := defaultMachine()
machine.Spec.AdditionalBlockDevices = []infrav1.AdditionalBlockDevice{
{
Name: "test-volume",
SizeGiB: 1,
},
{
Name: "test-volume",
SizeGiB: 2,
},
}
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with duplicate named additional block device should fail")
})
defaultMachineWithRootVolumeAZ := func(az *infrav1.VolumeAvailabilityZone) *infrav1.OpenStackMachine {
machine := defaultMachine()
machine.Spec.RootVolume = &infrav1.RootVolume{
SizeGiB: 1,
}
machine.Spec.RootVolume.AvailabilityZone = az
return machine
}
defaultMachineWithAdditionBlockDeviceAZ := func(az *infrav1.VolumeAvailabilityZone) *infrav1.OpenStackMachine {
machine := defaultMachine()
machine.Spec.AdditionalBlockDevices = []infrav1.AdditionalBlockDevice{
{
Name: "test-volume",
SizeGiB: 1,
Storage: infrav1.BlockDeviceStorage{
Type: infrav1.VolumeBlockDevice,
Volume: &infrav1.BlockDeviceVolume{
AvailabilityZone: az,
},
},
},
}
return machine
}
It("should allow volume with defaulted AZ from", func() {
azName := infrav1.VolumeAZName("test-az")
az := infrav1.VolumeAvailabilityZone{
Name: &azName,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with a root volume with an availability zone should succeed")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with an additional block device with an availability zone should succeed")
})
It("should allow volume with AZ from Name", func() {
azName := infrav1.VolumeAZName("test-az")
az := infrav1.VolumeAvailabilityZone{
From: infrav1.VolumeAZFromName,
Name: &azName,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with a root volume with an availability zone should succeed")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with an additional block device with an availability zone should succeed")
})
It("should allow volume AZ from Machine", func() {
az := infrav1.VolumeAvailabilityZone{
From: infrav1.VolumeAZFromMachine,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with a root volume with an availability zone should succeed")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with an additional block device with an availability zone should succeed")
})
It("should not allow volume AZ with invalid from", func() {
az := infrav1.VolumeAvailabilityZone{
From: "invalid",
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root volume with an invalid availability zone should fail")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an additional block device with an invalid availability zone should fail")
})
It("should not allow empty volume AZ", func() {
az := infrav1.VolumeAvailabilityZone{}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root volume with an empty availability zone should fail")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an additional block device with an empty availability zone should fail")
})
It("should not allow volume AZ from Name with missing name", func() {
az := infrav1.VolumeAvailabilityZone{
From: infrav1.VolumeAZFromName,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root volume with a missing name availability zone should fail")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an additional block device with a missing name availability zone should fail")
})
It("should not allow volume AZ from Machine with name", func() {
azName := infrav1.VolumeAZName("test-az")
az := infrav1.VolumeAvailabilityZone{
From: infrav1.VolumeAZFromMachine,
Name: &azName,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root volume with a name availability zone should fail")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an additional block device with a name availability zone should fail")
})
It("should not allow volume AZ from Name with empty name", func() {
azName := infrav1.VolumeAZName("")
az := infrav1.VolumeAvailabilityZone{
From: infrav1.VolumeAZFromName,
Name: &azName,
}
machine := defaultMachineWithRootVolumeAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a root volume with an empty name availability zone should fail")
machine = defaultMachineWithAdditionBlockDeviceAZ(&az)
Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with an additional block device with an empty name availability zone should fail")
})
})
})