@@ -17,6 +17,9 @@ limitations under the License.
1717package reconciler
1818
1919import (
20+ "errors"
21+ "fmt"
22+ "strings"
2023 "testing"
2124 "time"
2225
@@ -28,11 +31,10 @@ import (
2831
2932func TestEmitEvent (t * testing.T ) {
3033 testcases := []struct {
31- name string
32- before * apis.Condition
33- after * apis.Condition
34- expectEvent bool
35- expectedEvent string
34+ name string
35+ before * apis.Condition
36+ after * apis.Condition
37+ wantEvent string
3638 }{{
3739 name : "unknown to true with message" ,
3840 before : & apis.Condition {
@@ -44,8 +46,7 @@ func TestEmitEvent(t *testing.T) {
4446 Status : corev1 .ConditionTrue ,
4547 Message : "all done" ,
4648 },
47- expectEvent : true ,
48- expectedEvent : "Normal Succeeded all done" ,
49+ wantEvent : "Normal Succeeded all done" ,
4950 }, {
5051 name : "true to true" ,
5152 before : & apis.Condition {
@@ -58,8 +59,7 @@ func TestEmitEvent(t *testing.T) {
5859 Status : corev1 .ConditionTrue ,
5960 LastTransitionTime : apis.VolatileTime {Inner : metav1 .NewTime (time .Now ().Add (5 * time .Minute ))},
6061 },
61- expectEvent : false ,
62- expectedEvent : "" ,
62+ wantEvent : "" ,
6363 }, {
6464 name : "false to false" ,
6565 before : & apis.Condition {
@@ -70,8 +70,7 @@ func TestEmitEvent(t *testing.T) {
7070 Type : apis .ConditionSucceeded ,
7171 Status : corev1 .ConditionFalse ,
7272 },
73- expectEvent : false ,
74- expectedEvent : "" ,
73+ wantEvent : "" ,
7574 }, {
7675 name : "unknown to unknown" ,
7776 before : & apis.Condition {
@@ -86,26 +85,23 @@ func TestEmitEvent(t *testing.T) {
8685 Reason : "foo" ,
8786 Message : "bar" ,
8887 },
89- expectEvent : true ,
90- expectedEvent : "Normal foo bar" ,
88+ wantEvent : "Normal foo bar" ,
9189 }, {
9290 name : "true to nil" ,
9391 after : nil ,
9492 before : & apis.Condition {
9593 Type : apis .ConditionSucceeded ,
9694 Status : corev1 .ConditionTrue ,
9795 },
98- expectEvent : false ,
99- expectedEvent : "" ,
96+ wantEvent : "" ,
10097 }, {
10198 name : "nil to true" ,
10299 before : nil ,
103100 after : & apis.Condition {
104101 Type : apis .ConditionSucceeded ,
105102 Status : corev1 .ConditionTrue ,
106103 },
107- expectEvent : true ,
108- expectedEvent : "Normal Succeeded " ,
104+ wantEvent : "Normal Succeeded " ,
109105 }, {
110106 name : "nil to unknown with message" ,
111107 before : nil ,
@@ -114,8 +110,7 @@ func TestEmitEvent(t *testing.T) {
114110 Status : corev1 .ConditionUnknown ,
115111 Message : "just starting" ,
116112 },
117- expectEvent : true ,
118- expectedEvent : "Normal Started " ,
113+ wantEvent : "Normal Started " ,
119114 }, {
120115 name : "unknown to false with message" ,
121116 before : & apis.Condition {
@@ -127,43 +122,70 @@ func TestEmitEvent(t *testing.T) {
127122 Status : corev1 .ConditionFalse ,
128123 Message : "really bad" ,
129124 },
130- expectEvent : true ,
131- expectedEvent : "Warning Failed really bad" ,
125+ wantEvent : "Warning Failed really bad" ,
132126 }, {
133127 name : "nil to false" ,
134128 before : nil ,
135129 after : & apis.Condition {
136130 Type : apis .ConditionSucceeded ,
137131 Status : corev1 .ConditionFalse ,
138132 },
139- expectEvent : true ,
140- expectedEvent : "Warning Failed " ,
133+ wantEvent : "Warning Failed " ,
141134 }}
142135
143136 for _ , ts := range testcases {
144137 fr := record .NewFakeRecorder (1 )
145138 tr := & corev1.Pod {}
146139 EmitEvent (fr , ts .before , ts .after , tr )
147- timer := time .NewTimer (1 * time .Second )
148140
149- select {
150- case event := <- fr .Events :
151- if event == "" {
152- // The fake recorder reported empty, it should not happen
153- t .Fatalf ("Expected event but got empty for %s" , ts .name )
154- }
155- if ! ts .expectEvent {
156- // The fake recorder reported an event which we did not expect
157- t .Errorf ("Unxpected event \" %s\" but got one for %s" , event , ts .name )
158- }
159- if ! (event == ts .expectedEvent ) {
160- t .Errorf ("Expected event \" %s\" but got \" %s\" instead for %s" , ts .expectedEvent , event , ts .name )
161- }
162- case <- timer .C :
163- if ts .expectEvent {
164- // The fake recorder did not report, the timer timeout expired
165- t .Errorf ("Expected event but got none for %s" , ts .name )
166- }
141+ err := checkEvents (fr , ts .name , ts .wantEvent )
142+ if err != nil {
143+ t .Errorf (err .Error ())
167144 }
168145 }
169146}
147+
148+ func TestEmitErrorEvent (t * testing.T ) {
149+ testcases := []struct {
150+ name string
151+ err error
152+ wantEvent string
153+ }{{
154+ name : "with error" ,
155+ err : errors .New ("something went wrong" ),
156+ wantEvent : "Warning Error something went wrong" ,
157+ }, {
158+ name : "without error" ,
159+ err : nil ,
160+ wantEvent : "" ,
161+ }}
162+
163+ for _ , ts := range testcases {
164+ fr := record .NewFakeRecorder (1 )
165+ tr := & corev1.Pod {}
166+ EmitErrorEvent (fr , ts .err , tr )
167+
168+ err := checkEvents (fr , ts .name , ts .wantEvent )
169+ if err != nil {
170+ t .Errorf (err .Error ())
171+ }
172+ }
173+ }
174+
175+ func checkEvents (fr * record.FakeRecorder , testName string , wantEvent string ) error {
176+ timer := time .NewTimer (1 * time .Second )
177+ select {
178+ case event := <- fr .Events :
179+ if wantEvent == "" {
180+ return fmt .Errorf ("received event \" %s\" for %s but none expected" , event , testName )
181+ }
182+ if ! (strings .HasPrefix (event , wantEvent )) {
183+ return fmt .Errorf ("expected event \" %s\" but got \" %s\" instead for %s" , wantEvent , event , testName )
184+ }
185+ case <- timer .C :
186+ if wantEvent != "" {
187+ return fmt .Errorf ("received no events for %s but %s expected" , testName , wantEvent )
188+ }
189+ }
190+ return nil
191+ }
0 commit comments