Skip to content

Commit dca3875

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 c64dec6 commit dca3875

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

handlers/deploy.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ func makeDeploymentSpec(request requests.CreateFunctionRequest, existingSecrets
131131
probe = nil
132132
}
133133

134-
initialReplicas := getMinReplicaCount(request.Labels)
134+
initialReplicas, replicaErr := getMinReplicaCount(request.Labels)
135+
if replicaErr != nil {
136+
return nil, replicaErr
137+
}
135138
labels := buildLabels(request.Service, request.Labels)
136139
nodeSelector := createSelector(request.Constraints)
137140
resources, resourceErr := createResources(request)

handlers/labels.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package handlers
22

33
import (
44
"fmt"
5-
"log"
65
"strconv"
76
"time"
7+
8+
"github.com/pkg/errors"
89
)
910

1011
const (
@@ -42,20 +43,24 @@ func buildLabels(functionName string, requestLables *map[string]string) map[stri
4243

4344
// getMinReplicaCount extracts the functions minimum replica count from the user's
4445
// request labels. If the value is not found, this will return the default value, 1.
45-
func getMinReplicaCount(labels *map[string]string) *int32 {
46+
func getMinReplicaCount(labels *map[string]string) (*int32, error) {
4647
if labels == nil {
47-
return int32p(initialReplicasCount)
48+
return int32p(initialReplicasCount), nil
4849
}
4950

5051
l := *labels
5152
if value, exists := l[FunctionMinReplicaCount]; exists {
5253
minReplicas, err := strconv.Atoi(value)
53-
if err == nil && minReplicas > 0 {
54-
return int32p(int32(minReplicas))
54+
if err != nil {
55+
return nil, errors.Wrap(err, "could not parse the minimum replica value")
56+
}
57+
58+
if minReplicas > 0 {
59+
return int32p(int32(minReplicas)), nil
5560
}
5661

57-
log.Println(err)
62+
return nil, errors.New("replica count must be a positive integer")
5863
}
5964

60-
return int32p(initialReplicasCount)
65+
return int32p(initialReplicasCount), nil
6166
}

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ func MakeUpdateHandler(functionNamespace string, clientset *kubernetes.Clientset
5353
labels := buildLabels(request.Service, request.Labels)
5454
deployment.Labels = labels
5555
deployment.Spec.Template.ObjectMeta.Labels = labels
56-
deployment.Spec.Replicas = getMinReplicaCount(request.Labels)
56+
57+
replicaCount, replicaErr := getMinReplicaCount(request.Labels)
58+
if replicaErr != nil {
59+
w.WriteHeader(http.StatusBadRequest)
60+
w.Write([]byte(replicaErr.Error()))
61+
return
62+
}
63+
deployment.Spec.Replicas = replicaCount
5764

5865
resources, resourceErr := createResources(request)
5966
if resourceErr != nil {

0 commit comments

Comments
 (0)