From 73093167db4e276db168b1a2514c50b0d7da8fb4 Mon Sep 17 00:00:00 2001 From: Patryk Strusiewicz-Surmacki Date: Wed, 10 Jan 2024 17:44:55 +0100 Subject: [PATCH] Added test for empty gateway Signed-off-by: Patryk Strusiewicz-Surmacki --- internal/controllers/ipaddressclaim_test.go | 87 +++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/internal/controllers/ipaddressclaim_test.go b/internal/controllers/ipaddressclaim_test.go index 3675f74..038bf35 100644 --- a/internal/controllers/ipaddressclaim_test.go +++ b/internal/controllers/ipaddressclaim_test.go @@ -257,6 +257,93 @@ var _ = Describe("IPAddressClaimReconciler", func() { }) }) + When("the referenced namespaced pool does not define gateway for subnet", func() { + const poolName = "test-pool" + const claimName = "test-claim" + + var expectedIPAddress ipamv1.IPAddress + + var pool v1alpha1.InfobloxIPPool + + BeforeEach(func() { + localInfobloxClientMock = ibmock.NewMockClient(mockCtrl) + getInfobloxClientForInstanceFunc = mockGetInfobloxClientForInstance + pool = v1alpha1.InfobloxIPPool{ + ObjectMeta: metav1.ObjectMeta{ + Name: poolName, + Namespace: namespace, + }, + Spec: v1alpha1.InfobloxIPPoolSpec{ + InstanceRef: corev1.LocalObjectReference{Name: instanceName}, + Subnets: []v1alpha1.Subnet{ + {CIDR: "10.0.0.0/24"}, + {CIDR: "10.0.1.0/24"}, + }, + NetworkView: "default", + DNSZone: "", + }, + } + Expect(k8sClient.Create(context.Background(), &pool)).To(Succeed()) + }) + + AfterEach(func() { + deleteClaim(claimName, namespace) + deleteNamespacedPool(poolName, namespace) + getInfobloxClientForInstanceFunc = getInfobloxClientForInstance + }) + + It("should allocate an Address from the Pool", func() { + addr, err := netip.ParseAddr("10.0.0.2") + Expect(err).NotTo(HaveOccurred()) + localInfobloxClientMock.EXPECT().GetOrAllocateAddress(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(addr, nil).AnyTimes() + localInfobloxClientMock.EXPECT().ReleaseAddress(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes() + + claim := newClaim(claimName, namespace, "InfobloxIPPool", poolName) + expectedIPAddress = ipamv1.IPAddress{ + ObjectMeta: metav1.ObjectMeta{ + Name: claimName, + Namespace: namespace, + Finalizers: []string{ipamutil.ProtectAddressFinalizer}, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: ipamAPIVersion, + BlockOwnerDeletion: ptr.To[bool](true), + Controller: ptr.To[bool](true), + Kind: "IPAddressClaim", + Name: claimName, + }, + { + APIVersion: "ipam.cluster.x-k8s.io/v1alpha1", + BlockOwnerDeletion: ptr.To[bool](true), + Controller: ptr.To[bool](false), + Kind: "InfobloxIPPool", + Name: poolName, + }, + }, + }, + Spec: ipamv1.IPAddressSpec{ + ClaimRef: corev1.LocalObjectReference{ + Name: claimName, + }, + PoolRef: corev1.TypedLocalObjectReference{ + APIGroup: ptr.To[string]("ipam.cluster.x-k8s.io"), + Kind: "InfobloxIPPool", + Name: poolName, + }, + Address: "10.0.0.2", + Prefix: 24, + }, + } + + Expect(k8sClient.Create(context.Background(), &claim)).To(Succeed()) + + Eventually(findAddress(claimName, namespace)). + WithTimeout(1 * time.Second).WithPolling(100 * time.Millisecond).Should( + EqualObject(&expectedIPAddress, IgnoreAutogeneratedMetadata, IgnoreUIDsOnIPAddress), + ) + }) + }) + When("the referenced namespaced pool does not exists", func() { const wrongPoolName = "wrong-test-pool" const poolName = "test-pool"