@@ -18,6 +18,7 @@ import (
1818	"k8s.io/apimachinery/pkg/util/rand" 
1919	ctrl "sigs.k8s.io/controller-runtime" 
2020	"sigs.k8s.io/controller-runtime/pkg/client" 
21+ 	"sigs.k8s.io/controller-runtime/pkg/reconcile" 
2122
2223	"github.com/operator-framework/operator-registry/alpha/declcfg" 
2324
@@ -94,74 +95,103 @@ func TestClusterExtensionResolutionFails(t *testing.T) {
9495}
9596
9697func  TestClusterExtensionResolutionSuccessfulUnpackFails (t  * testing.T ) {
97- 	cl , reconciler  :=  newClientAndReconciler (t )
98- 	reconciler .Unpacker  =  & MockUnpacker {
99- 		err : errors .New ("unpack failure" ),
98+ 	type  testCase  struct  {
99+ 		name            string 
100+ 		unpackErr       error 
101+ 		expectTerminal  bool 
100102	}
101- 
102- 	ctx  :=  context .Background ()
103- 	extKey  :=  types.NamespacedName {Name : fmt .Sprintf ("cluster-extension-test-%s" , rand .String (8 ))}
104- 
105- 	t .Log ("When the cluster extension specifies a channel with version that exist" )
106- 	t .Log ("By initializing cluster state" )
107- 	pkgName  :=  "prometheus" 
108- 	pkgVer  :=  "1.0.0" 
109- 	pkgChan  :=  "beta" 
110- 	namespace  :=  fmt .Sprintf ("test-ns-%s" , rand .String (8 ))
111- 	serviceAccount  :=  fmt .Sprintf ("test-sa-%s" , rand .String (8 ))
112- 
113- 	clusterExtension  :=  & ocv1alpha1.ClusterExtension {
114- 		ObjectMeta : metav1.ObjectMeta {Name : extKey .Name },
115- 		Spec : ocv1alpha1.ClusterExtensionSpec {
116- 			Source : ocv1alpha1.SourceConfig {
117- 				SourceType : "Catalog" ,
118- 				Catalog : & ocv1alpha1.CatalogSource {
119- 					PackageName : pkgName ,
120- 					Version :     pkgVer ,
121- 					Channels :    []string {pkgChan },
122- 				},
123- 			},
124- 			Install : ocv1alpha1.ClusterExtensionInstallConfig {
125- 				Namespace : namespace ,
126- 				ServiceAccount : ocv1alpha1.ServiceAccountReference {
127- 					Name : serviceAccount ,
128- 				},
129- 			},
103+ 	for  _ , tc  :=  range  []testCase {
104+ 		{
105+ 			name :      "non-terminal unpack failure" ,
106+ 			unpackErr : errors .New ("unpack failure" ),
130107		},
108+ 		{
109+ 			name :           "terminal unpack failure" ,
110+ 			unpackErr :      reconcile .TerminalError (errors .New ("terminal unpack failure" )),
111+ 			expectTerminal : true ,
112+ 		},
113+ 	} {
114+ 		t .Run (tc .name , func (t  * testing.T ) {
115+ 			cl , reconciler  :=  newClientAndReconciler (t )
116+ 			reconciler .Unpacker  =  & MockUnpacker {
117+ 				err : tc .unpackErr ,
118+ 			}
119+ 
120+ 			ctx  :=  context .Background ()
121+ 			extKey  :=  types.NamespacedName {Name : fmt .Sprintf ("cluster-extension-test-%s" , rand .String (8 ))}
122+ 
123+ 			t .Log ("When the cluster extension specifies a channel with version that exist" )
124+ 			t .Log ("By initializing cluster state" )
125+ 			pkgName  :=  "prometheus" 
126+ 			pkgVer  :=  "1.0.0" 
127+ 			pkgChan  :=  "beta" 
128+ 			namespace  :=  fmt .Sprintf ("test-ns-%s" , rand .String (8 ))
129+ 			serviceAccount  :=  fmt .Sprintf ("test-sa-%s" , rand .String (8 ))
130+ 
131+ 			clusterExtension  :=  & ocv1alpha1.ClusterExtension {
132+ 				ObjectMeta : metav1.ObjectMeta {Name : extKey .Name },
133+ 				Spec : ocv1alpha1.ClusterExtensionSpec {
134+ 					Source : ocv1alpha1.SourceConfig {
135+ 						SourceType : "Catalog" ,
136+ 						Catalog : & ocv1alpha1.CatalogSource {
137+ 							PackageName : pkgName ,
138+ 							Version :     pkgVer ,
139+ 							Channels :    []string {pkgChan },
140+ 						},
141+ 					},
142+ 					Install : ocv1alpha1.ClusterExtensionInstallConfig {
143+ 						Namespace : namespace ,
144+ 						ServiceAccount : ocv1alpha1.ServiceAccountReference {
145+ 							Name : serviceAccount ,
146+ 						},
147+ 					},
148+ 				},
149+ 			}
150+ 			err  :=  cl .Create (ctx , clusterExtension )
151+ 			require .NoError (t , err )
152+ 
153+ 			t .Log ("It sets resolution success status" )
154+ 			t .Log ("By running reconcile" )
155+ 			reconciler .Resolver  =  resolve .Func (func (_  context.Context , _  * ocv1alpha1.ClusterExtension , _  * ocv1alpha1.BundleMetadata ) (* declcfg.Bundle , * bsemver.Version , * declcfg.Deprecation , error ) {
156+ 				v  :=  bsemver .MustParse ("1.0.0" )
157+ 				return  & declcfg.Bundle {
158+ 					Name :    "prometheus.v1.0.0" ,
159+ 					Package : "prometheus" ,
160+ 					Image :   "quay.io/operatorhubio/prometheus@fake1.0.0" ,
161+ 				}, & v , nil , nil 
162+ 			})
163+ 			res , err  :=  reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : extKey })
164+ 			require .Equal (t , ctrl.Result {}, res )
165+ 			require .Error (t , err )
166+ 
167+ 			isTerminal  :=  errors .Is (err , reconcile .TerminalError (nil ))
168+ 			assert .Equal (t , tc .expectTerminal , isTerminal , "expected terminal error: %v, got: %v" , tc .expectTerminal , isTerminal )
169+ 			assert .ErrorContains (t , err , tc .unpackErr .Error ())
170+ 
171+ 			t .Log ("By fetching updated cluster extension after reconcile" )
172+ 			require .NoError (t , cl .Get (ctx , extKey , clusterExtension ))
173+ 
174+ 			t .Log ("By checking the status fields" )
175+ 			expectedBundleMetadata  :=  ocv1alpha1.BundleMetadata {Name : "prometheus.v1.0.0" , Version : "1.0.0" }
176+ 			require .Equal (t , expectedBundleMetadata , clusterExtension .Status .Resolution .Bundle )
177+ 			require .Empty (t , clusterExtension .Status .Install )
178+ 
179+ 			t .Log ("By checking the expected conditions" )
180+ 			expectStatus  :=  metav1 .ConditionTrue 
181+ 			expectReason  :=  ocv1alpha1 .ReasonRetrying 
182+ 			if  tc .expectTerminal  {
183+ 				expectStatus  =  metav1 .ConditionFalse 
184+ 				expectReason  =  ocv1alpha1 .ReasonBlocked 
185+ 			}
186+ 			progressingCond  :=  apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeProgressing )
187+ 			require .NotNil (t , progressingCond )
188+ 			require .Equal (t , expectStatus , progressingCond .Status )
189+ 			require .Equal (t , expectReason , progressingCond .Reason )
190+ 			require .Contains (t , progressingCond .Message , fmt .Sprintf ("for resolved bundle %q with version %q" , expectedBundleMetadata .Name , expectedBundleMetadata .Version ))
191+ 
192+ 			require .NoError (t , cl .DeleteAllOf (ctx , & ocv1alpha1.ClusterExtension {}))
193+ 		})
131194	}
132- 	err  :=  cl .Create (ctx , clusterExtension )
133- 	require .NoError (t , err )
134- 
135- 	t .Log ("It sets resolution success status" )
136- 	t .Log ("By running reconcile" )
137- 	reconciler .Resolver  =  resolve .Func (func (_  context.Context , _  * ocv1alpha1.ClusterExtension , _  * ocv1alpha1.BundleMetadata ) (* declcfg.Bundle , * bsemver.Version , * declcfg.Deprecation , error ) {
138- 		v  :=  bsemver .MustParse ("1.0.0" )
139- 		return  & declcfg.Bundle {
140- 			Name :    "prometheus.v1.0.0" ,
141- 			Package : "prometheus" ,
142- 			Image :   "quay.io/operatorhubio/prometheus@fake1.0.0" ,
143- 		}, & v , nil , nil 
144- 	})
145- 	res , err  :=  reconciler .Reconcile (ctx , ctrl.Request {NamespacedName : extKey })
146- 	require .Equal (t , ctrl.Result {}, res )
147- 	require .Error (t , err )
148- 
149- 	t .Log ("By fetching updated cluster extension after reconcile" )
150- 	require .NoError (t , cl .Get (ctx , extKey , clusterExtension ))
151- 
152- 	t .Log ("By checking the status fields" )
153- 	expectedBundleMetadata  :=  ocv1alpha1.BundleMetadata {Name : "prometheus.v1.0.0" , Version : "1.0.0" }
154- 	require .Equal (t , expectedBundleMetadata , clusterExtension .Status .Resolution .Bundle )
155- 	require .Empty (t , clusterExtension .Status .Install )
156- 
157- 	t .Log ("By checking the expected conditions" )
158- 	progressingCond  :=  apimeta .FindStatusCondition (clusterExtension .Status .Conditions , ocv1alpha1 .TypeProgressing )
159- 	require .NotNil (t , progressingCond )
160- 	require .Equal (t , metav1 .ConditionTrue , progressingCond .Status )
161- 	require .Equal (t , ocv1alpha1 .ReasonRetrying , progressingCond .Reason )
162- 	require .Contains (t , progressingCond .Message , fmt .Sprintf ("for resolved bundle %q with version %q" , expectedBundleMetadata .Name , expectedBundleMetadata .Version ))
163- 
164- 	require .NoError (t , cl .DeleteAllOf (ctx , & ocv1alpha1.ClusterExtension {}))
165195}
166196
167197func  TestClusterExtensionUnpackUnexpectedState (t  * testing.T ) {
0 commit comments