@@ -18,6 +18,7 @@ package capacitybufferpodlister
1818
1919import (
2020 "context"
21+ "fmt"
2122 "testing"
2223
2324 "github.com/stretchr/testify/assert"
@@ -28,6 +29,7 @@ import (
2829 corev1 "k8s.io/api/core/v1"
2930 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3031 "k8s.io/apimachinery/pkg/runtime"
32+ "k8s.io/apimachinery/pkg/types"
3133
3234 buffersfake "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/client/clientset/versioned/fake"
3335 testutil "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/testutil"
@@ -169,6 +171,65 @@ func TestPodListProcessor(t *testing.T) {
169171 }
170172}
171173
174+ func TestCapacityBufferFakePodsRegistry (t * testing.T ) {
175+ tests := []struct {
176+ name string
177+ objectsInKubernetesClient []runtime.Object
178+ objectsInBuffersClient []runtime.Object
179+ unschedulablePods []* corev1.Pod
180+ expectedUnschedPodsCount int
181+ expectedBuffersPodsNum map [string ]int
182+ }{
183+ {
184+ name : "1 ready buffer and 1 not ready buffer" ,
185+ objectsInKubernetesClient : []runtime.Object {getTestingPodTemplate ("ref1" , 1 ), getTestingPodTemplate ("ref2" , 1 )},
186+ objectsInBuffersClient : []runtime.Object {
187+ getTestingBuffer ("buffer1" , "ref1" , 2 , 1 , false , 1 , testProvStrategyAllowed ),
188+ getTestingBuffer ("buffer2" , "ref2" , 3 , 1 , true , 1 , testProvStrategyAllowed ),
189+ },
190+ unschedulablePods : []* corev1.Pod {getTestingPod ("Pod1" ), getTestingPod ("Pod2" ), getTestingPod ("Pod3" )},
191+ expectedUnschedPodsCount : 6 ,
192+ expectedBuffersPodsNum : map [string ]int {"buffer2" : 3 },
193+ },
194+ {
195+ name : "2 ready buffers" ,
196+ objectsInKubernetesClient : []runtime.Object {getTestingPodTemplate ("ref1" , 1 ), getTestingPodTemplate ("ref2" , 1 )},
197+ objectsInBuffersClient : []runtime.Object {
198+ getTestingBuffer ("buffer1" , "ref1" , 2 , 1 , true , 1 , testProvStrategyAllowed ),
199+ getTestingBuffer ("buffer2" , "ref2" , 3 , 1 , true , 1 , testProvStrategyAllowed ),
200+ },
201+ unschedulablePods : []* corev1.Pod {getTestingPod ("Pod1" ), getTestingPod ("Pod2" ), getTestingPod ("Pod3" )},
202+ expectedUnschedPodsCount : 8 ,
203+ expectedBuffersPodsNum : map [string ]int {"buffer1" : 2 , "buffer2" : 3 },
204+ },
205+ }
206+ for _ , test := range tests {
207+ t .Run (test .name , func (t * testing.T ) {
208+ fakeKubernetesClient := fakeclient .NewSimpleClientset (test .objectsInKubernetesClient ... )
209+ fakeBuffersClient := buffersfake .NewSimpleClientset (test .objectsInBuffersClient ... )
210+ fakeCapacityBuffersClient , _ := client .NewCapacityBufferClientFromClients (fakeBuffersClient , fakeKubernetesClient , nil , nil )
211+
212+ registry := NewDefaultCapacityBuffersFakePodsRegistry ()
213+ processor := NewCapacityBufferPodListProcessor (fakeCapacityBuffersClient , []string {testProvStrategyAllowed }, registry )
214+ resUnschedulablePods , err := processor .Process (nil , test .unschedulablePods )
215+ assert .Equal (t , nil , err )
216+ assert .Equal (t , test .expectedUnschedPodsCount , len (resUnschedulablePods ))
217+ for _ , pod := range resUnschedulablePods {
218+ if isFakeCapacityBuffersPod (pod ) {
219+ podBufferObj , found := registry .fakePodsUIDToBuffer [string (pod .UID )]
220+ assert .True (t , found )
221+ expectedPodsNum , found := test .expectedBuffersPodsNum [podBufferObj .Name ]
222+ assert .True (t , found )
223+ test .expectedBuffersPodsNum [podBufferObj .Name ] = expectedPodsNum - 1
224+ }
225+ }
226+ for bufferName := range test .expectedBuffersPodsNum {
227+ assert .Equal (t , 0 , test .expectedBuffersPodsNum [bufferName ])
228+ }
229+ })
230+ }
231+ }
232+
172233func getTestingPod (name string ) * corev1.Pod {
173234 return & corev1.Pod {ObjectMeta : metav1.ObjectMeta {Name : name }}
174235}
@@ -178,7 +239,7 @@ func getTestingPodTemplate(name string, generation int64) *corev1.PodTemplate {
178239}
179240
180241func getTestingBuffer (bufferName , refName string , replicas int32 , generation int64 , ready bool , bufferGeneration int64 , provStrategy string ) * apiv1.CapacityBuffer {
181- buffer := & apiv1.CapacityBuffer {ObjectMeta : metav1.ObjectMeta {Name : bufferName , Namespace : "default" , Generation : bufferGeneration }}
242+ buffer := & apiv1.CapacityBuffer {ObjectMeta : metav1.ObjectMeta {Name : bufferName , Namespace : "default" , Generation : bufferGeneration , UID : types . UID ( fmt . Sprintf ( "%s-uid" , bufferName )) }}
182243 buffer .Status = * testutil .GetBufferStatus (& apiv1.LocalObjectRef {Name : refName }, & replicas , & generation , & provStrategy , nil )
183244 if ready {
184245 buffer .Status .Conditions = testutil .GetConditionReady ()
0 commit comments