agones.dev/agones@v1.54.0/cmd/controller/main_test.go (about) 1 // Copyright 2020 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "testing" 19 20 agonesv1 "agones.dev/agones/pkg/apis/agones/v1" 21 "agones.dev/agones/pkg/portallocator" 22 "github.com/stretchr/testify/assert" 23 "k8s.io/apimachinery/pkg/api/resource" 24 ) 25 26 func TestControllerConfigValidation(t *testing.T) { 27 t.Parallel() 28 29 c := config{PortRanges: map[string]portallocator.PortRange{ 30 agonesv1.DefaultPortRange: {MinPort: 10, MaxPort: 2}, 31 }} 32 errs := c.validate() 33 assert.Len(t, errs, 1) 34 errorsContainString(t, errs, "max Port cannot be set less that the Min Port") 35 36 c.PortRanges["game"] = portallocator.PortRange{MinPort: 20, MaxPort: 12} 37 errs = c.validate() 38 assert.Len(t, errs, 2) 39 errorsContainString(t, errs, "max Port cannot be set less that the Min Port for port range game") 40 41 c.SidecarMemoryRequest = resource.MustParse("2Gi") 42 c.SidecarMemoryLimit = resource.MustParse("1Gi") 43 errs = c.validate() 44 assert.Len(t, errs, 3) 45 errorsContainString(t, errs, "Request must be less than or equal to memory limit") 46 47 c.SidecarMemoryLimit = resource.MustParse("2Gi") 48 c.SidecarCPURequest = resource.MustParse("2m") 49 c.SidecarCPULimit = resource.MustParse("1m") 50 errs = c.validate() 51 assert.Len(t, errs, 3) 52 errorsContainString(t, errs, "Request must be less than or equal to cpu limit") 53 54 c.SidecarMemoryLimit = resource.MustParse("2Gi") 55 c.SidecarCPURequest = resource.MustParse("-2m") 56 c.SidecarCPULimit = resource.MustParse("2m") 57 errs = c.validate() 58 assert.Len(t, errs, 3) 59 errorsContainString(t, errs, "Resource cpu request value must be non negative") 60 } 61 62 func TestControllerConfigValidation_PortRangeOverlap(t *testing.T) { 63 t.Parallel() 64 65 c := config{ 66 PortRanges: map[string]portallocator.PortRange{ 67 agonesv1.DefaultPortRange: {MinPort: 10, MaxPort: 20}, 68 "game": {MinPort: 15, MaxPort: 25}, 69 "other": {MinPort: 21, MaxPort: 31}, 70 }, 71 } 72 errs := c.validate() 73 assert.Len(t, errs, 2) 74 errorsContainString(t, errs, "port range game overlaps with min/max port") 75 } 76 77 func errorsContainString(t *testing.T, errs []error, expected string) { 78 found := false 79 for _, v := range errs { 80 if expected == v.Error() { 81 found = true 82 break 83 } 84 } 85 assert.True(t, found, "Was not able to find '%s'", expected) 86 }