@@ -19,15 +19,17 @@ package translator
1919import (
2020 "testing"
2121
22+ "github.com/google/go-cmp/cmp"
23+ "github.com/google/go-cmp/cmp/cmpopts"
24+ "github.com/stretchr/testify/assert"
2225 corev1 "k8s.io/api/core/v1"
2326 "k8s.io/apimachinery/pkg/api/resource"
2427 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25- fakeClient "k8s.io/client-go/kubernetes/fake"
26-
27- "github.com/stretchr/testify/assert"
2828 v1 "k8s.io/autoscaler/cluster-autoscaler/apis/capacitybuffer/autoscaling.x-k8s.io/v1beta1"
2929 cbclient "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/client"
3030 "k8s.io/autoscaler/cluster-autoscaler/capacitybuffer/testutil"
31+ fakeClient "k8s.io/client-go/kubernetes/fake"
32+ "k8s.io/utils/ptr"
3133)
3234
3335func TestResourceLimitsTranslator (t * testing.T ) {
@@ -46,6 +48,7 @@ func TestResourceLimitsTranslator(t *testing.T) {
4648 })
4749 fakeClient := fakeClient .NewSimpleClientset (podTemp4mem100cpu , podTemp8mem200cpu , podTemp4gpu )
4850 fakeCapacityBuffersClient , _ := cbclient .NewCapacityBufferClient (nil , fakeClient , nil , nil , nil , nil , nil , nil , nil , nil , nil )
51+ noResourcesSetMessage := "couldn't calculate number of pods for buffer based on provided resource limits. Check if the pod template requests at least one limited resource"
4952
5053 tests := []struct {
5154 name string
@@ -71,9 +74,9 @@ func TestResourceLimitsTranslator(t *testing.T) {
7174 }),
7275 },
7376 expectedStatus : []* v1.CapacityBufferStatus {
74- testutil .GetBufferStatus (nil , nil , nil , & testutil .ProvisioningStrategy , testutil . GetConditionNotReady () ),
77+ testutil .GetBufferStatus (nil , nil , nil , & testutil .ProvisioningStrategy , nil ),
7578 },
76- expectedNumberOfErrors : 1 ,
79+ expectedNumberOfErrors : 0 ,
7780 },
7881 {
7982 name : "Limits exist and no replicas, buffer filtered" ,
@@ -185,27 +188,56 @@ func TestResourceLimitsTranslator(t *testing.T) {
185188 }),
186189 },
187190 expectedStatus : []* v1.CapacityBufferStatus {
188- testutil .GetBufferStatus (nil , nil , nil , & testutil .ProvisioningStrategy , testutil .GetConditionNotReady ()),
191+ testutil .GetBufferStatus (
192+ nil , nil , nil , & testutil .ProvisioningStrategy , testutil .GetConditionNotReadyWithMessage (noResourcesSetMessage )),
189193 },
190- expectedNumberOfErrors : 1 ,
194+ expectedNumberOfErrors : 0 ,
195+ },
196+ {
197+ name : "conditions are not overridden if buffer is not ready" ,
198+ buffers : []* v1.CapacityBuffer {
199+ testutil .NewBuffer (
200+ testutil .WithPodTemplateRef (podTemp4mem100cpu .Name ),
201+ testutil .WithLimits (v1.ResourceList {
202+ "nvidia.com/gpu" : resource .MustParse ("100m" ),
203+ }),
204+ testutil .WithActiveProvisioningStrategy (),
205+ func (buffer * v1.CapacityBuffer ) {
206+ buffer .Status .Conditions = testutil .GetConditionNotReadyWithMessage ("test error" )
207+ },
208+ ),
209+ },
210+ expectedStatus : []* v1.CapacityBufferStatus {
211+ testutil .GetBufferStatus (nil , nil , nil , nil , testutil .GetConditionNotReadyWithMessage ("test error" )),
212+ },
213+ expectedNumberOfErrors : 0 ,
191214 },
192215 }
193216 for _ , test := range tests {
194217 t .Run (test .name , func (t * testing.T ) {
195218 translator := NewResourceLimitsTranslator (fakeCapacityBuffersClient )
196219 errors := translator .Translate (test .buffers )
197220 assert .Equal (t , len (errors ), test .expectedNumberOfErrors )
198- assert .ElementsMatch (t , test .expectedStatus , testutil .SanitizeBuffersStatus (test .buffers ))
221+ for i , buffer := range test .buffers {
222+ wantStatus := test .expectedStatus [i ]
223+ if diff := cmp .Diff (wantStatus , & buffer .Status , cmpopts .IgnoreFields (metav1.Condition {}, "LastTransitionTime" )); diff != "" {
224+ t .Errorf ("buffer status mismatch (-want +got):\n %s" , diff )
225+ }
226+ }
199227 })
200228 }
201229}
202230
203231func getTestBufferWithLimits (podTemplateRef * v1.LocalObjectRef , replicas * int32 , limits * v1.ResourceList ) * v1.CapacityBuffer {
204- return testutil .GetBuffer (& testutil .ProvisioningStrategy , nil , nil , podTemplateRef , replicas , nil , nil , limits )
232+ var podTemplateGeneration * int64
233+ if podTemplateRef != nil {
234+ podTemplateGeneration = ptr.To [int64 ](1 )
235+ }
236+ return testutil .GetBuffer (& testutil .ProvisioningStrategy , nil , nil , podTemplateRef , replicas , podTemplateGeneration , nil , limits )
205237}
206238
207239func getTestBufferStatusWithReplicas (podTemplateRef * v1.LocalObjectRef , replicas int32 ) * v1.CapacityBufferStatus {
208- return testutil .GetBufferStatus (podTemplateRef , & replicas , pointerToInt64 (1 ), & testutil .ProvisioningStrategy , testutil .GetConditionReady ( ))
240+ return testutil .GetBufferStatus (podTemplateRef , & replicas , ptr. To [ int64 ] (1 ), & testutil .ProvisioningStrategy , testutil .GetConditionReadyWithMessage ( "ready" ))
209241}
210242
211243func getPodTemplateWithResources (name string , resources corev1.ResourceList ) * corev1.PodTemplate {
0 commit comments