Skip to content

Commit

Permalink
added test to check a generated URL
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
alexey-kremsa-globant committed May 14, 2020
1 parent 3730e5f commit a63f73d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
14 changes: 9 additions & 5 deletions pkg/fleetautoscalers/fleetautoscalers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ func applyWebhookPolicy(w *autoscalingv1.WebhookPolicy, f *agonesv1.Fleet) (int3
}
}

u = &url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s.%s.svc:8000", w.Service.Name, w.Service.Namespace),
Path: servicePath,
}
u = buildURL(scheme, w.Service.Name, w.Service.Namespace, servicePath)
}

faReq := autoscalingv1.FleetAutoscaleReview{
Expand Down Expand Up @@ -164,6 +160,14 @@ func applyWebhookPolicy(w *autoscalingv1.WebhookPolicy, f *agonesv1.Fleet) (int3
return f.Status.Replicas, false, nil
}

func buildURL(scheme, name, namespace, path string) *url.URL {
return &url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s.%s.svc:8000", name, namespace),
Path: path,
}
}

func applyBufferPolicy(b *autoscalingv1.BufferPolicy, f *agonesv1.Fleet) (int32, bool, error) {
var replicas int32

Expand Down
51 changes: 50 additions & 1 deletion pkg/fleetautoscalers/fleetautoscalers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,11 @@ func TestApplyWebhookPolicy(t *testing.T) {
}
})
}

}

func TestApplyWebhookPolicy_NilFleet(t *testing.T) {
t.Parallel()

url := "/scale"
w := &autoscalingv1.WebhookPolicy{
Service: &admregv1b.ServiceReference{
Expand All @@ -428,3 +429,51 @@ func TestApplyWebhookPolicy_NilFleet(t *testing.T) {
assert.False(t, limited)
assert.Zero(t, replicas)
}

func TestBuildURL(t *testing.T) {
t.Parallel()

var testCases = []struct {
description string
scheme string
name string
namespace string
path string
expected string
}{
{
description: "OK, path not empty",
scheme: "http",
name: "service1",
namespace: "default",
path: "scale",
expected: "http://service1.default.svc:8000/scale",
},
{
description: "OK, path not empty with slash",
scheme: "http",
name: "service1",
namespace: "default",
path: "/scale",
expected: "http://service1.default.svc:8000/scale",
},
{
description: "OK, path is empty",
scheme: "http",
name: "service1",
namespace: "default",
path: "",
expected: "http://service1.default.svc:8000",
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
res := buildURL(tc.scheme, tc.name, tc.namespace, tc.path)

if assert.NotNil(t, res) {
assert.Equal(t, tc.expected, res.String())
}
})
}
}

0 comments on commit a63f73d

Please sign in to comment.