flag_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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:4001"},
  88. waddr: &IPAddressPort{IP: "0.0.0.0", Port: 4001},
  89. },
  90. // bindAddr flag set
  91. {
  92. args: []string{"-bind-addr=127.0.0.1:4001"},
  93. waddr: &IPAddressPort{IP: "127.0.0.1", Port: 4001},
  94. },
  95. // both addr flags set
  96. {
  97. args: []string{"-bind-addr=127.0.0.1:4001", "-addr=192.0.3.17:4001"},
  98. waddr: &IPAddressPort{IP: "127.0.0.1", Port: 4001},
  99. },
  100. }
  101. for i, tt := range tests {
  102. fs := flag.NewFlagSet("test", flag.PanicOnError)
  103. fs.Var(&IPAddressPort{}, "addr", "")
  104. bindAddr := &IPAddressPort{}
  105. fs.Var(bindAddr, "bind-addr", "")
  106. if err := fs.Parse(tt.args); err != nil {
  107. t.Errorf("#%d: failed to parse flags: %v", i, err)
  108. continue
  109. }
  110. SetBindAddrFromAddr(fs, "bind-addr", "addr")
  111. if !reflect.DeepEqual(bindAddr, tt.waddr) {
  112. t.Errorf("#%d: bindAddr = %+v, want %+v", i, bindAddr, tt.waddr)
  113. }
  114. }
  115. }
  116. func TestURLsFromFlags(t *testing.T) {
  117. tests := []struct {
  118. args []string
  119. tlsInfo transport.TLSInfo
  120. wantURLs []url.URL
  121. wantFail bool
  122. }{
  123. // use -urls default when no flags defined
  124. {
  125. args: []string{},
  126. tlsInfo: transport.TLSInfo{},
  127. wantURLs: []url.URL{
  128. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  129. },
  130. wantFail: false,
  131. },
  132. // explicitly setting -urls should carry through
  133. {
  134. args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
  135. tlsInfo: transport.TLSInfo{},
  136. wantURLs: []url.URL{
  137. url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
  138. url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
  139. },
  140. wantFail: false,
  141. },
  142. // explicitly setting -addr should carry through
  143. {
  144. args: []string{"-addr=192.0.2.3:1024"},
  145. tlsInfo: transport.TLSInfo{},
  146. wantURLs: []url.URL{
  147. url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
  148. },
  149. wantFail: false,
  150. },
  151. // scheme prepended to -addr should be https if TLSInfo non-empty
  152. {
  153. args: []string{"-addr=192.0.2.3:1024"},
  154. tlsInfo: transport.TLSInfo{
  155. CertFile: "/tmp/foo",
  156. KeyFile: "/tmp/bar",
  157. },
  158. wantURLs: []url.URL{
  159. url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
  160. },
  161. wantFail: false,
  162. },
  163. // explicitly setting both -urls and -addr should fail
  164. {
  165. args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
  166. tlsInfo: transport.TLSInfo{},
  167. wantURLs: nil,
  168. wantFail: true,
  169. },
  170. }
  171. for i, tt := range tests {
  172. fs := flag.NewFlagSet("test", flag.PanicOnError)
  173. fs.Var(NewURLsValue("http://127.0.0.1:2379"), "urls", "")
  174. fs.Var(&IPAddressPort{}, "addr", "")
  175. if err := fs.Parse(tt.args); err != nil {
  176. t.Errorf("#%d: failed to parse flags: %v", i, err)
  177. continue
  178. }
  179. gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
  180. if tt.wantFail != (err != nil) {
  181. t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
  182. continue
  183. }
  184. if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
  185. t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
  186. }
  187. }
  188. }