k8s.io/kubernetes@v1.29.3/pkg/volume/util/nested_volumes_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "k8s.io/api/core/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/types" 27 "k8s.io/apimachinery/pkg/util/sets" 28 ) 29 30 type testCases struct { 31 name string 32 err bool 33 expected sets.String 34 volname string 35 pod v1.Pod 36 } 37 38 func TestGetNestedMountpoints(t *testing.T) { 39 var ( 40 testNamespace = "test_namespace" 41 testPodUID = types.UID("test_pod_uid") 42 ) 43 44 tc := []testCases{ 45 { 46 name: "Simple Pod", 47 err: false, 48 expected: sets.NewString(), 49 volname: "vol1", 50 pod: v1.Pod{ 51 ObjectMeta: metav1.ObjectMeta{ 52 Namespace: testNamespace, 53 UID: testPodUID, 54 }, 55 Spec: v1.PodSpec{ 56 Containers: []v1.Container{ 57 { 58 VolumeMounts: []v1.VolumeMount{ 59 {MountPath: "/dir", Name: "vol1"}, 60 }, 61 }, 62 }, 63 }, 64 }, 65 }, 66 { 67 name: "Simple Nested Pod", 68 err: false, 69 expected: sets.NewString("nested"), 70 volname: "vol1", 71 pod: v1.Pod{ 72 ObjectMeta: metav1.ObjectMeta{ 73 Namespace: testNamespace, 74 UID: testPodUID, 75 }, 76 Spec: v1.PodSpec{ 77 Containers: []v1.Container{ 78 { 79 VolumeMounts: []v1.VolumeMount{ 80 {MountPath: "/dir", Name: "vol1"}, 81 {MountPath: "/dir/nested", Name: "vol2"}, 82 }, 83 }, 84 }, 85 }, 86 }, 87 }, 88 { 89 name: "Unsorted Nested Pod", 90 err: false, 91 expected: sets.NewString("nested", "nested2", "nested-vol", "nested.vol"), 92 volname: "vol1", 93 pod: v1.Pod{ 94 ObjectMeta: metav1.ObjectMeta{ 95 Namespace: testNamespace, 96 UID: testPodUID, 97 }, 98 Spec: v1.PodSpec{ 99 Containers: []v1.Container{ 100 { 101 VolumeMounts: []v1.VolumeMount{ 102 {MountPath: "/dir/nested/double", Name: "vol3"}, 103 {MountPath: "/ignore", Name: "vol4"}, 104 {MountPath: "/dir/nested", Name: "vol2"}, 105 {MountPath: "/ignore2", Name: "vol5"}, 106 {MountPath: "/dir", Name: "vol1"}, 107 {MountPath: "/dir/nested-vol", Name: "vol6"}, 108 {MountPath: "/dir/nested.vol", Name: "vol7"}, 109 {MountPath: "/dir/nested2/double", Name: "vol8"}, 110 {MountPath: "/dir/nested2", Name: "vol3"}, 111 }, 112 }, 113 }, 114 }, 115 }, 116 }, 117 { 118 name: "Multiple vol1 mounts Pod", 119 err: false, 120 expected: sets.NewString("nested", "nested2"), 121 volname: "vol1", 122 pod: v1.Pod{ 123 ObjectMeta: metav1.ObjectMeta{ 124 Namespace: testNamespace, 125 UID: testPodUID, 126 }, 127 Spec: v1.PodSpec{ 128 Containers: []v1.Container{ 129 { 130 VolumeMounts: []v1.VolumeMount{ 131 {MountPath: "/dir", Name: "vol1"}, 132 {MountPath: "/dir/nested", Name: "vol2"}, 133 {MountPath: "/ignore", Name: "vol4"}, 134 {MountPath: "/other", Name: "vol1"}, 135 {MountPath: "/other/nested2", Name: "vol3"}, 136 }, 137 }, 138 }, 139 }, 140 }, 141 }, 142 { 143 name: "Big Pod", 144 err: false, 145 volname: "vol1", 146 expected: sets.NewString(filepath.Join("sub1", "sub2", "sub3"), filepath.Join("sub1", "sub2", "sub4"), filepath.Join("sub1", "sub2", "sub6"), "sub"), 147 pod: v1.Pod{ 148 ObjectMeta: metav1.ObjectMeta{ 149 Namespace: testNamespace, 150 UID: testPodUID, 151 }, 152 Spec: v1.PodSpec{ 153 Containers: []v1.Container{ 154 { 155 VolumeMounts: []v1.VolumeMount{ 156 {MountPath: "/mnt", Name: "vol1"}, 157 {MountPath: "/ignore", Name: "vol2"}, 158 {MountPath: "/mnt/sub1/sub2/sub3", Name: "vol3"}, 159 {MountPath: "/mnt/sub1/sub2/sub4", Name: "vol4"}, 160 {MountPath: "/mnt/sub1/sub2/sub4/skip", Name: "vol5"}, 161 {MountPath: "/mnt/sub1/sub2/sub4/skip2", Name: "vol5a"}, 162 {MountPath: "/mnt/sub1/sub2/sub6", Name: "vol6"}, 163 {MountPath: "/mnt7", Name: "vol7"}, 164 }, 165 }, 166 }, 167 InitContainers: []v1.Container{ 168 { 169 VolumeMounts: []v1.VolumeMount{ 170 {MountPath: "/mnt/dir", Name: "vol1"}, 171 {MountPath: "/mnt/dir_ignore", Name: "vol8"}, 172 {MountPath: "/ignore", Name: "vol9"}, 173 {MountPath: "/mnt/dir/sub", Name: "vol11"}, 174 }, 175 }, 176 }, 177 }, 178 }, 179 }, 180 { 181 name: "Naughty Pod", 182 err: true, 183 expected: nil, 184 volname: "vol1", 185 pod: v1.Pod{ 186 ObjectMeta: metav1.ObjectMeta{ 187 Namespace: testNamespace, 188 UID: testPodUID, 189 }, 190 Spec: v1.PodSpec{ 191 Containers: []v1.Container{ 192 { 193 VolumeMounts: []v1.VolumeMount{ 194 {MountPath: "foo/../../dir", Name: "vol1"}, 195 {MountPath: "foo/../../dir/skip", Name: "vol10"}, 196 }, 197 }, 198 }, 199 }, 200 }, 201 }, 202 } 203 for _, test := range tc { 204 dir, err := os.MkdirTemp("", "TestMakeNestedMountpoints.") 205 if err != nil { 206 t.Errorf("Unexpected error trying to create temp directory: %v", err) 207 return 208 } 209 defer os.RemoveAll(dir) 210 211 rootdir := filepath.Join(dir, "vol") 212 err = os.Mkdir(rootdir, 0755) 213 if err != nil { 214 t.Errorf("Unexpected error trying to create temp root directory: %v", err) 215 return 216 } 217 218 dirs, err := getNestedMountpoints(test.volname, rootdir, test.pod) 219 if test.err { 220 if err == nil { 221 t.Errorf("%v: expected error, got nil", test.name) 222 } 223 continue 224 } else { 225 if err != nil { 226 t.Errorf("%v: expected no error, got %v", test.name, err) 227 continue 228 } 229 } 230 actual := sets.NewString(dirs...) 231 if !test.expected.Equal(actual) { 232 t.Errorf("%v: unexpected nested directories created:\nexpected: %v\n got: %v", test.name, test.expected, actual) 233 } 234 } 235 }