go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/impl/testutil/cas.go (about) 1 // Copyright 2017 The LUCI Authors. 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package testutil 16 17 import ( 18 "context" 19 20 api "go.chromium.org/luci/cipd/api/cipd/v1" 21 "go.chromium.org/luci/cipd/appengine/impl/gs" 22 ) 23 24 // MockCAS implements cas.StorageServer interface. 25 type MockCAS struct { 26 api.UnimplementedStorageServer 27 28 Err error // an error to return or nil to pass through to the callback 29 30 GetReaderImpl func(context.Context, *api.ObjectRef) (gs.Reader, error) 31 GetObjectURLImpl func(context.Context, *api.GetObjectURLRequest) (*api.ObjectURL, error) 32 BeginUploadImpl func(context.Context, *api.BeginUploadRequest) (*api.UploadOperation, error) 33 FinishUploadImpl func(context.Context, *api.FinishUploadRequest) (*api.UploadOperation, error) 34 CancelUploadImpl func(context.Context, *api.CancelUploadRequest) (*api.UploadOperation, error) 35 } 36 37 // GetReader implements the corresponding method of cas.StorageServer interface. 38 func (m *MockCAS) GetReader(ctx context.Context, ref *api.ObjectRef) (gs.Reader, error) { 39 if m.Err != nil { 40 return nil, m.Err 41 } 42 if m.GetReaderImpl == nil { 43 panic("must not be called") 44 } 45 return m.GetReaderImpl(ctx, ref) 46 } 47 48 // GetObjectURL implements the corresponding RPC method, see the proto doc. 49 func (m *MockCAS) GetObjectURL(ctx context.Context, r *api.GetObjectURLRequest) (*api.ObjectURL, error) { 50 if m.Err != nil { 51 return nil, m.Err 52 } 53 if m.GetObjectURLImpl == nil { 54 panic("must not be called") 55 } 56 return m.GetObjectURLImpl(ctx, r) 57 } 58 59 // BeginUpload implements the corresponding RPC method, see the proto doc. 60 func (m *MockCAS) BeginUpload(ctx context.Context, r *api.BeginUploadRequest) (*api.UploadOperation, error) { 61 if m.Err != nil { 62 return nil, m.Err 63 } 64 if m.BeginUploadImpl == nil { 65 panic("must not be called") 66 } 67 return m.BeginUploadImpl(ctx, r) 68 } 69 70 // FinishUpload implements the corresponding RPC method, see the proto doc. 71 func (m *MockCAS) FinishUpload(ctx context.Context, r *api.FinishUploadRequest) (*api.UploadOperation, error) { 72 if m.Err != nil { 73 return nil, m.Err 74 } 75 if m.FinishUploadImpl == nil { 76 panic("must not be called") 77 } 78 return m.FinishUploadImpl(ctx, r) 79 } 80 81 // CancelUpload implements the corresponding RPC method, see the proto doc. 82 func (m *MockCAS) CancelUpload(ctx context.Context, r *api.CancelUploadRequest) (*api.UploadOperation, error) { 83 if m.Err != nil { 84 return nil, m.Err 85 } 86 if m.CancelUploadImpl == nil { 87 panic("must not be called") 88 } 89 return m.CancelUploadImpl(ctx, r) 90 }