1- package lifecycle
1+ package conditions
22
33import (
44 "errors"
@@ -11,32 +11,19 @@ import (
1111 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1212 controllerruntime "sigs.k8s.io/controller-runtime"
1313
14- "github.com/platform-mesh/golang-commons/controller/testSupport "
14+ pmtesting "github.com/platform-mesh/golang-commons/controller/lifecycle/testing "
1515 "github.com/platform-mesh/golang-commons/logger"
1616)
1717
18- // Test LifecycleManager.WithConditionManagement
19- func TestLifecycleManager_WithConditionManagement (t * testing.T ) {
20- // Given
21- fakeClient := testSupport .CreateFakeClient (t , & testSupport.TestApiObject {})
22- _ , log := createLifecycleManager ([]Subroutine {}, fakeClient )
23-
24- // When
25- l := NewLifecycleManager (log .Logger , "test-operator" , "test-controller" , fakeClient , []Subroutine {}).WithConditionManagement ()
26-
27- // Then
28- assert .True (t , true , l .manageConditions )
29- }
30-
3118// Test the setReady function with an empty array
3219func TestSetReady (t * testing.T ) {
3320
3421 t .Run ("TestSetReady with empty array" , func (t * testing.T ) {
3522 // Given
3623 condition := []metav1.Condition {}
37-
24+ cm := NewConditionManager ()
3825 // When
39- setInstanceConditionReady (& condition , metav1 .ConditionTrue )
26+ cm . SetInstanceConditionReady (& condition , metav1 .ConditionTrue )
4027
4128 // Then
4229 assert .Equal (t , 1 , len (condition ))
@@ -45,12 +32,13 @@ func TestSetReady(t *testing.T) {
4532
4633 t .Run ("TestSetReady with existing condition" , func (t * testing.T ) {
4734 // Given
35+ cm := NewConditionManager ()
4836 condition := []metav1.Condition {
4937 {Type : "test" , Status : metav1 .ConditionFalse },
5038 }
5139
5240 // When
53- setInstanceConditionReady (& condition , metav1 .ConditionTrue )
41+ cm . SetInstanceConditionReady (& condition , metav1 .ConditionTrue )
5442
5543 // Then
5644 assert .Equal (t , 2 , len (condition ))
@@ -62,10 +50,11 @@ func TestSetUnknown(t *testing.T) {
6250
6351 t .Run ("TestSetUnknown with empty array" , func (t * testing.T ) {
6452 // Given
53+ cm := NewConditionManager ()
6554 condition := []metav1.Condition {}
6655
6756 // When
68- setInstanceConditionUnknownIfNotSet (& condition )
57+ cm . SetInstanceConditionUnknownIfNotSet (& condition )
6958
7059 // Then
7160 assert .Equal (t , 1 , len (condition ))
@@ -74,12 +63,13 @@ func TestSetUnknown(t *testing.T) {
7463
7564 t .Run ("TestSetUnknown with existing ready condition" , func (t * testing.T ) {
7665 // Given
66+ cm := NewConditionManager ()
7767 condition := []metav1.Condition {
7868 {Type : ConditionReady , Status : metav1 .ConditionTrue },
7969 }
8070
8171 // When
82- setInstanceConditionUnknownIfNotSet (& condition )
72+ cm . SetInstanceConditionUnknownIfNotSet (& condition )
8373
8474 // Then
8575 assert .Equal (t , 1 , len (condition ))
@@ -111,9 +101,10 @@ func TestSetSubroutineConditionToUnknownIfNotSet(t *testing.T) {
111101 t .Run (tt .Name , func (t * testing.T ) {
112102 // Given
113103 condition := []metav1.Condition {}
104+ cm := NewConditionManager ()
114105
115106 // When
116- setSubroutineConditionToUnknownIfNotSet (& condition , changeStatusSubroutine {}, tt .IsFinalize , log )
107+ cm . SetSubroutineConditionToUnknownIfNotSet (& condition , pmtesting. ChangeStatusSubroutine {}, tt .IsFinalize , log )
117108
118109 // Then
119110 assert .Equal (t , 1 , len (condition ))
@@ -124,12 +115,13 @@ func TestSetSubroutineConditionToUnknownIfNotSet(t *testing.T) {
124115
125116 t .Run ("TestSetSubroutineConditionToUnknownIfNotSet with existing condition" , func (t * testing.T ) {
126117 // Given
118+ cm := NewConditionManager ()
127119 condition := []metav1.Condition {
128120 {Type : "test" , Status : metav1 .ConditionFalse },
129121 }
130122
131123 // When
132- setSubroutineConditionToUnknownIfNotSet (& condition , changeStatusSubroutine {}, false , log )
124+ cm . SetSubroutineConditionToUnknownIfNotSet (& condition , pmtesting. ChangeStatusSubroutine {}, false , log )
133125
134126 // Then
135127 assert .Equal (t , 2 , len (condition ))
@@ -138,14 +130,15 @@ func TestSetSubroutineConditionToUnknownIfNotSet(t *testing.T) {
138130
139131 t .Run ("TestSetSubroutineConditionToUnknownIfNotSet with existing ready" , func (t * testing.T ) {
140132 // Given
141- subroutine := changeStatusSubroutine {}
133+ cm := NewConditionManager ()
134+ subroutine := pmtesting.ChangeStatusSubroutine {}
142135 condition := []metav1.Condition {
143136 {Type : "test" , Status : metav1 .ConditionFalse },
144137 {Type : fmt .Sprintf ("%s_Ready" , subroutine .GetName ()), Status : metav1 .ConditionTrue },
145138 }
146139
147140 // When
148- setSubroutineConditionToUnknownIfNotSet (& condition , subroutine , false , log )
141+ cm . SetSubroutineConditionToUnknownIfNotSet (& condition , subroutine , false , log )
149142
150143 // Then
151144 assert .Equal (t , 2 , len (condition ))
@@ -160,11 +153,12 @@ func TestSubroutineCondition(t *testing.T) {
160153 // Add a test case to set a subroutine condition to ready if it was successfull
161154 t .Run ("TestSetSubroutineConditionReady" , func (t * testing.T ) {
162155 // Given
156+ cm := NewConditionManager ()
163157 condition := []metav1.Condition {}
164- subroutine := changeStatusSubroutine {}
158+ subroutine := pmtesting. ChangeStatusSubroutine {}
165159
166160 // When
167- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, nil , false , log )
161+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, nil , false , log )
168162
169163 // Then
170164 assert .Equal (t , 1 , len (condition ))
@@ -174,11 +168,12 @@ func TestSubroutineCondition(t *testing.T) {
174168 // Add a test case to set a subroutine condition to unknown if it is still processing
175169 t .Run ("TestSetSubroutineConditionProcessing" , func (t * testing.T ) {
176170 // Given
171+ cm := NewConditionManager ()
177172 condition := []metav1.Condition {}
178- subroutine := changeStatusSubroutine {}
173+ subroutine := pmtesting. ChangeStatusSubroutine {}
179174
180175 // When
181- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {RequeueAfter : 1 * time .Second }, nil , false , log )
176+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {RequeueAfter : 1 * time .Second }, nil , false , log )
182177
183178 // Then
184179 assert .Equal (t , 1 , len (condition ))
@@ -189,10 +184,11 @@ func TestSubroutineCondition(t *testing.T) {
189184 t .Run ("TestSetSubroutineConditionError" , func (t * testing.T ) {
190185 // Given
191186 condition := []metav1.Condition {}
192- subroutine := changeStatusSubroutine {}
187+ cm := NewConditionManager ()
188+ subroutine := pmtesting.ChangeStatusSubroutine {}
193189
194190 // When
195- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, errors .New ("failed" ), false , log )
191+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, errors .New ("failed" ), false , log )
196192
197193 // Then
198194 assert .Equal (t , 1 , len (condition ))
@@ -202,11 +198,12 @@ func TestSubroutineCondition(t *testing.T) {
202198 // Add a test case to set a subroutine condition for isFinalize true
203199 t .Run ("TestSetSubroutineFinalizeConditionReady" , func (t * testing.T ) {
204200 // Given
201+ cm := NewConditionManager ()
205202 condition := []metav1.Condition {}
206- subroutine := changeStatusSubroutine {}
203+ subroutine := pmtesting. ChangeStatusSubroutine {}
207204
208205 // When
209- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, nil , true , log )
206+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, nil , true , log )
210207
211208 // Then
212209 assert .Equal (t , 1 , len (condition ))
@@ -217,10 +214,11 @@ func TestSubroutineCondition(t *testing.T) {
217214 t .Run ("TestSetSubroutineFinalizeConditionProcessing" , func (t * testing.T ) {
218215 // Given
219216 condition := []metav1.Condition {}
220- subroutine := changeStatusSubroutine {}
217+ cm := NewConditionManager ()
218+ subroutine := pmtesting.ChangeStatusSubroutine {}
221219
222220 // When
223- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {RequeueAfter : 1 * time .Second }, nil , true , log )
221+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {RequeueAfter : 1 * time .Second }, nil , true , log )
224222
225223 // Then
226224 assert .Equal (t , 1 , len (condition ))
@@ -231,10 +229,11 @@ func TestSubroutineCondition(t *testing.T) {
231229 t .Run ("TestSetSubroutineFinalizeConditionError" , func (t * testing.T ) {
232230 // Given
233231 condition := []metav1.Condition {}
234- subroutine := changeStatusSubroutine {}
232+ cm := NewConditionManager ()
233+ subroutine := pmtesting.ChangeStatusSubroutine {}
235234
236235 // When
237- setSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, errors .New ("failed" ), true , log )
236+ cm . SetSubroutineCondition (& condition , subroutine , controllerruntime.Result {}, errors .New ("failed" ), true , log )
238237
239238 // Then
240239 assert .Equal (t , 1 , len (condition ))
0 commit comments