@@ -16,69 +16,82 @@ limitations under the License.
1616
1717package controller
1818
19- // import (
20- // "context"
21-
22- // . "github.com/onsi/ginkgo/v2"
23- // . "github.com/onsi/gomega"
24- // "k8s.io/apimachinery/pkg/api/errors"
25- // "k8s.io/apimachinery/pkg/types"
26- // "sigs.k8s.io/controller-runtime/pkg/reconcile"
27-
28- // metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29-
30- // netboxdevv1 "github.com/netbox-community/netbox-operator/api/v1"
31- // )
32-
33- // var _ = Describe("IpRange Controller", func() {
34- // Context("When reconciling a resource", func() {
35- // const resourceName = "test-resource"
36-
37- // ctx := context.Background()
38-
39- // typeNamespacedName := types.NamespacedName{
40- // Name: resourceName,
41- // Namespace: "default", // TODO(user):Modify as needed
42- // }
43- // iprange := &netboxdevv1.IpRange{}
44-
45- // BeforeEach(func() {
46- // By("creating the custom resource for the Kind IpRange")
47- // err := k8sClient.Get(ctx, typeNamespacedName, iprange)
48- // if err != nil && errors.IsNotFound(err) {
49- // resource := &netboxdevv1.IpRange{
50- // ObjectMeta: metav1.ObjectMeta{
51- // Name: resourceName,
52- // Namespace: "default",
53- // },
54- // // TODO(user): Specify other spec details if needed.
55- // }
56- // Expect(k8sClient.Create(ctx, resource)).To(Succeed())
57- // }
58- // })
59-
60- // AfterEach(func() {
61- // // TODO(user): Cleanup logic after each test, like removing the resource instance.
62- // resource := &netboxdevv1.IpRange{}
63- // err := k8sClient.Get(ctx, typeNamespacedName, resource)
64- // Expect(err).NotTo(HaveOccurred())
65-
66- // By("Cleanup the specific resource instance IpRange")
67- // Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
68- // })
69- // It("should successfully reconcile the resource", func() {
70- // By("Reconciling the created resource")
71- // controllerReconciler := &IpRangeReconciler{
72- // Client: k8sClient,
73- // Scheme: k8sClient.Scheme(),
74- // }
75-
76- // _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
77- // NamespacedName: typeNamespacedName,
78- // })
79- // Expect(err).NotTo(HaveOccurred())
80- // // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
81- // // Example: If you expect a certain status condition after reconciliation, verify it here.
82- // })
83- // })
84- // })
19+ import (
20+ . "github.com/onsi/ginkgo/v2"
21+ . "github.com/onsi/gomega"
22+
23+ "github.com/netbox-community/netbox-operator/pkg/netbox/api"
24+ "github.com/netbox-community/netbox-operator/pkg/netbox/models"
25+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+ "sigs.k8s.io/controller-runtime/pkg/client"
27+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
28+
29+ netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
30+ )
31+
32+ var ipRangeRecondiler *IpRangeReconciler
33+
34+ var _ = Describe("IpRange Controller", func() {
35+ Context("When generating NetBox IpRange Model form IpRangeSpec", func() {
36+ // dummy reconciler
37+ ipRangeRecondiler = &IpRangeReconciler{
38+ NetboxClient: &api.NetboxClient{
39+ Ipam: ipamMockIpAddress,
40+ Tenancy: tenancyMock,
41+ Dcim: dcimMock,
42+ },
43+ }
44+
45+ // default IpRange
46+ ipRange := &netboxv1.IpRange{
47+ ObjectMeta: metav1.ObjectMeta{
48+ Namespace: "default",
49+ },
50+ Spec: netboxv1.IpRangeSpec{
51+ StartAddress: "1.0.0.1/32",
52+ EndAddress: "1.0.0.5/32",
53+ Comments: "a comment",
54+ Description: "a description",
55+ Tenant: "a tenant",
56+ CustomFields: map[string]string{"custom_field_2": "valueToBeSet"},
57+ }}
58+ ipRange.Name = "test-claim"
59+
60+ // default managedCustomFieldsAnnotation
61+ managedCustomFieldsAnnotation := "{\"custom_field_1\":\"valueToBeRemoved\"}"
62+
63+ // default request
64+ req := reconcile.Request{
65+ NamespacedName: client.ObjectKey{
66+ Name: "test-claim",
67+ Namespace: "default",
68+ },
69+ }
70+
71+ It("should create the correct ip range model", func() {
72+ ipRangeModel, err := ipRangeRecondiler.generateNetboxIpRangeModelFromIpRangeSpec(ipRange, req, managedCustomFieldsAnnotation)
73+
74+ Expect(ipRangeModel).To(Equal(&models.IpRange{
75+ Metadata: &models.NetboxMetadata{
76+ Comments: "a comment",
77+ Description: "default/test-claim // a description // managed by netbox-operator, please don't edit it in Netbox unless you know what you're doing",
78+ Custom: map[string]string{"custom_field_2": "valueToBeSet", "custom_field_1": ""},
79+ Tenant: "a tenant",
80+ },
81+ StartAddress: "1.0.0.1/32",
82+ EndAddress: "1.0.0.5/32",
83+ }))
84+
85+ Expect(err).To(BeNil())
86+ })
87+
88+ It("should return error if parsing of annotation fails", func() {
89+ invalidManagedCustomFieldsAnnotation := "{:\"valueToBeRemoved\"}"
90+ ipRangeModel, err := ipRangeRecondiler.generateNetboxIpRangeModelFromIpRangeSpec(ipRange, req, invalidManagedCustomFieldsAnnotation)
91+
92+ Expect(ipRangeModel).To(BeNil())
93+
94+ Expect(err).To(HaveOccurred())
95+ })
96+ })
97+ })
0 commit comments