diff --git a/controllers/pipeline/pipeline_adapter_test.go b/controllers/pipeline/pipeline_adapter_test.go index c01224be3..487e38e1c 100644 --- a/controllers/pipeline/pipeline_adapter_test.go +++ b/controllers/pipeline/pipeline_adapter_test.go @@ -241,6 +241,7 @@ var _ = Describe("Pipeline Adapter", Ordered, func() { Name: "pipelinerun-component-sample", Namespace: "default", Labels: map[string]string{ + "pipelines.appstudio.openshift.io/type": "test", "pac.test.appstudio.openshift.io/url-org": "redhat-appstudio", "pac.test.appstudio.openshift.io/original-prname": "build-service-on-push", "pac.test.appstudio.openshift.io/url-repository": "build-service", diff --git a/controllers/pipeline/pipeline_controller.go b/controllers/pipeline/pipeline_controller.go index 7159e0715..28a0e5ed7 100644 --- a/controllers/pipeline/pipeline_controller.go +++ b/controllers/pipeline/pipeline_controller.go @@ -18,6 +18,7 @@ package pipeline import ( "context" + "fmt" "github.com/go-logr/logr" applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1" @@ -101,6 +102,12 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } } + if application == nil { + err := fmt.Errorf("failed to get Application") + logger.Error(err, "reconcile cannot resolve application") + return ctrl.Result{}, err + } + adapter := NewAdapter(pipelineRun, component, application, logger, r.Client, ctx) return reconciler.ReconcileHandler([]reconciler.ReconcileOperation{ diff --git a/controllers/pipeline/pipeline_controller_test.go b/controllers/pipeline/pipeline_controller_test.go index 177d5679a..30a773e3a 100644 --- a/controllers/pipeline/pipeline_controller_test.go +++ b/controllers/pipeline/pipeline_controller_test.go @@ -49,13 +49,12 @@ var _ = Describe("PipelineController", func() { hasComp *applicationapiv1alpha1.Component ) const ( - SampleRepoLink = "https://github.com/devfile-samples/devfile-sample-java-springboot-basic" + applicationName = "application-sample" + SampleRepoLink = "https://github.com/devfile-samples/devfile-sample-java-springboot-basic" ) BeforeEach(func() { - applicationName := "application-sample" - hasApp = &applicationapiv1alpha1.Application{ ObjectMeta: metav1.ObjectMeta{ Name: applicationName, @@ -223,4 +222,66 @@ var _ = Describe("PipelineController", func() { Expect(app).NotTo(BeNil()) Expect(app.ObjectMeta).To(Equal(hasApp.ObjectMeta)) }) + + When("pipelinerun has no component", func() { + + var ( + testPipelineRunNoComponent *tektonv1beta1.PipelineRun + reqNoComponent ctrl.Request + ) + + BeforeEach(func() { + testPipelineRunNoComponent = &tektonv1beta1.PipelineRun{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pipelinerun-sample-no-component", + Namespace: "default", + Labels: map[string]string{ + "pipelines.appstudio.openshift.io/type": "test", + "pipelines.openshift.io/used-by": "build-cloud", + "pipelines.openshift.io/runtime": "nodejs", + "pipelines.openshift.io/strategy": "s2i", + "appstudio.openshift.io/application": applicationName, + }, + Annotations: map[string]string{ + "appstudio.redhat.com/updateComponentOnSuccess": "false", + }, + }, + Spec: tektonv1beta1.PipelineRunSpec{ + PipelineRef: &tektonv1beta1.PipelineRef{ + Name: "build-pipeline-pass", + Bundle: "quay.io/kpavic/test-bundle:build-pipeline-pass", + }, + Params: []tektonv1beta1.Param{ + { + Name: "output-image", + Value: tektonv1beta1.ArrayOrString{ + Type: "string", + StringVal: "quay.io/redhat-appstudio/sample-image", + }, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, testPipelineRunNoComponent)).Should(Succeed()) + + reqNoComponent = ctrl.Request{ + NamespacedName: types.NamespacedName{ + Namespace: "default", + Name: testPipelineRunNoComponent.Name, + }, + } + }) + + AfterEach(func() { + err := k8sClient.Delete(ctx, testPipelineRunNoComponent) + Expect(err == nil || errors.IsNotFound(err)).To(BeTrue()) + }) + + It("reconcile with application taken from pipelinerun (test pipeline)", func() { + result, err := pipelineReconciler.Reconcile(ctx, reqNoComponent) + Expect(reflect.TypeOf(result)).To(Equal(reflect.TypeOf(reconcile.Result{}))) + Expect(err).To(BeNil()) + }) + + }) }) diff --git a/controllers/snapshot/snapshot_adapter_test.go b/controllers/snapshot/snapshot_adapter_test.go index b98c92f2c..1c5493bb7 100644 --- a/controllers/snapshot/snapshot_adapter_test.go +++ b/controllers/snapshot/snapshot_adapter_test.go @@ -374,4 +374,63 @@ var _ = Describe("Snapshot Adapter", Ordered, func() { }, time.Second*10).Should(BeTrue()) }) + When("multiple components exist", func() { + + var ( + secondComp *applicationapiv1alpha1.Component + ) + + BeforeEach(func() { + secondComp = &applicationapiv1alpha1.Component{ + ObjectMeta: metav1.ObjectMeta{ + Name: "component-second-sample", + Namespace: "default", + }, + Spec: applicationapiv1alpha1.ComponentSpec{ + ComponentName: "component-second-sample", + Application: "application-sample", + ContainerImage: "", + Source: applicationapiv1alpha1.ComponentSource{ + ComponentSourceUnion: applicationapiv1alpha1.ComponentSourceUnion{ + GitSource: &applicationapiv1alpha1.GitSource{ + URL: SampleRepoLink, + }, + }, + }, + }, + } + Expect(k8sClient.Create(ctx, secondComp)).Should(Succeed()) + + }) + + AfterEach(func() { + err := k8sClient.Delete(ctx, secondComp) + Expect(err == nil || errors.IsNotFound(err)).To(BeTrue()) + }) + + It("ensures updating existing snapshot works", func() { + var snapshotEnvironmentBinding *applicationapiv1alpha1.SnapshotEnvironmentBinding + + // create snapshot environment + components := []applicationapiv1alpha1.Component{ + *hasComp, + } + snapshotEnvironmentBinding, err := adapter.createSnapshotEnvironmentBindingForSnapshot(adapter.application, &env, hasSnapshot, &components) + Expect(err).To(BeNil()) + Expect(snapshotEnvironmentBinding).NotTo(BeNil()) + + // update snapshot environment with new component + componentsUpdate := []applicationapiv1alpha1.Component{ + *secondComp, + } + + updatedSnapshotEnvironmentBinding, err := adapter.updateExistingSnapshotEnvironmentBindingWithSnapshot(snapshotEnvironmentBinding, hasSnapshot, &componentsUpdate) + Expect(err).To(BeNil()) + Expect(updatedSnapshotEnvironmentBinding).NotTo(BeNil()) + Expect(len(updatedSnapshotEnvironmentBinding.Spec.Components) == 1) + Expect(updatedSnapshotEnvironmentBinding.Spec.Components[0].Name == secondComp.Spec.ComponentName) + + }) + }) + })