flag_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // first verify that flags are as expected before reading the env
  34. for f, want := range map[string]string{
  35. "a": "",
  36. "b": "bar",
  37. } {
  38. if got := fs.Lookup(f).Value.String(); got != want {
  39. t.Fatalf("flag %q=%q, want %q", f, got, want)
  40. }
  41. }
  42. // now read the env and verify flags were updated as expected
  43. err := SetFlagsFromEnv("ETCD", fs)
  44. if err != nil {
  45. t.Errorf("err=%v, want nil", err)
  46. }
  47. for f, want := range map[string]string{
  48. "a": "foo",
  49. "b": "bar",
  50. } {
  51. if got := fs.Lookup(f).Value.String(); got != want {
  52. t.Errorf("flag %q=%q, want %q", f, got, want)
  53. }
  54. }
  55. }
  56. func TestSetFlagsFromEnvBad(t *testing.T) {
  57. // now verify that an error is propagated
  58. fs := flag.NewFlagSet("testing", flag.ExitOnError)
  59. fs.Int("x", 0, "")
  60. os.Setenv("ETCD_X", "not_a_number")
  61. if err := SetFlagsFromEnv("ETCD", fs); err == nil {
  62. t.Errorf("err=nil, want != nil")
  63. }
  64. }
  65. func TestSetFlagsFromEnvParsingError(t *testing.T) {
  66. fs := flag.NewFlagSet("etcd", flag.ContinueOnError)
  67. var tickMs uint
  68. fs.UintVar(&tickMs, "heartbeat-interval", 0, "Time (in milliseconds) of a heartbeat interval.")
  69. if oerr := os.Setenv("ETCD_HEARTBEAT_INTERVAL", "100 # ms"); oerr != nil {
  70. t.Fatal(oerr)
  71. }
  72. defer os.Unsetenv("ETCD_HEARTBEAT_INTERVAL")
  73. if serr := SetFlagsFromEnv("ETCD", fs); serr.Error() != `invalid value "100 # ms" for ETCD_HEARTBEAT_INTERVAL: strconv.ParseUint: parsing "100 # ms": invalid syntax` {
  74. t.Fatalf("expected parsing error, got %v", serr)
  75. }
  76. }