flag_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 TestURLsFromFlags(t *testing.T) {
  76. tests := []struct {
  77. args []string
  78. tlsInfo transport.TLSInfo
  79. wantURLs []url.URL
  80. wantFail bool
  81. }{
  82. // use -urls default when no flags defined
  83. {
  84. args: []string{},
  85. tlsInfo: transport.TLSInfo{},
  86. wantURLs: []url.URL{
  87. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  88. },
  89. wantFail: false,
  90. },
  91. // explicitly setting -urls should carry through
  92. {
  93. args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
  94. tlsInfo: transport.TLSInfo{},
  95. wantURLs: []url.URL{
  96. url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
  97. url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
  98. },
  99. wantFail: false,
  100. },
  101. // explicitly setting -addr should carry through
  102. {
  103. args: []string{"-addr=192.0.2.3:1024"},
  104. tlsInfo: transport.TLSInfo{},
  105. wantURLs: []url.URL{
  106. url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
  107. },
  108. wantFail: false,
  109. },
  110. // scheme prepended to -addr should be https if TLSInfo non-empty
  111. {
  112. args: []string{"-addr=192.0.2.3:1024"},
  113. tlsInfo: transport.TLSInfo{
  114. CertFile: "/tmp/foo",
  115. KeyFile: "/tmp/bar",
  116. },
  117. wantURLs: []url.URL{
  118. url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
  119. },
  120. wantFail: false,
  121. },
  122. // explicitly setting both -urls and -addr should fail
  123. {
  124. args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
  125. tlsInfo: transport.TLSInfo{},
  126. wantURLs: nil,
  127. wantFail: true,
  128. },
  129. }
  130. for i, tt := range tests {
  131. fs := flag.NewFlagSet("test", flag.PanicOnError)
  132. fs.Var(NewURLsValue("http://127.0.0.1:2379"), "urls", "")
  133. fs.Var(&IPAddressPort{}, "addr", "")
  134. if err := fs.Parse(tt.args); err != nil {
  135. t.Errorf("#%d: failed to parse flags: %v", i, err)
  136. continue
  137. }
  138. gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
  139. if tt.wantFail != (err != nil) {
  140. t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
  141. continue
  142. }
  143. if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
  144. t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
  145. }
  146. }
  147. }