k8s.io/apiserver@v0.31.1/pkg/registry/rest/delete_test.go (about) 1 /* 2 Copyright 2021 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 rest 18 19 import ( 20 "context" 21 "testing" 22 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 utilpointer "k8s.io/utils/pointer" 28 "k8s.io/utils/ptr" 29 ) 30 31 type mockStrategy struct { 32 RESTDeleteStrategy 33 RESTGracefulDeleteStrategy 34 } 35 36 func (m mockStrategy) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) { 37 gvk := obj.GetObjectKind().GroupVersionKind() 38 if len(gvk.Kind) == 0 { 39 return nil, false, runtime.NewMissingKindErr("object has no kind field ") 40 } 41 if len(gvk.Version) == 0 { 42 return nil, false, runtime.NewMissingVersionErr("object has no apiVersion field") 43 } 44 return []schema.GroupVersionKind{obj.GetObjectKind().GroupVersionKind()}, false, nil 45 } 46 47 func TestBeforeDelete(t *testing.T) { 48 type args struct { 49 strategy RESTDeleteStrategy 50 ctx context.Context 51 pod *v1.Pod 52 options *metav1.DeleteOptions 53 } 54 55 makePod := func(deletionGracePeriodSeconds int64) *v1.Pod { 56 return &v1.Pod{ 57 TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}, 58 ObjectMeta: metav1.ObjectMeta{ 59 DeletionTimestamp: &metav1.Time{}, 60 DeletionGracePeriodSeconds: &deletionGracePeriodSeconds, 61 }, 62 } 63 } 64 makeOption := func(gracePeriodSeconds int64) *metav1.DeleteOptions { 65 return &metav1.DeleteOptions{ 66 GracePeriodSeconds: &gracePeriodSeconds, 67 } 68 } 69 70 tests := []struct { 71 name string 72 args args 73 wantGraceful bool 74 wantGracefulPending bool 75 wantGracePeriodSeconds *int64 76 wantDeletionGracePeriodSeconds *int64 77 wantErr bool 78 }{ 79 { 80 name: "when DeletionGracePeriodSeconds=-1, GracePeriodSeconds=-1", 81 args: args{ 82 pod: makePod(-1), 83 options: makeOption(-1), 84 }, 85 // want 1 86 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 87 wantGracePeriodSeconds: utilpointer.Int64(1), 88 wantGraceful: false, 89 wantGracefulPending: true, 90 }, 91 { 92 name: "when DeletionGracePeriodSeconds=-1, GracePeriodSeconds=0", 93 args: args{ 94 pod: makePod(-1), 95 options: makeOption(0), 96 }, 97 // want 0 98 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 99 wantGracePeriodSeconds: utilpointer.Int64(0), 100 wantGraceful: true, 101 wantGracefulPending: false, 102 }, 103 { 104 name: "when DeletionGracePeriodSeconds=-1, GracePeriodSeconds=1", 105 args: args{ 106 pod: makePod(-1), 107 options: makeOption(1), 108 }, 109 // want 1 110 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 111 wantGracePeriodSeconds: utilpointer.Int64(1), 112 wantGraceful: false, 113 wantGracefulPending: true, 114 }, 115 { 116 name: "when DeletionGracePeriodSeconds=-1, GracePeriodSeconds=2", 117 args: args{ 118 pod: makePod(-1), 119 options: makeOption(2), 120 }, 121 // want 1 122 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 123 wantGracePeriodSeconds: utilpointer.Int64(2), 124 wantGraceful: false, 125 wantGracefulPending: true, 126 }, 127 128 { 129 name: "when DeletionGracePeriodSeconds=0, GracePeriodSeconds=-1", 130 args: args{ 131 pod: makePod(0), 132 options: makeOption(-1), 133 }, 134 // want 0 135 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 136 wantGracePeriodSeconds: utilpointer.Int64(1), 137 wantGraceful: false, 138 wantGracefulPending: false, 139 }, 140 { 141 name: "when DeletionGracePeriodSeconds=0, GracePeriodSeconds=0", 142 args: args{ 143 pod: makePod(0), 144 options: makeOption(0), 145 }, 146 // want 0 147 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 148 wantGracePeriodSeconds: utilpointer.Int64(0), 149 wantGraceful: false, 150 wantGracefulPending: false, 151 }, 152 { 153 name: "when DeletionGracePeriodSeconds=0, GracePeriodSeconds=1", 154 args: args{ 155 pod: makePod(0), 156 options: makeOption(1), 157 }, 158 // want 0 159 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 160 wantGracePeriodSeconds: utilpointer.Int64(1), 161 wantGraceful: false, 162 wantGracefulPending: false, 163 }, 164 { 165 name: "when DeletionGracePeriodSeconds=0, GracePeriodSeconds=2", 166 args: args{ 167 pod: makePod(0), 168 options: makeOption(2), 169 }, 170 // want 0 171 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 172 wantGracePeriodSeconds: utilpointer.Int64(2), 173 wantGraceful: false, 174 wantGracefulPending: false, 175 }, 176 177 { 178 name: "when DeletionGracePeriodSeconds=1, GracePeriodSeconds=-1", 179 args: args{ 180 pod: makePod(1), 181 options: makeOption(-1), 182 }, 183 // want 1 184 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 185 wantGracePeriodSeconds: utilpointer.Int64(1), 186 wantGraceful: false, 187 wantGracefulPending: true, 188 }, 189 { 190 name: "when DeletionGracePeriodSeconds=1, GracePeriodSeconds=0", 191 args: args{ 192 pod: makePod(1), 193 options: makeOption(0), 194 }, 195 // want 0 196 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 197 wantGracePeriodSeconds: utilpointer.Int64(0), 198 wantGraceful: true, 199 wantGracefulPending: false, 200 }, 201 { 202 name: "when DeletionGracePeriodSeconds=1, GracePeriodSeconds=1", 203 args: args{ 204 pod: makePod(1), 205 options: makeOption(1), 206 }, 207 // want 1 208 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 209 wantGracePeriodSeconds: utilpointer.Int64(1), 210 wantGraceful: false, 211 wantGracefulPending: true, 212 }, 213 { 214 name: "when DeletionGracePeriodSeconds=1, GracePeriodSeconds=2", 215 args: args{ 216 pod: makePod(1), 217 options: makeOption(2), 218 }, 219 // want 1 220 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 221 wantGracePeriodSeconds: utilpointer.Int64(2), 222 wantGraceful: false, 223 wantGracefulPending: true, 224 }, 225 226 { 227 name: "when DeletionGracePeriodSeconds=2, GracePeriodSeconds=-1", 228 args: args{ 229 pod: makePod(2), 230 options: makeOption(-1), 231 }, 232 // want 1 233 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 234 wantGracePeriodSeconds: utilpointer.Int64(1), 235 wantGraceful: true, 236 wantGracefulPending: false, 237 }, 238 { 239 name: "when DeletionGracePeriodSeconds=2, GracePeriodSeconds=0", 240 args: args{ 241 pod: makePod(2), 242 options: makeOption(0), 243 }, 244 // want 0 245 wantDeletionGracePeriodSeconds: utilpointer.Int64(0), 246 wantGracePeriodSeconds: utilpointer.Int64(0), 247 wantGraceful: true, 248 wantGracefulPending: false, 249 }, 250 { 251 name: "when DeletionGracePeriodSeconds=2, GracePeriodSeconds=1", 252 args: args{ 253 pod: makePod(2), 254 options: makeOption(1), 255 }, 256 // want 1 257 wantDeletionGracePeriodSeconds: utilpointer.Int64(1), 258 wantGracePeriodSeconds: utilpointer.Int64(1), 259 wantGraceful: true, 260 wantGracefulPending: false, 261 }, 262 { 263 name: "when DeletionGracePeriodSeconds=2, GracePeriodSeconds=2", 264 args: args{ 265 pod: makePod(2), 266 options: makeOption(2), 267 }, 268 // want 2 269 wantDeletionGracePeriodSeconds: utilpointer.Int64(2), 270 wantGracePeriodSeconds: utilpointer.Int64(2), 271 wantGraceful: false, 272 wantGracefulPending: true, 273 }, 274 } 275 for _, tt := range tests { 276 t.Run(tt.name, func(t *testing.T) { 277 if tt.args.strategy == nil { 278 tt.args.strategy = mockStrategy{} 279 } 280 if tt.args.ctx == nil { 281 tt.args.ctx = context.Background() 282 } 283 284 gotGraceful, gotGracefulPending, err := BeforeDelete(tt.args.strategy, tt.args.ctx, tt.args.pod, tt.args.options) 285 if (err != nil) != tt.wantErr { 286 t.Errorf("BeforeDelete() error = %v, wantErr %v", err, tt.wantErr) 287 return 288 } 289 if gotGraceful != tt.wantGraceful { 290 t.Errorf("BeforeDelete() gotGraceful = %v, want %v", gotGraceful, tt.wantGraceful) 291 } 292 if gotGracefulPending != tt.wantGracefulPending { 293 t.Errorf("BeforeDelete() gotGracefulPending = %v, want %v", gotGracefulPending, tt.wantGracefulPending) 294 } 295 if gotGracefulPending != tt.wantGracefulPending { 296 t.Errorf("BeforeDelete() gotGracefulPending = %v, want %v", gotGracefulPending, tt.wantGracefulPending) 297 } 298 if !utilpointer.Int64Equal(tt.args.pod.DeletionGracePeriodSeconds, tt.wantDeletionGracePeriodSeconds) { 299 t.Errorf("metadata.DeletionGracePeriodSeconds = %v, want %v", ptr.Deref(tt.args.pod.DeletionGracePeriodSeconds, 0), ptr.Deref(tt.wantDeletionGracePeriodSeconds, 0)) 300 } 301 if !utilpointer.Int64Equal(tt.args.options.GracePeriodSeconds, tt.wantGracePeriodSeconds) { 302 t.Errorf("options.GracePeriodSeconds = %v, want %v", ptr.Deref(tt.args.options.GracePeriodSeconds, 0), ptr.Deref(tt.wantGracePeriodSeconds, 0)) 303 } 304 }) 305 } 306 }