@@ -18,6 +18,7 @@ import (
18
18
"k8s.io/apimachinery/pkg/util/rand"
19
19
ctrl "sigs.k8s.io/controller-runtime"
20
20
"sigs.k8s.io/controller-runtime/pkg/client"
21
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
21
22
22
23
"github.com/operator-framework/operator-registry/alpha/declcfg"
23
24
@@ -172,79 +173,100 @@ func TestClusterExtensionResolutionSucceeds(t *testing.T) {
172
173
}
173
174
174
175
func TestClusterExtensionUnpackFails (t * testing.T ) {
175
- cl , reconciler := newClientAndReconciler (t )
176
- reconciler .Unpacker = & MockUnpacker {
177
- err : errors .New ("unpack failure" ),
176
+ type testCase struct {
177
+ name string
178
+ unpackErr error
179
+ expectTerminal bool
178
180
}
179
-
180
- ctx := context .Background ()
181
- extKey := types.NamespacedName {Name : fmt .Sprintf ("cluster-extension-test-%s" , rand .String (8 ))}
182
-
183
- t .Log ("When the cluster extension specifies a channel with version that exist" )
184
- t .Log ("By initializing cluster state" )
185
- pkgName := "prometheus"
186
- pkgVer := "1.0.0"
187
- pkgChan := "beta"
188
- namespace := fmt .Sprintf ("test-ns-%s" , rand .String (8 ))
189
- serviceAccount := fmt .Sprintf ("test-sa-%s" , rand .String (8 ))
190
-
191
- clusterExtension := & ocv1alpha1.ClusterExtension {
192
- ObjectMeta : metav1.ObjectMeta {Name : extKey .Name },
193
- Spec : ocv1alpha1.ClusterExtensionSpec {
194
- Source : ocv1alpha1.SourceConfig {
195
- SourceType : "Catalog" ,
196
- Catalog : & ocv1alpha1.CatalogSource {
197
- PackageName : pkgName ,
198
- Version : pkgVer ,
199
- Channels : []string {pkgChan },
200
- },
201
- },
202
- Install : ocv1alpha1.ClusterExtensionInstallConfig {
203
- Namespace : namespace ,
204
- ServiceAccount : ocv1alpha1.ServiceAccountReference {
205
- Name : serviceAccount ,
206
- },
207
- },
181
+ for _ , tc := range []testCase {
182
+ {
183
+ name : "recoverable unpack failure" ,
184
+ unpackErr : errors .New ("unpack failure" ),
208
185
},
186
+ {
187
+ name : "unrecoverable unpack failure" ,
188
+ unpackErr : reconcile .TerminalError (errors .New ("unrecoverable unpack failure" )),
189
+ expectTerminal : true ,
190
+ },
191
+ } {
192
+ t .Run (tc .name , func (t * testing.T ) {
193
+ cl , reconciler := newClientAndReconciler (t )
194
+ reconciler .Unpacker = & MockUnpacker {
195
+ err : tc .unpackErr ,
196
+ }
197
+
198
+ ctx := context .Background ()
199
+ extKey := types.NamespacedName {Name : fmt .Sprintf ("cluster-extension-test-%s" , rand .String (8 ))}
200
+
201
+ t .Log ("When the cluster extension specifies a channel with version that exist" )
202
+ t .Log ("By initializing cluster state" )
203
+ pkgName := "prometheus"
204
+ pkgVer := "1.0.0"
205
+ pkgChan := "beta"
206
+ namespace := fmt .Sprintf ("test-ns-%s" , rand .String (8 ))
207
+ serviceAccount := fmt .Sprintf ("test-sa-%s" , rand .String (8 ))
208
+
209
+ clusterExtension := & ocv1alpha1.ClusterExtension {
210
+ ObjectMeta : metav1.ObjectMeta {Name : extKey .Name },
211
+ Spec : ocv1alpha1.ClusterExtensionSpec {
212
+ Source : ocv1alpha1.SourceConfig {
213
+ SourceType : "Catalog" ,
214
+ Catalog : & ocv1alpha1.CatalogSource {
215
+ PackageName : pkgName ,
216
+ Version : pkgVer ,
217
+ Channels : []string {pkgChan },
218
+ },
219
+ },
220
+ Install : ocv1alpha1.ClusterExtensionInstallConfig {
221
+ Namespace : namespace ,
222
+ ServiceAccount : ocv1alpha1.ServiceAccountReference {
223
+ Name : serviceAccount ,
224
+ },
225
+ },
226
+ },
227
+ }
228
+ err := cl .Create (ctx , clusterExtension )
229
+ require .NoError (t , err )
230
+
231
+ t .Log ("It sets resolution success status" )
232
+ t .Log ("By running reconcile" )
233
+ reconciler .Resolver = resolve .Func (func (_ context.Context , _ * ocv1alpha1.ClusterExtension , _ * ocv1alpha1.BundleMetadata ) (* declcfg.Bundle , * bsemver.Version , * declcfg.Deprecation , error ) {
234
+ v := bsemver .MustParse ("1.0.0" )
235
+ return & declcfg.Bundle {
236
+ Name : "prometheus.v1.0.0" ,
237
+ Package : "prometheus" ,
238
+ Image : "quay.io/operatorhubio/prometheus@fake1.0.0" ,
239
+ }, & v , nil , nil
240
+ })
241
+ res , err := reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : extKey })
242
+ require .Equal (t , ctrl.Result {}, res )
243
+ isTerminal := errors .Is (err , reconcile .TerminalError (nil ))
244
+ assert .Equal (t , tc .expectTerminal , isTerminal , "expected terminal error: %v, got: %v" , tc .expectTerminal , isTerminal )
245
+ assert .ErrorContains (t , err , tc .unpackErr .Error ())
246
+
247
+ t .Log ("By fetching updated cluster extension after reconcile" )
248
+ require .NoError (t , cl .Get (ctx , extKey , clusterExtension ))
249
+
250
+ t .Log ("By checking the status fields" )
251
+ require .Equal (t , ocv1alpha1.BundleMetadata {Name : "prometheus.v1.0.0" , Version : "1.0.0" }, clusterExtension .Status .Resolution .Bundle )
252
+ require .Empty (t , clusterExtension .Status .Install )
253
+
254
+ t .Log ("By checking the expected conditions" )
255
+ resolvedCond := apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeResolved )
256
+ require .NotNil (t , resolvedCond )
257
+ require .Equal (t , metav1 .ConditionTrue , resolvedCond .Status )
258
+ require .Equal (t , ocv1alpha1 .ReasonSuccess , resolvedCond .Reason )
259
+ require .Equal (t , "resolved to \" quay.io/operatorhubio/prometheus@fake1.0.0\" " , resolvedCond .Message )
260
+
261
+ t .Log ("By checking the expected unpacked conditions" )
262
+ unpackedCond := apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeUnpacked )
263
+ require .NotNil (t , unpackedCond )
264
+ require .Equal (t , metav1 .ConditionFalse , unpackedCond .Status )
265
+ require .Equal (t , ocv1alpha1 .ReasonFailed , unpackedCond .Reason )
266
+
267
+ require .NoError (t , cl .DeleteAllOf (ctx , & ocv1alpha1.ClusterExtension {}))
268
+ })
209
269
}
210
- err := cl .Create (ctx , clusterExtension )
211
- require .NoError (t , err )
212
-
213
- t .Log ("It sets resolution success status" )
214
- t .Log ("By running reconcile" )
215
- reconciler .Resolver = resolve .Func (func (_ context.Context , _ * ocv1alpha1.ClusterExtension , _ * ocv1alpha1.BundleMetadata ) (* declcfg.Bundle , * bsemver.Version , * declcfg.Deprecation , error ) {
216
- v := bsemver .MustParse ("1.0.0" )
217
- return & declcfg.Bundle {
218
- Name : "prometheus.v1.0.0" ,
219
- Package : "prometheus" ,
220
- Image : "quay.io/operatorhubio/prometheus@fake1.0.0" ,
221
- }, & v , nil , nil
222
- })
223
- res , err := reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : extKey })
224
- require .Equal (t , ctrl.Result {}, res )
225
- require .Error (t , err )
226
-
227
- t .Log ("By fetching updated cluster extension after reconcile" )
228
- require .NoError (t , cl .Get (ctx , extKey , clusterExtension ))
229
-
230
- t .Log ("By checking the status fields" )
231
- require .Equal (t , ocv1alpha1.BundleMetadata {Name : "prometheus.v1.0.0" , Version : "1.0.0" }, clusterExtension .Status .Resolution .Bundle )
232
- require .Empty (t , clusterExtension .Status .Install )
233
-
234
- t .Log ("By checking the expected conditions" )
235
- resolvedCond := apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeResolved )
236
- require .NotNil (t , resolvedCond )
237
- require .Equal (t , metav1 .ConditionTrue , resolvedCond .Status )
238
- require .Equal (t , ocv1alpha1 .ReasonSuccess , resolvedCond .Reason )
239
- require .Equal (t , "resolved to \" quay.io/operatorhubio/prometheus@fake1.0.0\" " , resolvedCond .Message )
240
-
241
- t .Log ("By checking the expected unpacked conditions" )
242
- unpackedCond := apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeUnpacked )
243
- require .NotNil (t , unpackedCond )
244
- require .Equal (t , metav1 .ConditionFalse , unpackedCond .Status )
245
- require .Equal (t , ocv1alpha1 .ReasonFailed , unpackedCond .Reason )
246
-
247
- require .NoError (t , cl .DeleteAllOf (ctx , & ocv1alpha1.ClusterExtension {}))
248
270
}
249
271
250
272
func TestClusterExtensionUnpackUnexpectedState (t * testing.T ) {
0 commit comments