Skip to content

Commit 07e673b

Browse files
committed
add webhook test
1 parent f125678 commit 07e673b

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

internal/webhook/v1alpha2/linodemachine_webhook_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"math"
23+
"slices"
2324
"strconv"
2425
"testing"
2526

@@ -54,6 +55,8 @@ func TestValidateLinodeMachine(t *testing.T) {
5455
Type: "example",
5556
},
5657
}
58+
region = linodego.Region{ID: "test"}
59+
capabilities = []string{linodego.CapabilityLinodeInterfaces}
5760
disk = infrav1alpha2.InstanceDisk{Size: resource.MustParse("1G")}
5861
disk_zero = infrav1alpha2.InstanceDisk{Size: *resource.NewQuantity(0, resource.BinarySI)}
5962
plan = linodego.LinodeType{Disk: 2 * int(disk.Size.ScaledValue(resource.Mega))}
@@ -163,6 +166,72 @@ func TestValidateLinodeMachine(t *testing.T) {
163166
}
164167
}),
165168
),
169+
Path(
170+
Call("invalid linode interfaces with private IP", func(ctx context.Context, mck Mock) {
171+
region := region
172+
region.Capabilities = slices.Clone(capabilities)
173+
mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(&region, nil).AnyTimes()
174+
mck.LinodeClient.EXPECT().GetType(gomock.Any(), gomock.Any()).Return(&plan_max, nil).AnyTimes()
175+
}),
176+
Result("error", func(ctx context.Context, mck Mock) {
177+
machine := machine
178+
machine.Spec.LinodeInterfaces = []infrav1alpha2.LinodeInterfaceCreateOptions{{}}
179+
machine.Spec.PrivateIP = ptr.To(true)
180+
errs := validator.validateLinodeMachineSpec(ctx, mck.LinodeClient, machine.Spec, SkipAPIValidation)
181+
for _, err := range errs {
182+
assert.ErrorContains(t, err, "Linode Interfaces do not support private IPs")
183+
}
184+
}),
185+
),
186+
Path(
187+
Call("invalid linode interfaces with network helpers", func(ctx context.Context, mck Mock) {
188+
region := region
189+
region.Capabilities = slices.Clone(capabilities)
190+
mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(&region, nil).AnyTimes()
191+
mck.LinodeClient.EXPECT().GetType(gomock.Any(), gomock.Any()).Return(&plan_max, nil).AnyTimes()
192+
}),
193+
Result("error", func(ctx context.Context, mck Mock) {
194+
machine := machine
195+
machine.Spec.LinodeInterfaces = []infrav1alpha2.LinodeInterfaceCreateOptions{{}}
196+
machine.Spec.NetworkHelper = ptr.To(true)
197+
errs := validator.validateLinodeMachineSpec(ctx, mck.LinodeClient, machine.Spec, SkipAPIValidation)
198+
for _, err := range errs {
199+
assert.ErrorContains(t, err, "Linode Interfaces do not support configuring network helper")
200+
}
201+
}),
202+
),
203+
Path(
204+
Call("invalid linode interfaces with legacy interfaces", func(ctx context.Context, mck Mock) {
205+
region := region
206+
region.Capabilities = slices.Clone(capabilities)
207+
mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(&region, nil).AnyTimes()
208+
mck.LinodeClient.EXPECT().GetType(gomock.Any(), gomock.Any()).Return(&plan_max, nil).AnyTimes()
209+
}),
210+
Result("error", func(ctx context.Context, mck Mock) {
211+
machine := machine
212+
machine.Spec.LinodeInterfaces = []infrav1alpha2.LinodeInterfaceCreateOptions{{}}
213+
machine.Spec.Interfaces = []infrav1alpha2.InstanceConfigInterfaceCreateOptions{{}}
214+
errs := validator.validateLinodeMachineSpec(ctx, mck.LinodeClient, machine.Spec, SkipAPIValidation)
215+
for _, err := range errs {
216+
assert.ErrorContains(t, err, "Cannot specify both LinodeInterfaces and Interfaces")
217+
}
218+
}),
219+
),
220+
Path(
221+
Call("linode interfaces with invalid region", func(ctx context.Context, mck Mock) {
222+
region := region
223+
mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(&region, nil).AnyTimes()
224+
mck.LinodeClient.EXPECT().GetType(gomock.Any(), gomock.Any()).Return(&plan_max, nil).AnyTimes()
225+
}),
226+
Result("error", func(ctx context.Context, mck Mock) {
227+
machine := machine
228+
machine.Spec.LinodeInterfaces = []infrav1alpha2.LinodeInterfaceCreateOptions{{}}
229+
errs := validator.validateLinodeMachineSpec(ctx, mck.LinodeClient, machine.Spec, SkipAPIValidation)
230+
for _, err := range errs {
231+
assert.ErrorContains(t, err, "no capability: Linode Interfaces")
232+
}
233+
}),
234+
),
166235
),
167236
)
168237
}

0 commit comments

Comments
 (0)