@@ -18,14 +18,17 @@ import (
1818 "errors"
1919 "testing"
2020
21- . "github.com/onsi/gomega "
21+ "github.com/stretchr/testify/require "
2222 corev1 "k8s.io/api/core/v1"
2323 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+ "k8s.io/apimachinery/pkg/runtime"
25+ "k8s.io/apimachinery/pkg/types"
26+ "k8s.io/utils/ptr"
2427 sandboxv1alpha1 "sigs.k8s.io/agent-sandbox/api/v1alpha1"
28+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
2529)
2630
2731func TestComputeReadyCondition (t * testing.T ) {
28- g := NewGomegaWithT (t )
2932 r := & SandboxReconciler {}
3033
3134 testCases := []struct {
@@ -143,10 +146,134 @@ func TestComputeReadyCondition(t *testing.T) {
143146 for _ , tc := range testCases {
144147 t .Run (tc .name , func (t * testing.T ) {
145148 condition := r .computeReadyCondition (tc .generation , tc .err , tc .svc , tc .pod )
146- g .Expect (condition .Type ).To (Equal (string (sandboxv1alpha1 .SandboxConditionReady )))
147- g .Expect (condition .ObservedGeneration ).To (Equal (tc .generation ))
148- g .Expect (condition .Status ).To (Equal (tc .expectedStatus ))
149- g .Expect (condition .Reason ).To (Equal (tc .expectedReason ))
149+ require .Equal (t , sandboxv1alpha1 .SandboxConditionReady .String (), condition .Type )
150+ require .Equal (t , tc .generation , condition .ObservedGeneration )
151+ require .Equal (t , tc .expectedStatus , condition .Status )
152+ require .Equal (t , tc .expectedReason , condition .Reason )
153+ })
154+ }
155+ }
156+
157+ func TestReconcilePod (t * testing.T ) {
158+ sandboxName := "sandbox-name"
159+ sandboxNs := "sandbox-ns"
160+ nameHash := "name-hash"
161+ sandboxObj := & sandboxv1alpha1.Sandbox {
162+ ObjectMeta : metav1.ObjectMeta {
163+ Name : sandboxName ,
164+ Namespace : sandboxNs ,
165+ },
166+ Spec : sandboxv1alpha1.SandboxSpec {
167+ PodTemplate : sandboxv1alpha1.PodTemplate {
168+ Spec : corev1.PodSpec {
169+ Containers : []corev1.Container {
170+ {
171+ Name : "test-container" ,
172+ },
173+ },
174+ },
175+ ObjectMeta : sandboxv1alpha1.PodMetadata {
176+ Labels : map [string ]string {
177+ "custom-label" : "label-val" ,
178+ },
179+ Annotations : map [string ]string {
180+ "custom-annotation" : "anno-val" ,
181+ },
182+ },
183+ },
184+ },
185+ }
186+ testCases := []struct {
187+ name string
188+ initialObjs []runtime.Object
189+ sandbox * sandboxv1alpha1.Sandbox
190+ wantPod * corev1.Pod
191+ }{
192+ {
193+ name : "no-op if Pod already exists" ,
194+ initialObjs : []runtime.Object {
195+ & corev1.Pod {
196+ ObjectMeta : metav1.ObjectMeta {
197+ Name : sandboxName ,
198+ Namespace : sandboxNs ,
199+ ResourceVersion : "1" ,
200+ },
201+ Spec : corev1.PodSpec {
202+ Containers : []corev1.Container {
203+ {
204+ Name : "foo" ,
205+ },
206+ },
207+ },
208+ },
209+ },
210+ sandbox : sandboxObj ,
211+ wantPod : & corev1.Pod { // Pod is not updated
212+ ObjectMeta : metav1.ObjectMeta {
213+ Name : sandboxName ,
214+ Namespace : sandboxNs ,
215+ ResourceVersion : "1" ,
216+ },
217+ Spec : corev1.PodSpec {
218+ Containers : []corev1.Container {
219+ {
220+ Name : "foo" ,
221+ },
222+ },
223+ },
224+ },
225+ },
226+ {
227+ name : "reconcilePod creates a new Pod" ,
228+ sandbox : sandboxObj ,
229+ wantPod : & corev1.Pod {
230+ ObjectMeta : metav1.ObjectMeta {
231+ Name : sandboxName ,
232+ Namespace : sandboxNs ,
233+ ResourceVersion : "1" ,
234+ Labels : map [string ]string {
235+ "agents.x-k8s.io/sandbox-name-hash" : nameHash ,
236+ "custom-label" : "label-val" ,
237+ },
238+ Annotations : map [string ]string {
239+ "custom-annotation" : "anno-val" ,
240+ },
241+ OwnerReferences : []metav1.OwnerReference {
242+ {
243+ APIVersion : "agents.x-k8s.io/v1alpha1" ,
244+ Kind : "Sandbox" ,
245+ Name : sandboxName ,
246+ Controller : ptr .To (true ),
247+ BlockOwnerDeletion : ptr .To (true ),
248+ },
249+ },
250+ },
251+ Spec : corev1.PodSpec {
252+ Containers : []corev1.Container {
253+ {
254+ Name : "test-container" ,
255+ },
256+ },
257+ },
258+ },
259+ },
260+ }
261+
262+ for _ , tc := range testCases {
263+ t .Run (tc .name , func (t * testing.T ) {
264+ r := SandboxReconciler {
265+ Client : fake .NewFakeClient (tc .initialObjs ... ),
266+ Scheme : Scheme ,
267+ }
268+
269+ pod , err := r .reconcilePod (t .Context (), tc .sandbox , nameHash )
270+ require .NoError (t , err )
271+ require .Equal (t , tc .wantPod , pod )
272+ // Validate the Pod from the "cluster" (fake client)
273+ livePod := & corev1.Pod {}
274+ err = r .Get (t .Context (), types.NamespacedName {Name : pod .Name , Namespace : pod .Namespace }, livePod )
275+ require .NoError (t , err )
276+ require .Equal (t , tc .wantPod , livePod )
150277 })
151278 }
152279}
0 commit comments