Skip to content

Commit

Permalink
add unittest for webhhook metadata feature
Browse files Browse the repository at this point in the history
  • Loading branch information
swermin committed Sep 22, 2024
1 parent 84105c0 commit f56ba00
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/fleetautoscalers/fleetautoscalers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f56ba00

Please sign in to comment.