From 67afd49220b38b4ae83c903fc976a4e22fa9a4b9 Mon Sep 17 00:00:00 2001 From: Varsha Prasad Narsing Date: Wed, 12 Apr 2023 12:32:27 -0400 Subject: [PATCH] [EntitySource] Modify entity source interface The "Get" function of the entity source did not return an error and instead only a nil entity when its not found. This PR modifies the same, to make it return an error. Reason: The method could be implmented with a client that fetches bundle contents from the cluster. The error returned from the client needs to be propagated. Signed-off-by: Varsha Prasad Narsing --- pkg/deppy/input/cache_entity_source.go | 7 ++++--- pkg/deppy/input/entity_source.go | 2 +- pkg/deppy/input/entity_source_test.go | 10 +++++++++- pkg/ext/olm/constraints_test.go | 4 ++-- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/deppy/input/cache_entity_source.go b/pkg/deppy/input/cache_entity_source.go index b0095f0..8078118 100644 --- a/pkg/deppy/input/cache_entity_source.go +++ b/pkg/deppy/input/cache_entity_source.go @@ -2,6 +2,7 @@ package input import ( "context" + "fmt" "github.com/operator-framework/deppy/pkg/deppy" ) @@ -19,11 +20,11 @@ func NewCacheQuerier(entities map[deppy.Identifier]Entity) *CacheEntitySource { } } -func (c CacheEntitySource) Get(_ context.Context, id deppy.Identifier) *Entity { +func (c CacheEntitySource) Get(_ context.Context, id deppy.Identifier) (*Entity, error) { if entity, ok := c.entities[id]; ok { - return &entity + return &entity, nil } - return nil + return nil, fmt.Errorf("entity with id: %s not found in the entity source", id.String()) } func (c CacheEntitySource) Filter(_ context.Context, filter Predicate) (EntityList, error) { diff --git a/pkg/deppy/input/entity_source.go b/pkg/deppy/input/entity_source.go index a21a85d..4cd7b01 100644 --- a/pkg/deppy/input/entity_source.go +++ b/pkg/deppy/input/entity_source.go @@ -24,7 +24,7 @@ type EntityListMap map[string]EntityList // EntitySource provides a query and content acquisition interface for arbitrary entity stores type EntitySource interface { - Get(ctx context.Context, id deppy.Identifier) *Entity + Get(ctx context.Context, id deppy.Identifier) (*Entity, error) Filter(ctx context.Context, filter Predicate) (EntityList, error) GroupBy(ctx context.Context, fn GroupByFunction) (EntityListMap, error) Iterate(ctx context.Context, fn IteratorFunction) error diff --git a/pkg/deppy/input/entity_source_test.go b/pkg/deppy/input/entity_source_test.go index dcdcc28..79c8ba8 100644 --- a/pkg/deppy/input/entity_source_test.go +++ b/pkg/deppy/input/entity_source_test.go @@ -90,10 +90,18 @@ var _ = Describe("EntitySource", func() { Describe("Get", func() { It("should return requested entity", func() { - e := entitySource.Get(context.Background(), "2-2") + e, err := entitySource.Get(context.Background(), "2-2") + Expect(err).To(BeNil()) Expect(e).NotTo(BeNil()) Expect(e.Identifier()).To(Equal(deppy.Identifier("2-2"))) }) + + It("should return an error when the requested entity is not found", func() { + e, err := entitySource.Get(context.Background(), "random") + Expect(err).To(HaveOccurred()) + Expect(e).To(BeNil()) + Expect(err.Error()).To(BeEquivalentTo(fmt.Sprintf("entity with id: %s not found in the entity source", "random"))) + }) }) Describe("Filter", func() { diff --git a/pkg/ext/olm/constraints_test.go b/pkg/ext/olm/constraints_test.go index b1c3bd6..db49b41 100644 --- a/pkg/ext/olm/constraints_test.go +++ b/pkg/ext/olm/constraints_test.go @@ -59,8 +59,8 @@ type MockQuerier struct { testEntityList input.EntityList } -func (t MockQuerier) Get(_ context.Context, _ deppy.Identifier) *input.Entity { - return &input.Entity{} +func (t MockQuerier) Get(_ context.Context, _ deppy.Identifier) (*input.Entity, error) { + return &input.Entity{}, nil } func (t MockQuerier) Filter(_ context.Context, filter input.Predicate) (input.EntityList, error) { if t.testError != nil {