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