You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

384 lines
11 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package testutil
  14. import (
  15. "errors"
  16. "fmt"
  17. "sync"
  18. "testing"
  19. "time"
  20. "golang.org/x/net/context"
  21. "google.golang.org/grpc/status"
  22. "github.com/golang/protobuf/proto"
  23. "github.com/golang/protobuf/ptypes/empty"
  24. proto3 "github.com/golang/protobuf/ptypes/struct"
  25. pbt "github.com/golang/protobuf/ptypes/timestamp"
  26. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. )
  30. // Action is a mocked RPC activity that MockCloudSpannerClient will take.
  31. type Action struct {
  32. Method string
  33. Err error
  34. }
  35. // MockCloudSpannerClient is a mock implementation of sppb.SpannerClient.
  36. type MockCloudSpannerClient struct {
  37. sppb.SpannerClient
  38. mu sync.Mutex
  39. t *testing.T
  40. // Live sessions on the client.
  41. sessions map[string]bool
  42. // Expected set of actions that will be executed by the client.
  43. actions []Action
  44. // Session ping history.
  45. pings []string
  46. // Injected error, will be returned by all APIs.
  47. injErr map[string]error
  48. // Client will not fail on any request.
  49. nice bool
  50. // Client will stall on any requests.
  51. freezed chan struct{}
  52. }
  53. // NewMockCloudSpannerClient creates new MockCloudSpannerClient instance.
  54. func NewMockCloudSpannerClient(t *testing.T, acts ...Action) *MockCloudSpannerClient {
  55. mc := &MockCloudSpannerClient{t: t, sessions: map[string]bool{}, injErr: map[string]error{}}
  56. mc.SetActions(acts...)
  57. // Produce a closed channel, so the default action of ready is to not block.
  58. mc.Freeze()
  59. mc.Unfreeze()
  60. return mc
  61. }
  62. // MakeNice makes this a nice mock which will not fail on any request.
  63. func (m *MockCloudSpannerClient) MakeNice() {
  64. m.mu.Lock()
  65. defer m.mu.Unlock()
  66. m.nice = true
  67. }
  68. // MakeStrict makes this a strict mock which will fail on any unexpected request.
  69. func (m *MockCloudSpannerClient) MakeStrict() {
  70. m.mu.Lock()
  71. defer m.mu.Unlock()
  72. m.nice = false
  73. }
  74. // InjectError injects a global error that will be returned by all calls to method
  75. // regardless of the actions array.
  76. func (m *MockCloudSpannerClient) InjectError(method string, err error) {
  77. m.mu.Lock()
  78. defer m.mu.Unlock()
  79. m.injErr[method] = err
  80. }
  81. // SetActions sets the new set of expected actions to MockCloudSpannerClient.
  82. func (m *MockCloudSpannerClient) SetActions(acts ...Action) {
  83. m.mu.Lock()
  84. defer m.mu.Unlock()
  85. m.actions = nil
  86. for _, act := range acts {
  87. m.actions = append(m.actions, act)
  88. }
  89. }
  90. // DumpPings dumps the ping history.
  91. func (m *MockCloudSpannerClient) DumpPings() []string {
  92. m.mu.Lock()
  93. defer m.mu.Unlock()
  94. return append([]string(nil), m.pings...)
  95. }
  96. // DumpSessions dumps the internal session table.
  97. func (m *MockCloudSpannerClient) DumpSessions() map[string]bool {
  98. m.mu.Lock()
  99. defer m.mu.Unlock()
  100. st := map[string]bool{}
  101. for s, v := range m.sessions {
  102. st[s] = v
  103. }
  104. return st
  105. }
  106. // CreateSession is a placeholder for SpannerClient.CreateSession.
  107. func (m *MockCloudSpannerClient) CreateSession(c context.Context, r *sppb.CreateSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
  108. m.ready()
  109. m.mu.Lock()
  110. defer m.mu.Unlock()
  111. if err := m.injErr["CreateSession"]; err != nil {
  112. return nil, err
  113. }
  114. s := &sppb.Session{}
  115. if r.Database != "mockdb" {
  116. // Reject other databases
  117. return s, status.Errorf(codes.NotFound, fmt.Sprintf("database not found: %v", r.Database))
  118. }
  119. // Generate & record session name.
  120. s.Name = fmt.Sprintf("mockdb-%v", time.Now().UnixNano())
  121. m.sessions[s.Name] = true
  122. return s, nil
  123. }
  124. // GetSession is a placeholder for SpannerClient.GetSession.
  125. func (m *MockCloudSpannerClient) GetSession(c context.Context, r *sppb.GetSessionRequest, opts ...grpc.CallOption) (*sppb.Session, error) {
  126. m.ready()
  127. m.mu.Lock()
  128. defer m.mu.Unlock()
  129. if err := m.injErr["GetSession"]; err != nil {
  130. return nil, err
  131. }
  132. m.pings = append(m.pings, r.Name)
  133. if _, ok := m.sessions[r.Name]; !ok {
  134. return nil, status.Errorf(codes.NotFound, fmt.Sprintf("Session not found: %v", r.Name))
  135. }
  136. return &sppb.Session{Name: r.Name}, nil
  137. }
  138. // DeleteSession is a placeholder for SpannerClient.DeleteSession.
  139. func (m *MockCloudSpannerClient) DeleteSession(c context.Context, r *sppb.DeleteSessionRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
  140. m.ready()
  141. m.mu.Lock()
  142. defer m.mu.Unlock()
  143. if err := m.injErr["DeleteSession"]; err != nil {
  144. return nil, err
  145. }
  146. if _, ok := m.sessions[r.Name]; !ok {
  147. // Session not found.
  148. return &empty.Empty{}, status.Errorf(codes.NotFound, fmt.Sprintf("Session not found: %v", r.Name))
  149. }
  150. // Delete session from in-memory table.
  151. delete(m.sessions, r.Name)
  152. return &empty.Empty{}, nil
  153. }
  154. // ExecuteStreamingSql is a mock implementation of SpannerClient.ExecuteStreamingSql.
  155. func (m *MockCloudSpannerClient) ExecuteStreamingSql(c context.Context, r *sppb.ExecuteSqlRequest, opts ...grpc.CallOption) (sppb.Spanner_ExecuteStreamingSqlClient, error) {
  156. m.ready()
  157. m.mu.Lock()
  158. defer m.mu.Unlock()
  159. act, err := m.expectAction("ExecuteStreamingSql")
  160. if err != nil {
  161. return nil, err
  162. }
  163. wantReq := &sppb.ExecuteSqlRequest{
  164. Session: "mocksession",
  165. Transaction: &sppb.TransactionSelector{
  166. Selector: &sppb.TransactionSelector_SingleUse{
  167. SingleUse: &sppb.TransactionOptions{
  168. Mode: &sppb.TransactionOptions_ReadOnly_{
  169. ReadOnly: &sppb.TransactionOptions_ReadOnly{
  170. TimestampBound: &sppb.TransactionOptions_ReadOnly_Strong{
  171. Strong: true,
  172. },
  173. ReturnReadTimestamp: false,
  174. },
  175. },
  176. },
  177. },
  178. },
  179. Sql: "mockquery",
  180. Params: &proto3.Struct{
  181. Fields: map[string]*proto3.Value{"var1": &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: "abc"}}},
  182. },
  183. ParamTypes: map[string]*sppb.Type{"var1": &sppb.Type{Code: sppb.TypeCode_STRING}},
  184. }
  185. if !proto.Equal(r, wantReq) {
  186. return nil, fmt.Errorf("got query request: %v, want: %v", r, wantReq)
  187. }
  188. if act.Err != nil {
  189. return nil, act.Err
  190. }
  191. return nil, errors.New("query never succeeds on mock client")
  192. }
  193. // StreamingRead is a placeholder for SpannerClient.StreamingRead.
  194. func (m *MockCloudSpannerClient) StreamingRead(c context.Context, r *sppb.ReadRequest, opts ...grpc.CallOption) (sppb.Spanner_StreamingReadClient, error) {
  195. m.ready()
  196. m.mu.Lock()
  197. defer m.mu.Unlock()
  198. act, err := m.expectAction("StreamingRead", "StreamingReadIndex")
  199. if err != nil {
  200. return nil, err
  201. }
  202. wantReq := &sppb.ReadRequest{
  203. Session: "mocksession",
  204. Transaction: &sppb.TransactionSelector{
  205. Selector: &sppb.TransactionSelector_SingleUse{
  206. SingleUse: &sppb.TransactionOptions{
  207. Mode: &sppb.TransactionOptions_ReadOnly_{
  208. ReadOnly: &sppb.TransactionOptions_ReadOnly{
  209. TimestampBound: &sppb.TransactionOptions_ReadOnly_Strong{
  210. Strong: true,
  211. },
  212. ReturnReadTimestamp: false,
  213. },
  214. },
  215. },
  216. },
  217. },
  218. Table: "t_mock",
  219. Columns: []string{"col1", "col2"},
  220. KeySet: &sppb.KeySet{
  221. Keys: []*proto3.ListValue{
  222. &proto3.ListValue{
  223. Values: []*proto3.Value{
  224. &proto3.Value{Kind: &proto3.Value_StringValue{StringValue: "foo"}},
  225. },
  226. },
  227. },
  228. Ranges: []*sppb.KeyRange{},
  229. All: false,
  230. },
  231. }
  232. if act.Method == "StreamingIndexRead" {
  233. wantReq.Index = "idx1"
  234. }
  235. if !proto.Equal(r, wantReq) {
  236. return nil, fmt.Errorf("got query request: %v, want: %v", r, wantReq)
  237. }
  238. if act.Err != nil {
  239. return nil, act.Err
  240. }
  241. return nil, errors.New("read never succeeds on mock client")
  242. }
  243. // BeginTransaction is a placeholder for SpannerClient.BeginTransaction.
  244. func (m *MockCloudSpannerClient) BeginTransaction(c context.Context, r *sppb.BeginTransactionRequest, opts ...grpc.CallOption) (*sppb.Transaction, error) {
  245. m.ready()
  246. m.mu.Lock()
  247. defer m.mu.Unlock()
  248. if !m.nice {
  249. act, err := m.expectAction("BeginTransaction")
  250. if err != nil {
  251. return nil, err
  252. }
  253. if act.Err != nil {
  254. return nil, act.Err
  255. }
  256. }
  257. resp := &sppb.Transaction{Id: []byte("transaction-1")}
  258. if _, ok := r.Options.Mode.(*sppb.TransactionOptions_ReadOnly_); ok {
  259. resp.ReadTimestamp = &pbt.Timestamp{Seconds: 3, Nanos: 4}
  260. }
  261. return resp, nil
  262. }
  263. // Commit is a placeholder for SpannerClient.Commit.
  264. func (m *MockCloudSpannerClient) Commit(c context.Context, r *sppb.CommitRequest, opts ...grpc.CallOption) (*sppb.CommitResponse, error) {
  265. m.ready()
  266. m.mu.Lock()
  267. defer m.mu.Unlock()
  268. if !m.nice {
  269. act, err := m.expectAction("Commit")
  270. if err != nil {
  271. return nil, err
  272. }
  273. if act.Err != nil {
  274. return nil, act.Err
  275. }
  276. }
  277. return &sppb.CommitResponse{CommitTimestamp: &pbt.Timestamp{Seconds: 1, Nanos: 2}}, nil
  278. }
  279. // Rollback is a placeholder for SpannerClient.Rollback.
  280. func (m *MockCloudSpannerClient) Rollback(c context.Context, r *sppb.RollbackRequest, opts ...grpc.CallOption) (*empty.Empty, error) {
  281. m.ready()
  282. m.mu.Lock()
  283. defer m.mu.Unlock()
  284. if !m.nice {
  285. act, err := m.expectAction("Rollback")
  286. if err != nil {
  287. return nil, err
  288. }
  289. if act.Err != nil {
  290. return nil, act.Err
  291. }
  292. }
  293. return nil, nil
  294. }
  295. // PartitionQuery is a placeholder for SpannerServer.PartitionQuery.
  296. func (m *MockCloudSpannerClient) PartitionQuery(ctx context.Context, r *sppb.PartitionQueryRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error) {
  297. m.ready()
  298. return nil, errors.New("Unimplemented")
  299. }
  300. // PartitionRead is a placeholder for SpannerServer.PartitionRead.
  301. func (m *MockCloudSpannerClient) PartitionRead(ctx context.Context, r *sppb.PartitionReadRequest, opts ...grpc.CallOption) (*sppb.PartitionResponse, error) {
  302. m.ready()
  303. return nil, errors.New("Unimplemented")
  304. }
  305. func (m *MockCloudSpannerClient) expectAction(methods ...string) (Action, error) {
  306. for _, me := range methods {
  307. if err := m.injErr[me]; err != nil {
  308. return Action{}, err
  309. }
  310. }
  311. if len(m.actions) == 0 {
  312. m.t.Fatalf("unexpected %v executed", methods)
  313. }
  314. act := m.actions[0]
  315. m.actions = m.actions[1:]
  316. for _, me := range methods {
  317. if me == act.Method {
  318. return act, nil
  319. }
  320. }
  321. m.t.Fatalf("unexpected call of one of %v, want method %s", methods, act.Method)
  322. return Action{}, nil
  323. }
  324. // Freeze stalls all requests.
  325. func (m *MockCloudSpannerClient) Freeze() {
  326. m.mu.Lock()
  327. defer m.mu.Unlock()
  328. m.freezed = make(chan struct{})
  329. }
  330. // Unfreeze restores processing requests.
  331. func (m *MockCloudSpannerClient) Unfreeze() {
  332. m.mu.Lock()
  333. defer m.mu.Unlock()
  334. close(m.freezed)
  335. }
  336. // CheckActionsConsumed checks that all actions have been consumed.
  337. func (m *MockCloudSpannerClient) CheckActionsConsumed() {
  338. if len(m.actions) != 0 {
  339. m.t.Fatalf("unconsumed mock client actions: %v", m.actions)
  340. }
  341. }
  342. // ready checks conditions before executing requests
  343. // TODO: add checks for injected errors, actions
  344. func (m *MockCloudSpannerClient) ready() {
  345. m.mu.Lock()
  346. freezed := m.freezed
  347. m.mu.Unlock()
  348. // check if client should be freezed
  349. <-freezed
  350. }