better testing of scaling parsing, fixed some broken tests by api

changes
This commit is contained in:
Chris Baker 2020-07-04 19:32:37 +00:00
parent bdbb066ec8
commit a77e012220
4 changed files with 27 additions and 9 deletions

View file

@ -1320,7 +1320,7 @@ func TestParse(t *testing.T) {
Name: helper.StringToPtr("group"), Name: helper.StringToPtr("group"),
Scaling: &api.ScalingPolicy{ Scaling: &api.ScalingPolicy{
Min: helper.Int64ToPtr(5), Min: helper.Int64ToPtr(5),
Max: 100, Max: helper.Int64ToPtr(100),
Policy: map[string]interface{}{ Policy: map[string]interface{}{
"foo": "bar", "foo": "bar",
"b": true, "b": true,
@ -1345,7 +1345,7 @@ func TestParse(t *testing.T) {
Name: helper.StringToPtr("group"), Name: helper.StringToPtr("group"),
Scaling: &api.ScalingPolicy{ Scaling: &api.ScalingPolicy{
Min: nil, Min: nil,
Max: 0, Max: helper.Int64ToPtr(10),
Policy: nil, Policy: nil,
Enabled: nil, Enabled: nil,
}, },
@ -1355,6 +1355,12 @@ func TestParse(t *testing.T) {
false, false,
}, },
{
"tg-scaling-policy-missing-max.hcl",
nil,
true,
},
{ {
"tg-scaling-policy-multi-policy.hcl", "tg-scaling-policy-multi-policy.hcl",
nil, nil,

View file

@ -1,5 +1,7 @@
job "elastic" { job "elastic" {
group "group" { group "group" {
scaling {} scaling {
max = 10
}
} }
} }

View file

@ -0,0 +1,7 @@
job "elastic" {
group "group" {
scaling {
// required: max = ...
}
}
}

View file

@ -5952,12 +5952,15 @@ func (tg *TaskGroup) validateScalingPolicy(j *Job) error {
if tg.Scaling.Max < 0 { if tg.Scaling.Max < 0 {
mErr.Errors = append(mErr.Errors, mErr.Errors = append(mErr.Errors,
fmt.Errorf("Scaling policy invalid: maximum count must be specified and non-negative")) fmt.Errorf("Scaling policy invalid: maximum count must be specified and non-negative"))
} else if tg.Scaling.Max < tg.Scaling.Min { } else {
mErr.Errors = append(mErr.Errors, if tg.Scaling.Max < tg.Scaling.Min {
fmt.Errorf("Scaling policy invalid: maximum count must not be less than minimum count")) mErr.Errors = append(mErr.Errors,
} else if tg.Scaling.Max < int64(tg.Count) { fmt.Errorf("Scaling policy invalid: maximum count must not be less than minimum count"))
mErr.Errors = append(mErr.Errors, }
fmt.Errorf("Scaling policy invalid: task group count must not be greater than maximum count in scaling policy")) if tg.Scaling.Max < int64(tg.Count) {
mErr.Errors = append(mErr.Errors,
fmt.Errorf("Scaling policy invalid: task group count must not be greater than maximum count in scaling policy"))
}
} }
if int64(tg.Count) < tg.Scaling.Min && !(j.IsMultiregion() && tg.Count == 0 && j.Region == "global") { if int64(tg.Count) < tg.Scaling.Min && !(j.IsMultiregion() && tg.Count == 0 && j.Region == "global") {