add ServiceCIDR APIs
Change-Id: Ia084c5505e43033ac34449031a1d32418ca326fd Change-Id: Iafc236d456f7185a5c89a65d7b96245e04060013
This commit is contained in:
@@ -2046,3 +2046,207 @@ func TestValidateIPAddressUpdate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateServiceCIDR(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
expectedErrors int
|
||||
ipRange *networking.ServiceCIDR
|
||||
}{
|
||||
"empty-iprange": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
},
|
||||
},
|
||||
"good-iprange-ipv4": {
|
||||
expectedErrors: 0,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.0/24",
|
||||
},
|
||||
},
|
||||
},
|
||||
"good-iprange-ipv6": {
|
||||
expectedErrors: 0,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv6: "fd00:1234::/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
"good-iprange-ipv4-ipv6": {
|
||||
expectedErrors: 0,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.0/24",
|
||||
IPv6: "fd00:1234::/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
"not-iprange-ipv4": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "sadasdsad",
|
||||
},
|
||||
},
|
||||
},
|
||||
"iponly-iprange-ipv4": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.1",
|
||||
},
|
||||
},
|
||||
},
|
||||
"badip-iprange-ipv4": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.1/24",
|
||||
},
|
||||
},
|
||||
},
|
||||
"badip-iprange-ipv6": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv6: "fd00:1234::2/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
"badip-iprange-caps-ipv6": {
|
||||
expectedErrors: 2,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv6: "FD00:1234::2/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
"good-iprange-ipv4-bad-ipv6": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.0/24",
|
||||
IPv6: "FD00:1234::/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
"good-iprange-ipv6-bad-ipv4": {
|
||||
expectedErrors: 1,
|
||||
ipRange: &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-name",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.007.0/24",
|
||||
IPv6: "fd00:1234::/64",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for name, testCase := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
errs := ValidateServiceCIDR(testCase.ipRange)
|
||||
if len(errs) != testCase.expectedErrors {
|
||||
t.Errorf("Expected %d errors, got %d errors: %v", testCase.expectedErrors, len(errs), errs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateServiceCIDRUpdate(t *testing.T) {
|
||||
oldServiceCIDR := &networking.ServiceCIDR{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "mysvc",
|
||||
ResourceVersion: "1",
|
||||
},
|
||||
Spec: networking.ServiceCIDRSpec{
|
||||
IPv4: "192.168.0.0/24",
|
||||
IPv6: "2001:db2::/64",
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
svc func(svc *networking.ServiceCIDR) *networking.ServiceCIDR
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "Successful update, no changes",
|
||||
svc: func(svc *networking.ServiceCIDR) *networking.ServiceCIDR {
|
||||
out := svc.DeepCopy()
|
||||
return out
|
||||
},
|
||||
expectErr: false,
|
||||
},
|
||||
|
||||
{
|
||||
name: "Failed update, update spec.IPv4",
|
||||
svc: func(svc *networking.ServiceCIDR) *networking.ServiceCIDR {
|
||||
out := svc.DeepCopy()
|
||||
out.Spec.IPv4 = "10.0.0.0/16"
|
||||
return out
|
||||
}, expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Failed update, update spec.IPv6",
|
||||
svc: func(svc *networking.ServiceCIDR) *networking.ServiceCIDR {
|
||||
out := svc.DeepCopy()
|
||||
out.Spec.IPv6 = "fd00:1:2:3::/64"
|
||||
return out
|
||||
}, expectErr: true,
|
||||
},
|
||||
{
|
||||
name: "Failed update, update spec.IPv4 and spec.IPv6",
|
||||
svc: func(svc *networking.ServiceCIDR) *networking.ServiceCIDR {
|
||||
out := svc.DeepCopy()
|
||||
out.Spec.IPv4 = "10.0.0.0/16"
|
||||
out.Spec.IPv6 = "fd00:1:2:3::/64"
|
||||
return out
|
||||
}, expectErr: true,
|
||||
},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := ValidateServiceCIDRUpdate(testCase.svc(oldServiceCIDR), oldServiceCIDR)
|
||||
if !testCase.expectErr && err != nil {
|
||||
t.Errorf("ValidateServiceCIDRUpdate must be successful for test '%s', got %v", testCase.name, err)
|
||||
}
|
||||
if testCase.expectErr && err == nil {
|
||||
t.Errorf("ValidateServiceCIDRUpdate must return error for test: %s, but got nil", testCase.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user