flag_test.go 3.8 KB

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