Skip to content

Commit 2910322

Browse files
committed
test: add Sandbox mapping test for AgentCard watch
Verify mapWorkloadToAgentCards correctly maps Sandbox objects with agent labels to AgentCard reconcile requests. Also verify Sandbox objects without agent labels are filtered out. Addresses review feedback on PR #344. Assisted-By: Claude (Anthropic AI) <noreply@anthropic.com> Signed-off-by: cwiklik <cwiklikj@gmail.com>
1 parent c8d7eb9 commit 2910322

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

kagenti-operator/internal/controller/indexers_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import (
2323
. "github.com/onsi/gomega"
2424
appsv1 "k8s.io/api/apps/v1"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2627
"sigs.k8s.io/controller-runtime/pkg/log"
28+
29+
agentv1alpha1 "github.com/kagenti/operator/api/v1alpha1"
2730
)
2831

2932
var _ = Describe("mapWorkloadToAgentCards", func() {
@@ -82,4 +85,49 @@ var _ = Describe("mapWorkloadToAgentCards", func() {
8285
Expect(requests).To(BeEmpty())
8386
})
8487
})
88+
89+
Context("when a Sandbox workload has agent labels and matching AgentCard exists", func() {
90+
It("should return a reconcile request for the AgentCard", func() {
91+
card := &agentv1alpha1.AgentCard{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Name: "sandbox-card",
94+
Namespace: namespace,
95+
},
96+
Spec: agentv1alpha1.AgentCardSpec{
97+
TargetRef: &agentv1alpha1.TargetRef{
98+
APIVersion: "agents.x-k8s.io/v1alpha1",
99+
Kind: "Sandbox",
100+
Name: "my-sandbox",
101+
},
102+
},
103+
}
104+
Expect(k8sClient.Create(ctx, card)).To(Succeed())
105+
DeferCleanup(func() { _ = k8sClient.Delete(ctx, card) })
106+
107+
sbx := &unstructured.Unstructured{}
108+
sbx.SetGroupVersionKind(sandboxGVK)
109+
sbx.SetName("my-sandbox")
110+
sbx.SetNamespace(namespace)
111+
sbx.SetLabels(map[string]string{LabelAgentType: LabelValueAgent})
112+
113+
mapFn := mapWorkloadToAgentCards(k8sClient, "agents.x-k8s.io/v1alpha1", "Sandbox", logger)
114+
requests := mapFn(ctx, sbx)
115+
Expect(requests).To(HaveLen(1))
116+
Expect(requests[0].Name).To(Equal("sandbox-card"))
117+
})
118+
})
119+
120+
Context("when a Sandbox workload lacks agent labels", func() {
121+
It("should return no reconcile requests", func() {
122+
sbx := &unstructured.Unstructured{}
123+
sbx.SetGroupVersionKind(sandboxGVK)
124+
sbx.SetName("unlabeled-sandbox")
125+
sbx.SetNamespace(namespace)
126+
sbx.SetLabels(map[string]string{"app": "something"})
127+
128+
mapFn := mapWorkloadToAgentCards(k8sClient, "agents.x-k8s.io/v1alpha1", "Sandbox", logger)
129+
requests := mapFn(ctx, sbx)
130+
Expect(requests).To(BeEmpty())
131+
})
132+
})
85133
})

0 commit comments

Comments
 (0)