|
| 1 | +// Copyright 2022 OnMetal authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package conditionutils_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "time" |
| 19 | + |
| 20 | + . "github.com/onsi/ginkgo/v2" |
| 21 | + . "github.com/onsi/gomega" |
| 22 | + appsv1 "k8s.io/api/apps/v1" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
| 26 | + . "github.com/onmetal/controller-utils/conditionutils" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("Alias", func() { |
| 30 | + var ( |
| 31 | + now time.Time |
| 32 | + cond appsv1.DeploymentCondition |
| 33 | + conds []appsv1.DeploymentCondition |
| 34 | + ) |
| 35 | + BeforeEach(func() { |
| 36 | + now = time.Now() |
| 37 | + cond = appsv1.DeploymentCondition{ |
| 38 | + Type: appsv1.DeploymentAvailable, |
| 39 | + Status: corev1.ConditionTrue, |
| 40 | + LastUpdateTime: metav1.Unix(2, 0), |
| 41 | + LastTransitionTime: metav1.Unix(1, 0), |
| 42 | + Reason: "MinimumReplicasAvailable", |
| 43 | + Message: "ReplicaSet \"foo\" has successfully progressed.", |
| 44 | + } |
| 45 | + conds = []appsv1.DeploymentCondition{cond} |
| 46 | + }) |
| 47 | + |
| 48 | + Describe("Update", func() { |
| 49 | + It("should update a condition", func() { |
| 50 | + Expect(Update(&cond, UpdateStatus(corev1.ConditionFalse))).To(Succeed()) |
| 51 | + Expect(cond.Status).To(Equal(corev1.ConditionFalse)) |
| 52 | + Expect(cond.LastTransitionTime.Time).To(BeTemporally(">=", now)) |
| 53 | + Expect(cond.LastUpdateTime.Time).To(BeTemporally(">=", now)) |
| 54 | + }) |
| 55 | + |
| 56 | + It("should error if it cannot update a condition", func() { |
| 57 | + Expect(Update(1)).To(HaveOccurred()) |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + Describe("MustUpdate", func() { |
| 62 | + It("should update a condition", func() { |
| 63 | + Expect(func() { MustUpdate(&cond, UpdateStatus(corev1.ConditionFalse)) }).NotTo(Panic()) |
| 64 | + Expect(cond.Status).To(Equal(corev1.ConditionFalse)) |
| 65 | + Expect(cond.LastTransitionTime.Time).To(BeTemporally(">=", now)) |
| 66 | + Expect(cond.LastUpdateTime.Time).To(BeTemporally(">=", now)) |
| 67 | + }) |
| 68 | + |
| 69 | + It("should panic if it cannot update a condition", func() { |
| 70 | + Expect(func() { MustUpdate(1) }).To(Panic()) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + Describe("UpdateSlice", func() { |
| 75 | + It("should update the condition slice", func() { |
| 76 | + Expect(UpdateSlice(&conds, string(appsv1.DeploymentAvailable), |
| 77 | + UpdateStatus(corev1.ConditionFalse), |
| 78 | + )).NotTo(HaveOccurred()) |
| 79 | + Expect(conds[0].Status).To(Equal(corev1.ConditionFalse)) |
| 80 | + Expect(conds[0].LastTransitionTime.Time).To(BeTemporally(">=", now)) |
| 81 | + Expect(conds[0].LastUpdateTime.Time).To(BeTemporally(">=", now)) |
| 82 | + }) |
| 83 | + |
| 84 | + It("should error if it cannot update the condition slice", func() { |
| 85 | + Expect(UpdateSlice(1, "foo")).To(HaveOccurred()) |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + Describe("MustUpdateSlice", func() { |
| 90 | + It("should update the condition slice", func() { |
| 91 | + Expect(func() { |
| 92 | + MustUpdateSlice(&conds, string(appsv1.DeploymentAvailable), |
| 93 | + UpdateStatus(corev1.ConditionFalse), |
| 94 | + ) |
| 95 | + }).NotTo(Panic()) |
| 96 | + Expect(conds[0].Status).To(Equal(corev1.ConditionFalse)) |
| 97 | + Expect(conds[0].LastTransitionTime.Time).To(BeTemporally(">=", now)) |
| 98 | + Expect(conds[0].LastUpdateTime.Time).To(BeTemporally(">=", now)) |
| 99 | + }) |
| 100 | + |
| 101 | + It("should panic if it cannot update the condition slice", func() { |
| 102 | + Expect(func() { MustUpdateSlice(1, "foo") }).To(Panic()) |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + Describe("FindSliceIndex", func() { |
| 107 | + It("should find the slice index", func() { |
| 108 | + idx, err := FindSliceIndex(conds, string(appsv1.DeploymentAvailable)) |
| 109 | + Expect(err).NotTo(HaveOccurred()) |
| 110 | + Expect(idx).To(Equal(0)) |
| 111 | + }) |
| 112 | + |
| 113 | + It("should error if it cannot find the slice index", func() { |
| 114 | + _, err := FindSliceIndex(1, "foo") |
| 115 | + Expect(err).To(HaveOccurred()) |
| 116 | + }) |
| 117 | + }) |
| 118 | + |
| 119 | + Describe("MustFindSliceIndex", func() { |
| 120 | + It("should find the slice index", func() { |
| 121 | + idx := MustFindSliceIndex(conds, string(appsv1.DeploymentAvailable)) |
| 122 | + Expect(idx).To(Equal(0)) |
| 123 | + }) |
| 124 | + |
| 125 | + It("should panic if it cannot find the slice index", func() { |
| 126 | + Expect(func() { MustFindSliceIndex(1, "foo") }).To(Panic()) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + Describe("FindSlice", func() { |
| 131 | + It("should find the slice", func() { |
| 132 | + var actual appsv1.DeploymentCondition |
| 133 | + ok, err := FindSlice(conds, string(appsv1.DeploymentAvailable), &actual) |
| 134 | + Expect(err).NotTo(HaveOccurred()) |
| 135 | + Expect(ok).To(BeTrue()) |
| 136 | + Expect(actual).To(Equal(cond)) |
| 137 | + }) |
| 138 | + |
| 139 | + It("should error if it cannot find the slice", func() { |
| 140 | + _, err := FindSlice(1, "foo", 1) |
| 141 | + Expect(err).To(HaveOccurred()) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + Describe("MustFindSlice", func() { |
| 146 | + It("should find the slice", func() { |
| 147 | + var actual appsv1.DeploymentCondition |
| 148 | + ok := MustFindSlice(conds, string(appsv1.DeploymentAvailable), &actual) |
| 149 | + Expect(ok).To(BeTrue()) |
| 150 | + Expect(actual).To(Equal(cond)) |
| 151 | + }) |
| 152 | + |
| 153 | + It("should panic if it cannot find the slice", func() { |
| 154 | + Expect(func() { MustFindSlice(1, "foo", 1) }).To(Panic()) |
| 155 | + }) |
| 156 | + }) |
| 157 | + |
| 158 | + Describe("FindSliceStatus", func() { |
| 159 | + It("should find the slice status", func() { |
| 160 | + status, err := FindSliceStatus(conds, string(appsv1.DeploymentAvailable)) |
| 161 | + Expect(err).NotTo(HaveOccurred()) |
| 162 | + Expect(status).To(Equal(corev1.ConditionTrue)) |
| 163 | + }) |
| 164 | + |
| 165 | + It("should error if it cannot find the slice status", func() { |
| 166 | + _, err := FindSliceStatus(1, "foo") |
| 167 | + Expect(err).To(HaveOccurred()) |
| 168 | + }) |
| 169 | + }) |
| 170 | + |
| 171 | + Describe("MustFindSliceStatus", func() { |
| 172 | + It("should find the slice status", func() { |
| 173 | + status := MustFindSliceStatus(conds, string(appsv1.DeploymentAvailable)) |
| 174 | + Expect(status).To(Equal(corev1.ConditionTrue)) |
| 175 | + }) |
| 176 | + |
| 177 | + It("should panic if it cannot find the slice status", func() { |
| 178 | + Expect(func() { MustFindSliceStatus(1, "foo") }).To(Panic()) |
| 179 | + }) |
| 180 | + }) |
| 181 | +}) |
0 commit comments