@@ -4,8 +4,12 @@ import (
44 "testing"
55 "time"
66
7- "github.com/openshift/origin/pkg/monitor/monitorapi "
7+ configv1 "github.com/openshift/api/config/v1 "
88 "github.com/stretchr/testify/assert"
9+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+ "k8s.io/apimachinery/pkg/util/sets"
11+
12+ "github.com/openshift/origin/pkg/monitor/monitorapi"
913)
1014
1115// buildUpgradeInterval creates a standard upgrade interval using the given reason and eventTime.
@@ -192,3 +196,104 @@ func Test_isInUpgradeWindow(t *testing.T) {
192196 })
193197 }
194198}
199+
200+ func Test_getOperatorsFromProgressingMessage (t * testing.T ) {
201+ tests := []struct {
202+ name string
203+ message string
204+ expect sets.Set [string ]
205+ }{
206+ {
207+ name : "unknown message " ,
208+ message : "bar foo" ,
209+ },
210+ {
211+ name : "single CO" ,
212+ message : "working towards ${VERSION}: 106 of 841 done (12% complete), waiting on single" ,
213+ expect : sets.New [string ]("single" ),
214+ },
215+ {
216+ name : "multiple COs" ,
217+ message : "working towards ${VERSION}: 106 of 841 done (12% complete), waiting on etcd, kube-apiserver" ,
218+ expect : sets .New [string ]("kube-apiserver" , "etcd" ),
219+ },
220+ }
221+ for _ , tt := range tests {
222+ t .Run (tt .name , func (t * testing.T ) {
223+ actual := getOperatorsFromProgressingMessage (tt .message )
224+ assert .Equal (t , tt .expect , actual )
225+ })
226+ }
227+ }
228+
229+ func Test_updateCOWaiting (t * testing.T ) {
230+ now := time .Now ()
231+ next := 3 * time .Hour
232+ interval := func (m string , start time.Time , d time.Duration ) monitorapi.Interval {
233+ return monitorapi .NewInterval (monitorapi .SourceOperatorState , monitorapi .Warning ).
234+ Locator (monitorapi .NewLocator ().ClusterVersion (& configv1.ClusterVersion {
235+ ObjectMeta : metav1.ObjectMeta {Name : "version" }})).
236+ Message (monitorapi .NewMessage ().Reason ("reason" ).
237+ HumanMessage (m ).
238+ WithAnnotation (monitorapi .AnnotationCondition , string (configv1 .OperatorProgressing )).
239+ WithAnnotation (monitorapi .AnnotationStatus , string (configv1 .ConditionTrue ))).
240+ Display ().
241+ Build (start , start .Add (d ))
242+ }
243+
244+ tests := []struct {
245+ name string
246+ message string
247+ d time.Duration
248+ waiting map [string ]monitorapi.Intervals
249+ expect map [string ]time.Duration
250+ }{
251+ {
252+ name : "happy case" ,
253+ d : time .Hour ,
254+ message : "working towards ${VERSION}: 106 of 841 done (12% complete), waiting on etcd, kube-apiserver" ,
255+ waiting : map [string ]monitorapi.Intervals {},
256+ expect : map [string ]time.Duration {"etcd" : time .Hour , "kube-apiserver" : time .Hour },
257+ },
258+ {
259+ name : "incremental one" ,
260+ d : 2 * time .Minute ,
261+ message : "working towards ${VERSION}: 106 of 841 done (12% complete), waiting on etcd, kube-apiserver" ,
262+ waiting : map [string ]monitorapi.Intervals {"etcd" : {interval ("some" , now , 3 * time .Minute )}},
263+ expect : map [string ]time.Duration {"etcd" : next + 2 * time .Minute , "kube-apiserver" : 2 * time .Minute },
264+ },
265+ {
266+ name : "incremental all" ,
267+ d : 2 * time .Minute ,
268+ message : "working towards ${VERSION}: 106 of 841 done (12% complete), waiting on etcd, kube-apiserver" ,
269+ waiting : map [string ]monitorapi.Intervals {"etcd" : {interval ("some" , now , 3 * time .Minute )}, "kube-apiserver" : {interval ("some" , now , 6 * time .Minute )}},
270+ expect : map [string ]time.Duration {"etcd" : next + 2 * time .Minute , "kube-apiserver" : next + 2 * time .Minute },
271+ },
272+ {
273+ name : "unknown message" ,
274+ message : "unknown message" ,
275+ waiting : map [string ]monitorapi.Intervals {},
276+ expect : map [string ]time.Duration {},
277+ },
278+ }
279+ for _ , tt := range tests {
280+ i := monitorapi .NewInterval (monitorapi .SourceOperatorState , monitorapi .Warning ).
281+ Locator (monitorapi .NewLocator ().ClusterVersion (& configv1.ClusterVersion {
282+ ObjectMeta : metav1.ObjectMeta {Name : "version" }})).
283+ Message (monitorapi .NewMessage ().Reason ("reason" ).
284+ HumanMessage (tt .message ).
285+ WithAnnotation (monitorapi .AnnotationCondition , string (configv1 .OperatorProgressing )).
286+ WithAnnotation (monitorapi .AnnotationStatus , string (configv1 .ConditionTrue ))).
287+ Display ().
288+ Build (now .Add (next ), now .Add (next + tt .d ))
289+ t .Run (tt .name , func (t * testing.T ) {
290+ updateCOWaiting (i , tt .waiting )
291+ actual := map [string ]time.Duration {}
292+ for co , intervals := range tt .waiting {
293+ from , to := fromAndTo (intervals )
294+ actual [co ] = to .Sub (from )
295+ }
296+ assert .Equal (t , tt .expect , actual )
297+ })
298+ }
299+ }
0 commit comments