Skip to content

Commit

Permalink
Propagate getMinReplicaCount error to the handler
Browse files Browse the repository at this point in the history
**What**
- Return parsing errors from getMinReplicaCount
- Add tests for the expected errors

Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
  • Loading branch information
LucasRoesler committed Aug 3, 2018
1 parent 06b32a3 commit da51428
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
5 changes: 4 additions & 1 deletion handlers/deploy.go
Expand Up @@ -160,7 +160,10 @@ func makeDeploymentSpec(request requests.CreateFunctionRequest, existingSecrets
FailureThreshold: 3,
}

initialReplicas := getMinReplicaCount(request.Labels)
initialReplicas, replicaErr := getMinReplicaCount(request.Labels)
if replicaErr != nil {
return nil, replicaErr
}
labels := buildLabels(request.Service, request.Labels)
nodeSelector := createSelector(request.Constraints)
resources, resourceErr := createResources(request)
Expand Down
18 changes: 11 additions & 7 deletions handlers/labels.go
@@ -1,8 +1,8 @@
package handlers

import (
"errors"
"fmt"
"log"
"strconv"
"time"
)
Expand Down Expand Up @@ -42,20 +42,24 @@ func buildLabels(functionName string, requestLables *map[string]string) map[stri

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

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

log.Println(err)
if minReplicas > 0 {
return int32p(int32(minReplicas)), nil
}

return nil, errors.New("replica count must be a positive integer")
}

return int32p(initialReplicasCount)
return int32p(initialReplicasCount), nil
}
51 changes: 49 additions & 2 deletions handlers/lables_test.go
@@ -1,6 +1,9 @@
package handlers

import "testing"
import (
"strings"
"testing"
)

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

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
output := getMinReplicaCount(s.labels)
output, err := getMinReplicaCount(s.labels)
if err != nil {
t.Errorf("getMinReplicaCount should not error on an empty or valid label")
}
if output == nil {
t.Errorf("getMinReplicaCount should not return nil pointer")
}
Expand All @@ -40,6 +46,47 @@ func Test_getMinReplicaCount(t *testing.T) {
}
}

func Test_getMinReplicaCount_Error(t *testing.T) {
scenarios := []struct {
name string
labels *map[string]string
msg string
}{
{
name: "negative values should return an error",
labels: &map[string]string{FunctionMinReplicaCount: "-2"},
msg: "replica count must be a positive integer",
},
{
name: "zero values should return an error",
labels: &map[string]string{FunctionMinReplicaCount: "0"},
msg: "replica count must be a positive integer",
},
{
name: "decimal values should return an error",
labels: &map[string]string{FunctionMinReplicaCount: "10.5"},
msg: "could not parse the minimum replica value",
},
{
name: "non-integer values should return an error",
labels: &map[string]string{FunctionMinReplicaCount: "test"},
msg: "could not parse the minimum replica value",
},
}

for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
output, err := getMinReplicaCount(s.labels)
if output != nil {
t.Errorf("getMinReplicaCount should return nil value on invalid input")
}
if !strings.Contains(err.Error(), s.msg) {
t.Errorf("unexpected error: expected '%s', got '%s'", s.msg, err.Error())
}
})
}
}

func Test_parseLabels(t *testing.T) {
scenarios := []struct {
name string
Expand Down
7 changes: 6 additions & 1 deletion handlers/update.go
Expand Up @@ -75,7 +75,12 @@ func updateDeploymentSpec(

deployment.Labels = labels
deployment.Spec.Template.ObjectMeta.Labels = labels
deployment.Spec.Replicas = getMinReplicaCount(request.Labels)

replicaCount, replicaErr := getMinReplicaCount(request.Labels)
if replicaErr != nil {
return replicaErr, http.StatusBadRequest
}
deployment.Spec.Replicas = replicaCount

deployment.Annotations = annotations
deployment.Spec.Template.Annotations = annotations
Expand Down

0 comments on commit da51428

Please sign in to comment.