feature_gate_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package config
  14. import (
  15. "fmt"
  16. "strings"
  17. "testing"
  18. "github.com/spf13/pflag"
  19. )
  20. func TestFeatureGateFlag(t *testing.T) {
  21. // gates for testing
  22. const testAlphaGate = "TestAlpha"
  23. const testBetaGate = "TestBeta"
  24. tests := []struct {
  25. arg string
  26. expect map[string]bool
  27. parseError string
  28. }{
  29. {
  30. arg: "",
  31. expect: map[string]bool{
  32. allAlphaGate: false,
  33. testAlphaGate: false,
  34. testBetaGate: false,
  35. },
  36. },
  37. {
  38. arg: "fooBarBaz=maybeidk",
  39. expect: map[string]bool{
  40. allAlphaGate: false,
  41. testAlphaGate: false,
  42. testBetaGate: false,
  43. },
  44. parseError: "unrecognized key: fooBarBaz",
  45. },
  46. {
  47. arg: "AllAlpha=false",
  48. expect: map[string]bool{
  49. allAlphaGate: false,
  50. testAlphaGate: false,
  51. testBetaGate: false,
  52. },
  53. },
  54. {
  55. arg: "AllAlpha=true",
  56. expect: map[string]bool{
  57. allAlphaGate: true,
  58. testAlphaGate: true,
  59. testBetaGate: false,
  60. },
  61. },
  62. {
  63. arg: "AllAlpha=banana",
  64. expect: map[string]bool{
  65. allAlphaGate: false,
  66. testAlphaGate: false,
  67. testBetaGate: false,
  68. },
  69. parseError: "invalid value of AllAlpha",
  70. },
  71. {
  72. arg: "AllAlpha=false,TestAlpha=true",
  73. expect: map[string]bool{
  74. allAlphaGate: false,
  75. testAlphaGate: true,
  76. testBetaGate: false,
  77. },
  78. },
  79. {
  80. arg: "TestAlpha=true,AllAlpha=false",
  81. expect: map[string]bool{
  82. allAlphaGate: false,
  83. testAlphaGate: true,
  84. testBetaGate: false,
  85. },
  86. },
  87. {
  88. arg: "AllAlpha=true,TestAlpha=false",
  89. expect: map[string]bool{
  90. allAlphaGate: true,
  91. testAlphaGate: false,
  92. testBetaGate: false,
  93. },
  94. },
  95. {
  96. arg: "TestAlpha=false,AllAlpha=true",
  97. expect: map[string]bool{
  98. allAlphaGate: true,
  99. testAlphaGate: false,
  100. testBetaGate: false,
  101. },
  102. },
  103. {
  104. arg: "TestBeta=true,AllAlpha=false",
  105. expect: map[string]bool{
  106. allAlphaGate: false,
  107. testAlphaGate: false,
  108. testBetaGate: true,
  109. },
  110. },
  111. }
  112. for i, test := range tests {
  113. fs := pflag.NewFlagSet("testfeaturegateflag", pflag.ContinueOnError)
  114. f := DefaultFeatureGate
  115. f.known[testAlphaGate] = featureSpec{false, alpha}
  116. f.known[testBetaGate] = featureSpec{false, beta}
  117. f.AddFlag(fs)
  118. err := fs.Parse([]string{fmt.Sprintf("--%s=%s", flagName, test.arg)})
  119. if test.parseError != "" {
  120. if !strings.Contains(err.Error(), test.parseError) {
  121. t.Errorf("%d: Parse() Expected %v, Got %v", i, test.parseError, err)
  122. }
  123. } else if err != nil {
  124. t.Errorf("%d: Parse() Expected nil, Got %v", i, err)
  125. }
  126. for k, v := range test.expect {
  127. if f.enabled[k] != v {
  128. t.Errorf("%d: expected %s=%v, Got %v", i, k, v, f.enabled[k])
  129. }
  130. }
  131. }
  132. }
  133. func TestFeatureGateFlagDefaults(t *testing.T) {
  134. // gates for testing
  135. const testAlphaGate = "TestAlpha"
  136. const testBetaGate = "TestBeta"
  137. // Don't parse the flag, assert defaults are used.
  138. f := DefaultFeatureGate
  139. f.known[testAlphaGate] = featureSpec{false, alpha}
  140. f.known[testBetaGate] = featureSpec{true, beta}
  141. if f.lookup(testAlphaGate) != false {
  142. t.Errorf("Expected false")
  143. }
  144. if f.lookup(testBetaGate) != true {
  145. t.Errorf("Expected true")
  146. }
  147. }