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:
@@ -6,6 +6,7 @@ import (
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
|
||||
@@ -30,11 +31,6 @@ var _ = Describe("NBSetupKey Webhook", func() {
|
||||
validator = NBSetupKeyCustomValidator{
|
||||
client: k8sClient,
|
||||
}
|
||||
Expect(validator).NotTo(BeNil(), "Expected validator to be initialized")
|
||||
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
})
|
||||
|
||||
Context("When creating or updating NBSetupKey under Validating Webhook", func() {
|
||||
@@ -119,6 +115,96 @@ var _ = Describe("NBSetupKey Webhook", func() {
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Context("Delete", func() {
|
||||
When("No pods exist with annotation", func() {
|
||||
BeforeEach(func() {
|
||||
pod := &corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "notannotated",
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{Name: "test", Image: "test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, pod)).To(Succeed())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
pod := &corev1.Pod{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "notannotated"}, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(pod.Finalizers) > 0 {
|
||||
pod.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, pod)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should allow delete", func() {
|
||||
obj = &netbirdiov1.NBSetupKey{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().NotTo(HaveOccurred())
|
||||
})
|
||||
})
|
||||
When("Pods exist with annotation", func() {
|
||||
BeforeEach(func() {
|
||||
pod := &corev1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: "default",
|
||||
Name: "annotated",
|
||||
Annotations: map[string]string{
|
||||
setupKeyAnnotation: "test",
|
||||
},
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{
|
||||
{Name: "test", Image: "test"},
|
||||
},
|
||||
},
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, pod)).To(Succeed())
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
pod := &corev1.Pod{}
|
||||
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: "default", Name: "annotated"}, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
if len(pod.Finalizers) > 0 {
|
||||
pod.Finalizers = nil
|
||||
Expect(k8sClient.Update(ctx, pod)).To(Succeed())
|
||||
}
|
||||
err = k8sClient.Delete(ctx, pod)
|
||||
if !errors.IsNotFound(err) {
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should deny delete", func() {
|
||||
obj = &netbirdiov1.NBSetupKey{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
Expect(validator.ValidateDelete(ctx, obj)).Error().To(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user