Skip to content

Commit

Permalink
Added a few UT cases to cover the new IPv6 allocation pool cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Levovar committed Feb 6, 2020
1 parent 09a29fa commit 5ed12de
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
6 changes: 6 additions & 0 deletions pkg/admit/netadmit.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func createPatchListFromNetChanges(origNetwork danmtypes.DanmNet, changedNetwork
//TODO: Could (?) use some reflecting here to determine name of the struct field
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Alloc"], changedNetwork.Spec.Options.Alloc))
}
if origNetwork.Spec.Options.Alloc6 != changedNetwork.Spec.Options.Alloc6 {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Alloc6"], changedNetwork.Spec.Options.Alloc6))
}
if origNetwork.Spec.NetworkType != changedNetwork.Spec.NetworkType {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["NetworkType"], changedNetwork.Spec.NetworkType))
}
Expand All @@ -218,6 +221,9 @@ func createPatchListFromNetChanges(origNetwork danmtypes.DanmNet, changedNetwork
if !reflect.DeepEqual(origNetwork.Spec.Options.Pool, changedNetwork.Spec.Options.Pool) {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Pool"], changedNetwork.Spec.Options.Pool))
}
if !reflect.DeepEqual(origNetwork.Spec.Options.Pool6, changedNetwork.Spec.Options.Pool6) {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Pool6"], changedNetwork.Spec.Options.Pool6))
}
if origNetwork.Spec.Options.Device != changedNetwork.Spec.Options.Device {
patchList = append(patchList, CreateGenericPatchFromChange(NetworkPatchPaths["Device"], changedNetwork.Spec.Options.Device))
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/admit/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func validateAllocV6(newManifest *danmtypes.DanmNet, v4PoolMask int) error {
if err != nil {
return errors.New("spec.Options.Pool6.CIDR is invalid!")
}
if allocCidr.IP.To16() == nil || netCidr.IP.To16() == nil {
if allocCidr.IP.To4() != nil || netCidr.IP.To4() != nil {
return errors.New("IPv6 CIDRs are not valid V6 subnets!")
}
netMaskSize, _ := allocCidr.Mask.Size()
Expand All @@ -150,10 +150,10 @@ func validateAllocV6(newManifest *danmtypes.DanmNet, v4PoolMask int) error {
if newManifest.Spec.Options.Pool6.Start == "" {
newManifest.Spec.Options.Pool6.Start = cidr.Inc(allocCidr.IP).String()
}
if newManifest.Spec.Options.Pool.End == "" {
newManifest.Spec.Options.Pool.End = cidr.Dec(GetBroadcastAddress(allocCidr)).String()
if newManifest.Spec.Options.Pool6.End == "" {
newManifest.Spec.Options.Pool6.End = cidr.Dec(GetBroadcastAddress(allocCidr)).String()
}
if ipam.Ip2int(net.ParseIP(newManifest.Spec.Options.Pool.End)) <= ipam.Ip2int(net.ParseIP(newManifest.Spec.Options.Pool.Start)) {
if ipam.Ip62int(net.ParseIP(newManifest.Spec.Options.Pool6.End)).Cmp(ipam.Ip62int(net.ParseIP(newManifest.Spec.Options.Pool6.Start))) <=0 {
return errors.New("Allocation pool start:" + newManifest.Spec.Options.Pool.Start + " is bigger than or equal to allocation pool end:" + newManifest.Spec.Options.Pool.End)
}
if newManifest.Spec.Options.Alloc6 == "" {
Expand Down
10 changes: 8 additions & 2 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bytes"
"errors"
"log"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -205,20 +206,25 @@ func validatePatches(response *v1beta1.AdmissionResponse, expectedPatches []admi
if err != nil {
return err
}
if len(patches) != len(expectedPatches) {
return errors.New("received number of patches:" + strconv.Itoa(len(patches)) + " was not what we expected:" + strconv.Itoa(len(expectedPatches)))
for _, recPatch := range patches {
log.Println("Received patch for path:" + recPatch.Path)
}
for _, expPatch := range expectedPatches {
var foundMatchingPatch bool
for _, recPatch := range patches {
if expPatch.Path == recPatch.Path {
foundMatchingPatch = true
break
}
}
if !foundMatchingPatch {
return errors.New("Patch expected to modify path:" + expPatch.Path + " was not included in the response")
}
}
if len(patches) != len(expectedPatches) {
return errors.New("received number of patches:" + strconv.Itoa(len(patches)) + " was not what we expected:" + strconv.Itoa(len(expectedPatches)))
}

return nil
}

Expand Down
52 changes: 52 additions & 0 deletions test/uts/admit_tests/netadmit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ var validateNetworkTcs = []struct {
{"NotOkayToModifyDeviceDNet", "vniOld", "deviceNew", DnetType, v1beta1.Update, nil, matchDnet, true, nil, 0},
{"NotOkayToModifyDeviceCNet", "vniOld", "deviceNew", CnetType, v1beta1.Update, nil, matchCnet, true, nil, 0},
{"OkayToModifyRandomChangeCNet", "vniOld", "nidNew", CnetType, v1beta1.Update, nil, matchCnet, false, nil, 0},
{"Ipv6ProvidedAsCidrDNet", "", "v6-as-cidr", DnetType, "", nil, nil, true, nil, 0},
{"Ipv6ProvidedAsCidrTNet", "", "v6-as-cidr", TnetType, "", nil, nil, true, nil, 0},
{"Ipv6ProvidedAsCidrCNet", "", "v6-as-cidr", CnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsNet6DNet", "", "v4-as-net6", DnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsNet6TNet", "", "v4-as-net6", TnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsNet6CNet", "", "v4-as-net6", CnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsPool6CidrDNet", "", "v4-as-pool6", DnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsPool6CidrTNet", "", "v4-as-pool6", TnetType, "", nil, nil, true, nil, 0},
{"Ipv4ProvidedAsPool6CidrCNet", "", "v4-as-pool6", CnetType, "", nil, nil, true, nil, 0},
{"InvalidPool6CidrDNet", "", "invalid-pool6", DnetType, "", nil, nil, true, nil, 0},
{"InvalidPool6CidrTNet", "", "invalid-pool6", TnetType, "", nil, nil, true, nil, 0},
{"InvalidPool6CidrCNet", "", "invalid-pool6", CnetType, "", nil, nil, true, nil, 0},
{"Pool6CidrWithoutNet6DNet", "", "pool6-wo-net6", DnetType, "", nil, nil, true, nil, 0},
{"Pool6CidrWithoutNet6TNet", "", "pool6-wo-net6", TnetType, "", nil, nil, true, nil, 0},
{"Pool6CidrWithoutNet6CNet", "", "pool6-wo-net6", CnetType, "", nil, nil, true, nil, 0},
{"CreateV6NetworkWithoutPool6DNet", "", "net6-without-pool6", DnetType, v1beta1.Create, nil, nil, false, v6Allocs, 0},
{"CreateV6NetworkWithoutPool6TNet", "", "net6-without-pool6", TnetType, v1beta1.Create, randomDev, nil, false, v6AllocsForTnet, 1},
{"CreateV6NetworkWithoutPool6CNet", "", "net6-without-pool6", CnetType, v1beta1.Create, nil, nil, false, v6Allocs, 0},
}

var (
Expand Down Expand Up @@ -304,6 +322,30 @@ var (
ObjectMeta: meta_v1.ObjectMeta {Name: "nidNew", Namespace: "vni-test"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "e2", Options: danmtypes.DanmNetOption{Device: "ens4", Vlan: 50}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "v6-as-cidr"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Cidr: "2a00:8a00:a000:1193::/64"}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "v4-as-net6"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Net6: "192.168.1.0/24"}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "v4-as-pool6"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Net6: "2a00:8a00:a000:1193::/64", Pool6: danmtypes.IpPoolV6{Cidr: "192.168.1.0/24"}}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "invalid-pool6"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Net6: "2a00:8a00:a000:1193::/64", Pool6: danmtypes.IpPoolV6{Cidr: "2a00:8a00:a000:1193::/129"}}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "pool6-wo-net6"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Pool6: danmtypes.IpPoolV6{Cidr: "2a00:8a00:a000:1193::/64"}}},
},
danmtypes.DanmNet {
ObjectMeta: meta_v1.ObjectMeta {Name: "net6-without-pool6"},
Spec: danmtypes.DanmNetSpec{NetworkType: "ipvlan", NetworkID: "nanomsg", Options: danmtypes.DanmNetOption{Net6: "2a00:8a00:a000:1193::/64"}},
},
}
)

Expand Down Expand Up @@ -337,6 +379,16 @@ var (
admit.Patch {Path: "/spec/Options/host_device"},
admit.Patch {Path: "/spec/Options/vxlan"},
}
v6Allocs = []admit.Patch {
admit.Patch {Path: "/spec/Options/alloc6"},
admit.Patch {Path: "/spec/Options/allocation_pool_v6"},
}
v6AllocsForTnet = []admit.Patch {
admit.Patch {Path: "/spec/Options/host_device"},
admit.Patch {Path: "/spec/Options/vxlan"},
admit.Patch {Path: "/spec/Options/alloc6"},
admit.Patch {Path: "/spec/Options/allocation_pool_v6"},
}
)

var (
Expand Down

0 comments on commit 5ed12de

Please sign in to comment.