|
| 1 | +// +build linux |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2018 The Kubernetes Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package kubelet |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "io/ioutil" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "testing" |
| 27 | + |
| 28 | + v1 "k8s.io/api/core/v1" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + |
| 31 | + _ "k8s.io/kubernetes/pkg/apis/core/install" |
| 32 | +) |
| 33 | + |
| 34 | +func validateDirExists(dir string) error { |
| 35 | + _, err := ioutil.ReadDir(dir) |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +func validateDirNotExists(dir string) error { |
| 43 | + _, err := ioutil.ReadDir(dir) |
| 44 | + if os.IsNotExist(err) { |
| 45 | + return nil |
| 46 | + } |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + return fmt.Errorf("dir %q still exists", dir) |
| 51 | +} |
| 52 | + |
| 53 | +func TestCleanupOrphanedPodDirs(t *testing.T) { |
| 54 | + testCases := map[string]struct { |
| 55 | + pods []*v1.Pod |
| 56 | + prepareFunc func(kubelet *Kubelet) error |
| 57 | + validateFunc func(kubelet *Kubelet) error |
| 58 | + expectErr bool |
| 59 | + }{ |
| 60 | + "nothing-to-do": {}, |
| 61 | + "pods-dir-not-found": { |
| 62 | + prepareFunc: func(kubelet *Kubelet) error { |
| 63 | + return os.Remove(kubelet.getPodsDir()) |
| 64 | + }, |
| 65 | + expectErr: true, |
| 66 | + }, |
| 67 | + "pod-doesnot-exist-novolume": { |
| 68 | + prepareFunc: func(kubelet *Kubelet) error { |
| 69 | + podDir := kubelet.getPodDir("pod1uid") |
| 70 | + return os.MkdirAll(filepath.Join(podDir, "not/a/volume"), 0750) |
| 71 | + }, |
| 72 | + validateFunc: func(kubelet *Kubelet) error { |
| 73 | + podDir := kubelet.getPodDir("pod1uid") |
| 74 | + return validateDirNotExists(filepath.Join(podDir, "not")) |
| 75 | + }, |
| 76 | + }, |
| 77 | + "pod-exists-with-volume": { |
| 78 | + pods: []*v1.Pod{ |
| 79 | + { |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: "pod1", |
| 82 | + UID: "pod1uid", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + prepareFunc: func(kubelet *Kubelet) error { |
| 87 | + podDir := kubelet.getPodDir("pod1uid") |
| 88 | + return os.MkdirAll(filepath.Join(podDir, "volumes/plugin/name"), 0750) |
| 89 | + }, |
| 90 | + validateFunc: func(kubelet *Kubelet) error { |
| 91 | + podDir := kubelet.getPodDir("pod1uid") |
| 92 | + return validateDirExists(filepath.Join(podDir, "volumes/plugin/name")) |
| 93 | + }, |
| 94 | + }, |
| 95 | + "pod-doesnot-exist-with-volume": { |
| 96 | + prepareFunc: func(kubelet *Kubelet) error { |
| 97 | + podDir := kubelet.getPodDir("pod1uid") |
| 98 | + return os.MkdirAll(filepath.Join(podDir, "volumes/plugin/name"), 0750) |
| 99 | + }, |
| 100 | + validateFunc: func(kubelet *Kubelet) error { |
| 101 | + podDir := kubelet.getPodDir("pod1uid") |
| 102 | + return validateDirExists(filepath.Join(podDir, "volumes/plugin/name")) |
| 103 | + }, |
| 104 | + }, |
| 105 | + "pod-doesnot-exist-with-subpath": { |
| 106 | + prepareFunc: func(kubelet *Kubelet) error { |
| 107 | + podDir := kubelet.getPodDir("pod1uid") |
| 108 | + return os.MkdirAll(filepath.Join(podDir, "volume-subpaths/volume/container/index"), 0750) |
| 109 | + }, |
| 110 | + validateFunc: func(kubelet *Kubelet) error { |
| 111 | + podDir := kubelet.getPodDir("pod1uid") |
| 112 | + return validateDirExists(filepath.Join(podDir, "volume-subpaths/volume/container/index")) |
| 113 | + }, |
| 114 | + }, |
| 115 | + "pod-doesnot-exist-with-subpath-top": { |
| 116 | + prepareFunc: func(kubelet *Kubelet) error { |
| 117 | + podDir := kubelet.getPodDir("pod1uid") |
| 118 | + return os.MkdirAll(filepath.Join(podDir, "volume-subpaths"), 0750) |
| 119 | + }, |
| 120 | + validateFunc: func(kubelet *Kubelet) error { |
| 121 | + podDir := kubelet.getPodDir("pod1uid") |
| 122 | + return validateDirExists(filepath.Join(podDir, "volume-subpaths")) |
| 123 | + }, |
| 124 | + }, |
| 125 | + // TODO: test volume in volume-manager |
| 126 | + } |
| 127 | + |
| 128 | + for name, tc := range testCases { |
| 129 | + t.Run(name, func(t *testing.T) { |
| 130 | + testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */) |
| 131 | + defer testKubelet.Cleanup() |
| 132 | + kubelet := testKubelet.kubelet |
| 133 | + |
| 134 | + if tc.prepareFunc != nil { |
| 135 | + if err := tc.prepareFunc(kubelet); err != nil { |
| 136 | + t.Fatalf("%s failed preparation: %v", name, err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + err := kubelet.cleanupOrphanedPodDirs(tc.pods, nil) |
| 141 | + if tc.expectErr && err == nil { |
| 142 | + t.Errorf("%s failed: expected error, got success", name) |
| 143 | + } |
| 144 | + if !tc.expectErr && err != nil { |
| 145 | + t.Errorf("%s failed: got error %v", name, err) |
| 146 | + } |
| 147 | + |
| 148 | + if tc.validateFunc != nil { |
| 149 | + if err := tc.validateFunc(kubelet); err != nil { |
| 150 | + t.Errorf("%s failed validation: %v", name, err) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments