| 1 | /* |
| 2 | Copyright 2024 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 | package leases |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
| 23 | "k8s.io/apimachinery/pkg/api/errors" |
| 24 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | "k8s.io/client-go/kubernetes/fake" |
| 26 | clocktesting "k8s.io/utils/clock/testing" |
| 27 | |
| 28 | proxytesting "sigs.k8s.io/apiserver-network-proxy/pkg/testing" |
| 29 | ) |
| 30 | |
| 31 | func TestGarbageCollectionController(t *testing.T) { |
| 32 | testCases := []struct { |
| 33 | name string |
| 34 | template proxytesting.LeaseTemplate |
| 35 | selector string |
| 36 | expectDeletion bool |
| 37 | }{ |
| 38 | { |
| 39 | name: "does not delete valid acquired lease matching selector", |
| 40 | template: proxytesting.LeaseTemplate{ |
| 41 | Labels: map[string]string{"some": "label"}, |
| 42 | TimeSinceAcquire: 2 * time.Minute, |
| 43 | DurationSecs: 1000, |
| 44 | }, |
| 45 | selector: "some=label", |
| 46 | expectDeletion: false, |
| 47 | }, { |
| 48 | name: "does not delete valid renewed lease matching selector", |
| 49 | template: proxytesting.LeaseTemplate{ |
| 50 | Labels: map[string]string{"some": "label"}, |
| 51 | TimeSinceAcquire: 10 * time.Minute, |
| 52 | TimeSinceRenew: time.Minute, |
| 53 | DurationSecs: 120, |
| 54 | }, |
| 55 | selector: "some=label", |
| 56 | expectDeletion: false, |
| 57 | }, { |
| 58 | name: "does not delete expired lease not matching selector", |
| 59 | template: proxytesting.LeaseTemplate{ |
| 60 | Labels: map[string]string{"another": "label"}, |
| 61 | TimeSinceAcquire: 2 * time.Minute, |
| 62 | TimeSinceRenew: time.Minute, |
| 63 | DurationSecs: 1000, |
| 64 | }, |
| 65 | selector: "some=label", |
| 66 | expectDeletion: false, |
| 67 | }, { |
| 68 | name: "deletes expired lease matching selector", |
| 69 | template: proxytesting.LeaseTemplate{ |
| 70 | Labels: map[string]string{"some": "label"}, |
| 71 | TimeSinceAcquire: 4 * time.Minute, |
| 72 | TimeSinceRenew: 3 * time.Minute, |
| 73 | DurationSecs: 1, |
| 74 | }, |
| 75 | selector: "some=label", |
| 76 | expectDeletion: true, |
| 77 | }, |
| 78 | } |
| 79 | |
| 80 | for _, tc := range testCases { |
| 81 | t.Run(tc.name, func(t *testing.T) { |
| 82 | now := time.Now() |
| 83 | pc := clocktesting.NewFakePassiveClock(now) |
| 84 | lease := proxytesting.NewLeaseFromTemplate(pc, tc.template) |
| 85 | k8sClient := fake.NewSimpleClientset(lease) |
| 86 | |
| 87 | controller := NewGarbageCollectionController(pc, k8sClient, "", 10*time.Millisecond, tc.selector) |
| 88 | |
| 89 | go controller.Run(context.Background()) |
| 90 | |
| 91 | pc.SetTime(now.Add(100 * time.Millisecond)) |
| 92 | time.Sleep(100 * time.Millisecond) |
| 93 | |
| 94 | gotLease, err := k8sClient.CoordinationV1().Leases("").Get(context.Background(), lease.Name, metav1.GetOptions{}) |
| 95 | if errors.IsNotFound(err) && tc.expectDeletion { |
| 96 | return |
| 97 | } else if err != nil { |
| 98 | t.Fatalf("error while getting lease: %v", err) |
| 99 | } else if tc.expectDeletion { |
| 100 | t.Errorf("lease should have been deleted, instead got: %v", gotLease) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | |
| 105 | } |