config_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. Copyright 2018 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. "bytes"
  16. "flag"
  17. "testing"
  18. "time"
  19. "github.com/stretchr/testify/assert"
  20. "github.com/stretchr/testify/require"
  21. )
  22. func TestInt(t *testing.T) {
  23. flags := flag.NewFlagSet("test", 0)
  24. var context struct {
  25. Number int `default:"5" usage:"some number"`
  26. }
  27. require.NotPanics(t, func() {
  28. AddOptionsToSet(flags, &context, "")
  29. })
  30. require.Equal(t, []simpleFlag{
  31. {
  32. name: "number",
  33. usage: "some number",
  34. defValue: "5",
  35. }},
  36. allFlags(flags))
  37. assert.Equal(t, 5, context.Number)
  38. }
  39. func TestLower(t *testing.T) {
  40. flags := flag.NewFlagSet("test", 0)
  41. var context struct {
  42. Ahem string
  43. MixedCase string
  44. }
  45. require.NotPanics(t, func() {
  46. AddOptionsToSet(flags, &context, "")
  47. })
  48. require.Equal(t, []simpleFlag{
  49. {
  50. name: "ahem",
  51. },
  52. {
  53. name: "mixedCase",
  54. },
  55. },
  56. allFlags(flags))
  57. }
  58. func TestPrefix(t *testing.T) {
  59. flags := flag.NewFlagSet("test", 0)
  60. var context struct {
  61. Number int `usage:"some number"`
  62. }
  63. require.NotPanics(t, func() {
  64. AddOptionsToSet(flags, &context, "some.prefix")
  65. })
  66. require.Equal(t, []simpleFlag{
  67. {
  68. name: "some.prefix.number",
  69. usage: "some number",
  70. defValue: "0",
  71. }},
  72. allFlags(flags))
  73. }
  74. func TestRecursion(t *testing.T) {
  75. flags := flag.NewFlagSet("test", 0)
  76. type Nested struct {
  77. Number1 int `usage:"embedded number"`
  78. }
  79. var context struct {
  80. Nested
  81. A struct {
  82. B struct {
  83. C struct {
  84. Number2 int `usage:"some number"`
  85. }
  86. }
  87. }
  88. }
  89. require.NotPanics(t, func() {
  90. AddOptionsToSet(flags, &context, "")
  91. })
  92. require.Equal(t, []simpleFlag{
  93. {
  94. name: "a.b.c.number2",
  95. usage: "some number",
  96. defValue: "0",
  97. },
  98. {
  99. name: "number1",
  100. usage: "embedded number",
  101. defValue: "0",
  102. },
  103. },
  104. allFlags(flags))
  105. }
  106. func TestPanics(t *testing.T) {
  107. flags := flag.NewFlagSet("test", 0)
  108. assert.PanicsWithValue(t, `invalid default "a" for int entry prefix.number: strconv.Atoi: parsing "a": invalid syntax`, func() {
  109. var context struct {
  110. Number int `default:"a"`
  111. }
  112. AddOptionsToSet(flags, &context, "prefix")
  113. })
  114. assert.PanicsWithValue(t, `invalid default "10000000000000000000" for int entry prefix.number: strconv.Atoi: parsing "10000000000000000000": value out of range`, func() {
  115. var context struct {
  116. Number int `default:"10000000000000000000"`
  117. }
  118. AddOptionsToSet(flags, &context, "prefix")
  119. })
  120. assert.PanicsWithValue(t, `options parameter without a type - nil?!`, func() {
  121. AddOptionsToSet(flags, nil, "")
  122. })
  123. assert.PanicsWithValue(t, `need a pointer to a struct, got instead: *int`, func() {
  124. number := 0
  125. AddOptionsToSet(flags, &number, "")
  126. })
  127. assert.PanicsWithValue(t, `struct entry "prefix.number" not exported`, func() {
  128. var context struct {
  129. number int
  130. }
  131. AddOptionsToSet(flags, &context, "prefix")
  132. })
  133. assert.PanicsWithValue(t, `unsupported struct entry type "prefix.someNumber": config.MyInt`, func() {
  134. type MyInt int
  135. var context struct {
  136. SomeNumber MyInt
  137. }
  138. AddOptionsToSet(flags, &context, "prefix")
  139. })
  140. }
  141. type TypesTestCase struct {
  142. name string
  143. copyFlags bool
  144. }
  145. func TestTypes(t *testing.T) {
  146. testcases := []TypesTestCase{
  147. {name: "directly"},
  148. {name: "CopyFlags", copyFlags: true},
  149. }
  150. for _, testcase := range testcases {
  151. testTypes(t, testcase)
  152. }
  153. }
  154. func testTypes(t *testing.T, testcase TypesTestCase) {
  155. flags := flag.NewFlagSet("test", 0)
  156. type Context struct {
  157. Bool bool `default:"true"`
  158. Duration time.Duration `default:"1ms"`
  159. Float64 float64 `default:"1.23456789"`
  160. String string `default:"hello world"`
  161. Int int `default:"-1" usage:"some number"`
  162. Int64 int64 `default:"-1234567890123456789"`
  163. Uint uint `default:"1"`
  164. Uint64 uint64 `default:"1234567890123456789"`
  165. }
  166. var context Context
  167. require.NotPanics(t, func() {
  168. AddOptionsToSet(flags, &context, "")
  169. })
  170. if testcase.copyFlags {
  171. original := bytes.Buffer{}
  172. flags.SetOutput(&original)
  173. flags.PrintDefaults()
  174. flags2 := flag.NewFlagSet("test", 0)
  175. CopyFlags(flags, flags2)
  176. flags = flags2
  177. copy := bytes.Buffer{}
  178. flags.SetOutput(&copy)
  179. flags.PrintDefaults()
  180. assert.Equal(t, original.String(), copy.String(), testcase.name+": help messages equal")
  181. assert.Contains(t, copy.String(), "some number", testcase.name+": copied help message contains defaults")
  182. }
  183. require.Equal(t, []simpleFlag{
  184. {
  185. name: "bool",
  186. defValue: "true",
  187. isBool: true,
  188. },
  189. {
  190. name: "duration",
  191. defValue: "1ms",
  192. },
  193. {
  194. name: "float64",
  195. defValue: "1.23456789",
  196. },
  197. {
  198. name: "int",
  199. usage: "some number",
  200. defValue: "-1",
  201. },
  202. {
  203. name: "int64",
  204. defValue: "-1234567890123456789",
  205. },
  206. {
  207. name: "string",
  208. defValue: "hello world",
  209. },
  210. {
  211. name: "uint",
  212. defValue: "1",
  213. },
  214. {
  215. name: "uint64",
  216. defValue: "1234567890123456789",
  217. },
  218. },
  219. allFlags(flags), testcase.name)
  220. assert.Equal(t,
  221. Context{true, time.Millisecond, 1.23456789, "hello world",
  222. -1, -1234567890123456789, 1, 1234567890123456789,
  223. },
  224. context,
  225. "default values must match")
  226. require.NoError(t, flags.Parse([]string{
  227. "-int", "-2",
  228. "-int64", "-9123456789012345678",
  229. "-uint", "2",
  230. "-uint64", "9123456789012345678",
  231. "-string", "pong",
  232. "-float64", "-1.23456789",
  233. "-bool=false",
  234. "-duration=1s",
  235. }), testcase.name)
  236. assert.Equal(t,
  237. Context{false, time.Second, -1.23456789, "pong",
  238. -2, -9123456789012345678, 2, 9123456789012345678,
  239. },
  240. context,
  241. testcase.name+": parsed values must match")
  242. }
  243. func allFlags(fs *flag.FlagSet) []simpleFlag {
  244. var flags []simpleFlag
  245. fs.VisitAll(func(f *flag.Flag) {
  246. s := simpleFlag{
  247. name: f.Name,
  248. usage: f.Usage,
  249. defValue: f.DefValue,
  250. }
  251. type boolFlag interface {
  252. flag.Value
  253. IsBoolFlag() bool
  254. }
  255. if fv, ok := f.Value.(boolFlag); ok && fv.IsBoolFlag() {
  256. s.isBool = true
  257. }
  258. flags = append(flags, s)
  259. })
  260. return flags
  261. }
  262. type simpleFlag struct {
  263. name string
  264. usage string
  265. defValue string
  266. isBool bool
  267. }