mirror of
https://github.com/YuzuZensai/netbird-kubernetes-operator.git
synced 2026-09-13 18:59:09 +00:00
Add unit tests to new controllers and fix minor bugs (#12)
This commit is contained in:
@@ -3,9 +3,13 @@ package v1
|
||||
import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
|
||||
netbirdiov1 "github.com/netbirdio/kubernetes-operator/api/v1"
|
||||
// TODO (user): Add any additional imports if needed
|
||||
)
|
||||
|
||||
var _ = Describe("NBResource Webhook", func() {
|
||||
@@ -16,41 +20,103 @@ var _ = Describe("NBResource Webhook", func() {
|
||||
)
|
||||
|
||||
BeforeEach(func() {
|
||||
Skip("Not implemented yet")
|
||||
obj = &netbirdiov1.NBResource{}
|
||||
oldObj = &netbirdiov1.NBResource{}
|
||||
validator = NBResourceCustomValidator{}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(oldObj).NotTo(BeNil(), "Expected oldObj to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
// TODO (user): Add any setup logic common to all tests
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO (user): Add any teardown logic common to all tests
|
||||
validator = NBResourceCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
})
|
||||
|
||||
Context("When creating or updating NBResource under Validating Webhook", func() {
|
||||
// TODO (user): Add logic for validating webhooks
|
||||
// Example:
|
||||
// It("Should deny creation if a required field is missing", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = ""
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).Error().To(HaveOccurred())
|
||||
// })
|
||||
//
|
||||
// It("Should admit creation if all required fields are present", func() {
|
||||
// By("simulating an invalid creation scenario")
|
||||
// obj.SomeRequiredField = "valid_value"
|
||||
// Expect(validator.ValidateCreate(ctx, obj)).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("Should validate updates correctly", func() {
|
||||
// By("simulating a valid update scenario")
|
||||
// oldObj.SomeRequiredField = "updated_value"
|
||||
// obj.SomeRequiredField = "updated_value"
|
||||
// Expect(validator.ValidateUpdate(ctx, oldObj, obj)).To(BeNil())
|
||||
// })
|
||||
})
|
||||
It("should allow creation", func() {
|
||||
Expect(validator.ValidateCreate(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
It("should allow update", func() {
|
||||
Expect(validator.ValidateUpdate(ctx, oldObj, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
When("No services are exposed", func() {
|
||||
BeforeEach(func() {
|
||||
obj.Name = "maw"
|
||||
obj.Namespace = "default"
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "ne",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
})
|
||||
AfterEach(func() {
|
||||
svc := &netbirdiov1.NBResource{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "ne"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should allow deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
|
||||
When("A service is exposed", func() {
|
||||
BeforeEach(func() {
|
||||
obj.Name = "maw"
|
||||
obj.Namespace = "default"
|
||||
svc := &corev1.Service{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
Name: "maw",
|
||||
Namespace: "default",
|
||||
Annotations: map[string]string{
|
||||
"netbird.io/expose": "true",
|
||||
},
|
||||
},
|
||||
Spec: corev1.ServiceSpec{
|
||||
Ports: []corev1.ServicePort{
|
||||
{
|
||||
Protocol: corev1.ProtocolTCP,
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt32(80),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, svc)).To(Succeed())
|
||||
})
|
||||
AfterEach(func() {
|
||||
svc := &corev1.Service{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "maw"}, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(svc.Finalizers) > 0 {
|
||||
svc.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, svc)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, svc)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
It("should deny deletion", func() {
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user