env_file_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package versioned
  14. import (
  15. "os"
  16. "strings"
  17. "testing"
  18. )
  19. // Test the cases of proccessEnvFileLine that can be run without touching the
  20. // environment.
  21. func Test_processEnvFileLine(t *testing.T) {
  22. testCases := []struct {
  23. name string
  24. line []byte
  25. currentLine int
  26. expectedKey string
  27. expectedValue string
  28. expectedErr string
  29. }{
  30. {"the utf8bom is trimmed on the first line",
  31. append(utf8bom, 'a', '=', 'c'), 0, "a", "c", ""},
  32. {"the utf8bom is NOT trimmed on the second line",
  33. append(utf8bom, 'a', '=', 'c'), 1, "", "", "not a valid key name"},
  34. {"no key is returned on a comment line",
  35. []byte{' ', '#', 'c'}, 0, "", "", ""},
  36. {"no key is returned on a blank line",
  37. []byte{' ', ' ', '\t'}, 0, "", "", ""},
  38. {"key is returned even with no value",
  39. []byte{' ', 'x', '='}, 0, "x", "", ""},
  40. }
  41. for _, tt := range testCases {
  42. t.Run(tt.name, func(t *testing.T) {
  43. key, value, err := proccessEnvFileLine(tt.line, `filename`, tt.currentLine)
  44. t.Logf("Testing that %s.", tt.name)
  45. if tt.expectedKey != key {
  46. t.Errorf("\texpected key %q, received %q", tt.expectedKey, key)
  47. }
  48. if tt.expectedValue != value {
  49. t.Errorf("\texpected value %q, received %q", tt.expectedValue, value)
  50. }
  51. if len(tt.expectedErr) == 0 {
  52. if err != nil {
  53. t.Errorf("\tunexpected err %v", err)
  54. }
  55. } else {
  56. if !strings.Contains(err.Error(), tt.expectedErr) {
  57. t.Errorf("\terr %v doesn't match expected %q", err, tt.expectedErr)
  58. }
  59. }
  60. })
  61. }
  62. }
  63. // proccessEnvFileLine needs to fetch the value from the environment if no
  64. // equals sign is provided.
  65. // For example:
  66. //
  67. // my_key1=alpha
  68. // my_key2=beta
  69. // my_key3
  70. //
  71. // In this file, my_key3 must be fetched from the environment.
  72. // Test this capability.
  73. func Test_processEnvFileLine_readEnvironment(t *testing.T) {
  74. const realKey = "k8s_test_env_file_key"
  75. const realValue = `my_value`
  76. // Just in case, these two lines ensure the environment is restored to
  77. // its original state.
  78. original := os.Getenv(realKey)
  79. defer func() { os.Setenv(realKey, original) }()
  80. os.Setenv(realKey, `my_value`)
  81. key, value, err := proccessEnvFileLine([]byte(realKey), `filename`, 3)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if key != realKey {
  86. t.Errorf(`expected key %q, received %q`, realKey, key)
  87. }
  88. if value != realValue {
  89. t.Errorf(`expected value %q, received %q`, realValue, value)
  90. }
  91. }