Skip to content

Commit

Permalink
chore: add new ipclaim tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aamoyel committed May 23, 2024
1 parent 8e99533 commit 878f5f0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It works with 2 custom resources:
- IPClaim

IPCidr allows you to create a specific CIDR in the IPAM (IPv4 and IPv6).
IPClaim is used to allocate/claim specific or non specific IPs and child CIDRs in the IPAM.
IPClaim is used to allocate/claim specific or non-specific IPs and child CIDRs in the IPAM.

## Getting Started

Expand Down Expand Up @@ -74,7 +74,7 @@ spec:
specificChildCidr: 172.16.1.0/24
```
### Non specific
### Non-specific
#### IP
Expand Down
23 changes: 12 additions & 11 deletions internal/controllers/ipcidr_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (
ipamv1alpha1 "github.com/aamoyel/kubipam/api/v1alpha1"
)

const (
timeout = time.Second * 4
interval = time.Second * 1
validCidrName = "test-cidr"
)

var _ = Describe("IPCidr controller", func() {

const (
timeout = time.Second * 10
interval = time.Millisecond * 250
validCidrName = "test-cidr"
)
Context("When create IPCidr", func() {
It("Should change 'registered' field in status and create the cidr in the ipam", func() {
ipCidrLookupKey := types.NamespacedName{Name: validCidrName}
createdIpCidr := &ipamv1alpha1.IPCidr{}

By("Creating a new IPCidr")
ctx := context.Background()
ipcidr := getIpCidr(validCidrName, "192.168.0.0/16")
Expand All @@ -47,12 +48,12 @@ var _ = Describe("IPCidr controller", func() {
By("Creating a new IPCidr with bad prefix")
ipCidrName := "bad-prefix"
ctx := context.Background()
ipcidr := getIpCidr(ipCidrName, "172.16.0.0/33")
ipcidr := getIpCidr(ipCidrName, "10.10.10.0/33")
Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed())
ipCidrLookupKey := types.NamespacedName{Name: ipCidrName}
createdIpCidr := &ipamv1alpha1.IPCidr{}

By("Checking if the 'registered' status field is set to 'false' for the IPCidr that has a bad prefix")
ipCidrLookupKey := types.NamespacedName{Name: ipCidrName}
createdIpCidr := &ipamv1alpha1.IPCidr{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, ipCidrLookupKey, createdIpCidr)
if err != nil {
Expand All @@ -65,10 +66,10 @@ var _ = Describe("IPCidr controller", func() {
ipCidrName = "overlap-cidr"
ipcidr = getIpCidr(ipCidrName, "192.168.1.0/24")
Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed())
ipCidrLookupKey = types.NamespacedName{Name: ipCidrName}
createdIpCidr = &ipamv1alpha1.IPCidr{}

By("Checking if the 'registered' status field is set to 'false' for the IPCidr that overlap an existing registered cidr")
ipCidrLookupKey = types.NamespacedName{Name: ipCidrName}
createdIpCidr = &ipamv1alpha1.IPCidr{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, ipCidrLookupKey, createdIpCidr)
if err != nil {
Expand Down
66 changes: 66 additions & 0 deletions internal/controllers/ipclaim_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package controllers

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

ipamv1alpha1 "github.com/aamoyel/kubipam/api/v1alpha1"
)

var _ = Describe("IPClaim controller", func() {
Context("When create a claim for a non specific IP", func() {
It("Should change 'registered' field in status and get an ip from the ipam", func() {
ipClaimCidr := "ipclaim-cidr"
ctx := context.Background()

By("Creating a new IPCidr")
ipcidr := getIpCidr(ipClaimCidr, "172.16.0.0/16")
Expect(k8sClient.Create(ctx, ipcidr)).Should(Succeed())

By("Creating a new IPClaim with bad prefix")
ipClaimName := "test-ip"
ipclaim := getIPClaim("IP", false, ipClaimName, ipClaimCidr)
Expect(k8sClient.Create(ctx, ipclaim)).Should(Succeed())

By("Checking if the 'registered' status field is set to 'true'")
ipClaimLookupKey := types.NamespacedName{Name: ipClaimName}
claimedIP := &ipamv1alpha1.IPClaim{}
Eventually(func() (bool, error) {
err := k8sClient.Get(ctx, ipClaimLookupKey, claimedIP)
if err != nil {
return false, err
}
return claimedIP.Status.Registered, nil
}, timeout, interval).Should(Equal(true))
})
})
})

func getIPClaim(claimType string, specific bool, name string, cidrName string) *ipamv1alpha1.IPClaim {
claim := &ipamv1alpha1.IPClaim{}
if claimType == "IP" {
if !specific {
claim = &ipamv1alpha1.IPClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: ipamv1alpha1.GroupVersion.Group + "/" + ipamv1alpha1.GroupVersion.Version,
Kind: "IPClaim",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: ipamv1alpha1.IPClaimSpec{
Type: claimType,
IPCidrRef: &ipamv1alpha1.IPCidrRefSpec{
Name: cidrName,
},
},
}
}
}

return claim
}

0 comments on commit 878f5f0

Please sign in to comment.