Skip to content

Commit b5df440

Browse files
committed
Test changes
Signed-off-by: Vu Dinh <[email protected]>
1 parent f931af4 commit b5df440

File tree

5 files changed

+141
-109
lines changed

5 files changed

+141
-109
lines changed

pkg/controller/registry/grpc/source.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ func (s *SourceStore) Remove(key resolver.CatalogKey) error {
190190
return source.Conn.Close()
191191
}
192192

193-
func (s *SourceStore) AsClients(namespaces ...string) (map[resolver.CatalogKey]client.Interface, map[resolver.CatalogKey]*client.Client) {
193+
func (s *SourceStore) AsClients(namespaces ...string) (map[resolver.CatalogKey]client.Interface, map[resolver.CatalogKey]resolver.RegistryClientInterface) {
194194
refsInterface := map[resolver.CatalogKey]client.Interface{}
195-
refsClient := map[resolver.CatalogKey]*client.Client{}
195+
refsClient := map[resolver.CatalogKey]resolver.RegistryClientInterface{}
196196
s.sourcesLock.RLock()
197197
defer s.sourcesLock.RUnlock()
198198
for key, source := range s.sources {
@@ -202,7 +202,7 @@ func (s *SourceStore) AsClients(namespaces ...string) (map[resolver.CatalogKey]c
202202
for _, namespace := range namespaces {
203203
if key.Namespace == namespace {
204204
refsInterface[key] = client.NewClientFromConn(source.Conn)
205-
refsClient[key] = client.NewClientFromConn(source.Conn)
205+
refsClient[key] = resolver.NewRegistryClient(client.NewClientFromConn(source.Conn))
206206
}
207207
}
208208
}

pkg/controller/registry/resolver/querier.go

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package resolver
55
import (
66
"context"
77
"fmt"
8-
"io"
98

109
"github.com/blang/semver"
1110
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,43 +35,12 @@ type SourceQuerier interface {
3635

3736
type NamespaceSourceQuerier struct {
3837
sources map[CatalogKey]client.Interface
39-
clients map[CatalogKey]*client.Client
38+
clients map[CatalogKey]RegistryClientInterface
4039
}
4140

4241
var _ SourceQuerier = &NamespaceSourceQuerier{}
4342

44-
type ChannelEntryStream interface {
45-
Recv() (*api.ChannelEntry, error)
46-
}
47-
48-
type ChannelEntryIterator struct {
49-
stream ChannelEntryStream
50-
error error
51-
}
52-
53-
func NewChannelEntryIterator(stream ChannelEntryStream) *ChannelEntryIterator {
54-
return &ChannelEntryIterator{stream: stream}
55-
}
56-
57-
func (ceit *ChannelEntryIterator) Next() *registryapi.ChannelEntry {
58-
if ceit.error != nil {
59-
return nil
60-
}
61-
next, err := ceit.stream.Recv()
62-
if err == io.EOF {
63-
return nil
64-
}
65-
if err != nil {
66-
ceit.error = err
67-
}
68-
return next
69-
}
70-
71-
func (ceit *ChannelEntryIterator) Error() error {
72-
return ceit.error
73-
}
74-
75-
func NewNamespaceSourceQuerier(sources map[CatalogKey]client.Interface, clients map[CatalogKey]*client.Client) *NamespaceSourceQuerier {
43+
func NewNamespaceSourceQuerier(sources map[CatalogKey]client.Interface, clients map[CatalogKey]RegistryClientInterface) *NamespaceSourceQuerier {
7644
return &NamespaceSourceQuerier{
7745
sources: sources,
7846
clients: clients,
@@ -90,19 +58,19 @@ func (q *NamespaceSourceQuerier) FindProvider(api opregistry.APIKey, initialSour
9058
if initialSource.Name != "" && initialSource.Namespace != "" {
9159
client, ok := q.clients[initialSource]
9260
if ok {
93-
if bundle, err := FindBundleThatProvides(context.TODO(), client, api.Group, api.Version, api.Kind, pkgName); err == nil {
61+
if bundle, err := client.FindBundleThatProvides(context.TODO(), api.Group, api.Version, api.Kind, pkgName); err == nil {
9462
return bundle, &initialSource, nil
9563
}
96-
if bundle, err := FindBundleThatProvides(context.TODO(), client, api.Plural+"."+api.Group, api.Version, api.Kind, pkgName); err == nil {
64+
if bundle, err := client.FindBundleThatProvides(context.TODO(), api.Plural+"."+api.Group, api.Version, api.Kind, pkgName); err == nil {
9765
return bundle, &initialSource, nil
9866
}
9967
}
10068
}
10169
for key, client := range q.clients {
102-
if bundle, err := FindBundleThatProvides(context.TODO(), client, api.Group, api.Version, api.Kind, pkgName); err == nil {
70+
if bundle, err := client.FindBundleThatProvides(context.TODO(), api.Group, api.Version, api.Kind, pkgName); err == nil {
10371
return bundle, &key, nil
10472
}
105-
if bundle, err := FindBundleThatProvides(context.TODO(), client, api.Plural+"."+api.Group, api.Version, api.Kind, pkgName); err == nil {
73+
if bundle, err := client.FindBundleThatProvides(context.TODO(), api.Plural+"."+api.Group, api.Version, api.Kind, pkgName); err == nil {
10674
return bundle, &key, nil
10775
}
10876
}
@@ -227,51 +195,3 @@ func (q *NamespaceSourceQuerier) findChannelHead(currentVersion *semver.Version,
227195
}
228196
return nil, nil
229197
}
230-
231-
// GetLatestChannelEntriesThatProvide uses registry client to get a list of
232-
// latest channel entries that provide the requested API (via an iterator)
233-
func GetLatestChannelEntriesThatProvide(ctx context.Context, c *client.Client, group, version, kind string) (*ChannelEntryIterator, error) {
234-
stream, err := c.Registry.GetLatestChannelEntriesThatProvide(ctx, &registryapi.GetLatestProvidersRequest{Group: group, Version: version, Kind: kind})
235-
if err != nil {
236-
return nil, err
237-
}
238-
return NewChannelEntryIterator(stream), nil
239-
}
240-
241-
// FilterChannelEntries filters out a channel entries that provide the requested
242-
// API and come from the same package with original operator and returns the
243-
// first entry on the list
244-
func FilterChannelEntries(it *ChannelEntryIterator, pkgName string) *opregistry.ChannelEntry {
245-
var entry *opregistry.ChannelEntry
246-
for e := it.Next(); e != nil; e = it.Next() {
247-
if e.PackageName != pkgName {
248-
entry = &opregistry.ChannelEntry{
249-
PackageName: e.PackageName,
250-
ChannelName: e.ChannelName,
251-
BundleName: e.BundleName,
252-
Replaces: e.Replaces,
253-
}
254-
break
255-
}
256-
}
257-
return entry
258-
}
259-
260-
// FindBundleThatProvides returns a bundle that provides the request API and
261-
// doesn't belong to the provided package
262-
func FindBundleThatProvides(ctx context.Context, client *client.Client, group, version, kind, pkgName string) (*api.Bundle, error) {
263-
it, err := GetLatestChannelEntriesThatProvide(ctx, client, group, version, kind)
264-
if err != nil {
265-
return nil, err
266-
}
267-
268-
entry := FilterChannelEntries(it, pkgName)
269-
if entry != nil {
270-
return nil, fmt.Errorf("Unable to find a channel entry which doesn't belong to package %s", pkgName)
271-
}
272-
bundle, err := client.Registry.GetBundle(ctx, &registryapi.GetBundleRequest{PkgName: entry.PackageName, ChannelName: entry.ChannelName, CsvName: entry.BundleName})
273-
if err != nil {
274-
return nil, err
275-
}
276-
return bundle, nil
277-
}

pkg/controller/registry/resolver/querier_test.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ func TestNewNamespaceSourceQuerier(t *testing.T) {
2424
CatalogKey{"test", "ns"}: &fakes.FakeInterface{},
2525
}
2626

27-
emptyClients := map[CatalogKey]*client.Client{}
28-
nonEmptyClients := map[CatalogKey]*client.Client{
29-
CatalogKey{"test", "ns"}: &client.Client{
30-
Registry: &fakes.FakeRegistryClient{},
27+
emptyClients := map[CatalogKey]RegistryClientInterface{}
28+
nonEmptyClients := map[CatalogKey]RegistryClientInterface{
29+
CatalogKey{"test", "ns"}: &OLMRegistryClient{
30+
Client: &client.Client{
31+
Registry: &fakes.FakeRegistryClient{},
32+
},
3133
},
3234
}
3335
type args struct {
3436
sources map[CatalogKey]client.Interface
35-
clients map[CatalogKey]*client.Client
37+
clients map[CatalogKey]RegistryClientInterface
3638
}
3739
tests := []struct {
3840
name string
@@ -74,7 +76,7 @@ func TestNewNamespaceSourceQuerier(t *testing.T) {
7476
func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
7577
type fields struct {
7678
sources map[CatalogKey]client.Interface
77-
clients map[CatalogKey]*client.Client
79+
clients map[CatalogKey]RegistryClientInterface
7880
}
7981
tests := []struct {
8082
name string
@@ -93,7 +95,7 @@ func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
9395
name: "empty",
9496
fields: fields{
9597
sources: map[CatalogKey]client.Interface{},
96-
clients: map[CatalogKey]*client.Client{},
98+
clients: map[CatalogKey]RegistryClientInterface{},
9799
},
98100
error: fmt.Errorf("no catalog sources available"),
99101
},
@@ -103,9 +105,11 @@ func TestNamespaceSourceQuerier_Queryable(t *testing.T) {
103105
sources: map[CatalogKey]client.Interface{
104106
CatalogKey{"test", "ns"}: &fakes.FakeInterface{},
105107
},
106-
clients: map[CatalogKey]*client.Client{
107-
CatalogKey{"test", "ns"}: &client.Client{
108-
Registry: &fakes.FakeRegistryClient{},
108+
clients: map[CatalogKey]RegistryClientInterface{
109+
CatalogKey{"test", "ns"}: &OLMRegistryClient{
110+
Client: &client.Client{
111+
Registry: &fakes.FakeRegistryClient{},
112+
},
109113
},
110114
},
111115
},
@@ -237,7 +241,7 @@ func TestNamespaceSourceQuerier_FindProvider(t *testing.T) {
237241
func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
238242
initialSource := fakes.FakeInterface{}
239243
otherSource := fakes.FakeInterface{}
240-
clients := map[CatalogKey]*client.Client{}
244+
clients := map[CatalogKey]RegistryClientInterface{}
241245
initalBundle := &api.Bundle{CsvName: "test", PackageName: "testPkg", ChannelName: "testChannel"}
242246
startingBundle := &api.Bundle{CsvName: "starting-test", PackageName: "testPkg", ChannelName: "testChannel"}
243247
otherBundle := &api.Bundle{CsvName: "other", PackageName: "otherPkg", ChannelName: "otherChannel"}
@@ -268,7 +272,7 @@ func TestNamespaceSourceQuerier_FindPackage(t *testing.T) {
268272

269273
type fields struct {
270274
sources map[CatalogKey]client.Interface
271-
clients map[CatalogKey]*client.Client
275+
clients map[CatalogKey]RegistryClientInterface
272276
}
273277
type args struct {
274278
pkgName string
@@ -352,7 +356,7 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
352356
replacementSource := fakes.FakeInterface{}
353357
replacementAndLatestSource := fakes.FakeInterface{}
354358
replacementAndNoAnnotationLatestSource := fakes.FakeInterface{}
355-
clients := map[CatalogKey]*client.Client{}
359+
clients := map[CatalogKey]RegistryClientInterface{}
356360

357361
latestVersion := semver.MustParse("1.0.0-1556661308")
358362
csv := v1alpha1.ClusterServiceVersion{
@@ -440,7 +444,7 @@ func TestNamespaceSourceQuerier_FindReplacement(t *testing.T) {
440444

441445
type fields struct {
442446
sources map[CatalogKey]client.Interface
443-
clients map[CatalogKey]*client.Client
447+
clients map[CatalogKey]RegistryClientInterface
444448
}
445449
type args struct {
446450
currentVersion *semver.Version
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_registry_client_interface.go . RegistryClientInterface
2+
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_channel_entry_stream.go . ChannelEntryStream
3+
package resolver
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"io"
9+
10+
registryapi "github.com/operator-framework/operator-registry/pkg/api"
11+
"github.com/operator-framework/operator-registry/pkg/client"
12+
opregistry "github.com/operator-framework/operator-registry/pkg/registry"
13+
)
14+
15+
var _ RegistryClientInterface = &OLMRegistryClient{}
16+
17+
type ChannelEntryStream interface {
18+
Recv() (*registryapi.ChannelEntry, error)
19+
}
20+
21+
type RegistryClientInterface interface {
22+
FindBundleThatProvides(ctx context.Context, group, version, kind, pkgName string) (*registryapi.Bundle, error)
23+
GetLatestChannelEntriesThatProvide(ctx context.Context, group, version, kind string) (*ChannelEntryIterator, error)
24+
}
25+
26+
type ChannelEntryIterator struct {
27+
stream ChannelEntryStream
28+
error error
29+
}
30+
31+
func NewChannelEntryIterator(stream ChannelEntryStream) *ChannelEntryIterator {
32+
return &ChannelEntryIterator{stream: stream}
33+
}
34+
35+
func (ceit *ChannelEntryIterator) Next() *registryapi.ChannelEntry {
36+
if ceit.error != nil {
37+
return nil
38+
}
39+
next, err := ceit.stream.Recv()
40+
if err == io.EOF {
41+
return nil
42+
}
43+
if err != nil {
44+
ceit.error = err
45+
}
46+
return next
47+
}
48+
49+
func (ceit *ChannelEntryIterator) Error() error {
50+
return ceit.error
51+
}
52+
53+
type OLMRegistryClient struct {
54+
Client *client.Client
55+
}
56+
57+
func NewRegistryClient(client *client.Client) *OLMRegistryClient {
58+
return &OLMRegistryClient{Client: client}
59+
}
60+
61+
// GetLatestChannelEntriesThatProvide uses registry client to get a list of
62+
// latest channel entries that provide the requested API (via an iterator)
63+
func (rc *OLMRegistryClient) GetLatestChannelEntriesThatProvide(ctx context.Context, group, version, kind string) (*ChannelEntryIterator, error) {
64+
stream, err := rc.Client.Registry.GetLatestChannelEntriesThatProvide(ctx, &registryapi.GetLatestProvidersRequest{Group: group, Version: version, Kind: kind})
65+
if err != nil {
66+
return nil, err
67+
}
68+
return NewChannelEntryIterator(stream), nil
69+
}
70+
71+
// FindBundleThatProvides returns a bundle that provides the request API and
72+
// doesn't belong to the provided package
73+
func (rc *OLMRegistryClient) FindBundleThatProvides(ctx context.Context, group, version, kind, pkgName string) (*registryapi.Bundle, error) {
74+
it, err := rc.GetLatestChannelEntriesThatProvide(ctx, group, version, kind)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
entry := FilterChannelEntries(it, pkgName)
80+
if entry != nil {
81+
return nil, fmt.Errorf("Unable to find a channel entry which doesn't belong to package %s", pkgName)
82+
}
83+
bundle, err := rc.Client.Registry.GetBundle(ctx, &registryapi.GetBundleRequest{PkgName: entry.PackageName, ChannelName: entry.ChannelName, CsvName: entry.BundleName})
84+
if err != nil {
85+
return nil, err
86+
}
87+
return bundle, nil
88+
}
89+
90+
// FilterChannelEntries filters out a channel entries that provide the requested
91+
// API and come from the same package with original operator and returns the
92+
// first entry on the list
93+
func FilterChannelEntries(it *ChannelEntryIterator, pkgName string) *opregistry.ChannelEntry {
94+
var entry *opregistry.ChannelEntry
95+
for e := it.Next(); e != nil; e = it.Next() {
96+
if e.PackageName != pkgName {
97+
entry = &opregistry.ChannelEntry{
98+
PackageName: e.PackageName,
99+
ChannelName: e.ChannelName,
100+
BundleName: e.BundleName,
101+
Replaces: e.Replaces,
102+
}
103+
break
104+
}
105+
}
106+
return entry
107+
}

pkg/controller/registry/resolver/util_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,12 @@ func withReplaces(operator *Operator, replaces string) *Operator {
313313
// NewFakeSourceQuerier builds a querier that talks to fake registry stubs for testing
314314
func NewFakeSourceQuerier(bundlesByCatalog map[CatalogKey][]*api.Bundle) *NamespaceSourceQuerier {
315315
sources := map[CatalogKey]client.Interface{}
316-
clients := map[CatalogKey]*client.Client{}
316+
clients := map[CatalogKey]RegistryClientInterface{}
317317
for catKey, bundles := range bundlesByCatalog {
318318
source := &fakes.FakeInterface{}
319-
client := &client.Client{
320-
Registry: &fakes.FakeRegistryClient{},
321-
}
319+
// client := &client.Client{
320+
// Registry: &fakes.FakeRegistryClient{},
321+
// }
322322
source.GetBundleThatProvidesStub = func(ctx context.Context, groupOrName, version, kind string) (*api.Bundle, error) {
323323
for _, b := range bundles {
324324
apis := b.GetProvidedApis()
@@ -363,7 +363,8 @@ func NewFakeSourceQuerier(bundlesByCatalog map[CatalogKey][]*api.Bundle) *Namesp
363363

364364
return nil, fmt.Errorf("no bundle found")
365365
}
366-
clients[catKey] = client
366+
367+
// clients[catKey] = client
367368
sources[catKey] = source
368369
}
369370
return NewNamespaceSourceQuerier(sources, clients)
@@ -430,7 +431,7 @@ func SortBundleInChannel(replaces string, replacedBundle map[string]*api.Bundle,
430431
// NewFakeSourceQuerier builds a querier that talks to fake registry stubs for testing
431432
func NewFakeSourceQuerierCustomReplacement(catKey CatalogKey, bundle *api.Bundle) *NamespaceSourceQuerier {
432433
sources := map[CatalogKey]client.Interface{}
433-
clients := map[CatalogKey]*client.Client{}
434+
clients := map[CatalogKey]RegistryClientInterface{}
434435
source := &fakes.FakeInterface{}
435436
source.GetBundleThatProvidesStub = func(ctx context.Context, groupOrName, version, kind string) (*api.Bundle, error) {
436437
return nil, fmt.Errorf("no bundle found")

0 commit comments

Comments
 (0)