Skip to content

Commit

Permalink
revert: delete test for isTargeted
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsuya0828 committed Apr 2, 2024
1 parent 22f42b7 commit 394ba96
Showing 1 changed file with 1 addition and 107 deletions.
108 changes: 1 addition & 107 deletions internal/controller/broom_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

Expand All @@ -35,11 +32,7 @@ import (

var _ = Describe("Broom Controller", func() {
Context("When reconciling a resource", func() {
const (
resourceName = "broom-sample"
cronJobName = "oom-sample"
cronJobNamespace = "broom"
)
const resourceName = "test-resource"

ctx := context.Background()

Expand All @@ -62,56 +55,6 @@ var _ = Describe("Broom Controller", func() {
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}

By("creating the Namespace")
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: cronJobNamespace,
},
}
Expect(k8sClient.Create(ctx, ns)).To(Succeed())

By("creating the CronJob")
cronJob := &batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: cronJobName,
Namespace: cronJobNamespace,
Labels: map[string]string{
"m3.com/use-broom": "true",
},
},
Spec: batchv1.CronJobSpec{
Schedule: "*/2 * * * *",
JobTemplate: batchv1.JobTemplateSpec{
Spec: batchv1.JobSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "oom-container",
Image: "ubuntu:latest",
Command: []string{"/bin/bash", "-c"},
Args: []string{
"echo PID=$$; for i in {0..9}; do eval a$i'=$(head --bytes 5000000 /dev/zero | cat -v)'; echo $((i++)); done",
},
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("100Mi"),
},
Requests: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("50Mi"),
},
},
},
},
RestartPolicy: corev1.RestartPolicyNever,
},
},
},
},
},
}
Expect(k8sClient.Create(ctx, cronJob)).To(Succeed())
})

AfterEach(func() {
Expand All @@ -122,23 +65,6 @@ var _ = Describe("Broom Controller", func() {

By("Cleanup the specific resource instance Broom")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())

By("Cleanup the CronJob")
cronJob := &batchv1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: cronJobName,
Namespace: cronJobNamespace,
},
}
Expect(k8sClient.Delete(ctx, cronJob)).To(Succeed())

By("Cleanup the Namespace")
ns := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: cronJobNamespace,
},
}
Expect(k8sClient.Delete(ctx, ns)).To(Succeed())
})
It("should successfully reconcile the resource", func() {
By("Reconciling the created resource")
Expand All @@ -153,38 +79,6 @@ var _ = Describe("Broom Controller", func() {
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.

By("Checking if the CronJob is targeted correctly")
cronJob := &batchv1.CronJob{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cronJobName, Namespace: cronJobNamespace}, cronJob)).To(Succeed())

target := aiv1alpha1.BroomTarget{
Namespace: cronJobNamespace,
Name: cronJobName,
Labels: map[string]string{
"m3.com/use-broom": "true",
},
}
res := isTargeted(*cronJob, target)
Expect(res).To(BeTrue(), "Expected CronJob to be targeted but it is not")

emptyTarget := aiv1alpha1.BroomTarget{}
res = isTargeted(*cronJob, emptyTarget)
Expect(res).To(BeTrue(), "Expected CronJob to be targeted but it is not")

wrongNamespaceTarget := aiv1alpha1.BroomTarget{
Namespace: "default",
}
res = isTargeted(*cronJob, wrongNamespaceTarget)
Expect(res).To(BeFalse(), "Expected CronJob not to be targeted but it is")

wrongLabelTarget := aiv1alpha1.BroomTarget{
Labels: map[string]string{
"m3.com/foo": "bar",
},
}
res = isTargeted(*cronJob, wrongLabelTarget)
Expect(res).To(BeFalse(), "Expected CronJob not to be targeted but it is")
})
})
})

0 comments on commit 394ba96

Please sign in to comment.