From 7d15dcbc3f80ed5df70a71f24c8691e1df61e0d9 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 18 Aug 2020 14:28:58 -0400 Subject: [PATCH] multiregion: validation should always return error for OSS --- nomad/structs/structs.go | 35 --------------- nomad/structs/structs_oss.go | 13 ++++++ nomad/structs/structs_test.go | 84 ----------------------------------- 3 files changed, 13 insertions(+), 119 deletions(-) create mode 100644 nomad/structs/structs_oss.go diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 37cab5cd0eb..3a77d15dc6c 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -4591,41 +4591,6 @@ func (m *Multiregion) Copy() *Multiregion { return copy } -func (m *Multiregion) Validate(jobType string, jobDatacenters []string) error { - var mErr multierror.Error - seen := map[string]struct{}{} - for _, region := range m.Regions { - if _, ok := seen[region.Name]; ok { - mErr.Errors = append(mErr.Errors, - fmt.Errorf("Multiregion region %q can't be listed twice", - region.Name)) - } - seen[region.Name] = struct{}{} - if len(region.Datacenters) == 0 && len(jobDatacenters) == 0 { - mErr.Errors = append(mErr.Errors, - fmt.Errorf("Multiregion region %q must have at least 1 datacenter", - region.Name), - ) - } - } - if m.Strategy != nil { - switch jobType { - case JobTypeBatch: - if m.Strategy.OnFailure != "" || m.Strategy.MaxParallel != 0 { - mErr.Errors = append(mErr.Errors, - errors.New("Multiregion batch jobs can't have an update strategy")) - } - case JobTypeSystem: - if m.Strategy.OnFailure != "" { - mErr.Errors = append(mErr.Errors, - errors.New("Multiregion system jobs can't have an on_failure setting")) - } - default: // service - } - } - return mErr.ErrorOrNil() -} - type MultiregionStrategy struct { MaxParallel int OnFailure string diff --git a/nomad/structs/structs_oss.go b/nomad/structs/structs_oss.go new file mode 100644 index 00000000000..b138e85f83d --- /dev/null +++ b/nomad/structs/structs_oss.go @@ -0,0 +1,13 @@ +// +build !ent + +package structs + +import "errors" + +func (m *Multiregion) Validate(jobType string, jobDatacenters []string) error { + if m != nil { + return errors.New("Multiregion jobs are unlicensed.") + } + + return nil +} diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index 2212e62b054..94bbe1cb6fd 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -5508,87 +5508,3 @@ func TestNodeResources_Merge(t *testing.T) { }, }, res) } - -func TestMultiregion_Validate(t *testing.T) { - require := require.New(t) - cases := []struct { - Name string - JobType string - Case *Multiregion - Errors []string - }{ - { - Name: "empty valid multiregion spec", - JobType: JobTypeService, - Case: &Multiregion{}, - Errors: []string{}, - }, - - { - Name: "non-empty valid multiregion spec", - JobType: JobTypeService, - Case: &Multiregion{ - Strategy: &MultiregionStrategy{ - MaxParallel: 2, - OnFailure: "fail_all", - }, - Regions: []*MultiregionRegion{ - { - - Count: 2, - Datacenters: []string{"west-1", "west-2"}, - Meta: map[string]string{}, - }, - { - Name: "east", - Count: 1, - Datacenters: []string{"east-1"}, - Meta: map[string]string{}, - }, - }, - }, - Errors: []string{}, - }, - - { - Name: "repeated region, wrong strategy, missing DCs", - JobType: JobTypeBatch, - Case: &Multiregion{ - Strategy: &MultiregionStrategy{ - MaxParallel: 2, - }, - Regions: []*MultiregionRegion{ - { - Name: "west", - Datacenters: []string{"west-1", "west-2"}, - }, - - { - Name: "west", - }, - }, - }, - Errors: []string{ - "Multiregion region \"west\" can't be listed twice", - "Multiregion region \"west\" must have at least 1 datacenter", - "Multiregion batch jobs can't have an update strategy", - }, - }, - } - - for _, tc := range cases { - t.Run(tc.Name, func(t *testing.T) { - err := tc.Case.Validate(tc.JobType, []string{}) - if len(tc.Errors) == 0 { - require.NoError(err) - } else { - mErr := err.(*multierror.Error) - for i, expectedErr := range tc.Errors { - if !strings.Contains(mErr.Errors[i].Error(), expectedErr) { - t.Fatalf("err: %s, expected: %s", err, expectedErr) - } - } - } - }) - } -}