volume_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2023 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 v1
  14. import (
  15. "fmt"
  16. "reflect"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. v1 "k8s.io/api/core/v1"
  20. )
  21. func validateToVolumeSource(
  22. t *testing.T,
  23. fieldUnderTest string, fieldValue reflect.Value,
  24. in *ConfigFileVolumeSource,
  25. ) {
  26. got := in.ToKubernetesVolumeSource()
  27. // validate got
  28. vGot := reflect.ValueOf(got).Elem()
  29. for _, gField := range reflect.VisibleFields(vGot.Type()) {
  30. gFieldVal := vGot.FieldByName(gField.Name)
  31. if gField.Name != fieldUnderTest {
  32. assert.Nilf(t, gFieldVal.Interface(), "fields NOT under test should be nil")
  33. continue
  34. }
  35. assert.Equalf(t, fieldValue.Interface(), gFieldVal.Interface(),
  36. "fields under test should be deeply equal to what was created")
  37. }
  38. }
  39. func TestConfigFileVolumeSource_ToVolumeSource(t *testing.T) {
  40. t.Run("nil receiver", func(t *testing.T) {
  41. var in *ConfigFileVolumeSource = nil
  42. got := in.ToKubernetesVolumeSource()
  43. assert.Nil(t, got)
  44. })
  45. t.Run("zero-value receiver", func(t *testing.T) {
  46. in := &ConfigFileVolumeSource{}
  47. got := in.ToKubernetesVolumeSource()
  48. assert.Equal(t, v1.VolumeSource{}, *got)
  49. })
  50. for _, field := range reflect.VisibleFields(reflect.TypeOf(ConfigFileVolumeSource{})) {
  51. // for each struct field of ConfigFileVolumeSource, create a new CFVS with that field filled
  52. // in with some non-nil value to test ToVolumeSource() with. Then ensure that every
  53. // possible volume type of the CFVS converts to k8s' corev1.VolumeSource successfully
  54. in := &ConfigFileVolumeSource{}
  55. // use reflection to set the field under test with a non-nil created object
  56. vIn := reflect.ValueOf(in).Elem()
  57. fIn := vIn.FieldByName(field.Name)
  58. baseType := field.Type.Elem()
  59. fVal := reflect.New(baseType)
  60. fIn.Set(fVal)
  61. t.Run(fmt.Sprintf("%s: %s{}", field.Name, field.Type), func(t *testing.T) {
  62. // test with zero object
  63. validateToVolumeSource(t, field.Name, fVal, in)
  64. })
  65. t.Run(fmt.Sprintf("%s: %s{<some-data>}", field.Name, field.Type), func(t *testing.T) {
  66. // set some data set on the object
  67. setSomeFields(field.Type.Elem(), fVal.Elem())
  68. fIn.Set(fVal)
  69. validateToVolumeSource(t, field.Name, fVal, in)
  70. })
  71. }
  72. }
  73. func setSomeFields(t reflect.Type, v reflect.Value) {
  74. for _, f := range reflect.VisibleFields(t) {
  75. fVal := v.FieldByName(f.Name)
  76. setSomeData(fVal)
  77. }
  78. }
  79. func setSomeData(v reflect.Value) {
  80. switch v.Kind() {
  81. case reflect.Pointer:
  82. v.Set(reflect.New(v.Type().Elem()))
  83. setSomeData(v.Elem())
  84. case reflect.String:
  85. v.SetString("string-data")
  86. case reflect.Bool:
  87. v.SetBool(true)
  88. case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
  89. v.SetInt(0755)
  90. }
  91. }