|
| 1 | +/* |
| 2 | +Copyright 2019 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 protectionutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/api/core/v1" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/kubernetes/pkg/volume/util" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + defaultNS = "default" |
| 29 | + defaultPVCName = "pvc1" |
| 30 | + defaultPVName = "pv1" |
| 31 | +) |
| 32 | + |
| 33 | +type TestCase struct { |
| 34 | + name string |
| 35 | + obj metav1.Object |
| 36 | + finalizer string |
| 37 | + result bool |
| 38 | +} |
| 39 | + |
| 40 | +func pvc() *v1.PersistentVolumeClaim { |
| 41 | + return &v1.PersistentVolumeClaim{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: defaultPVCName, |
| 44 | + Namespace: defaultNS, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func pv() *v1.PersistentVolume { |
| 50 | + return &v1.PersistentVolume{ |
| 51 | + ObjectMeta: metav1.ObjectMeta{ |
| 52 | + Name: defaultPVName, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestIsDeletionCandidateLackDeleteTimeAndFinalizer(t *testing.T) { |
| 58 | + |
| 59 | + tests := []TestCase{ |
| 60 | + { |
| 61 | + name: "pv lacks delete time and finalizer", |
| 62 | + obj: pv(), |
| 63 | + finalizer: util.PVProtectionFinalizer, |
| 64 | + result: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "pvc lacks delete time and finalizer", |
| 68 | + obj: pvc(), |
| 69 | + finalizer: util.PVCProtectionFinalizer, |
| 70 | + |
| 71 | + result: false, |
| 72 | + }, |
| 73 | + } |
| 74 | + for _, test := range tests { |
| 75 | + if test.result != IsDeletionCandidate(test.obj, test.finalizer) { |
| 76 | + t.Error(test.name) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestIsDeletionCandidateLackDeleteTime(t *testing.T) { |
| 82 | + pv := pv() |
| 83 | + pv.SetFinalizers([]string{util.PVProtectionFinalizer}) |
| 84 | + pvc := pvc() |
| 85 | + pvc.SetFinalizers([]string{util.PVCProtectionFinalizer}) |
| 86 | + tests := []TestCase{ |
| 87 | + { |
| 88 | + name: "pv lacks delete time", |
| 89 | + obj: pv, |
| 90 | + finalizer: util.PVProtectionFinalizer, |
| 91 | + result: false, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "pvc lacks delete time", |
| 95 | + obj: pvc, |
| 96 | + finalizer: util.PVCProtectionFinalizer, |
| 97 | + |
| 98 | + result: false, |
| 99 | + }, |
| 100 | + } |
| 101 | + for _, test := range tests { |
| 102 | + if test.result != IsDeletionCandidate(test.obj, test.finalizer) { |
| 103 | + t.Error(test.name) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestIsDeletionCandidateLackFinalizer(t *testing.T) { |
| 109 | + |
| 110 | + pv := pv() |
| 111 | + pv.SetDeletionTimestamp(&metav1.Time{}) |
| 112 | + pvc := pvc() |
| 113 | + pvc.SetDeletionTimestamp(&metav1.Time{}) |
| 114 | + tests := []TestCase{ |
| 115 | + { |
| 116 | + name: "pv lacks finalizer", |
| 117 | + obj: pv, |
| 118 | + finalizer: util.PVProtectionFinalizer, |
| 119 | + result: false, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "pvc lacks finalizer", |
| 123 | + obj: pvc, |
| 124 | + finalizer: util.PVCProtectionFinalizer, |
| 125 | + result: false, |
| 126 | + }, |
| 127 | + } |
| 128 | + for _, test := range tests { |
| 129 | + if test.result != IsDeletionCandidate(test.obj, test.finalizer) { |
| 130 | + t.Error(test.name) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestIsDeletionCandidateSuccess(t *testing.T) { |
| 136 | + |
| 137 | + pv := pv() |
| 138 | + pv.SetDeletionTimestamp(&metav1.Time{}) |
| 139 | + pv.SetFinalizers([]string{util.PVProtectionFinalizer}) |
| 140 | + pvc := pvc() |
| 141 | + pvc.SetDeletionTimestamp(&metav1.Time{}) |
| 142 | + pvc.SetFinalizers([]string{util.PVCProtectionFinalizer}) |
| 143 | + |
| 144 | + tests := []TestCase{ |
| 145 | + { |
| 146 | + name: "pv is to delete", |
| 147 | + obj: pv, |
| 148 | + finalizer: util.PVProtectionFinalizer, |
| 149 | + result: true, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "pvc is to delete", |
| 153 | + obj: pvc, |
| 154 | + finalizer: util.PVCProtectionFinalizer, |
| 155 | + result: true, |
| 156 | + }, |
| 157 | + } |
| 158 | + for _, test := range tests { |
| 159 | + if test.result != IsDeletionCandidate(test.obj, test.finalizer) { |
| 160 | + t.Error(test.name) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +func TestNeedToAddFinalizerHasDeleteTimeAndFinalizer(t *testing.T) { |
| 166 | + pv := pv() |
| 167 | + pv.SetDeletionTimestamp(&metav1.Time{}) |
| 168 | + pv.SetFinalizers([]string{util.PVProtectionFinalizer}) |
| 169 | + pvc := pvc() |
| 170 | + pvc.SetDeletionTimestamp(&metav1.Time{}) |
| 171 | + pvc.SetFinalizers([]string{util.PVCProtectionFinalizer}) |
| 172 | + |
| 173 | + tests := []TestCase{ |
| 174 | + { |
| 175 | + name: "pv has delete time and finalizer", |
| 176 | + obj: pv, |
| 177 | + finalizer: util.PVProtectionFinalizer, |
| 178 | + result: false, |
| 179 | + }, |
| 180 | + { |
| 181 | + name: "pvc has delete time and finalizer", |
| 182 | + obj: pvc, |
| 183 | + finalizer: util.PVCProtectionFinalizer, |
| 184 | + result: false, |
| 185 | + }, |
| 186 | + } |
| 187 | + for _, test := range tests { |
| 188 | + if test.result != NeedToAddFinalizer(test.obj, test.finalizer) { |
| 189 | + t.Error(test.name) |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func TestNeedToAddFinalizerHasDeleteTime(t *testing.T) { |
| 195 | + pv := pv() |
| 196 | + pv.SetDeletionTimestamp(&metav1.Time{}) |
| 197 | + pvc := pvc() |
| 198 | + pvc.SetDeletionTimestamp(&metav1.Time{}) |
| 199 | + tests := []TestCase{ |
| 200 | + { |
| 201 | + name: "pv has delete", |
| 202 | + obj: pv, |
| 203 | + finalizer: util.PVProtectionFinalizer, |
| 204 | + result: false, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "pvc has delete", |
| 208 | + obj: pvc, |
| 209 | + finalizer: util.PVCProtectionFinalizer, |
| 210 | + result: false, |
| 211 | + }, |
| 212 | + } |
| 213 | + for _, test := range tests { |
| 214 | + if test.result != NeedToAddFinalizer(test.obj, test.finalizer) { |
| 215 | + t.Error(test.name) |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func TestNeedToAddFinalizerHasFinalizer(t *testing.T) { |
| 221 | + pv := pv() |
| 222 | + pv.SetFinalizers([]string{util.PVProtectionFinalizer}) |
| 223 | + pvc := pvc() |
| 224 | + pvc.SetFinalizers([]string{util.PVCProtectionFinalizer}) |
| 225 | + |
| 226 | + tests := []TestCase{ |
| 227 | + { |
| 228 | + name: "pv has finalizer", |
| 229 | + obj: pv, |
| 230 | + finalizer: util.PVProtectionFinalizer, |
| 231 | + result: false, |
| 232 | + }, |
| 233 | + { |
| 234 | + name: "pvc has finalizer", |
| 235 | + obj: pvc, |
| 236 | + finalizer: util.PVCProtectionFinalizer, |
| 237 | + result: false, |
| 238 | + }, |
| 239 | + } |
| 240 | + for _, test := range tests { |
| 241 | + if test.result != NeedToAddFinalizer(test.obj, test.finalizer) { |
| 242 | + t.Error(test.name) |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +func TestNeedToAddFinalizerSuccess(t *testing.T) { |
| 248 | + tests := []TestCase{ |
| 249 | + { |
| 250 | + name: "pv needs add finalizer", |
| 251 | + obj: pv(), |
| 252 | + finalizer: util.PVProtectionFinalizer, |
| 253 | + result: true, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "pvc needs add finalizer", |
| 257 | + obj: pvc(), |
| 258 | + finalizer: util.PVCProtectionFinalizer, |
| 259 | + result: true, |
| 260 | + }, |
| 261 | + } |
| 262 | + for _, test := range tests { |
| 263 | + if test.result != NeedToAddFinalizer(test.obj, test.finalizer) { |
| 264 | + t.Error(test.name) |
| 265 | + } |
| 266 | + } |
| 267 | +} |
0 commit comments