env_parse_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 env
  14. import (
  15. "fmt"
  16. "io"
  17. "strings"
  18. )
  19. func ExampleIsEnvironmentArgument_true() {
  20. test := "returns=true"
  21. fmt.Println(IsEnvironmentArgument(test))
  22. // Output: true
  23. }
  24. func ExampleIsEnvironmentArgument_false() {
  25. test := "returnsfalse"
  26. fmt.Println(IsEnvironmentArgument(test))
  27. // Output: false
  28. }
  29. func ExampleIsValidEnvironmentArgument_true() {
  30. test := "wordcharacters=true"
  31. fmt.Println(IsValidEnvironmentArgument(test))
  32. // Output: true
  33. }
  34. func ExampleIsValidEnvironmentArgument_false() {
  35. test := "not$word^characters=test"
  36. fmt.Println(IsValidEnvironmentArgument(test))
  37. // Output: false
  38. }
  39. func ExampleSplitEnvironmentFromResources() {
  40. args := []string{`resource`, "ENV\\=ARG", `ONE\=MORE`, `DASH-`}
  41. fmt.Println(SplitEnvironmentFromResources(args))
  42. // Output: [resource] [ENV\=ARG ONE\=MORE DASH-] true
  43. }
  44. func ExampleParseEnv_good() {
  45. r := strings.NewReader("FROM=READER")
  46. ss := []string{"ENV=VARIABLE", "AND=ANOTHER", "REMOVE-", "-"}
  47. fmt.Println(ParseEnv(ss, r))
  48. // Output:
  49. // [{ENV VARIABLE nil} {AND ANOTHER nil} {FROM READER nil}] [REMOVE] <nil>
  50. }
  51. func ExampleParseEnv_bad() {
  52. var r io.Reader
  53. bad := []string{"This not in the key=value format."}
  54. fmt.Println(ParseEnv(bad, r))
  55. // Output:
  56. // [] [] environment variables must be of the form key=value and can only contain letters, numbers, and underscores
  57. }