Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bruelea committed Nov 18, 2024
1 parent 7204b59 commit 6dd0a08
Show file tree
Hide file tree
Showing 12 changed files with 804 additions and 162 deletions.
2 changes: 1 addition & 1 deletion config/crd/bases/netbox.dev_iprangeclaims.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
listKind: IpRangeClaimList
plural: iprangeclaims
shortNames:
- iprc
- irc
singular: iprangeclaim
scope: Namespaced
versions:
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/netbox.dev_ipranges.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
listKind: IpRangeList
plural: ipranges
shortNames:
- ipr
- ir
singular: iprange
scope: Namespaced
versions:
Expand Down
112 changes: 56 additions & 56 deletions internal/controller/iprange_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,69 @@ limitations under the License.

package controller

import (
"context"
// import (
// "context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
// . "github.com/onsi/ginkgo/v2"
// . "github.com/onsi/gomega"
// "k8s.io/apimachinery/pkg/api/errors"
// "k8s.io/apimachinery/pkg/types"
// "sigs.k8s.io/controller-runtime/pkg/reconcile"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

netboxdevv1 "github.com/netbox-community/netbox-operator/api/v1"
)
// netboxdevv1 "github.com/netbox-community/netbox-operator/api/v1"
// )

var _ = Describe("IpRange Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
// var _ = Describe("IpRange Controller", func() {
// Context("When reconciling a resource", func() {
// const resourceName = "test-resource"

ctx := context.Background()
// ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
iprange := &netboxdevv1.IpRange{}
// typeNamespacedName := types.NamespacedName{
// Name: resourceName,
// Namespace: "default", // TODO(user):Modify as needed
// }
// iprange := &netboxdevv1.IpRange{}

BeforeEach(func() {
By("creating the custom resource for the Kind IpRange")
err := k8sClient.Get(ctx, typeNamespacedName, iprange)
if err != nil && errors.IsNotFound(err) {
resource := &netboxdevv1.IpRange{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
})
// BeforeEach(func() {
// By("creating the custom resource for the Kind IpRange")
// err := k8sClient.Get(ctx, typeNamespacedName, iprange)
// if err != nil && errors.IsNotFound(err) {
// resource := &netboxdevv1.IpRange{
// ObjectMeta: metav1.ObjectMeta{
// Name: resourceName,
// Namespace: "default",
// },
// // TODO(user): Specify other spec details if needed.
// }
// Expect(k8sClient.Create(ctx, resource)).To(Succeed())
// }
// })

AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &netboxdevv1.IpRange{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())
// AfterEach(func() {
// // TODO(user): Cleanup logic after each test, like removing the resource instance.
// resource := &netboxdevv1.IpRange{}
// err := k8sClient.Get(ctx, typeNamespacedName, resource)
// Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance IpRange")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &IpRangeReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
// By("Cleanup the specific resource instance IpRange")
// Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
// })
// It("should successfully reconcile the resource", func() {
// By("Reconciling the created resource")
// controllerReconciler := &IpRangeReconciler{
// Client: k8sClient,
// Scheme: k8sClient.Scheme(),
// }

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// Example: If you expect a certain status condition after reconciliation, verify it here.
})
})
})
// _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
// NamespacedName: typeNamespacedName,
// })
// Expect(err).NotTo(HaveOccurred())
// // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// // Example: If you expect a certain status condition after reconciliation, verify it here.
// })
// })
// })
112 changes: 56 additions & 56 deletions internal/controller/iprangeclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,69 @@ limitations under the License.

package controller

import (
"context"
// import (
// "context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
// . "github.com/onsi/ginkgo/v2"
// . "github.com/onsi/gomega"
// "k8s.io/apimachinery/pkg/api/errors"
// "k8s.io/apimachinery/pkg/types"
// "sigs.k8s.io/controller-runtime/pkg/reconcile"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

netboxdevv1 "github.com/netbox-community/netbox-operator/api/v1"
)
// netboxdevv1 "github.com/netbox-community/netbox-operator/api/v1"
// )

var _ = Describe("IpRangeClaim Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"
// var _ = Describe("IpRangeClaim Controller", func() {
// Context("When reconciling a resource", func() {
// const resourceName = "test-resource"

ctx := context.Background()
// ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
iprangeclaim := &netboxdevv1.IpRangeClaim{}
// typeNamespacedName := types.NamespacedName{
// Name: resourceName,
// Namespace: "default", // TODO(user):Modify as needed
// }
// iprangeclaim := &netboxdevv1.IpRangeClaim{}

BeforeEach(func() {
By("creating the custom resource for the Kind IpRangeClaim")
err := k8sClient.Get(ctx, typeNamespacedName, iprangeclaim)
if err != nil && errors.IsNotFound(err) {
resource := &netboxdevv1.IpRangeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
})
// BeforeEach(func() {
// By("creating the custom resource for the Kind IpRangeClaim")
// err := k8sClient.Get(ctx, typeNamespacedName, iprangeclaim)
// if err != nil && errors.IsNotFound(err) {
// resource := &netboxdevv1.IpRangeClaim{
// ObjectMeta: metav1.ObjectMeta{
// Name: resourceName,
// Namespace: "default",
// },
// // TODO(user): Specify other spec details if needed.
// }
// Expect(k8sClient.Create(ctx, resource)).To(Succeed())
// }
// })

AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &netboxdevv1.IpRangeClaim{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())
// AfterEach(func() {
// // TODO(user): Cleanup logic after each test, like removing the resource instance.
// resource := &netboxdevv1.IpRangeClaim{}
// err := k8sClient.Get(ctx, typeNamespacedName, resource)
// Expect(err).NotTo(HaveOccurred())

By("Cleanup the specific resource instance IpRangeClaim")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
controllerReconciler := &IpRangeClaimReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
// By("Cleanup the specific resource instance IpRangeClaim")
// Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
// })
// It("should successfully reconcile the resource", func() {
// By("Reconciling the created resource")
// controllerReconciler := &IpRangeClaimReconciler{
// Client: k8sClient,
// Scheme: k8sClient.Scheme(),
// }

_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespacedName,
})
Expect(err).NotTo(HaveOccurred())
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// Example: If you expect a certain status condition after reconciliation, verify it here.
})
})
})
// _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
// NamespacedName: typeNamespacedName,
// })
// Expect(err).NotTo(HaveOccurred())
// // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// // Example: If you expect a certain status condition after reconciliation, verify it here.
// })
// })
// })
4 changes: 3 additions & 1 deletion pkg/netbox/api/ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ func (r *NetboxClient) DeleteIpAddress(ipAddressId int64) error {
case *ipam.IpamIPAddressesDeleteDefault:
if typedErr.IsCode(http.StatusNotFound) {
return nil
} else {
return utils.NetboxError("Failed to delete ip address from Netbox", err)
}
default:
return utils.NetboxError("Failed to delete IP Address from Netbox", err)
return utils.NetboxError("Failed to delete ip address from Netbox", err)
}
}
return nil
Expand Down
24 changes: 11 additions & 13 deletions pkg/netbox/api/ip_address_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,7 @@ func (r *NetboxClient) GetAvailableIpAddressByClaim(ipAddressClaim *models.IPAdd
return nil, err
}

var ipMask string
if responseAvailableIPs.Payload[0].Family == int64(IPv4Family) {
ipMask = ipMaskIPv4
} else if responseAvailableIPs.Payload[0].Family == int64(IPv6Family) {
ipMask = ipMaskIPv6
} else {
return nil, errors.New("available ip has unknown IP family")
}

ipAddress, err := r.SetIpAddressMask(responseAvailableIPs.Payload[0].Address, ipMask)
ipAddress, err := r.SetIpAddressMask(responseAvailableIPs.Payload[0].Address, responseAvailableIPs.Payload[0].Family)
if err != nil {
return nil, err
}
Expand All @@ -120,11 +111,18 @@ func (r *NetboxClient) GetAvailableIpAddressesByParentPrefix(parentPrefixId int6
return responseAvailableIPs, nil
}

func (r *NetboxClient) SetIpAddressMask(ip string, ipMask string) (string, error) {
func (r *NetboxClient) SetIpAddressMask(ip string, ipFamily int64) (string, error) {
ipAddress, _, err := net.ParseCIDR(ip)
if err != nil {
return "", err
}
ipAddressWithNewMask := ipAddress.String() + ipMask
return ipAddressWithNewMask, nil

switch ipFamily {
case int64(IPv4Family):
return ipAddress.String() + ipMaskIPv4, nil
case int64(IPv6Family):
return ipAddress.String() + ipMaskIPv6, nil
default:
return "", errors.New("unknown IP family")
}
}
2 changes: 1 addition & 1 deletion pkg/netbox/api/ip_address_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestIPAddressClaim(t *testing.T) {
})

// assert error
AssertError(t, err, "available ip has unknown IP family")
AssertError(t, err, "unknown IP family")

var expected *models.IPAddress

Expand Down
4 changes: 3 additions & 1 deletion pkg/netbox/api/ip_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ func (r *NetboxClient) DeleteIpRange(ipRangeId int64) error {
case *ipam.IpamIPRangesDeleteDefault:
if typedErr.IsCode(http.StatusNotFound) {
return nil
} else {
return utils.NetboxError("Failed to delete ip range from Netbox", err)
}
default:
return utils.NetboxError("Failed to delete IP Range from Netbox", err)
return utils.NetboxError("Failed to delete ip range from Netbox", err)
}
}
return nil
Expand Down
Loading

0 comments on commit 6dd0a08

Please sign in to comment.