From f56ba009d45964a19a2ade54032f15828be6f72d Mon Sep 17 00:00:00 2001 From: Ermin Hrkalovic <160042555+swermin@users.noreply.github.com> Date: Sun, 22 Sep 2024 21:28:49 +0200 Subject: [PATCH] add unittest for webhhook metadata feature --- pkg/fleetautoscalers/fleetautoscalers_test.go | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/pkg/fleetautoscalers/fleetautoscalers_test.go b/pkg/fleetautoscalers/fleetautoscalers_test.go index cdb07f9d7c..0a9ef75a9f 100644 --- a/pkg/fleetautoscalers/fleetautoscalers_test.go +++ b/pkg/fleetautoscalers/fleetautoscalers_test.go @@ -22,6 +22,7 @@ import ( "io" "net/http" "net/http/httptest" + "strconv" "testing" "time" @@ -92,6 +93,15 @@ func (t testServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { faResp.Replicas = faReq.Status.Replicas * scaleFactor } + // override value for tests + if faReq.MetaData != nil { + if value, ok := faReq.MetaData.Annotations["fixedReplicas"]; ok { + faResp.Scale = true + replicas, _ := strconv.Atoi(value) + faResp.Replicas = int32(replicas) + } + } + review := &autoscalingv1.FleetAutoscaleReview{ Request: faReq, Response: &faResp, @@ -579,6 +589,33 @@ func TestApplyWebhookPolicy(t *testing.T) { } } +func TestApplyWebhookPolicyWithMetadata(t *testing.T) { + err := utilruntime.ParseFeatures(string(utilruntime.FeatureFleetAutoscaleRequestMetaData) + "=true") + + t.Parallel() + ts := testServer{} + server := httptest.NewServer(ts) + defer server.Close() + + _, fleet := defaultWebhookFixtures() + + fixedReplicas := int32(11) + fleet.ObjectMeta.Annotations = map[string]string{ + "fixedReplicas": fmt.Sprintf("%d", fixedReplicas), + } + + webhookPolicy := &autoscalingv1.WebhookPolicy{ + Service: nil, + URL: &(server.URL), + } + + replicas, limited, err := applyWebhookPolicy(webhookPolicy, fleet) + + assert.Nil(t, err) + assert.False(t, limited) + assert.Equal(t, fixedReplicas, replicas) +} + func TestApplyWebhookPolicyNilFleet(t *testing.T) { t.Parallel()