flag_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. SetFlagsFromEnv(fs)
  52. for f, want := range map[string]string{
  53. "a": "foo",
  54. "b": "bar",
  55. "c": "quack",
  56. } {
  57. if got := fs.Lookup(f).Value.String(); got != want {
  58. t.Errorf("flag %q=%q, want %q", f, got, want)
  59. }
  60. }
  61. }
  62. func TestURLsFromFlags(t *testing.T) {
  63. tests := []struct {
  64. args []string
  65. tlsInfo transport.TLSInfo
  66. wantURLs []url.URL
  67. wantFail bool
  68. }{
  69. // use -urls default when no flags defined
  70. {
  71. args: []string{},
  72. tlsInfo: transport.TLSInfo{},
  73. wantURLs: []url.URL{
  74. url.URL{Scheme: "http", Host: "127.0.0.1:2379"},
  75. },
  76. wantFail: false,
  77. },
  78. // explicitly setting -urls should carry through
  79. {
  80. args: []string{"-urls=https://192.0.3.17:2930,http://127.0.0.1:1024"},
  81. tlsInfo: transport.TLSInfo{},
  82. wantURLs: []url.URL{
  83. url.URL{Scheme: "http", Host: "127.0.0.1:1024"},
  84. url.URL{Scheme: "https", Host: "192.0.3.17:2930"},
  85. },
  86. wantFail: false,
  87. },
  88. // explicitly setting -addr should carry through
  89. {
  90. args: []string{"-addr=192.0.2.3:1024"},
  91. tlsInfo: transport.TLSInfo{},
  92. wantURLs: []url.URL{
  93. url.URL{Scheme: "http", Host: "192.0.2.3:1024"},
  94. },
  95. wantFail: false,
  96. },
  97. // scheme prepended to -addr should be https if TLSInfo non-empty
  98. {
  99. args: []string{"-addr=192.0.2.3:1024"},
  100. tlsInfo: transport.TLSInfo{
  101. CertFile: "/tmp/foo",
  102. KeyFile: "/tmp/bar",
  103. },
  104. wantURLs: []url.URL{
  105. url.URL{Scheme: "https", Host: "192.0.2.3:1024"},
  106. },
  107. wantFail: false,
  108. },
  109. // explicitly setting both -urls and -addr should fail
  110. {
  111. args: []string{"-urls=https://127.0.0.1:1024", "-addr=192.0.2.3:1024"},
  112. tlsInfo: transport.TLSInfo{},
  113. wantURLs: nil,
  114. wantFail: true,
  115. },
  116. }
  117. for i, tt := range tests {
  118. fs := flag.NewFlagSet("test", flag.PanicOnError)
  119. fs.Var(NewURLsValue("http://127.0.0.1:2379"), "urls", "")
  120. fs.Var(&IPAddressPort{}, "addr", "")
  121. if err := fs.Parse(tt.args); err != nil {
  122. t.Errorf("#%d: failed to parse flags: %v", i, err)
  123. continue
  124. }
  125. gotURLs, err := URLsFromFlags(fs, "urls", "addr", tt.tlsInfo)
  126. if tt.wantFail != (err != nil) {
  127. t.Errorf("#%d: wantFail=%t, got err=%v", i, tt.wantFail, err)
  128. continue
  129. }
  130. if !reflect.DeepEqual(tt.wantURLs, gotURLs) {
  131. t.Errorf("#%d: incorrect URLs\nwant=%#v\ngot=%#v", i, tt.wantURLs, gotURLs)
  132. }
  133. }
  134. }