@@ -7,14 +7,143 @@ import (
77 "github.com/coreos/stream-metadata-go/stream/rhcos"
88 osconfigv1 "github.com/openshift/api/config/v1"
99 machinev1beta1 "github.com/openshift/api/machine/v1beta1"
10+ configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
1011 "github.com/stretchr/testify/assert"
1112 "github.com/stretchr/testify/require"
1213 corev1 "k8s.io/api/core/v1"
1314 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415 "k8s.io/apimachinery/pkg/runtime"
1516 "k8s.io/client-go/kubernetes/fake"
17+ "k8s.io/client-go/tools/cache"
18+ "k8s.io/client-go/util/workqueue"
1619)
1720
21+ func TestIsClusterStable (t * testing.T ) {
22+ cases := []struct {
23+ name string
24+ history []osconfigv1.UpdateHistory
25+ expectStable bool
26+ }{
27+ {
28+ name : "empty history - install in progress" ,
29+ history : []osconfigv1.UpdateHistory {},
30+ expectStable : false ,
31+ },
32+ {
33+ name : "only partial entry - install in progress" ,
34+ history : []osconfigv1.UpdateHistory {
35+ {State : osconfigv1 .PartialUpdate , Version : "4.18.0" },
36+ },
37+ expectStable : false ,
38+ },
39+ {
40+ name : "completed entry - stable after initial install" ,
41+ history : []osconfigv1.UpdateHistory {
42+ {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" },
43+ },
44+ expectStable : true ,
45+ },
46+ {
47+ name : "upgrade in progress" ,
48+ history : []osconfigv1.UpdateHistory {
49+ {State : osconfigv1 .PartialUpdate , Version : "4.19.0" },
50+ {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" },
51+ },
52+ expectStable : false ,
53+ },
54+ {
55+ name : "upgrade complete - stable" ,
56+ history : []osconfigv1.UpdateHistory {
57+ {State : osconfigv1 .CompletedUpdate , Version : "4.19.0" },
58+ {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" },
59+ },
60+ expectStable : true ,
61+ },
62+ }
63+
64+ for _ , tc := range cases {
65+ t .Run (tc .name , func (t * testing.T ) {
66+ cv := & osconfigv1.ClusterVersion {
67+ ObjectMeta : v1.ObjectMeta {Name : "version" },
68+ Status : osconfigv1.ClusterVersionStatus {History : tc .history },
69+ }
70+ indexer := cache .NewIndexer (cache .MetaNamespaceKeyFunc , cache.Indexers {})
71+ require .NoError (t , indexer .Add (cv ))
72+ ctrl := & Controller {
73+ clusterVersionLister : configlistersv1 .NewClusterVersionLister (indexer ),
74+ }
75+ stable , err := ctrl .isClusterStable ()
76+ require .NoError (t , err )
77+ assert .Equal (t , tc .expectStable , stable )
78+ })
79+ }
80+ }
81+
82+ func TestUpdateClusterVersion (t * testing.T ) {
83+ cases := []struct {
84+ name string
85+ oldHistory []osconfigv1.UpdateHistory
86+ newHistory []osconfigv1.UpdateHistory
87+ expectEnqueue bool
88+ }{
89+ {
90+ name : "partial to completed - install finishes" ,
91+ oldHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .PartialUpdate , Version : "4.18.0" }},
92+ newHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
93+ expectEnqueue : true ,
94+ },
95+ {
96+ // not sure this is possible, added for safety
97+ name : "empty to completed - install finishes from scratch" ,
98+ oldHistory : []osconfigv1.UpdateHistory {},
99+ newHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
100+ expectEnqueue : true ,
101+ },
102+ {
103+ name : "upgrade partial to completed - upgrade finishes" ,
104+ oldHistory : []osconfigv1.UpdateHistory {
105+ {State : osconfigv1 .PartialUpdate , Version : "4.19.0" },
106+ {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" },
107+ },
108+ newHistory : []osconfigv1.UpdateHistory {
109+ {State : osconfigv1 .CompletedUpdate , Version : "4.19.0" },
110+ {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" },
111+ },
112+ expectEnqueue : true ,
113+ },
114+ {
115+ name : "completed to completed - no change in stability" ,
116+ oldHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
117+ newHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
118+ expectEnqueue : false ,
119+ },
120+ {
121+ name : "partial to partial - still in progress" ,
122+ oldHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .PartialUpdate , Version : "4.18.0" }},
123+ newHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .PartialUpdate , Version : "4.18.0" }},
124+ expectEnqueue : false ,
125+ },
126+ {
127+ name : "completed to partial - new upgrade started" ,
128+ oldHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
129+ newHistory : []osconfigv1.UpdateHistory {{State : osconfigv1 .PartialUpdate , Version : "4.19.0" }, {State : osconfigv1 .CompletedUpdate , Version : "4.18.0" }},
130+ expectEnqueue : false ,
131+ },
132+ }
133+
134+ for _ , tc := range cases {
135+ t .Run (tc .name , func (t * testing.T ) {
136+ ctrl := & Controller {
137+ queue : workqueue .NewTypedRateLimitingQueue (workqueue .DefaultTypedControllerRateLimiter [string ]()),
138+ }
139+ oldCV := & osconfigv1.ClusterVersion {Status : osconfigv1.ClusterVersionStatus {History : tc .oldHistory }}
140+ newCV := & osconfigv1.ClusterVersion {Status : osconfigv1.ClusterVersionStatus {History : tc .newHistory }}
141+ ctrl .updateClusterVersion (oldCV , newCV )
142+ assert .Equal (t , tc .expectEnqueue , ctrl .queue .Len () > 0 )
143+ })
144+ }
145+ }
146+
18147func TestHotLoop (t * testing.T ) {
19148 cases := []struct {
20149 name string
@@ -411,7 +540,7 @@ func TestReconcileAzureProviderSpec(t *testing.T) {
411540 expectedImage machinev1beta1.Image
412541 expectPatch bool
413542 expectSkip bool
414- streamData * stream.Stream // Custom stream data for specific tests
543+ streamData * stream.Stream // Custom stream data for specific tests
415544 securityProfile * machinev1beta1.SecurityProfile // Custom security profile for specific tests
416545 }{
417546 {
0 commit comments