github.com/lablabs/operator-sdk@v0.8.2/pkg/ansible/operator/operator_test.go (about) 1 // Copyright 2018 The Operator-SDK Authors 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 operator 16 17 import ( 18 "os" 19 "strconv" 20 "testing" 21 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 // TODO: add a test for the Run method 28 29 func TestFormatEnvVar(t *testing.T) { 30 testCases := []struct { 31 name string 32 kind string 33 group string 34 expected string 35 }{ 36 { 37 name: "easy path", 38 kind: "FooCluster", 39 group: "cache.example.com", 40 expected: "WORKER_FOOCLUSTER_CACHE_EXAMPLE_COM", 41 }, 42 { 43 name: "missing kind", 44 kind: "", 45 group: "cache.example.com", 46 expected: "WORKER__CACHE_EXAMPLE_COM", 47 }, 48 { 49 name: "missing group", 50 kind: "FooCluster", 51 group: "", 52 expected: "WORKER_FOOCLUSTER_", 53 }, 54 } 55 56 for _, tc := range testCases { 57 t.Run(tc.name, func(t *testing.T) { 58 assert.Equal(t, tc.expected, formatEnvVar(tc.kind, tc.group)) 59 }) 60 } 61 } 62 63 func TestMaxWorkers(t *testing.T) { 64 testCases := []struct { 65 name string 66 gvk schema.GroupVersionKind 67 defvalue int 68 expected int 69 setenvvar bool 70 }{ 71 { 72 name: "no env, use default value", 73 gvk: schema.GroupVersionKind{ 74 Group: "cache.example.com", 75 Version: "v1alpha1", 76 Kind: "MemCacheService", 77 }, 78 defvalue: 1, 79 expected: 1, 80 setenvvar: false, 81 }, 82 { 83 name: "env set to 3, expect 3", 84 gvk: schema.GroupVersionKind{ 85 Group: "cache.example.com", 86 Version: "v1alpha1", 87 Kind: "MemCacheService", 88 }, 89 defvalue: 1, 90 expected: 3, 91 setenvvar: true, 92 }, 93 } 94 95 for _, tc := range testCases { 96 t.Run(tc.name, func(t *testing.T) { 97 os.Unsetenv(formatEnvVar(tc.gvk.Kind, tc.gvk.Group)) 98 if tc.setenvvar { 99 os.Setenv(formatEnvVar(tc.gvk.Kind, tc.gvk.Group), strconv.Itoa(tc.expected)) 100 } 101 assert.Equal(t, tc.expected, getMaxWorkers(tc.gvk, tc.defvalue)) 102 }) 103 } 104 }