flag_test.go 3.2 KB

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