@@ -82,7 +82,7 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
8282 name string
8383 application * capabilitiesv1beta1.Application
8484 account * capabilitiesv1beta1.DeveloperAccount
85- product * capabilitiesv1beta1.Product
85+ product [] * capabilitiesv1beta1.Product
8686 httpHandlerOptions []mocks.ApplicationAPIHandlerOpt
8787 testBody func (t * testing.T , reconciler * reconcilers.BaseReconciler , req reconcile.Request )
8888 }{
@@ -105,7 +105,7 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
105105 Description : "test" ,
106106 },
107107 },
108- product : getProductCR (),
108+ product : [] * capabilitiesv1beta1. Product { getProductCR ()} ,
109109 testBody : func (t * testing.T , r * reconcilers.BaseReconciler , req reconcile.Request ) {
110110 applicationReconciler := ApplicationReconciler {BaseReconciler : r }
111111 _ , err := applicationReconciler .Reconcile (context .Background (), req )
@@ -208,7 +208,7 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
208208 },
209209 },
210210 },
211- product : getProductCR (),
211+ product : [] * capabilitiesv1beta1. Product { getProductCR ()} ,
212212 httpHandlerOptions : []mocks.ApplicationAPIHandlerOpt {
213213 mocks .WithService (3 , getApplicationPlanListByProductJson ()),
214214 mocks .WithAccount (3 , & client.ApplicationList {Applications : []client.ApplicationElem {
@@ -250,7 +250,7 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
250250 },
251251 },
252252 account : getApplicationDeveloperAccount (),
253- product : getProductCR (),
253+ product : [] * capabilitiesv1beta1. Product { getProductCR ()} ,
254254 httpHandlerOptions : []mocks.ApplicationAPIHandlerOpt {
255255 mocks .WithService (3 , getApplicationPlanListByProductJson ()),
256256 mocks .WithAccount (3 , & client.ApplicationList {Applications : []client.ApplicationElem {
@@ -297,8 +297,9 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
297297 },
298298 },
299299 account : getApplicationDeveloperAccount (),
300- product : getProductCR (),
300+ product : [] * capabilitiesv1beta1. Product { getProductCR ()} ,
301301 httpHandlerOptions : []mocks.ApplicationAPIHandlerOpt {
302+ mocks .WithInitAppID (3 ),
302303 mocks .WithService (3 , getApplicationPlanListByProductJson ()),
303304 mocks .WithAccount (3 , & client.ApplicationList {Applications : []client.ApplicationElem {
304305 {Application : * getApplicationJson ("live" )},
@@ -357,7 +358,7 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
357358 },
358359 },
359360 account : getApplicationDeveloperAccount (),
360- product : getProductCR (),
361+ product : [] * capabilitiesv1beta1. Product { getProductCR ()} ,
361362 httpHandlerOptions : []mocks.ApplicationAPIHandlerOpt {
362363 mocks .WithService (3 , getApplicationPlanListByProductJson ()),
363364 mocks .WithAccount (3 , & client.ApplicationList {Applications : []client.ApplicationElem {
@@ -397,6 +398,111 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
397398 require .Equal (t , err .Error (), "applications.capabilities.3scale.net \" test\" not found" )
398399 },
399400 },
401+ {
402+ name : "Update Product reference successful" ,
403+ application : & capabilitiesv1beta1.Application {
404+ ObjectMeta : metav1.ObjectMeta {
405+ Name : "test" ,
406+ Namespace : "test" ,
407+ },
408+ Spec : capabilitiesv1beta1.ApplicationSpec {
409+ AccountCR : & corev1.LocalObjectReference {
410+ Name : "test" ,
411+ },
412+ ProductCR : & corev1.LocalObjectReference {
413+ Name : "test" ,
414+ },
415+ Name : "test" ,
416+ Description : "test" ,
417+ ApplicationPlanName : "test" ,
418+ },
419+ },
420+ account : getApplicationDeveloperAccount (),
421+ product : []* capabilitiesv1beta1.Product {
422+ getProductCR (),
423+ {
424+ TypeMeta : metav1.TypeMeta {},
425+ ObjectMeta : metav1.ObjectMeta {
426+ Name : "test2" ,
427+ Namespace : "test" ,
428+ },
429+ Spec : capabilitiesv1beta1.ProductSpec {
430+ Name : "test2" ,
431+ SystemName : "test2" ,
432+ Description : "test2" ,
433+ },
434+ Status : capabilitiesv1beta1.ProductStatus {
435+ ID : ptr .To (int64 (4 )),
436+ ProviderAccountHost : "some string" ,
437+ ObservedGeneration : 1 ,
438+ Conditions : common.Conditions {common.Condition {
439+ Type : capabilitiesv1beta1 .ProductSyncedConditionType ,
440+ Status : corev1 .ConditionTrue ,
441+ }},
442+ },
443+ },
444+ },
445+ httpHandlerOptions : []mocks.ApplicationAPIHandlerOpt {
446+ mocks .WithInitAppID (3 ),
447+ mocks .WithService (3 , getApplicationPlanListByProductJson ()),
448+ mocks .WithService (4 , getApplicationPlanListByProductJson ()),
449+ mocks .WithAccount (3 , & client.ApplicationList {Applications : []client.ApplicationElem {
450+ {Application : * getApplicationJson ("live" )},
451+ }}),
452+ },
453+ testBody : func (t * testing.T , r * reconcilers.BaseReconciler , req reconcile.Request ) {
454+ ctx := context .Background ()
455+ applicationReconciler := ApplicationReconciler {BaseReconciler : r }
456+ _ , err := applicationReconciler .Reconcile (ctx , req )
457+ require .NoError (t , err )
458+
459+ t .Log ("verifying the Application gets finalizers assigned" )
460+ var application capabilitiesv1beta1.Application
461+ require .NoError (t , r .Client ().Get (ctx , req .NamespacedName , & application ))
462+ require .ElementsMatch (t , application .GetFinalizers (), []string {
463+ applicationFinalizer ,
464+ })
465+
466+ // TODO: check owner reference
467+
468+ // need to trigger the Reconcile again because the first one only updated the finalizers
469+ _ , err = applicationReconciler .Reconcile (ctx , req )
470+ require .NoError (t , err , "reconciliation returned an error" )
471+ // need to trigger the Reconcile again because the previous updated the Status
472+ _ , err = applicationReconciler .Reconcile (ctx , req )
473+ require .NoError (t , err , "reconciliation returned an error" )
474+
475+ var currentApplication capabilitiesv1beta1.Application
476+ require .NoError (t , r .Client ().Get (context .Background (), req .NamespacedName , & currentApplication ))
477+
478+ // Check status ID
479+ require .Equal (t , currentApplication .Status .ID , ptr .To (int64 (3 )))
480+ // check annotation
481+ require .Equal (t , currentApplication .Annotations [applicationIdAnnotation ], "3" )
482+ // Check condition
483+ condition := currentApplication .Status .Conditions .GetCondition (capabilitiesv1beta1 .ApplicationReadyConditionType )
484+ require .Equal (t , corev1 .ConditionTrue , condition .Status )
485+
486+ // Update productReference
487+ currentApplication .Spec .ProductCR .Name = "test2"
488+ require .NoError (t , r .Client ().Update (context .Background (), & currentApplication ))
489+
490+ _ , err = applicationReconciler .Reconcile (ctx , req )
491+ require .NoError (t , err , "reconciliation returned an error" )
492+
493+ // AppID should increase to 4
494+ require .NoError (t , r .Client ().Get (context .Background (), req .NamespacedName , & currentApplication ))
495+ require .Equal (t , currentApplication .Status .ID , ptr .To (int64 (4 )))
496+ condition = currentApplication .Status .Conditions .GetCondition (capabilitiesv1beta1 .ApplicationReadyConditionType )
497+ require .Equal (t , corev1 .ConditionTrue , condition .Status )
498+
499+ // Reconcile one more time and check annotation
500+ _ , err = applicationReconciler .Reconcile (ctx , req )
501+ require .NoError (t , err , "reconciliation returned an error" )
502+ require .NoError (t , r .Client ().Get (context .Background (), req .NamespacedName , & currentApplication ))
503+ require .Equal (t , currentApplication .Annotations [applicationIdAnnotation ], "4" )
504+ },
505+ },
400506 }
401507
402508 for _ , tc := range testCases {
@@ -409,7 +515,9 @@ func TestApplicationReconciler_Reconcile(t *testing.T) {
409515 objectsToAdd = append (objectsToAdd , tc .account )
410516 }
411517 if tc .product != nil {
412- objectsToAdd = append (objectsToAdd , tc .product )
518+ for _ , product := range tc .product {
519+ objectsToAdd = append (objectsToAdd , product )
520+ }
413521 }
414522
415523 if tc .httpHandlerOptions != nil {
0 commit comments