volumes_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "testing"
  16. v1 "k8s.io/api/core/v1"
  17. )
  18. func TestVolumeExists(t *testing.T) {
  19. vols := []v1.Volume{{Name: "a"}, {Name: "b"}, {Name: "d"}}
  20. type args struct {
  21. volumeName string
  22. volumes []v1.Volume
  23. }
  24. tests := []struct {
  25. name string
  26. args args
  27. wantErr bool
  28. }{
  29. {"exists", args{"d", vols}, false},
  30. {"does-not-exist", args{"c", vols}, true},
  31. }
  32. for _, tt := range tests {
  33. t.Run(tt.name, func(t *testing.T) {
  34. if err := VolumeExists(tt.args.volumeName, tt.args.volumes); (err != nil) != tt.wantErr {
  35. t.Errorf("VolumeExists() error = %v, wantErr %v", err, tt.wantErr)
  36. }
  37. })
  38. }
  39. }
  40. func vols(v ...v1.Volume) []v1.Volume {
  41. return v
  42. }
  43. func TestVolumeIsEmptyDir(t *testing.T) {
  44. emptyVolume := v1.Volume{Name: "e", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
  45. emptyAndHostVolume := v1.Volume{Name: "e&hp", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
  46. emptyAndHostVolume.VolumeSource.HostPath = &v1.HostPathVolumeSource{Path: "/dev/sdx"}
  47. hostVolume := v1.Volume{Name: "h", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev/vdh"}}}
  48. type args struct {
  49. volumeName string
  50. volumes []v1.Volume
  51. }
  52. tests := []struct {
  53. name string
  54. args args
  55. wantErr bool
  56. }{
  57. {"is EmptyDir", args{"e", vols(emptyVolume)}, false},
  58. {"is HostPath", args{"h", vols(hostVolume)}, true},
  59. {"EmptyDir and HostPath", args{"e&hp", vols(emptyAndHostVolume)}, true},
  60. {"not found", args{"e", vols(hostVolume)}, true},
  61. {"many ; ok", args{"e", vols(emptyVolume, hostVolume, emptyAndHostVolume)}, false},
  62. {"many ; nf", args{"e", vols(hostVolume, emptyAndHostVolume)}, true},
  63. }
  64. for _, tt := range tests {
  65. t.Run(tt.name, func(t *testing.T) {
  66. if err := VolumeIsEmptyDir(tt.args.volumeName, tt.args.volumes); (err != nil) != tt.wantErr {
  67. t.Errorf("VolumeIsEmptyDir() error = %v, wantErr %v", err, tt.wantErr)
  68. }
  69. })
  70. }
  71. }
  72. func TestVolumeIsHostPath(t *testing.T) {
  73. emptyVolume := v1.Volume{Name: "e", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
  74. emptyAndHostVolume := v1.Volume{Name: "e&hp", VolumeSource: v1.VolumeSource{EmptyDir: &v1.EmptyDirVolumeSource{}}}
  75. emptyAndHostVolume.VolumeSource.HostPath = &v1.HostPathVolumeSource{Path: "/dev/sdx"}
  76. hostVolume := v1.Volume{Name: "h", VolumeSource: v1.VolumeSource{HostPath: &v1.HostPathVolumeSource{Path: "/dev/vdh"}}}
  77. type args struct {
  78. volumeName string
  79. path string
  80. volumes []v1.Volume
  81. }
  82. tests := []struct {
  83. name string
  84. args args
  85. wantErr bool
  86. }{
  87. {"is EmptyDir", args{"e", "/dev/sdx", vols(emptyVolume)}, true},
  88. {"is HostPath", args{"h", "/dev/vdh", vols(hostVolume)}, false},
  89. {"wrong HostPath", args{"h", "/dev/sdx", vols(hostVolume)}, true},
  90. {"EmptyDir and HostPath", args{"e&hp", "/dev/sdx", vols(emptyAndHostVolume)}, true},
  91. {"not found", args{"e", "/dev/sdx", vols(hostVolume)}, true},
  92. {"many ; ok", args{"h", "/dev/vdh", vols(emptyVolume, hostVolume, emptyAndHostVolume)}, false},
  93. {"many ; nf", args{"h", "/dev/vdh", vols(emptyVolume, emptyAndHostVolume)}, true},
  94. }
  95. for _, tt := range tests {
  96. t.Run(tt.name, func(t *testing.T) {
  97. if err := VolumeIsHostPath(tt.args.volumeName, tt.args.path, tt.args.volumes); (err != nil) != tt.wantErr {
  98. t.Errorf("VolumeIsHostPath() error = %v, wantErr %v", err, tt.wantErr)
  99. }
  100. })
  101. }
  102. }
  103. func TestVolumeMountExists(t *testing.T) {
  104. mounts := []v1.VolumeMount{{Name: "a"}, {Name: "b"}, {Name: "d"}}
  105. type args struct {
  106. mountName string
  107. mounts []v1.VolumeMount
  108. }
  109. tests := []struct {
  110. name string
  111. args args
  112. wantErr bool
  113. }{
  114. {"exists", args{"d", mounts}, false},
  115. {"does-not-exist", args{"c", mounts}, true},
  116. }
  117. for _, tt := range tests {
  118. t.Run(tt.name, func(t *testing.T) {
  119. if err := VolumeMountExists(tt.args.mountName, tt.args.mounts); (err != nil) != tt.wantErr {
  120. t.Errorf("VolumeMountExists() error = %v, wantErr %v", err, tt.wantErr)
  121. }
  122. })
  123. }
  124. }
  125. // Don't test human readables since they aren't critical to function