config_test.go 5.6 KB

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