|
| 1 | +package discovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/jonboulle/clockwork" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb" |
| 9 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Discovery" |
| 10 | + "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations" |
| 11 | + "go.uber.org/mock/gomock" |
| 12 | + grpcCodes "google.golang.org/grpc/codes" |
| 13 | + "google.golang.org/grpc/status" |
| 14 | + "google.golang.org/protobuf/types/known/anypb" |
| 15 | + |
| 16 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/discovery/config" |
| 17 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/endpoint" |
| 18 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" |
| 19 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" |
| 20 | +) |
| 21 | + |
| 22 | +func must[T any](t T, err error) T { |
| 23 | + if err != nil { |
| 24 | + panic(err) |
| 25 | + } |
| 26 | + |
| 27 | + return t |
| 28 | +} |
| 29 | + |
| 30 | +func TestDiscover(t *testing.T) { |
| 31 | + t.Run("HappyWay", func(t *testing.T) { |
| 32 | + ctx := xtest.Context(t) |
| 33 | + ctrl := gomock.NewController(t) |
| 34 | + clock := clockwork.NewFakeClock() |
| 35 | + client := NewMockDiscoveryServiceClient(ctrl) |
| 36 | + client.EXPECT().ListEndpoints(gomock.Any(), &Ydb_Discovery.ListEndpointsRequest{ |
| 37 | + Database: "test", |
| 38 | + }).Return(&Ydb_Discovery.ListEndpointsResponse{ |
| 39 | + Operation: &Ydb_Operations.Operation{ |
| 40 | + Ready: true, |
| 41 | + Status: Ydb.StatusIds_SUCCESS, |
| 42 | + Result: must(anypb.New(&Ydb_Discovery.ListEndpointsResult{ |
| 43 | + Endpoints: []*Ydb_Discovery.EndpointInfo{ |
| 44 | + { |
| 45 | + Address: "node1", |
| 46 | + Port: 1, |
| 47 | + Ssl: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + Address: "node2", |
| 51 | + Port: 2, |
| 52 | + Location: "AZ0", |
| 53 | + Ssl: true, |
| 54 | + }, |
| 55 | + { |
| 56 | + Address: "node3", |
| 57 | + Port: 3, |
| 58 | + Ssl: false, |
| 59 | + }, |
| 60 | + { |
| 61 | + Address: "node4", |
| 62 | + Port: 4, |
| 63 | + Location: "AZ0", |
| 64 | + Ssl: false, |
| 65 | + }, |
| 66 | + }, |
| 67 | + SelfLocation: "AZ0", |
| 68 | + })), |
| 69 | + }, |
| 70 | + }, nil) |
| 71 | + endpoints, location, err := discover(ctx, client, config.New( |
| 72 | + config.WithDatabase("test"), |
| 73 | + config.WithSecure(false), |
| 74 | + config.WithClock(clock), |
| 75 | + )) |
| 76 | + require.NoError(t, err) |
| 77 | + require.EqualValues(t, "AZ0", location) |
| 78 | + require.EqualValues(t, []endpoint.Endpoint{ |
| 79 | + endpoint.New("node3:3", |
| 80 | + endpoint.WithLocalDC(false), |
| 81 | + endpoint.WithLastUpdated(clock.Now()), |
| 82 | + ), |
| 83 | + endpoint.New("node4:4", |
| 84 | + endpoint.WithLocalDC(true), |
| 85 | + endpoint.WithLocation("AZ0"), |
| 86 | + endpoint.WithLastUpdated(clock.Now()), |
| 87 | + ), |
| 88 | + }, endpoints) |
| 89 | + }) |
| 90 | + t.Run("TransportError", func(t *testing.T) { |
| 91 | + ctx := xtest.Context(t) |
| 92 | + ctrl := gomock.NewController(t) |
| 93 | + client := NewMockDiscoveryServiceClient(ctrl) |
| 94 | + client.EXPECT().ListEndpoints(gomock.Any(), &Ydb_Discovery.ListEndpointsRequest{ |
| 95 | + Database: "test", |
| 96 | + }).Return(nil, xerrors.Transport(status.Error(grpcCodes.Unavailable, ""))) |
| 97 | + endpoints, location, err := discover(ctx, client, config.New( |
| 98 | + config.WithDatabase("test"), |
| 99 | + )) |
| 100 | + require.Error(t, err) |
| 101 | + require.Empty(t, endpoints) |
| 102 | + require.Equal(t, "", location) |
| 103 | + require.True(t, xerrors.IsTransportError(err, grpcCodes.Unavailable)) |
| 104 | + }) |
| 105 | + t.Run("OperationError", func(t *testing.T) { |
| 106 | + ctx := xtest.Context(t) |
| 107 | + ctrl := gomock.NewController(t) |
| 108 | + client := NewMockDiscoveryServiceClient(ctrl) |
| 109 | + client.EXPECT().ListEndpoints(gomock.Any(), &Ydb_Discovery.ListEndpointsRequest{ |
| 110 | + Database: "test", |
| 111 | + }).Return(&Ydb_Discovery.ListEndpointsResponse{ |
| 112 | + Operation: &Ydb_Operations.Operation{ |
| 113 | + Ready: true, |
| 114 | + Status: Ydb.StatusIds_UNAVAILABLE, |
| 115 | + }, |
| 116 | + }, nil) |
| 117 | + endpoints, location, err := discover(ctx, client, config.New( |
| 118 | + config.WithDatabase("test"), |
| 119 | + )) |
| 120 | + require.Error(t, err) |
| 121 | + require.Empty(t, endpoints) |
| 122 | + require.Equal(t, "", location) |
| 123 | + require.True(t, xerrors.IsOperationError(err, Ydb.StatusIds_UNAVAILABLE)) |
| 124 | + }) |
| 125 | + t.Run("WithAddressMutator", func(t *testing.T) { |
| 126 | + ctx := xtest.Context(t) |
| 127 | + ctrl := gomock.NewController(t) |
| 128 | + clock := clockwork.NewFakeClock() |
| 129 | + client := NewMockDiscoveryServiceClient(ctrl) |
| 130 | + client.EXPECT().ListEndpoints(gomock.Any(), &Ydb_Discovery.ListEndpointsRequest{ |
| 131 | + Database: "test", |
| 132 | + }).Return(&Ydb_Discovery.ListEndpointsResponse{ |
| 133 | + Operation: &Ydb_Operations.Operation{ |
| 134 | + Ready: true, |
| 135 | + Status: Ydb.StatusIds_SUCCESS, |
| 136 | + Result: must(anypb.New(&Ydb_Discovery.ListEndpointsResult{ |
| 137 | + Endpoints: []*Ydb_Discovery.EndpointInfo{ |
| 138 | + { |
| 139 | + Address: "node1", |
| 140 | + Port: 1, |
| 141 | + }, |
| 142 | + { |
| 143 | + Address: "node2", |
| 144 | + Port: 2, |
| 145 | + Location: "AZ0", |
| 146 | + }, |
| 147 | + }, |
| 148 | + SelfLocation: "AZ0", |
| 149 | + })), |
| 150 | + }, |
| 151 | + }, nil) |
| 152 | + endpoints, location, err := discover(ctx, client, config.New( |
| 153 | + config.WithDatabase("test"), |
| 154 | + config.WithAddressMutator(func(address string) string { |
| 155 | + return "u-" + address |
| 156 | + }), |
| 157 | + config.WithClock(clock), |
| 158 | + )) |
| 159 | + require.NoError(t, err) |
| 160 | + require.EqualValues(t, "AZ0", location) |
| 161 | + require.EqualValues(t, []endpoint.Endpoint{ |
| 162 | + endpoint.New("u-node1:1", |
| 163 | + endpoint.WithLocalDC(false), |
| 164 | + endpoint.WithLastUpdated(clock.Now()), |
| 165 | + ), |
| 166 | + endpoint.New("u-node2:2", |
| 167 | + endpoint.WithLocalDC(true), |
| 168 | + endpoint.WithLocation("AZ0"), |
| 169 | + endpoint.WithLastUpdated(clock.Now()), |
| 170 | + ), |
| 171 | + }, endpoints) |
| 172 | + }) |
| 173 | +} |
0 commit comments