|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/operator-framework/operator-registry/pkg/api" |
| 9 | + "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "google.golang.org/grpc" |
| 13 | +) |
| 14 | + |
| 15 | +type RegistryClientStub struct { |
| 16 | + ListBundlesClient api.Registry_ListBundlesClient |
| 17 | + error error |
| 18 | +} |
| 19 | + |
| 20 | +func (s *RegistryClientStub) ListPackages(ctx context.Context, in *api.ListPackageRequest, opts ...grpc.CallOption) (api.Registry_ListPackagesClient, error) { |
| 21 | + return nil, nil |
| 22 | +} |
| 23 | + |
| 24 | +func (s *RegistryClientStub) GetPackage(ctx context.Context, in *api.GetPackageRequest, opts ...grpc.CallOption) (*api.Package, error) { |
| 25 | + return nil, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (s *RegistryClientStub) GetBundle(ctx context.Context, in *api.GetBundleRequest, opts ...grpc.CallOption) (*api.Bundle, error) { |
| 29 | + return nil, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (s *RegistryClientStub) GetBundleForChannel(ctx context.Context, in *api.GetBundleInChannelRequest, opts ...grpc.CallOption) (*api.Bundle, error) { |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (s *RegistryClientStub) GetChannelEntriesThatReplace(ctx context.Context, in *api.GetAllReplacementsRequest, opts ...grpc.CallOption) (api.Registry_GetChannelEntriesThatReplaceClient, error) { |
| 37 | + return nil, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (s *RegistryClientStub) GetBundleThatReplaces(ctx context.Context, in *api.GetReplacementRequest, opts ...grpc.CallOption) (*api.Bundle, error) { |
| 41 | + return nil, nil |
| 42 | +} |
| 43 | + |
| 44 | +func (s *RegistryClientStub) GetChannelEntriesThatProvide(ctx context.Context, in *api.GetAllProvidersRequest, opts ...grpc.CallOption) (api.Registry_GetChannelEntriesThatProvideClient, error) { |
| 45 | + return nil, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (s *RegistryClientStub) GetLatestChannelEntriesThatProvide(ctx context.Context, in *api.GetLatestProvidersRequest, opts ...grpc.CallOption) (api.Registry_GetLatestChannelEntriesThatProvideClient, error) { |
| 49 | + return nil, nil |
| 50 | +} |
| 51 | + |
| 52 | +func (s *RegistryClientStub) GetDefaultBundleThatProvides(ctx context.Context, in *api.GetDefaultProviderRequest, opts ...grpc.CallOption) (*api.Bundle, error) { |
| 53 | + return nil, nil |
| 54 | +} |
| 55 | + |
| 56 | +func (s *RegistryClientStub) ListBundles(ctx context.Context, in *api.ListBundlesRequest, opts ...grpc.CallOption) (api.Registry_ListBundlesClient, error) { |
| 57 | + return s.ListBundlesClient, s.error |
| 58 | +} |
| 59 | + |
| 60 | +func (s *RegistryClientStub) Check(ctx context.Context, in *grpc_health_v1.HealthCheckRequest, opts ...grpc.CallOption) (*grpc_health_v1.HealthCheckResponse, error) { |
| 61 | + return nil, nil |
| 62 | +} |
| 63 | + |
| 64 | +type BundleReceiverStub struct { |
| 65 | + Bundle *api.Bundle |
| 66 | + error error |
| 67 | + grpc.ClientStream |
| 68 | +} |
| 69 | + |
| 70 | +func (s *BundleReceiverStub) Recv() (*api.Bundle, error) { |
| 71 | + return s.Bundle, s.error |
| 72 | +} |
| 73 | + |
| 74 | +func TestListBundlesError(t *testing.T) { |
| 75 | + expected := errors.New("test error") |
| 76 | + stub := &RegistryClientStub{ |
| 77 | + error: expected, |
| 78 | + } |
| 79 | + c := Client{ |
| 80 | + Registry: stub, |
| 81 | + Health: stub, |
| 82 | + } |
| 83 | + |
| 84 | + _, actual := c.ListBundles(context.TODO()) |
| 85 | + require.Equal(t, expected, actual) |
| 86 | +} |
| 87 | + |
| 88 | +func TestListBundlesRecvError(t *testing.T) { |
| 89 | + expected := errors.New("test error") |
| 90 | + rstub := &BundleReceiverStub{ |
| 91 | + error: expected, |
| 92 | + } |
| 93 | + cstub := &RegistryClientStub{ |
| 94 | + ListBundlesClient: rstub, |
| 95 | + } |
| 96 | + c := Client{ |
| 97 | + Registry: cstub, |
| 98 | + Health: cstub, |
| 99 | + } |
| 100 | + |
| 101 | + it, err := c.ListBundles(context.TODO()) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + require.Nil(t, it.Next()) |
| 105 | + require.Equal(t, expected, it.Error()) |
| 106 | +} |
| 107 | + |
| 108 | +func TestListBundlesNext(t *testing.T) { |
| 109 | + expected := &api.Bundle{CsvName: "test"} |
| 110 | + rstub := &BundleReceiverStub{ |
| 111 | + Bundle: expected, |
| 112 | + } |
| 113 | + cstub := &RegistryClientStub{ |
| 114 | + ListBundlesClient: rstub, |
| 115 | + } |
| 116 | + c := Client{ |
| 117 | + Registry: cstub, |
| 118 | + Health: cstub, |
| 119 | + } |
| 120 | + |
| 121 | + it, err := c.ListBundles(context.TODO()) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + actual := it.Next() |
| 125 | + require.NoError(t, it.Error()) |
| 126 | + require.Equal(t, expected, actual) |
| 127 | +} |
0 commit comments