/* Copyright 2016 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package feature import ( "fmt" "strings" "testing" "github.com/spf13/pflag" ) func TestFeatureGateFlag(t *testing.T) { // gates for testing const testAlphaGate Feature = "TestAlpha" const testBetaGate Feature = "TestBeta" tests := []struct { arg string expect map[Feature]bool parseError string }{ { arg: "", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: false, testBetaGate: false, }, }, { arg: "fooBarBaz=maybeidk", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: false, testBetaGate: false, }, parseError: "unrecognized key: fooBarBaz", }, { arg: "AllAlpha=false", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: false, testBetaGate: false, }, }, { arg: "AllAlpha=true", expect: map[Feature]bool{ allAlphaGate: true, testAlphaGate: true, testBetaGate: false, }, }, { arg: "AllAlpha=banana", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: false, testBetaGate: false, }, parseError: "invalid value of AllAlpha", }, { arg: "AllAlpha=false,TestAlpha=true", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: true, testBetaGate: false, }, }, { arg: "TestAlpha=true,AllAlpha=false", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: true, testBetaGate: false, }, }, { arg: "AllAlpha=true,TestAlpha=false", expect: map[Feature]bool{ allAlphaGate: true, testAlphaGate: false, testBetaGate: false, }, }, { arg: "TestAlpha=false,AllAlpha=true", expect: map[Feature]bool{ allAlphaGate: true, testAlphaGate: false, testBetaGate: false, }, }, { arg: "TestBeta=true,AllAlpha=false", expect: map[Feature]bool{ allAlphaGate: false, testAlphaGate: false, testBetaGate: true, }, }, } for i, test := range tests { fs := pflag.NewFlagSet("testfeaturegateflag", pflag.ContinueOnError) f := NewFeatureGate() f.Add(map[Feature]FeatureSpec{ testAlphaGate: {Default: false, PreRelease: Alpha}, testBetaGate: {Default: false, PreRelease: Beta}, }) f.AddFlag(fs) err := fs.Parse([]string{fmt.Sprintf("--%s=%s", flagName, test.arg)}) if test.parseError != "" { if !strings.Contains(err.Error(), test.parseError) { t.Errorf("%d: Parse() Expected %v, Got %v", i, test.parseError, err) } } else if err != nil { t.Errorf("%d: Parse() Expected nil, Got %v", i, err) } for k, v := range test.expect { if f.enabled[k] != v { t.Errorf("%d: expected %s=%v, Got %v", i, k, v, f.enabled[k]) } } } } func TestFeatureGateOverride(t *testing.T) { const testAlphaGate Feature = "TestAlpha" const testBetaGate Feature = "TestBeta" // Don't parse the flag, assert defaults are used. var f FeatureGate = NewFeatureGate() f.Add(map[Feature]FeatureSpec{ testAlphaGate: {Default: false, PreRelease: Alpha}, testBetaGate: {Default: false, PreRelease: Beta}, }) f.Set("TestAlpha=true,TestBeta=true") if f.Enabled(testAlphaGate) != true { t.Errorf("Expected true") } if f.Enabled(testBetaGate) != true { t.Errorf("Expected true") } f.Set("TestAlpha=false") if f.Enabled(testAlphaGate) != false { t.Errorf("Expected false") } if f.Enabled(testBetaGate) != true { t.Errorf("Expected true") } } func TestFeatureGateFlagDefaults(t *testing.T) { // gates for testing const testAlphaGate Feature = "TestAlpha" const testBetaGate Feature = "TestBeta" // Don't parse the flag, assert defaults are used. var f FeatureGate = NewFeatureGate() f.Add(map[Feature]FeatureSpec{ testAlphaGate: {Default: false, PreRelease: Alpha}, testBetaGate: {Default: true, PreRelease: Beta}, }) if f.Enabled(testAlphaGate) != false { t.Errorf("Expected false") } if f.Enabled(testBetaGate) != true { t.Errorf("Expected true") } }