From a77e012220ad2410c1880d64459f04025f3ec572 Mon Sep 17 00:00:00 2001 From: Chris Baker <1675087+cgbaker@users.noreply.github.com> Date: Sat, 4 Jul 2020 19:32:37 +0000 Subject: [PATCH] better testing of scaling parsing, fixed some broken tests by api changes --- jobspec/parse_test.go | 10 ++++++++-- .../test-fixtures/tg-scaling-policy-minimal.hcl | 4 +++- .../tg-scaling-policy-missing-max.hcl | 7 +++++++ nomad/structs/structs.go | 15 +++++++++------ 4 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 jobspec/test-fixtures/tg-scaling-policy-missing-max.hcl diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 35a79287b..734916450 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -1320,7 +1320,7 @@ func TestParse(t *testing.T) { Name: helper.StringToPtr("group"), Scaling: &api.ScalingPolicy{ Min: helper.Int64ToPtr(5), - Max: 100, + Max: helper.Int64ToPtr(100), Policy: map[string]interface{}{ "foo": "bar", "b": true, @@ -1345,7 +1345,7 @@ func TestParse(t *testing.T) { Name: helper.StringToPtr("group"), Scaling: &api.ScalingPolicy{ Min: nil, - Max: 0, + Max: helper.Int64ToPtr(10), Policy: nil, Enabled: nil, }, @@ -1355,6 +1355,12 @@ func TestParse(t *testing.T) { false, }, + { + "tg-scaling-policy-missing-max.hcl", + nil, + true, + }, + { "tg-scaling-policy-multi-policy.hcl", nil, diff --git a/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl b/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl index 60aedac05..f145506c8 100644 --- a/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl +++ b/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl @@ -1,5 +1,7 @@ job "elastic" { group "group" { - scaling {} + scaling { + max = 10 + } } } diff --git a/jobspec/test-fixtures/tg-scaling-policy-missing-max.hcl b/jobspec/test-fixtures/tg-scaling-policy-missing-max.hcl new file mode 100644 index 000000000..1d60684fd --- /dev/null +++ b/jobspec/test-fixtures/tg-scaling-policy-missing-max.hcl @@ -0,0 +1,7 @@ +job "elastic" { + group "group" { + scaling { + // required: max = ... + } + } +} diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 651db5152..4996b3638 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -5952,12 +5952,15 @@ func (tg *TaskGroup) validateScalingPolicy(j *Job) error { if tg.Scaling.Max < 0 { mErr.Errors = append(mErr.Errors, fmt.Errorf("Scaling policy invalid: maximum count must be specified and non-negative")) - } else if tg.Scaling.Max < tg.Scaling.Min { - mErr.Errors = append(mErr.Errors, - fmt.Errorf("Scaling policy invalid: maximum count must not be less than minimum count")) - } else 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")) + } else { + if tg.Scaling.Max < tg.Scaling.Min { + mErr.Errors = append(mErr.Errors, + fmt.Errorf("Scaling policy invalid: maximum count must not be less than minimum count")) + } + 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") {