flag_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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 flags
  14. import (
  15. "flag"
  16. "net/url"
  17. "os"
  18. "reflect"
  19. "testing"
  20. "github.com/coreos/etcd/pkg/transport"
  21. )
  22. func TestSetFlagsFromEnv(t *testing.T) {
  23. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  24. fs.String("a", "", "")
  25. fs.String("b", "", "")
  26. fs.String("c", "", "")
  27. fs.Parse([]string{})
  28. os.Clearenv()
  29. // flags should be settable using env vars
  30. os.Setenv("ETCD_A", "foo")
  31. // and command-line flags
  32. if err := fs.Set("b", "bar"); err != nil {
  33. t.Fatal(err)
  34. }
  35. // command-line flags take precedence over env vars
  36. os.Setenv("ETCD_C", "woof")
  37. if err := fs.Set("c", "quack"); err != nil {
  38. t.Fatal(err)
  39. }
  40. // first verify that flags are as expected before reading the env
  41. for f, want := range map[string]string{
  42. "a": "",
  43. "b": "bar",
  44. "c": "quack",
  45. } {
  46. if got := fs.Lookup(f).Value.String(); got != want {
  47. t.Fatalf("flag %q=%q, want %q", f, got, want)
  48. }
  49. }
  50. // now read the env and verify flags were updated as expected
  51. err := SetFlagsFromEnv(fs)
  52. if err != nil {
  53. t.Errorf("err=%v, want nil", err)
  54. }
  55. for f, want := range map[string]string{
  56. "a": "foo",
  57. "b": "bar",
  58. "c": "quack",
  59. } {
  60. if got := fs.Lookup(f).Value.String(); got != want {
  61. t.Errorf("flag %q=%q, want %q", f, got, want)
  62. }
  63. }
  64. }
  65. func TestSetFlagsFromEnvBad(t *testing.T) {
  66. // now verify that an error is propagated
  67. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  68. fs.Int("x", 0, "")
  69. os.Setenv("ETCD_X", "not_a_number")
  70. if err := SetFlagsFromEnv(fs); err == nil {
  71. t.Errorf("err=nil, want != nil")
  72. }
  73. }
  74. func TestURLsFromFlags(t *testing.T) {
  75. tests := []struct {
  76. args []string
  77. tlsInfo transport.TLSInfo
  78. wantURLs []url.URL
  79. wantFail bool
  80. }{
  81. // use -urls default when no flags defined
  82. {
  83. args: []string{},
  84. tlsInfo: transport.TLSInfo{},
  85. wantURLs: []url.URL{
  86. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  87. },
  88. wantFail: false,
  89. },
  90. // explicitly setting -urls should carry through
  91. {
  92. args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
  93. tlsInfo: transport.TLSInfo{},
  94. wantURLs: []url.URL{
  95. url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
  96. url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
  97. },
  98. wantFail: false,
  99. },
  100. // explicitly setting -addr should carry through
  101. {
  102. args: []string{"-addr=192.0.2.3:1024"},
  103. tlsInfo: transport.TLSInfo{},
  104. wantURLs: []url.URL{
  105. url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
  106. },
  107. wantFail: false,
  108. },
  109. // scheme prepended to -addr should be https if TLSInfo non-empty
  110. {
  111. args: []string{"-addr=192.0.2.3:1024"},
  112. tlsInfo: transport.TLSInfo{
  113. CertFile: "/tmp/foo",
  114. KeyFile: "/tmp/bar",
  115. },
  116. wantURLs: []url.URL{
  117. url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
  118. },
  119. wantFail: false,
  120. },
  121. // explicitly setting both -urls and -addr should fail
  122. {
  123. args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
  124. tlsInfo: transport.TLSInfo{},
  125. wantURLs: nil,
  126. wantFail: true,
  127. },
  128. }
  129. for i, tt := range tests {
  130. fs := flag.NewFlagSet("test", flag.PanicOnError)
  131. fs.Var(NewURLsValue("http://127.0.0.1:2379"), "urls", "")
  132. fs.Var(&IPAddressPort{}, "addr", "")
  133. if err := fs.Parse(tt.args); err != nil {
  134. t.Errorf("#%d: failed to parse flags: %v", i, err)
  135. continue
  136. }
  137. gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
  138. if tt.wantFail != (err != nil) {
  139. t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
  140. continue
  141. }
  142. if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
  143. t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
  144. }
  145. }
  146. }