flag_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2015 The etcd Authors
  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. "os"
  18. "testing"
  19. )
  20. func TestSetFlagsFromEnv(t *testing.T) {
  21. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  22. fs.String("a", "", "")
  23. fs.String("b", "", "")
  24. fs.String("c", "", "")
  25. fs.Parse([]string{})
  26. os.Clearenv()
  27. // flags should be settable using env vars
  28. os.Setenv("ETCD_A", "foo")
  29. // and command-line flags
  30. if err := fs.Set("b", "bar"); err != nil {
  31. t.Fatal(err)
  32. }
  33. // command-line flags take precedence over env vars
  34. os.Setenv("ETCD_C", "woof")
  35. if err := fs.Set("c", "quack"); err != nil {
  36. t.Fatal(err)
  37. }
  38. // first verify that flags are as expected before reading the env
  39. for f, want := range map[string]string{
  40. "a": "",
  41. "b": "bar",
  42. "c": "quack",
  43. } {
  44. if got := fs.Lookup(f).Value.String(); got != want {
  45. t.Fatalf("flag %q=%q, want %q", f, got, want)
  46. }
  47. }
  48. // now read the env and verify flags were updated as expected
  49. err := SetFlagsFromEnv("ETCD", fs)
  50. if err != nil {
  51. t.Errorf("err=%v, want nil", err)
  52. }
  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 TestSetFlagsFromEnvBad(t *testing.T) {
  64. // now verify that an error is propagated
  65. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  66. fs.Int("x", 0, "")
  67. os.Setenv("ETCD_X", "not_a_number")
  68. if err := SetFlagsFromEnv("ETCD", fs); err == nil {
  69. t.Errorf("err=nil, want != nil")
  70. }
  71. }
  72. func TestSetFlagsFromEnvParsingError(t *testing.T) {
  73. fs := flag.NewFlagSet("etcd", flag.ContinueOnError)
  74. var tickMs uint
  75. fs.UintVar(&tickMs, "heartbeat-interval", 0, "Time (in milliseconds) of a heartbeat interval.")
  76. if oerr := os.Setenv("ETCD_HEARTBEAT_INTERVAL", "100 # ms"); oerr != nil {
  77. t.Fatal(oerr)
  78. }
  79. defer os.Unsetenv("ETCD_HEARTBEAT_INTERVAL")
  80. if serr := SetFlagsFromEnv("ETCD", fs); serr.Error() != `invalid value "100 # ms" for ETCD_HEARTBEAT_INTERVAL: parse error` {
  81. t.Fatalf("expected parsing error, got %v", serr)
  82. }
  83. }