flag_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package flags
  15. import (
  16. "flag"
  17. "net/url"
  18. "os"
  19. "reflect"
  20. "testing"
  21. "github.com/coreos/etcd/pkg/transport"
  22. )
  23. func TestSetFlagsFromEnv(t *testing.T) {
  24. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  25. fs.String("a", "", "")
  26. fs.String("b", "", "")
  27. fs.String("c", "", "")
  28. fs.Parse([]string{})
  29. os.Clearenv()
  30. // flags should be settable using env vars
  31. os.Setenv("ETCD_A", "foo")
  32. // and command-line flags
  33. if err := fs.Set("b", "bar"); err != nil {
  34. t.Fatal(err)
  35. }
  36. // command-line flags take precedence over env vars
  37. os.Setenv("ETCD_C", "woof")
  38. if err := fs.Set("c", "quack"); err != nil {
  39. t.Fatal(err)
  40. }
  41. // first verify that flags are as expected before reading the env
  42. for f, want := range map[string]string{
  43. "a": "",
  44. "b": "bar",
  45. "c": "quack",
  46. } {
  47. if got := fs.Lookup(f).Value.String(); got != want {
  48. t.Fatalf("flag %q=%q, want %q", f, got, want)
  49. }
  50. }
  51. // now read the env and verify flags were updated as expected
  52. err := SetFlagsFromEnv(fs)
  53. if err != nil {
  54. t.Errorf("err=%v, want nil", err)
  55. }
  56. for f, want := range map[string]string{
  57. "a": "foo",
  58. "b": "bar",
  59. "c": "quack",
  60. } {
  61. if got := fs.Lookup(f).Value.String(); got != want {
  62. t.Errorf("flag %q=%q, want %q", f, got, want)
  63. }
  64. }
  65. }
  66. func TestSetFlagsFromEnvBad(t *testing.T) {
  67. // now verify that an error is propagated
  68. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  69. fs.Int("x", 0, "")
  70. os.Setenv("ETCD_X", "not_a_number")
  71. if err := SetFlagsFromEnv(fs); err == nil {
  72. t.Errorf("err=nil, want != nil")
  73. }
  74. }
  75. func TestSetBindAddrFromAddr(t *testing.T) {
  76. tests := []struct {
  77. args []string
  78. waddr *IPAddressPort
  79. }{
  80. // no flags set
  81. {
  82. args: []string{},
  83. waddr: &IPAddressPort{},
  84. },
  85. // addr flag set
  86. {
  87. args: []string{"-addr=192.0.3.17:2379"},
  88. waddr: &IPAddressPort{IP: "::", Port: 2379},
  89. },
  90. // bindAddr flag set
  91. {
  92. args: []string{"-bind-addr=127.0.0.1:2379"},
  93. waddr: &IPAddressPort{IP: "127.0.0.1", Port: 2379},
  94. },
  95. // both addr flags set
  96. {
  97. args: []string{"-bind-addr=127.0.0.1:2379", "-addr=192.0.3.17:2379"},
  98. waddr: &IPAddressPort{IP: "127.0.0.1", Port: 2379},
  99. },
  100. // both addr flags set, IPv6
  101. {
  102. args: []string{"-bind-addr=[2001:db8::4:9]:2379", "-addr=[2001:db8::4:f0]:2379"},
  103. waddr: &IPAddressPort{IP: "2001:db8::4:9", Port: 2379},
  104. },
  105. }
  106. for i, tt := range tests {
  107. fs := flag.NewFlagSet("test", flag.PanicOnError)
  108. fs.Var(&IPAddressPort{}, "addr", "")
  109. bindAddr := &IPAddressPort{}
  110. fs.Var(bindAddr, "bind-addr", "")
  111. if err := fs.Parse(tt.args); err != nil {
  112. t.Errorf("#%d: failed to parse flags: %v", i, err)
  113. continue
  114. }
  115. SetBindAddrFromAddr(fs, "bind-addr", "addr")
  116. if !reflect.DeepEqual(bindAddr, tt.waddr) {
  117. t.Errorf("#%d: bindAddr = %+v, want %+v", i, bindAddr, tt.waddr)
  118. }
  119. }
  120. }
  121. func TestURLsFromFlags(t *testing.T) {
  122. tests := []struct {
  123. args []string
  124. tlsInfo transport.TLSInfo
  125. wantURLs []url.URL
  126. wantFail bool
  127. }{
  128. // use -urls default when no flags defined
  129. {
  130. args: []string{},
  131. tlsInfo: transport.TLSInfo{},
  132. wantURLs: []url.URL{
  133. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  134. },
  135. wantFail: false,
  136. },
  137. // explicitly setting -urls should carry through
  138. {
  139. args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
  140. tlsInfo: transport.TLSInfo{},
  141. wantURLs: []url.URL{
  142. url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
  143. url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
  144. },
  145. wantFail: false,
  146. },
  147. // explicitly setting -addr should carry through
  148. {
  149. args: []string{"-addr=192.0.2.3:1024"},
  150. tlsInfo: transport.TLSInfo{},
  151. wantURLs: []url.URL{
  152. url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
  153. },
  154. wantFail: false,
  155. },
  156. // scheme prepended to -addr should be https if TLSInfo non-empty
  157. {
  158. args: []string{"-addr=192.0.2.3:1024"},
  159. tlsInfo: transport.TLSInfo{
  160. CertFile: "/tmp/foo",
  161. KeyFile: "/tmp/bar",
  162. },
  163. wantURLs: []url.URL{
  164. url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
  165. },
  166. wantFail: false,
  167. },
  168. // explicitly setting both -urls and -addr should fail
  169. {
  170. args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
  171. tlsInfo: transport.TLSInfo{},
  172. wantURLs: nil,
  173. wantFail: true,
  174. },
  175. }
  176. for i, tt := range tests {
  177. fs := flag.NewFlagSet("test", flag.PanicOnError)
  178. fs.Var(NewURLsValue("http://127.0.0.1:2379"), "urls", "")
  179. fs.Var(&IPAddressPort{}, "addr", "")
  180. if err := fs.Parse(tt.args); err != nil {
  181. t.Errorf("#%d: failed to parse flags: %v", i, err)
  182. continue
  183. }
  184. gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
  185. if tt.wantFail != (err != nil) {
  186. t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
  187. continue
  188. }
  189. if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
  190. t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
  191. }
  192. }
  193. }