Skip to content

Commit

Permalink
merged ip_test and iploop_test
Browse files Browse the repository at this point in the history
Signed-off-by: nicklesimba <[email protected]>
  • Loading branch information
nicklesimba committed Jul 7, 2022
1 parent 1e4de63 commit 204387c
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 153 deletions.
141 changes: 141 additions & 0 deletions pkg/reconciler/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ import (
"fmt"
"net"
"strings"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

multusv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/api/whereabouts.cni.cncf.io/v1alpha1"
"github.com/k8snetworkplumbingwg/whereabouts/pkg/types"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8stypes "k8s.io/apimachinery/pkg/types"
)

func TestIPReconciler(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Reconcile IP address allocation in the system")
}

var _ = Describe("Whereabouts IP reconciler", func() {
const (
firstIPInRange = "10.10.10.1"
Expand Down Expand Up @@ -285,6 +292,117 @@ var _ = Describe("Whereabouts IP reconciler", func() {
})
})

// mock the pool
type dummyPool struct {
orphans []types.IPReservation
pool v1alpha1.IPPool
}

func (dp dummyPool) Allocations() []types.IPReservation {
return dp.orphans
}

func (dp dummyPool) Update(context.Context, []types.IPReservation) error {
return nil
}

var _ = Describe("IPReconciler", func() {
var ipReconciler *ReconcileLooper

newIPReconciler := func(orphanedIPs ...OrphanedIPReservations) *ReconcileLooper {
reconciler := &ReconcileLooper{
orphanedIPs: orphanedIPs,
}

return reconciler
}

When("there are no IP addresses to reconcile", func() {
BeforeEach(func() {
ipReconciler = newIPReconciler()
})

It("does not delete anything", func() {
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
Expect(err).NotTo(HaveOccurred())
Expect(reconciledIPs).To(BeEmpty())
})
})

When("there are IP addresses to reconcile", func() {
const (
firstIPInRange = "192.168.14.1"
ipCIDR = "192.168.14.0/24"
namespace = "default"
podName = "pod1"
)

BeforeEach(func() {
podRef := "default/pod1"
reservations := generateIPReservation(firstIPInRange, podRef)

pool := generateIPPool(ipCIDR, podRef)
orphanedIPAddr := OrphanedIPReservations{
Pool: dummyPool{orphans: reservations, pool: pool},
Allocations: reservations,
}

ipReconciler = newIPReconciler(orphanedIPAddr)
})

It("does delete the orphaned IP address", func() {
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
Expect(err).NotTo(HaveOccurred())
Expect(reconciledIPs).To(Equal([]net.IP{net.ParseIP(firstIPInRange)}))
})

Context("and they are actually multiple IPs", func() {
BeforeEach(func() {
podRef := "default/pod2"
reservations := generateIPReservation("192.168.14.2", podRef)

pool := generateIPPool(ipCIDR, podRef, "default/pod2", "default/pod3")
orphanedIPAddr := OrphanedIPReservations{
Pool: dummyPool{orphans: reservations, pool: pool},
Allocations: reservations,
}

ipReconciler = newIPReconciler(orphanedIPAddr)
})

It("does delete *only the orphaned* the IP address", func() {
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
Expect(err).NotTo(HaveOccurred())
Expect(reconciledIPs).To(ConsistOf([]net.IP{net.ParseIP("192.168.14.2")}))
})
})

Context("but the IP reservation owner does not match", func() {
var reservationPodRef string
BeforeEach(func() {
reservationPodRef = "default/pod2"
podRef := "default/pod1"
reservations := generateIPReservation(firstIPInRange, podRef)
erroredReservations := generateIPReservation(firstIPInRange, reservationPodRef)

pool := generateIPPool(ipCIDR, podRef)
orphanedIPAddr := OrphanedIPReservations{
Pool: dummyPool{orphans: reservations, pool: pool},
Allocations: erroredReservations,
}

ipReconciler = newIPReconciler(orphanedIPAddr)
})

It("errors when attempting to clean up the IP address", func() {
reconciledIPs, err := ipReconciler.ReconcileIPPools(context.TODO())
Expect(err).To(MatchError(fmt.Sprintf("did not find reserved IP for container %s", reservationPodRef)))
Expect(reconciledIPs).To(BeEmpty())
})
})
})
})

func generateIPPoolSpec(ipRange string, namespace string, poolName string, podNames ...string) *v1alpha1.IPPool {
allocations := map[string]v1alpha1.IPAllocation{}
for i, podName := range podNames {
Expand Down Expand Up @@ -369,3 +487,26 @@ func generatePodNetworkStatusAnnotation(ipNetworks ...ipInNetwork) string {

return string(networkStatusStr)
}

func generateIPPool(cidr string, podRefs ...string) v1alpha1.IPPool {
allocations := map[string]v1alpha1.IPAllocation{}
for i, podRef := range podRefs {
allocations[fmt.Sprintf("%d", i)] = v1alpha1.IPAllocation{PodRef: podRef}
}

return v1alpha1.IPPool{
Spec: v1alpha1.IPPoolSpec{
Range: cidr,
Allocations: allocations,
},
}
}

func generateIPReservation(ip string, podRef string) []types.IPReservation {
return []types.IPReservation{
{
IP: net.ParseIP(ip),
PodRef: podRef,
},
}
}
153 changes: 0 additions & 153 deletions pkg/reconciler/iploop_test.go

This file was deleted.

0 comments on commit 204387c

Please sign in to comment.