mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 16:56:47 +00:00
Moved label extraction to dedicated function.
Further I created some unit test which should cover all relevant scenarios for the created function. Signed-off-by: Simon Pelczer <templum.dev@gmail.com>
This commit is contained in:
parent
7fe67d7af6
commit
6cd6975fe5
@ -85,48 +85,22 @@ func (s ExternalServiceQuery) GetReplicas(serviceName string) (handlers.ServiceQ
|
||||
}
|
||||
}
|
||||
|
||||
maxReplicas := uint64(handlers.DefaultMaxReplicas)
|
||||
minReplicas := uint64(handlers.DefaultMinReplicas)
|
||||
maxReplicas := uint64(handlers.DefaultMaxReplicas)
|
||||
scalingFactor := uint64(handlers.DefaultScalingFactor)
|
||||
availableReplicas := function.AvailableReplicas
|
||||
|
||||
if function.Labels != nil {
|
||||
labels := *function.Labels
|
||||
minScale := labels[handlers.MinScaleLabel]
|
||||
maxScale := labels[handlers.MaxScaleLabel]
|
||||
scaleFactor := labels[handlers.ScalingFactorLabel]
|
||||
|
||||
if len(minScale) > 0 {
|
||||
labelValue, err := strconv.Atoi(minScale)
|
||||
if err != nil {
|
||||
log.Printf("Bad replica count: %s, should be uint", minScale)
|
||||
} else {
|
||||
minReplicas = uint64(labelValue)
|
||||
}
|
||||
}
|
||||
|
||||
if len(maxScale) > 0 {
|
||||
labelValue, err := strconv.Atoi(maxScale)
|
||||
if err != nil {
|
||||
log.Printf("Bad replica count: %s, should be uint", maxScale)
|
||||
} else {
|
||||
maxReplicas = uint64(labelValue)
|
||||
}
|
||||
}
|
||||
|
||||
if len(scaleFactor) > 0 {
|
||||
labelValue, err := strconv.Atoi(scaleFactor)
|
||||
if err != nil {
|
||||
log.Printf("Bad Scaling Factor: %s, should be uint", scaleFactor)
|
||||
} else {
|
||||
var temp = uint64(labelValue)
|
||||
minReplicas = extractLabelValue(labels[handlers.MinScaleLabel], minReplicas)
|
||||
maxReplicas = extractLabelValue(labels[handlers.MaxScaleLabel], maxReplicas)
|
||||
temp := extractLabelValue(labels[handlers.ScalingFactorLabel], scalingFactor)
|
||||
|
||||
if temp >= 0 && temp <= 100 {
|
||||
scalingFactor = temp
|
||||
} else {
|
||||
log.Printf("Bad Scaling Factor: %s, is in range [0 - 100]", temp)
|
||||
}
|
||||
}
|
||||
log.Printf("Bad Scaling Factor: %d, is in range [0 - 100]", temp)
|
||||
}
|
||||
}
|
||||
|
||||
@ -172,3 +146,20 @@ func (s ExternalServiceQuery) SetReplicas(serviceName string, count uint64) erro
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// This methods tries to parse the provided raw label value and if it fails
|
||||
// it will return the provided fallback value and log and message
|
||||
func extractLabelValue(rawLabelValue string, fallback uint64 ) uint64{
|
||||
if len(rawLabelValue) <= 0 {
|
||||
return fallback
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(rawLabelValue)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Provided label value %s should be of type uint", rawLabelValue)
|
||||
return fallback
|
||||
}
|
||||
|
||||
return uint64(value)
|
||||
}
|
32
gateway/plugin/external_test.go
Normal file
32
gateway/plugin/external_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package plugin
|
||||
|
||||
import "testing"
|
||||
|
||||
const fallbackValue = 120
|
||||
|
||||
func TestLabelValueWasEmpty(t *testing.T) {
|
||||
extractedValue := extractLabelValue("", fallbackValue)
|
||||
|
||||
if extractedValue != fallbackValue {
|
||||
t.Log("Expected extractedValue to equal the fallbackValue")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabelValueWasValid(t *testing.T) {
|
||||
extractedValue := extractLabelValue("42", fallbackValue)
|
||||
|
||||
if extractedValue != 42 {
|
||||
t.Log("Expected extractedValue to equal answer to life (42)")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestLabelValueWasInValid(t *testing.T) {
|
||||
extractedValue := extractLabelValue("InvalidValue", fallbackValue)
|
||||
|
||||
if extractedValue != fallbackValue {
|
||||
t.Log("Expected extractedValue to equal the fallbackValue")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user