agones.dev/agones@v1.53.0/pkg/util/runtime/features_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 runtime 16 17 import ( 18 "os" 19 "strings" 20 "testing" 21 22 "github.com/spf13/pflag" 23 "github.com/spf13/viper" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 func TestFeatures(t *testing.T) { 28 t.Parallel() 29 FeatureTestMutex.Lock() 30 defer FeatureTestMutex.Unlock() 31 32 orig := featureDefaults 33 // stable feature flag state 34 featureDefaults = map[Feature]bool{ 35 FeatureExample: true, 36 Feature("Test"): false, 37 } 38 39 t.Run("invalid Feature gate", func(t *testing.T) { 40 err := ParseFeatures("Foo") 41 assert.EqualError(t, err, "Feature Gate \"Foo\" is not a valid Feature Gate") 42 }) 43 44 t.Run("Empty query string", func(t *testing.T) { 45 err := ParseFeatures("") 46 assert.NoError(t, err) 47 assert.True(t, FeatureEnabled(FeatureExample)) 48 assert.Equal(t, "Example=true&Test=false", EncodeFeatures()) 49 }) 50 51 t.Run("query string", func(t *testing.T) { 52 err := ParseFeatures("Example=0&Test=1") 53 assert.NoError(t, err) 54 assert.False(t, FeatureEnabled(FeatureExample)) 55 assert.True(t, FeatureEnabled(Feature("Test"))) 56 assert.Equal(t, "Example=false&Test=true", EncodeFeatures()) 57 err = ParseFeatures(EncodeFeatures()) 58 assert.NoError(t, err) 59 assert.False(t, FeatureEnabled(FeatureExample)) 60 assert.True(t, FeatureEnabled(Feature("Test"))) 61 }) 62 63 t.Run("Error on query parsing", func(t *testing.T) { 64 err := ParseFeatures("Example=foobar") 65 assert.Error(t, err) 66 }) 67 68 t.Run("parse env vars", func(t *testing.T) { 69 assert.NoError(t, os.Setenv("FEATURE_GATES", "Test=true")) 70 71 FeaturesBindFlags() 72 pflag.Parse() 73 viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) 74 assert.NoError(t, FeaturesBindEnv()) 75 assert.NoError(t, ParseFeaturesFromEnv()) 76 77 assert.True(t, FeatureEnabled(Feature("Test"))) 78 assert.True(t, FeatureEnabled(FeatureExample)) 79 }) 80 81 // reset things back the way they were 82 featureDefaults = orig 83 } 84 85 func TestEnableAllFeatures(t *testing.T) { 86 t.Parallel() 87 FeatureTestMutex.Lock() 88 defer FeatureTestMutex.Unlock() 89 90 orig := featureDefaults 91 // stable feature flag state 92 featureDefaults = map[Feature]bool{ 93 FeatureExample: true, 94 Feature("Test"): false, 95 } 96 97 err := ParseFeatures("Example=0&Test=0") 98 assert.NoError(t, err) 99 assert.False(t, FeatureEnabled("Example")) 100 assert.False(t, FeatureEnabled("Test")) 101 102 EnableAllFeatures() 103 104 assert.True(t, FeatureEnabled("Example")) 105 assert.True(t, FeatureEnabled("Test")) 106 107 // reset things back the way they were 108 featureDefaults = orig 109 }