flags_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 flag
  14. import (
  15. "strings"
  16. "testing"
  17. "github.com/spf13/pflag"
  18. )
  19. func TestIPVar(t *testing.T) {
  20. defaultIP := "0.0.0.0"
  21. testCases := []struct {
  22. argc string
  23. expectErr bool
  24. expectVal string
  25. }{
  26. {
  27. argc: "blah --ip=1.2.3.4",
  28. expectVal: "1.2.3.4",
  29. },
  30. {
  31. argc: "blah --ip=1.2.3.4a",
  32. expectErr: true,
  33. expectVal: defaultIP,
  34. },
  35. }
  36. for _, tc := range testCases {
  37. fs := pflag.NewFlagSet("blah", pflag.PanicOnError)
  38. ip := defaultIP
  39. fs.Var(IPVar{&ip}, "ip", "the ip")
  40. var err error
  41. func() {
  42. defer func() {
  43. if r := recover(); r != nil {
  44. err = r.(error)
  45. }
  46. }()
  47. fs.Parse(strings.Split(tc.argc, " "))
  48. }()
  49. if tc.expectErr && err == nil {
  50. t.Errorf("did not observe an expected error")
  51. continue
  52. }
  53. if !tc.expectErr && err != nil {
  54. t.Errorf("observed an unexpected error: %v", err)
  55. continue
  56. }
  57. if tc.expectVal != ip {
  58. t.Errorf("unexpected ip: expected %q, saw %q", tc.expectVal, ip)
  59. }
  60. }
  61. }
  62. func TestIPPortVar(t *testing.T) {
  63. defaultIPPort := "0.0.0.0:8080"
  64. testCases := []struct {
  65. desc string
  66. argc string
  67. expectErr bool
  68. expectVal string
  69. }{
  70. {
  71. desc: "valid ipv4 1",
  72. argc: "blah --ipport=0.0.0.0",
  73. expectVal: "0.0.0.0",
  74. },
  75. {
  76. desc: "valid ipv4 2",
  77. argc: "blah --ipport=127.0.0.1",
  78. expectVal: "127.0.0.1",
  79. },
  80. {
  81. desc: "invalid IP",
  82. argc: "blah --ipport=invalidip",
  83. expectErr: true,
  84. expectVal: defaultIPPort,
  85. },
  86. {
  87. desc: "valid ipv4 with port",
  88. argc: "blah --ipport=0.0.0.0:8080",
  89. expectVal: "0.0.0.0:8080",
  90. },
  91. {
  92. desc: "invalid ipv4 with invalid port",
  93. argc: "blah --ipport=0.0.0.0:invalidport",
  94. expectErr: true,
  95. expectVal: defaultIPPort,
  96. },
  97. {
  98. desc: "invalid IP with port",
  99. argc: "blah --ipport=invalidip:8080",
  100. expectErr: true,
  101. expectVal: defaultIPPort,
  102. },
  103. {
  104. desc: "valid ipv6 1",
  105. argc: "blah --ipport=::1",
  106. expectVal: "::1",
  107. },
  108. {
  109. desc: "valid ipv6 2",
  110. argc: "blah --ipport=::",
  111. expectVal: "::",
  112. },
  113. {
  114. desc: "valid ipv6 with port",
  115. argc: "blah --ipport=[::1]:8080",
  116. expectVal: "[::1]:8080",
  117. },
  118. {
  119. desc: "invalid ipv6 with port without bracket",
  120. argc: "blah --ipport=fd00:f00d:600d:f00d:8080",
  121. expectErr: true,
  122. expectVal: defaultIPPort,
  123. },
  124. }
  125. for _, tc := range testCases {
  126. fs := pflag.NewFlagSet("blah", pflag.PanicOnError)
  127. ipport := defaultIPPort
  128. fs.Var(IPPortVar{&ipport}, "ipport", "the ip:port")
  129. var err error
  130. func() {
  131. defer func() {
  132. if r := recover(); r != nil {
  133. err = r.(error)
  134. }
  135. }()
  136. fs.Parse(strings.Split(tc.argc, " "))
  137. }()
  138. if tc.expectErr && err == nil {
  139. t.Errorf("%q: Did not observe an expected error", tc.desc)
  140. continue
  141. }
  142. if !tc.expectErr && err != nil {
  143. t.Errorf("%q: Observed an unexpected error: %v", tc.desc, err)
  144. continue
  145. }
  146. if tc.expectVal != ipport {
  147. t.Errorf("%q: Unexpected ipport: expected %q, saw %q", tc.desc, tc.expectVal, ipport)
  148. }
  149. }
  150. }