Skip to content

Commit 4cdfee7

Browse files
authored
Support multiple instance name for fake cas (#632)
1 parent 212b8b2 commit 4cdfee7

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

go/pkg/fakes/cas.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ func (f *Writer) QueryWriteStatus(context.Context, *bspb.QueryWriteStatusRequest
262262
// CAS is a fake CAS that implements FindMissingBlobs, Read and Write, storing stored blobs
263263
// in a map. It also counts the number of requests to store received, for validating batching logic.
264264
type CAS struct {
265+
// InstanceName is the expected instance name for all requests.
266+
InstanceName string
265267
// Maximum batch byte size to verify requests against.
266268
BatchSize int
267269
ReqSleepDuration time.Duration
@@ -278,17 +280,26 @@ type CAS struct {
278280
maxConcReqs int
279281
}
280282

281-
// NewCAS returns a new empty fake CAS.
282-
func NewCAS() *CAS {
283+
func newCASBase(instanceName string) *CAS {
283284
c := &CAS{
285+
InstanceName: instanceName,
284286
BatchSize: client.DefaultMaxBatchSize,
285287
PerDigestBlockFn: make(map[digest.Digest]func()),
286288
}
287-
288289
c.Clear()
289290
return c
290291
}
291292

293+
// NewCAS returns a new empty fake CAS with the default instance name.
294+
func NewCAS() *CAS {
295+
return newCASBase("instance")
296+
}
297+
298+
// NewCASNamed returns a new empty fake CAS with the given instance name.
299+
func NewCASNamed(name string) *CAS {
300+
return newCASBase(name)
301+
}
302+
292303
// Clear removes all results from the cache.
293304
func (f *CAS) Clear() {
294305
f.mu.Lock()
@@ -371,8 +382,8 @@ func (f *CAS) FindMissingBlobs(ctx context.Context, req *repb.FindMissingBlobsRe
371382
f.mu.Lock()
372383
defer f.mu.Unlock()
373384

374-
if req.InstanceName != "instance" {
375-
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
385+
if req.InstanceName != f.InstanceName {
386+
return nil, status.Errorf(codes.InvalidArgument, "test fake expected instance name %q, got %q", f.InstanceName, req.InstanceName)
376387
}
377388
resp := new(repb.FindMissingBlobsResponse)
378389
for _, dg := range req.BlobDigests {
@@ -417,8 +428,8 @@ func (f *CAS) BatchUpdateBlobs(ctx context.Context, req *repb.BatchUpdateBlobsRe
417428
}
418429
f.mu.Unlock()
419430

420-
if req.InstanceName != "instance" {
421-
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
431+
if req.InstanceName != f.InstanceName {
432+
return nil, status.Errorf(codes.InvalidArgument, "test fake expected instance name %q, got %q", f.InstanceName, req.InstanceName)
422433
}
423434

424435
reqBlob, _ := proto.Marshal(req)
@@ -479,8 +490,8 @@ func (f *CAS) BatchReadBlobs(ctx context.Context, req *repb.BatchReadBlobsReques
479490
}
480491
f.mu.Unlock()
481492

482-
if req.InstanceName != "instance" {
483-
return nil, status.Error(codes.InvalidArgument, "test fake expected instance name \"instance\"")
493+
if req.InstanceName != f.InstanceName {
494+
return nil, status.Errorf(codes.InvalidArgument, "test fake expected instance name %q, got %q", f.InstanceName, req.InstanceName)
484495
}
485496

486497
reqBlob, _ := proto.Marshal(req)
@@ -531,6 +542,9 @@ func (f *CAS) BatchReadBlobs(ctx context.Context, req *repb.BatchReadBlobsReques
531542
// GetTree implements the corresponding RE API function.
532543
func (f *CAS) GetTree(req *repb.GetTreeRequest, stream regrpc.ContentAddressableStorage_GetTreeServer) error {
533544
f.maybeSleep()
545+
if req.InstanceName != f.InstanceName {
546+
return status.Errorf(codes.InvalidArgument, "test fake expected instance name %q, got %q", f.InstanceName, req.InstanceName)
547+
}
534548
rootDigest, err := digest.NewFromProto(req.RootDigest)
535549
if err != nil {
536550
return fmt.Errorf("unable to parsse root digest %v", req.RootDigest)
@@ -584,8 +598,8 @@ func (f *CAS) Write(stream bsgrpc.ByteStream_WriteServer) (err error) {
584598
}
585599

586600
path := strings.Split(req.ResourceName, "/")
587-
if (len(path) != 6 && len(path) != 7) || path[0] != "instance" || path[1] != "uploads" || (path[3] != "blobs" && path[3] != "compressed-blobs") {
588-
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
601+
if (len(path) != 6 && len(path) != 7) || path[0] != f.InstanceName || path[1] != "uploads" || (path[3] != "blobs" && path[3] != "compressed-blobs") {
602+
return status.Errorf(codes.InvalidArgument, "test fake expected resource name of the form \"%s/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
589603
}
590604
// indexOffset for all 4+ paths - `compressed-blobs` paths have one more element.
591605
indexOffset := 0
@@ -598,14 +612,14 @@ func (f *CAS) Write(stream bsgrpc.ByteStream_WriteServer) (err error) {
598612
}
599613
size, err := strconv.ParseInt(path[5+indexOffset], 10, 64)
600614
if err != nil {
601-
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
615+
return status.Errorf(codes.InvalidArgument, "test fake expected resource name of the form \"%s/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
602616
}
603617
dg, err := digest.New(path[4+indexOffset], size)
604618
if err != nil {
605-
return status.Error(codes.InvalidArgument, "test fake expected a valid digest as part of the resource name: \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
619+
return status.Errorf(codes.InvalidArgument, "test fake expected a valid digest as part of the resource name: \"%s/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
606620
}
607621
if _, err := uuid.Parse(path[2]); err != nil {
608-
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
622+
return status.Errorf(codes.InvalidArgument, "test fake expected resource name of the form \"%s/uploads/<uuid>/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
609623
}
610624

611625
f.maybeSleep()
@@ -692,8 +706,8 @@ func (f *CAS) Read(req *bspb.ReadRequest, stream bsgrpc.ByteStream_ReadServer) e
692706
}
693707

694708
path := strings.Split(req.ResourceName, "/")
695-
if (len(path) != 4 && len(path) != 5) || path[0] != "instance" || (path[1] != "blobs" && path[1] != "compressed-blobs") {
696-
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
709+
if (len(path) != 4 && len(path) != 5) || path[0] != f.InstanceName || (path[1] != "blobs" && path[1] != "compressed-blobs") {
710+
return status.Errorf(codes.InvalidArgument, "test fake expected resource name of the form \"%s/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
697711
}
698712
// indexOffset for all 2+ paths - `compressed-blobs` has one more URI element.
699713
indexOffset := 0
@@ -703,7 +717,7 @@ func (f *CAS) Read(req *bspb.ReadRequest, stream bsgrpc.ByteStream_ReadServer) e
703717

704718
size, err := strconv.Atoi(path[3+indexOffset])
705719
if err != nil {
706-
return status.Error(codes.InvalidArgument, "test fake expected resource name of the form \"instance/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"")
720+
return status.Errorf(codes.InvalidArgument, "test fake expected resource name of the form \"%s/blobs|compressed-blobs/<compressor?>/<hash>/<size>\"", f.InstanceName)
707721
}
708722
dg := digest.TestNew(path[2+indexOffset], int64(size))
709723
f.maybeSleep()

0 commit comments

Comments
 (0)