|
| 1 | +/* |
| 2 | +Copyright 2025 Tim Ebert. |
| 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 leases_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "time" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + coordinationv1 "k8s.io/api/coordination/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/utils/ptr" |
| 27 | + |
| 28 | + . "github.com/timebertt/kubernetes-controller-sharding/pkg/sharding/leases" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("ShardState", func() { |
| 32 | + Describe("#String", func() { |
| 33 | + It("should return the state as a string", func() { |
| 34 | + Expect(Unknown.String()).To(Equal("unknown")) |
| 35 | + Expect(Orphaned.String()).To(Equal("orphaned")) |
| 36 | + Expect(Dead.String()).To(Equal("dead")) |
| 37 | + Expect(Uncertain.String()).To(Equal("uncertain")) |
| 38 | + Expect(Expired.String()).To(Equal("expired")) |
| 39 | + Expect(Ready.String()).To(Equal("ready")) |
| 40 | + Expect(ShardState(-1).String()).To(Equal("unknown")) |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + Describe("#StateFromString", func() { |
| 45 | + It("should return the correct state matching the string", func() { |
| 46 | + Expect(StateFromString("unknown")).To(Equal(Unknown)) |
| 47 | + Expect(StateFromString("orphaned")).To(Equal(Orphaned)) |
| 48 | + Expect(StateFromString("dead")).To(Equal(Dead)) |
| 49 | + Expect(StateFromString("uncertain")).To(Equal(Uncertain)) |
| 50 | + Expect(StateFromString("expired")).To(Equal(Expired)) |
| 51 | + Expect(StateFromString("ready")).To(Equal(Ready)) |
| 52 | + Expect(StateFromString("foo")).To(Equal(Unknown)) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + Describe("#IsAvailable", func() { |
| 57 | + It("should return false for unavailable states", func() { |
| 58 | + Expect(Unknown.IsAvailable()).To(BeFalse()) |
| 59 | + Expect(Orphaned.IsAvailable()).To(BeFalse()) |
| 60 | + Expect(Dead.IsAvailable()).To(BeFalse()) |
| 61 | + }) |
| 62 | + |
| 63 | + It("should return true for available states", func() { |
| 64 | + Expect(Uncertain.IsAvailable()).To(BeTrue()) |
| 65 | + Expect(Expired.IsAvailable()).To(BeTrue()) |
| 66 | + Expect(Ready.IsAvailable()).To(BeTrue()) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + Describe("#ToState", func() { |
| 71 | + var ( |
| 72 | + now time.Time |
| 73 | + lease *coordinationv1.Lease |
| 74 | + ) |
| 75 | + |
| 76 | + BeforeEach(func() { |
| 77 | + now = time.Now() |
| 78 | + |
| 79 | + lease = &coordinationv1.Lease{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: "shard", |
| 82 | + }, |
| 83 | + Spec: coordinationv1.LeaseSpec{ |
| 84 | + HolderIdentity: ptr.To("shard"), |
| 85 | + LeaseDurationSeconds: ptr.To[int32](10), |
| 86 | + AcquireTime: ptr.To(metav1.NewMicroTime(now.Add(-2 * time.Minute))), |
| 87 | + RenewTime: ptr.To(metav1.NewMicroTime(now.Add(-2 * time.Second))), |
| 88 | + }, |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + It("should return ready if lease has not expired", func() { |
| 93 | + Expect(ToState(lease, now)).To(Equal(Ready)) |
| 94 | + }) |
| 95 | + |
| 96 | + It("should return expired if lease has expired", func() { |
| 97 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now.Add(-15 * time.Second))) |
| 98 | + Expect(ToState(lease, now)).To(Equal(Expired)) |
| 99 | + }) |
| 100 | + |
| 101 | + It("should return expired if acquireTime is missing", func() { |
| 102 | + lease.Spec.AcquireTime = nil |
| 103 | + Expect(ToState(lease, now)).To(Equal(Expired)) |
| 104 | + }) |
| 105 | + |
| 106 | + It("should return expired if renewTime is missing", func() { |
| 107 | + lease.Spec.RenewTime = nil |
| 108 | + Expect(ToState(lease, now)).To(Equal(Expired)) |
| 109 | + }) |
| 110 | + |
| 111 | + It("should return expired if leaseDuration is missing", func() { |
| 112 | + lease.Spec.LeaseDurationSeconds = nil |
| 113 | + Expect(ToState(lease, now)).To(Equal(Expired)) |
| 114 | + }) |
| 115 | + |
| 116 | + It("should return uncertain if lease has expired more than leaseDuration ago", func() { |
| 117 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now.Add(-25 * time.Second))) |
| 118 | + Expect(ToState(lease, now)).To(Equal(Uncertain)) |
| 119 | + }) |
| 120 | + |
| 121 | + It("should return dead if lease has been released", func() { |
| 122 | + lease.Spec.HolderIdentity = nil |
| 123 | + Expect(ToState(lease, now)).To(Equal(Dead)) |
| 124 | + |
| 125 | + lease.Spec.HolderIdentity = ptr.To("") |
| 126 | + Expect(ToState(lease, now)).To(Equal(Dead)) |
| 127 | + }) |
| 128 | + |
| 129 | + It("should return dead if lease has been acquired by sharder", func() { |
| 130 | + lease.Spec.HolderIdentity = ptr.To("shardlease-controller") |
| 131 | + lease.Spec.LeaseDurationSeconds = ptr.To[int32](20) |
| 132 | + lease.Spec.AcquireTime = ptr.To(metav1.NewMicroTime(now)) |
| 133 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now)) |
| 134 | + Expect(ToState(lease, now)).To(Equal(Dead)) |
| 135 | + }) |
| 136 | + |
| 137 | + It("should return orphaned if lease has been released 1 minute and leaseDuration ago", func() { |
| 138 | + lease.Spec.HolderIdentity = nil |
| 139 | + lease.Spec.LeaseDurationSeconds = ptr.To[int32](1) |
| 140 | + lease.Spec.AcquireTime = ptr.To(metav1.NewMicroTime(now.Add(-time.Minute - time.Second))) |
| 141 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now.Add(-time.Minute - time.Second))) |
| 142 | + Expect(ToState(lease, now)).To(Equal(Orphaned)) |
| 143 | + }) |
| 144 | + |
| 145 | + It("should return orphaned if lease has been acquired by sharder 1 minute and ago", func() { |
| 146 | + lease.Spec.HolderIdentity = ptr.To("shardlease-controller") |
| 147 | + lease.Spec.LeaseDurationSeconds = ptr.To[int32](20) |
| 148 | + lease.Spec.AcquireTime = ptr.To(metav1.NewMicroTime(now.Add(-time.Minute - 20*time.Second))) |
| 149 | + lease.Spec.RenewTime = ptr.To(metav1.NewMicroTime(now.Add(-time.Minute - 20*time.Second))) |
| 150 | + Expect(ToState(lease, now)).To(Equal(Orphaned)) |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments