envconfig_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. *
  3. * Copyright 2022 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package envconfig
  19. import (
  20. "os"
  21. "testing"
  22. "google.golang.org/grpc/internal/grpctest"
  23. )
  24. type s struct {
  25. grpctest.Tester
  26. }
  27. func Test(t *testing.T) {
  28. grpctest.RunSubTests(t, s{})
  29. }
  30. func (s) TestUint64FromEnv(t *testing.T) {
  31. var testCases = []struct {
  32. name string
  33. val string
  34. def, min, max uint64
  35. want uint64
  36. }{
  37. {
  38. name: "error parsing",
  39. val: "asdf", def: 5, want: 5,
  40. }, {
  41. name: "unset",
  42. val: "", def: 5, want: 5,
  43. }, {
  44. name: "too low",
  45. val: "5", min: 10, want: 10,
  46. }, {
  47. name: "too high",
  48. val: "5", max: 2, want: 2,
  49. }, {
  50. name: "in range",
  51. val: "17391", def: 13000, min: 12000, max: 18000, want: 17391,
  52. },
  53. }
  54. for _, tc := range testCases {
  55. t.Run(tc.name, func(t *testing.T) {
  56. const testVar = "testvar"
  57. if tc.val == "" {
  58. os.Unsetenv(testVar)
  59. } else {
  60. os.Setenv(testVar, tc.val)
  61. }
  62. if got := uint64FromEnv(testVar, tc.def, tc.min, tc.max); got != tc.want {
  63. t.Errorf("uint64FromEnv(%q(=%q), %v, %v, %v) = %v; want %v", testVar, tc.val, tc.def, tc.min, tc.max, got, tc.want)
  64. }
  65. })
  66. }
  67. }
  68. func (s) TestBoolFromEnv(t *testing.T) {
  69. var testCases = []struct {
  70. val string
  71. def bool
  72. want bool
  73. }{
  74. {val: "", def: true, want: true},
  75. {val: "", def: false, want: false},
  76. {val: "true", def: true, want: true},
  77. {val: "true", def: false, want: true},
  78. {val: "false", def: true, want: false},
  79. {val: "false", def: false, want: false},
  80. {val: "asdf", def: true, want: true},
  81. {val: "asdf", def: false, want: false},
  82. }
  83. for _, tc := range testCases {
  84. t.Run("", func(t *testing.T) {
  85. const testVar = "testvar"
  86. if tc.val == "" {
  87. os.Unsetenv(testVar)
  88. } else {
  89. os.Setenv(testVar, tc.val)
  90. }
  91. if got := boolFromEnv(testVar, tc.def); got != tc.want {
  92. t.Errorf("boolFromEnv(%q(=%q), %v) = %v; want %v", testVar, tc.val, tc.def, got, tc.want)
  93. }
  94. })
  95. }
  96. }