Skip to content

Commit

Permalink
Merge pull request #560 from konflux-ci/release1124
Browse files Browse the repository at this point in the history
feat(RELEASE-1124): validate Application reference
  • Loading branch information
happybhati authored Oct 29, 2024
2 parents 89942ee + 93973c4 commit 0f25f5a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
29 changes: 29 additions & 0 deletions controllers/release/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func newAdapter(ctx context.Context, client client.Client, release *v1alpha1.Rel
releaseAdapter.validations = []controller.ValidationFunction{
releaseAdapter.validatePipelineDefined,
releaseAdapter.validateProcessingResources,
releaseAdapter.validateApplication,
releaseAdapter.validateAuthor,
releaseAdapter.validatePipelineSource,
}
Expand Down Expand Up @@ -832,6 +833,34 @@ func (a *adapter) registerFinalProcessingStatus(pipelineRun *tektonv1.PipelineRu
return a.client.Status().Patch(a.ctx, a.release, patch)
}

// validateApplication will ensure that the same Application is used in both, the Snapshot and the ReleasePlan. If the
// resources reference different Applications, an error will be returned.
func (a *adapter) validateApplication() *controller.ValidationResult {
releasePlan, err := a.loader.GetReleasePlan(a.ctx, a.client, a.release)
if err != nil {
if errors.IsNotFound(err) {
a.release.MarkValidationFailed(err.Error())
return &controller.ValidationResult{Valid: false}
}
return &controller.ValidationResult{Err: err}
}

snapshot, err := a.loader.GetSnapshot(a.ctx, a.client, a.release)
if err != nil {
if errors.IsNotFound(err) {
a.release.MarkValidationFailed(err.Error())
return &controller.ValidationResult{Valid: false}
}
return &controller.ValidationResult{Err: err}
}

if releasePlan.Spec.Application != snapshot.Spec.Application {
return &controller.ValidationResult{Err: fmt.Errorf("different Application referenced in ReleasePlan and Snapshot")}
}

return &controller.ValidationResult{Valid: true}
}

// validateAuthor will ensure that a valid author exists for the Release and add it to its status. If the Release
// has the automated label but doesn't have automated set in its status, this function will return an error so the
// operation knows to requeue the Release.
Expand Down
60 changes: 60 additions & 0 deletions controllers/release/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,66 @@ var _ = Describe("Release adapter", Ordered, func() {

})

When("validateApplication is called", func() {
var adapter *adapter

AfterEach(func() {
_ = adapter.client.Delete(ctx, adapter.release)
})

BeforeEach(func() {
adapter = createReleaseAndAdapter()
})

It("returns valid and no error if the Application match", func() {
result := adapter.validateApplication()
Expect(result.Valid).To(BeTrue())
Expect(result.Err).NotTo(HaveOccurred())
})

It("returns invalid and error if the Application doesn't match", func() {
newReleasePlan := releasePlan.DeepCopy()
newReleasePlan.Spec.Application = "non-existent"
adapter.ctx = toolkit.GetMockedContext(ctx, []toolkit.MockData{
{
ContextKey: loader.ReleasePlanContextKey,
Resource: newReleasePlan,
},
})

result := adapter.validateApplication()
Expect(result.Valid).To(BeFalse())
Expect(result.Err).To(HaveOccurred())
Expect(result.Err.Error()).To(Equal("different Application referenced in ReleasePlan and Snapshot"))
})

It("returns invalid if the ReleasePlan is not found", func() {
adapter.ctx = toolkit.GetMockedContext(ctx, []toolkit.MockData{
{
ContextKey: loader.ReleasePlanContextKey,
Err: errors.NewNotFound(schema.GroupResource{}, ""),
},
})

result := adapter.validateApplication()
Expect(result.Valid).To(BeFalse())
Expect(result.Err).NotTo(HaveOccurred())
})

It("returns invalid if the Snapshot is not found", func() {
adapter.ctx = toolkit.GetMockedContext(ctx, []toolkit.MockData{
{
ContextKey: loader.SnapshotContextKey,
Err: errors.NewNotFound(schema.GroupResource{}, ""),
},
})

result := adapter.validateApplication()
Expect(result.Valid).To(BeFalse())
Expect(result.Err).NotTo(HaveOccurred())
})
})

When("calling validateAuthor", func() {
var adapter *adapter
var conditionMsg string
Expand Down

0 comments on commit 0f25f5a

Please sign in to comment.