env_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2018 The Rook Authors. All rights reserved.
  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 test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. )
  19. func TestGetEnv(t *testing.T) {
  20. type args struct {
  21. name string
  22. envs []v1.EnvVar
  23. }
  24. waldo := v1.EnvVar{Name: "waldo", Value: "peekaboo"}
  25. carmen := v1.EnvVar{Name: "carmen", Value: "sandiego"}
  26. batman := v1.EnvVar{Name: "batman", Value: "404: Not Found"}
  27. empty := v1.EnvVar{Name: "", Value: ""}
  28. tests := []struct {
  29. name string
  30. args args
  31. want *v1.EnvVar
  32. wantErr bool
  33. }{
  34. {"env in 1-item list", args{"waldo", []v1.EnvVar{waldo}}, &waldo, false},
  35. {"env not in empty list", args{"carmen", []v1.EnvVar{}}, nil, true},
  36. {"env in 3-item list", args{"batman", []v1.EnvVar{waldo, carmen, batman}}, &batman, false},
  37. {"empty env not in list", args{"", []v1.EnvVar{waldo, carmen, batman}}, nil, true},
  38. {"empty env in list", args{"", []v1.EnvVar{waldo, carmen, batman, empty}}, &empty, false},
  39. }
  40. for _, tt := range tests {
  41. t.Run(tt.name, func(t *testing.T) {
  42. got, err := GetEnv(tt.args.name, tt.args.envs)
  43. if (err != nil) != tt.wantErr {
  44. t.Errorf("GetEnv() error = %v, wantErr %v", err, tt.wantErr)
  45. return
  46. }
  47. // Don't care about return value if it reports error
  48. if !tt.wantErr && !reflect.DeepEqual(got, tt.want) {
  49. t.Errorf("GetEnv() = %v, want %v", got, tt.want)
  50. }
  51. })
  52. }
  53. }