@@ -7,12 +7,13 @@ import (
77 . "github.com/onsi/ginkgo/v2"
88 . "github.com/onsi/gomega"
99 operatorv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
10- olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned/fake"
1110 corev1 "k8s.io/api/core/v1"
1211 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13- "k8s.io/apimachinery/pkg/runtime "
12+ "k8s.io/apimachinery/pkg/types "
1413 kubeclient "k8s.io/client-go/kubernetes/fake"
15- "k8s.io/client-go/testing"
14+ "sigs.k8s.io/controller-runtime/pkg/client"
15+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
16+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1617)
1718
1819var defaultTestSubscription = operatorv1alpha1.Subscription {
@@ -53,26 +54,23 @@ var defaultTestSubConfigMap = corev1.ConfigMap{
5354}
5455
5556var _ = Describe ("Subscription Functions" , func () {
56- Context ("getSubscription" , func () {
57- var testSubscription * operatorv1alpha1.Subscription
58- var fakeOlmClientSet * olmclient.Clientset
59-
60- BeforeEach (func () {
61- testSubscription = defaultTestSubscription .DeepCopy ()
62- fakeOlmClientSet = olmclient .NewSimpleClientset ()
63- })
57+ var fakeClient client.Client
6458
59+ Context ("getSubscription" , func () {
6560 It ("should error out with a non existing a Subscription" , func () {
66- err := createSubscription ( fakeOlmClientSet , testSubscription )
67- Expect ( err ). ToNot ( HaveOccurred () )
68- _ , err = getSubscription (fakeOlmClientSet , "foo" )
61+ fakeClient = fake . NewClientBuilder (). WithScheme ( testEnv . Scheme ).
62+ WithRuntimeObjects (). Build ( )
63+ _ , err : = getSubscription (fakeClient , "foo" )
6964 Expect (err ).To (HaveOccurred ())
7065 })
7166
7267 It ("should return a proper Subscription" , func () {
73- err := createSubscription (fakeOlmClientSet , testSubscription )
68+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).Build ()
69+
70+ err := createSubscription (fakeClient , & defaultTestSubscription )
7471 Expect (err ).ToNot (HaveOccurred ())
75- sub , err := getSubscription (fakeOlmClientSet , "foosubscription" )
72+
73+ sub , err := getSubscription (fakeClient , "foosubscription" )
7674 Expect (err ).ToNot (HaveOccurred ())
7775 Expect (sub .Spec .Channel ).To (Equal ("foochannel" ))
7876 Expect (sub .Spec .CatalogSource ).To (Equal ("foosource" ))
@@ -123,14 +121,14 @@ var _ = Describe("Subscription Functions", func() {
123121
124122var _ = Describe ("UpdateSubscription" , func () {
125123 var (
126- client * olmclient. Clientset
124+ fakeClient client. Client
127125 target * operatorv1alpha1.Subscription
128126 current * operatorv1alpha1.Subscription
129127 subscriptionNs string
130128 )
131129
132130 BeforeEach (func () {
133- client = olmclient . NewSimpleClientset ()
131+
134132 subscriptionNs = "openshift-operators"
135133
136134 current = & operatorv1alpha1.Subscription {
@@ -148,11 +146,15 @@ var _ = Describe("UpdateSubscription", func() {
148146 },
149147 }
150148 target = current .DeepCopy ()
149+
150+ fakeClient = fake .NewClientBuilder ().WithScheme (testEnv .Scheme ).
151+ WithRuntimeObjects (current ).Build ()
152+
151153 })
152154
153155 Context ("when current subscription is nil" , func () {
154156 It ("should return an error" , func () {
155- changed , err := updateSubscription (client , target , nil )
157+ changed , err := updateSubscription (fakeClient , target , nil )
156158 Expect (changed ).To (BeFalse ())
157159 Expect (err ).To (HaveOccurred ())
158160 Expect (err .Error ()).To (ContainSubstring ("current subscription was nil" ))
@@ -161,7 +163,7 @@ var _ = Describe("UpdateSubscription", func() {
161163
162164 Context ("when target subscription is nil" , func () {
163165 It ("should return an error" , func () {
164- changed , err := updateSubscription (client , nil , current )
166+ changed , err := updateSubscription (fakeClient , nil , current )
165167 Expect (changed ).To (BeFalse ())
166168 Expect (err ).To (HaveOccurred ())
167169 Expect (err .Error ()).To (ContainSubstring ("target subscription was nil" ))
@@ -170,79 +172,85 @@ var _ = Describe("UpdateSubscription", func() {
170172
171173 Context ("when the subscription specs are the same" , func () {
172174 It ("should return false and no error" , func () {
173- changed , err := updateSubscription (client , target , current )
175+ changed , err := updateSubscription (fakeClient , target , current )
174176 Expect (changed ).To (BeFalse ())
175177 Expect (err ).ToNot (HaveOccurred ())
176178 })
177179 })
178180
179181 Context ("when the subscription specs are different" , func () {
180- BeforeEach (func () {
181- _ , _ = client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Create (context .Background (), current , metav1.CreateOptions {})
182- })
183182
184183 It ("channel difference should return true and update the current subscription" , func () {
185184 target .Spec .Channel = "beta"
186- changed , err := updateSubscription (client , target , current )
185+ changed , err := updateSubscription (fakeClient , target , current )
187186 Expect (changed ).To (BeTrue ())
188187 Expect (err ).ToNot (HaveOccurred ())
189188
190- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
189+ updated := operatorv1alpha1.Subscription {}
190+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
191191 Expect (err ).ToNot (HaveOccurred ())
192192 Expect (updated .Spec .Channel ).To (Equal ("beta" ))
193193 })
194194
195195 It ("catalgsource difference should return true and update the current subscription" , func () {
196196 target .Spec .CatalogSource = "somesource"
197- changed , err := updateSubscription (client , target , current )
197+ changed , err := updateSubscription (fakeClient , target , current )
198198 Expect (changed ).To (BeTrue ())
199199 Expect (err ).ToNot (HaveOccurred ())
200200
201- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
201+ updated := operatorv1alpha1.Subscription {}
202+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
202203 Expect (err ).ToNot (HaveOccurred ())
203204 Expect (updated .Spec .CatalogSource ).To (Equal ("somesource" ))
204205 })
205206
206207 It ("catalogsourcenamespace difference should return true and update the current subscription" , func () {
207208 target .Spec .CatalogSourceNamespace = "another"
208- changed , err := updateSubscription (client , target , current )
209+ changed , err := updateSubscription (fakeClient , target , current )
209210 Expect (changed ).To (BeTrue ())
210211 Expect (err ).ToNot (HaveOccurred ())
211212
212- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
213+ updated := operatorv1alpha1.Subscription {}
214+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
213215 Expect (err ).ToNot (HaveOccurred ())
214216 Expect (updated .Spec .CatalogSourceNamespace ).To (Equal ("another" ))
215217 })
216218
217219 It ("package difference should return true and update the current subscription" , func () {
218220 target .Spec .Package = "notdefault"
219- changed , err := updateSubscription (client , target , current )
221+ changed , err := updateSubscription (fakeClient , target , current )
220222 Expect (changed ).To (BeTrue ())
221223 Expect (err ).ToNot (HaveOccurred ())
222224
223- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
225+ updated := operatorv1alpha1.Subscription {}
226+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
227+
224228 Expect (err ).ToNot (HaveOccurred ())
225229 Expect (updated .Spec .Package ).To (Equal ("notdefault" ))
226230 })
227231
228232 It ("InstallPlanApproval difference should return true and update the current subscription" , func () {
229233 target .Spec .InstallPlanApproval = operatorv1alpha1 .ApprovalManual
230- changed , err := updateSubscription (client , target , current )
234+ changed , err := updateSubscription (fakeClient , target , current )
231235 Expect (changed ).To (BeTrue ())
232236 Expect (err ).ToNot (HaveOccurred ())
233237
234- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
238+ updated := operatorv1alpha1.Subscription {}
239+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
240+
235241 Expect (err ).ToNot (HaveOccurred ())
236242 Expect (updated .Spec .InstallPlanApproval ).To (Equal (operatorv1alpha1 .ApprovalManual ))
237243 })
238244
239245 It ("StartingCSV difference should return true and update the current subscription" , func () {
240246 target .Spec .StartingCSV = "v1.1.0"
241- changed , err := updateSubscription (client , target , current )
247+ changed , err := updateSubscription (fakeClient , target , current )
242248 Expect (changed ).To (BeTrue ())
243249 Expect (err ).ToNot (HaveOccurred ())
244250
245- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
251+ updated := operatorv1alpha1.Subscription {}
252+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
253+
246254 Expect (err ).ToNot (HaveOccurred ())
247255 Expect (updated .Spec .StartingCSV ).To (Equal ("v1.1.0" ))
248256 })
@@ -257,26 +265,28 @@ var _ = Describe("UpdateSubscription", func() {
257265 },
258266 }
259267 target .Spec .Config = tmp
260- changed , err := updateSubscription (client , target , current )
268+ changed , err := updateSubscription (fakeClient , target , current )
261269 Expect (changed ).To (BeTrue ())
262270 Expect (err ).ToNot (HaveOccurred ())
263271
264- updated , err := client .OperatorsV1alpha1 ().Subscriptions (subscriptionNs ).Get (context .Background (), current .Name , metav1.GetOptions {})
272+ updated := operatorv1alpha1.Subscription {}
273+ err = fakeClient .Get (context .Background (), types.NamespacedName {Name : current .Name , Namespace : current .Namespace }, & updated )
274+
265275 Expect (err ).ToNot (HaveOccurred ())
266276 Expect (updated .Spec .Config .Env [0 ].Name ).To (Equal ("foo" ))
267277 })
268278 })
269279
270280 Context ("when there is an error updating the subscription" , func () {
271- BeforeEach (func () {
272- client .PrependReactor ("update" , "subscriptions" , func (testing.Action ) (handled bool , ret runtime.Object , err error ) {
273- return true , nil , fmt .Errorf ("update error" )
274- })
275- target .Spec .Channel = "beta"
276- })
277281
278282 It ("should return true and an error" , func () {
279- changed , err := updateSubscription (client , target , current )
283+ fakeClient = fake .NewClientBuilder ().WithInterceptorFuncs (
284+ interceptor.Funcs {Update : func (ctx context.Context , client client.WithWatch , obj client.Object , opts ... client.UpdateOption ) error {
285+ return fmt .Errorf ("update error" )
286+ }}).WithScheme (testEnv .Scheme ).Build ()
287+ target .Spec .Channel = "beta"
288+
289+ changed , err := updateSubscription (fakeClient , target , current )
280290 Expect (changed ).To (BeTrue ())
281291 Expect (err ).To (HaveOccurred ())
282292 Expect (err .Error ()).To (ContainSubstring ("update error" ))
0 commit comments