options_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright 2017 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 options
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "github.com/spf13/pflag"
  19. "k8s.io/apimachinery/pkg/util/diff"
  20. cliflag "k8s.io/component-base/cli/flag"
  21. )
  22. func newKubeletServerOrDie() *KubeletServer {
  23. s, err := NewKubeletServer()
  24. if err != nil {
  25. panic(err)
  26. }
  27. return s
  28. }
  29. func cleanFlags(s *KubeletServer) {
  30. s.DynamicConfigDir = cliflag.NewStringFlag(s.DynamicConfigDir.Value())
  31. }
  32. // TestRoundTrip ensures that flag values from the Kubelet can be serialized
  33. // to arguments and then read back and have the same value. Also catches cases
  34. // where the default value reported by the flag is not actually allowed to be
  35. // specified.
  36. func TestRoundTrip(t *testing.T) {
  37. testCases := []struct {
  38. name string
  39. inputFlags func() *KubeletServer
  40. outputFlags func() *KubeletServer
  41. flagDefaulter func(*pflag.FlagSet)
  42. skipDefault bool
  43. err bool
  44. expectArgs bool
  45. }{
  46. {
  47. name: "default flags are eliminated",
  48. inputFlags: newKubeletServerOrDie,
  49. outputFlags: newKubeletServerOrDie,
  50. flagDefaulter: newKubeletServerOrDie().AddFlags,
  51. err: false,
  52. expectArgs: false,
  53. },
  54. {
  55. name: "default flag values round trip",
  56. inputFlags: newKubeletServerOrDie,
  57. outputFlags: newKubeletServerOrDie,
  58. flagDefaulter: func(*pflag.FlagSet) {},
  59. err: false,
  60. expectArgs: true,
  61. },
  62. {
  63. name: "nil address does not fail for optional argument",
  64. inputFlags: func() *KubeletServer {
  65. s := newKubeletServerOrDie()
  66. s.HealthzBindAddress = ""
  67. return s
  68. },
  69. outputFlags: func() *KubeletServer {
  70. s := newKubeletServerOrDie()
  71. s.HealthzBindAddress = ""
  72. return s
  73. },
  74. flagDefaulter: func(*pflag.FlagSet) {},
  75. err: false,
  76. expectArgs: true,
  77. },
  78. }
  79. for _, testCase := range testCases {
  80. modifiedFlags := testCase.inputFlags()
  81. args := asArgs(modifiedFlags.AddFlags, testCase.flagDefaulter)
  82. if testCase.expectArgs != (len(args) > 0) {
  83. t.Errorf("%s: unexpected args: %v", testCase.name, args)
  84. continue
  85. }
  86. t.Logf("%s: args: %v", testCase.name, args)
  87. flagSet := pflag.NewFlagSet("output", pflag.ContinueOnError)
  88. outputFlags := testCase.outputFlags()
  89. outputFlags.AddFlags(flagSet)
  90. if err := flagSet.Parse(args); err != nil {
  91. if !testCase.err {
  92. t.Errorf("%s: unexpected flag error: %v", testCase.name, err)
  93. }
  94. continue
  95. }
  96. cleanFlags(outputFlags)
  97. if !reflect.DeepEqual(modifiedFlags, outputFlags) {
  98. t.Errorf("%s: flags did not round trip: %s", testCase.name, diff.ObjectReflectDiff(modifiedFlags, outputFlags))
  99. continue
  100. }
  101. }
  102. }
  103. func asArgs(fn, defaultFn func(*pflag.FlagSet)) []string {
  104. fs := pflag.NewFlagSet("extended", pflag.ContinueOnError)
  105. fn(fs)
  106. defaults := pflag.NewFlagSet("defaults", pflag.ContinueOnError)
  107. defaultFn(defaults)
  108. var args []string
  109. fs.VisitAll(func(flag *pflag.Flag) {
  110. // if the flag implements cliflag.OmitEmpty and the value is Empty, then just omit it from the command line
  111. if omit, ok := flag.Value.(cliflag.OmitEmpty); ok && omit.Empty() {
  112. return
  113. }
  114. s := flag.Value.String()
  115. // if the flag has the same value as the default, we can omit it without changing the meaning of the command line
  116. var defaultValue string
  117. if defaultFlag := defaults.Lookup(flag.Name); defaultFlag != nil {
  118. defaultValue = defaultFlag.Value.String()
  119. if s == defaultValue {
  120. return
  121. }
  122. }
  123. // if the flag is a string slice, each element is specified with an independent flag invocation
  124. if values, err := fs.GetStringSlice(flag.Name); err == nil {
  125. for _, s := range values {
  126. args = append(args, fmt.Sprintf("--%s=%s", flag.Name, s))
  127. }
  128. } else {
  129. if len(s) == 0 {
  130. s = defaultValue
  131. }
  132. args = append(args, fmt.Sprintf("--%s=%s", flag.Name, s))
  133. }
  134. })
  135. return args
  136. }