Skip to content

Commit 3c3fa0c

Browse files
author
Yuvaraj Kakaraparthi
committed
add unit tests
1 parent ab58f6d commit 3c3fa0c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

util/collections/machine_filters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func ShouldRolloutIfCertificatesExpireWithinMinutes(expiry *int32) Func {
184184
if expiry == nil {
185185
return false
186186
}
187-
if machine.Status.CertificatesExpiryDate == nil {
187+
if machine == nil || machine.Status.CertificatesExpiryDate == nil {
188188
return false
189189
}
190190
certsExpiryTime := machine.Status.CertificatesExpiryDate.Time

util/collections/machine_filters_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,49 @@ func TestShouldRolloutAfter(t *testing.T) {
165165
})
166166
}
167167

168+
func TestShouldRolloutIfCertificatesExpireWithinMinutes(t *testing.T) {
169+
t.Run("if expiry time is nil it should return false", func(t *testing.T) {
170+
g := NewWithT(t)
171+
m := &clusterv1.Machine{}
172+
g.Expect(collections.ShouldRolloutIfCertificatesExpireWithinMinutes(nil)(m)).To(BeFalse())
173+
})
174+
t.Run("if machine is nil it should return false", func(t *testing.T) {
175+
g := NewWithT(t)
176+
g.Expect(collections.ShouldRolloutIfCertificatesExpireWithinMinutes(pointer.Int32(10))(nil)).To(BeFalse())
177+
})
178+
t.Run("if the machine certificate expiry infromation is not available it should return false", func(t *testing.T) {
179+
g := NewWithT(t)
180+
m := &clusterv1.Machine{}
181+
g.Expect(collections.ShouldRolloutIfCertificatesExpireWithinMinutes(pointer.Int32(10))(m)).To(BeFalse())
182+
})
183+
t.Run("if the machine certificates are not going to expiry within the expiry time it should return false", func(t *testing.T) {
184+
g := NewWithT(t)
185+
// TODO: adjust this later after the API is finalized.
186+
// Might have to change this to days instead of minutes.
187+
expiry := pointer.Int32(10)
188+
certificateExpiryTime := time.Now().Add(60 * time.Minute) // certificates will expire in 60 minutes from 'now'.
189+
m := &clusterv1.Machine{
190+
Status: clusterv1.MachineStatus{
191+
CertificatesExpiryDate: &metav1.Time{Time: certificateExpiryTime},
192+
},
193+
}
194+
g.Expect(collections.ShouldRolloutIfCertificatesExpireWithinMinutes(expiry)(m)).To(BeFalse())
195+
})
196+
t.Run("if machine certificates will expire within the expiry time then it should return true", func(t *testing.T) {
197+
g := NewWithT(t)
198+
// TODO: adjust this later after the API is finalized.
199+
// Might have to change this to days instead of minutes.
200+
expiry := pointer.Int32(60)
201+
certificateExpiryTime := time.Now().Add(10 * time.Minute) // certificates will expire in 10 minutes from 'now'.
202+
m := &clusterv1.Machine{
203+
Status: clusterv1.MachineStatus{
204+
CertificatesExpiryDate: &metav1.Time{Time: certificateExpiryTime},
205+
},
206+
}
207+
g.Expect(collections.ShouldRolloutIfCertificatesExpireWithinMinutes(expiry)(m)).To(BeTrue())
208+
})
209+
}
210+
168211
func TestHashAnnotationKey(t *testing.T) {
169212
t.Run("machine with specified annotation returns true", func(t *testing.T) {
170213
g := NewWithT(t)

0 commit comments

Comments
 (0)