nested_volumes_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2018 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 util
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. "k8s.io/apimachinery/pkg/types"
  22. "k8s.io/apimachinery/pkg/util/sets"
  23. )
  24. type testCases struct {
  25. name string
  26. err bool
  27. expected sets.String
  28. volname string
  29. pod v1.Pod
  30. }
  31. func TestGetNestedMountpoints(t *testing.T) {
  32. var (
  33. testNamespace = "test_namespace"
  34. testPodUID = types.UID("test_pod_uid")
  35. )
  36. tc := []testCases{
  37. {
  38. name: "Simple Pod",
  39. err: false,
  40. expected: sets.NewString(),
  41. volname: "vol1",
  42. pod: v1.Pod{
  43. ObjectMeta: metav1.ObjectMeta{
  44. Namespace: testNamespace,
  45. UID: testPodUID,
  46. },
  47. Spec: v1.PodSpec{
  48. Containers: []v1.Container{
  49. {
  50. VolumeMounts: []v1.VolumeMount{
  51. {MountPath: "/dir", Name: "vol1"},
  52. },
  53. },
  54. },
  55. },
  56. },
  57. },
  58. {
  59. name: "Simple Nested Pod",
  60. err: false,
  61. expected: sets.NewString("nested"),
  62. volname: "vol1",
  63. pod: v1.Pod{
  64. ObjectMeta: metav1.ObjectMeta{
  65. Namespace: testNamespace,
  66. UID: testPodUID,
  67. },
  68. Spec: v1.PodSpec{
  69. Containers: []v1.Container{
  70. {
  71. VolumeMounts: []v1.VolumeMount{
  72. {MountPath: "/dir", Name: "vol1"},
  73. {MountPath: "/dir/nested", Name: "vol2"},
  74. },
  75. },
  76. },
  77. },
  78. },
  79. },
  80. {
  81. name: "Unsorted Nested Pod",
  82. err: false,
  83. expected: sets.NewString("nested", "nested2"),
  84. volname: "vol1",
  85. pod: v1.Pod{
  86. ObjectMeta: metav1.ObjectMeta{
  87. Namespace: testNamespace,
  88. UID: testPodUID,
  89. },
  90. Spec: v1.PodSpec{
  91. Containers: []v1.Container{
  92. {
  93. VolumeMounts: []v1.VolumeMount{
  94. {MountPath: "/dir/nested/double", Name: "vol3"},
  95. {MountPath: "/ignore", Name: "vol4"},
  96. {MountPath: "/dir/nested", Name: "vol2"},
  97. {MountPath: "/ignore2", Name: "vol5"},
  98. {MountPath: "/dir", Name: "vol1"},
  99. {MountPath: "/dir/nested2", Name: "vol3"},
  100. },
  101. },
  102. },
  103. },
  104. },
  105. },
  106. {
  107. name: "Multiple vol1 mounts Pod",
  108. err: false,
  109. expected: sets.NewString("nested", "nested2"),
  110. volname: "vol1",
  111. pod: v1.Pod{
  112. ObjectMeta: metav1.ObjectMeta{
  113. Namespace: testNamespace,
  114. UID: testPodUID,
  115. },
  116. Spec: v1.PodSpec{
  117. Containers: []v1.Container{
  118. {
  119. VolumeMounts: []v1.VolumeMount{
  120. {MountPath: "/dir", Name: "vol1"},
  121. {MountPath: "/dir/nested", Name: "vol2"},
  122. {MountPath: "/ignore", Name: "vol4"},
  123. {MountPath: "/other", Name: "vol1"},
  124. {MountPath: "/other/nested2", Name: "vol3"},
  125. },
  126. },
  127. },
  128. },
  129. },
  130. },
  131. {
  132. name: "Big Pod",
  133. err: false,
  134. volname: "vol1",
  135. expected: sets.NewString("sub1/sub2/sub3", "sub1/sub2/sub4", "sub1/sub2/sub6", "sub"),
  136. pod: v1.Pod{
  137. ObjectMeta: metav1.ObjectMeta{
  138. Namespace: testNamespace,
  139. UID: testPodUID,
  140. },
  141. Spec: v1.PodSpec{
  142. Containers: []v1.Container{
  143. {
  144. VolumeMounts: []v1.VolumeMount{
  145. {MountPath: "/mnt", Name: "vol1"},
  146. {MountPath: "/ignore", Name: "vol2"},
  147. {MountPath: "/mnt/sub1/sub2/sub3", Name: "vol3"},
  148. {MountPath: "/mnt/sub1/sub2/sub4", Name: "vol4"},
  149. {MountPath: "/mnt/sub1/sub2/sub4/skip", Name: "vol5"},
  150. {MountPath: "/mnt/sub1/sub2/sub4/skip2", Name: "vol5a"},
  151. {MountPath: "/mnt/sub1/sub2/sub6", Name: "vol6"},
  152. {MountPath: "/mnt7", Name: "vol7"},
  153. },
  154. },
  155. },
  156. InitContainers: []v1.Container{
  157. {
  158. VolumeMounts: []v1.VolumeMount{
  159. {MountPath: "/mnt/dir", Name: "vol1"},
  160. {MountPath: "/mnt/dir_ignore", Name: "vol8"},
  161. {MountPath: "/ignore", Name: "vol9"},
  162. {MountPath: "/mnt/dir/sub", Name: "vol11"},
  163. },
  164. },
  165. },
  166. },
  167. },
  168. },
  169. {
  170. name: "Naughty Pod",
  171. err: true,
  172. expected: nil,
  173. volname: "vol1",
  174. pod: v1.Pod{
  175. ObjectMeta: metav1.ObjectMeta{
  176. Namespace: testNamespace,
  177. UID: testPodUID,
  178. },
  179. Spec: v1.PodSpec{
  180. Containers: []v1.Container{
  181. {
  182. VolumeMounts: []v1.VolumeMount{
  183. {MountPath: "foo/../../dir", Name: "vol1"},
  184. {MountPath: "foo/../../dir/skip", Name: "vol10"},
  185. },
  186. },
  187. },
  188. },
  189. },
  190. },
  191. }
  192. for _, test := range tc {
  193. dir, err := ioutil.TempDir("", "TestMakeNestedMountpoints.")
  194. if err != nil {
  195. t.Errorf("Unexpected error trying to create temp directory: %v", err)
  196. return
  197. }
  198. defer os.RemoveAll(dir)
  199. rootdir := filepath.Join(dir, "vol")
  200. err = os.Mkdir(rootdir, 0755)
  201. if err != nil {
  202. t.Errorf("Unexpected error trying to create temp root directory: %v", err)
  203. return
  204. }
  205. dirs, err := getNestedMountpoints(test.volname, rootdir, test.pod)
  206. if test.err {
  207. if err == nil {
  208. t.Errorf("%v: expected error, got nil", test.name)
  209. }
  210. continue
  211. } else {
  212. if err != nil {
  213. t.Errorf("%v: expected no error, got %v", test.name, err)
  214. continue
  215. }
  216. }
  217. actual := sets.NewString(dirs...)
  218. if !test.expected.Equal(actual) {
  219. t.Errorf("%v: unexpected nested directories created:\nexpected: %v\n got: %v", test.name, test.expected, actual)
  220. }
  221. }
  222. }