Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(HACBS-1535): Improve test coverage #97

Merged
merged 4 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controllers/pipeline/pipeline_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions controllers/pipeline/pipeline_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package pipeline

import (
"context"
"fmt"

"github.com/go-logr/logr"
applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
Expand Down Expand Up @@ -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{
Expand Down
67 changes: 64 additions & 3 deletions controllers/pipeline/pipeline_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
})

})
})
59 changes: 59 additions & 0 deletions controllers/snapshot/snapshot_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
})

})