Skip to content

Commit da51428

Browse files
committed
Propagate getMinReplicaCount error to the handler
**What** - Return parsing errors from getMinReplicaCount - Add tests for the expected errors Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
1 parent 06b32a3 commit da51428

File tree

4 files changed

+70
-11
lines changed

4 files changed

+70
-11
lines changed

handlers/deploy.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ func makeDeploymentSpec(request requests.CreateFunctionRequest, existingSecrets
160160
FailureThreshold: 3,
161161
}
162162

163-
initialReplicas := getMinReplicaCount(request.Labels)
163+
initialReplicas, replicaErr := getMinReplicaCount(request.Labels)
164+
if replicaErr != nil {
165+
return nil, replicaErr
166+
}
164167
labels := buildLabels(request.Service, request.Labels)
165168
nodeSelector := createSelector(request.Constraints)
166169
resources, resourceErr := createResources(request)

handlers/labels.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package handlers
22

33
import (
4+
"errors"
45
"fmt"
5-
"log"
66
"strconv"
77
"time"
88
)
@@ -42,20 +42,24 @@ func buildLabels(functionName string, requestLables *map[string]string) map[stri
4242

4343
// getMinReplicaCount extracts the functions minimum replica count from the user's
4444
// request labels. If the value is not found, this will return the default value, 1.
45-
func getMinReplicaCount(labels *map[string]string) *int32 {
45+
func getMinReplicaCount(labels *map[string]string) (*int32, error) {
4646
if labels == nil {
47-
return int32p(initialReplicasCount)
47+
return int32p(initialReplicasCount), nil
4848
}
4949

5050
l := *labels
5151
if value, exists := l[FunctionMinReplicaCount]; exists {
5252
minReplicas, err := strconv.Atoi(value)
53-
if err == nil && minReplicas > 0 {
54-
return int32p(int32(minReplicas))
53+
if err != nil {
54+
return nil, errors.New("could not parse the minimum replica value")
5555
}
5656

57-
log.Println(err)
57+
if minReplicas > 0 {
58+
return int32p(int32(minReplicas)), nil
59+
}
60+
61+
return nil, errors.New("replica count must be a positive integer")
5862
}
5963

60-
return int32p(initialReplicasCount)
64+
return int32p(initialReplicasCount), nil
6165
}

handlers/lables_test.go

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package handlers
22

3-
import "testing"
3+
import (
4+
"strings"
5+
"testing"
6+
)
47

58
func Test_getMinReplicaCount(t *testing.T) {
69
scenarios := []struct {
@@ -27,7 +30,10 @@ func Test_getMinReplicaCount(t *testing.T) {
2730

2831
for _, s := range scenarios {
2932
t.Run(s.name, func(t *testing.T) {
30-
output := getMinReplicaCount(s.labels)
33+
output, err := getMinReplicaCount(s.labels)
34+
if err != nil {
35+
t.Errorf("getMinReplicaCount should not error on an empty or valid label")
36+
}
3137
if output == nil {
3238
t.Errorf("getMinReplicaCount should not return nil pointer")
3339
}
@@ -40,6 +46,47 @@ func Test_getMinReplicaCount(t *testing.T) {
4046
}
4147
}
4248

49+
func Test_getMinReplicaCount_Error(t *testing.T) {
50+
scenarios := []struct {
51+
name string
52+
labels *map[string]string
53+
msg string
54+
}{
55+
{
56+
name: "negative values should return an error",
57+
labels: &map[string]string{FunctionMinReplicaCount: "-2"},
58+
msg: "replica count must be a positive integer",
59+
},
60+
{
61+
name: "zero values should return an error",
62+
labels: &map[string]string{FunctionMinReplicaCount: "0"},
63+
msg: "replica count must be a positive integer",
64+
},
65+
{
66+
name: "decimal values should return an error",
67+
labels: &map[string]string{FunctionMinReplicaCount: "10.5"},
68+
msg: "could not parse the minimum replica value",
69+
},
70+
{
71+
name: "non-integer values should return an error",
72+
labels: &map[string]string{FunctionMinReplicaCount: "test"},
73+
msg: "could not parse the minimum replica value",
74+
},
75+
}
76+
77+
for _, s := range scenarios {
78+
t.Run(s.name, func(t *testing.T) {
79+
output, err := getMinReplicaCount(s.labels)
80+
if output != nil {
81+
t.Errorf("getMinReplicaCount should return nil value on invalid input")
82+
}
83+
if !strings.Contains(err.Error(), s.msg) {
84+
t.Errorf("unexpected error: expected '%s', got '%s'", s.msg, err.Error())
85+
}
86+
})
87+
}
88+
}
89+
4390
func Test_parseLabels(t *testing.T) {
4491
scenarios := []struct {
4592
name string

handlers/update.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ func updateDeploymentSpec(
7575

7676
deployment.Labels = labels
7777
deployment.Spec.Template.ObjectMeta.Labels = labels
78-
deployment.Spec.Replicas = getMinReplicaCount(request.Labels)
78+
79+
replicaCount, replicaErr := getMinReplicaCount(request.Labels)
80+
if replicaErr != nil {
81+
return replicaErr, http.StatusBadRequest
82+
}
83+
deployment.Spec.Replicas = replicaCount
7984

8085
deployment.Annotations = annotations
8186
deployment.Spec.Template.Annotations = annotations

0 commit comments

Comments
 (0)