flag_test.go 2.4 KB

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