go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cipd/appengine/impl/testutil/gs.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  	"bytes"
    19  	"context"
    20  	"io"
    21  
    22  	"go.chromium.org/luci/cipd/appengine/impl/gs"
    23  )
    24  
    25  // NoopGoogleStorage implements gs.GoogleStorage interface by returning errors.
    26  //
    27  // Can be embedded into mock implementations that override some subset of
    28  // methods.
    29  type NoopGoogleStorage struct {
    30  	Err error // an error to return or nil to panic
    31  }
    32  
    33  var _ gs.GoogleStorage = NoopGoogleStorage{}
    34  
    35  // Size is part of gs.GoogleStorage interface.
    36  func (n NoopGoogleStorage) Size(ctx context.Context, path string) (size uint64, exists bool, err error) {
    37  	if n.Err == nil {
    38  		panic("must not be called")
    39  	}
    40  	return 0, false, n.Err
    41  }
    42  
    43  // Exists is part of gs.GoogleStorage interface.
    44  func (n NoopGoogleStorage) Exists(ctx context.Context, path string) (exists bool, err error) {
    45  	if n.Err == nil {
    46  		panic("must not be called")
    47  	}
    48  	return false, n.Err
    49  }
    50  
    51  // Copy is part of gs.GoogleStorage interface.
    52  func (n NoopGoogleStorage) Copy(ctx context.Context, dst string, dstGen int64, src string, srcGen int64) error {
    53  	if n.Err == nil {
    54  		panic("must not be called")
    55  	}
    56  	return n.Err
    57  }
    58  
    59  // Delete is part of gs.GoogleStorage interface.
    60  func (n NoopGoogleStorage) Delete(ctx context.Context, path string) error {
    61  	if n.Err == nil {
    62  		panic("must not be called")
    63  	}
    64  	return n.Err
    65  }
    66  
    67  // Publish is part of gs.GoogleStorage interface.
    68  func (n NoopGoogleStorage) Publish(ctx context.Context, dst, src string, srcGen int64) error {
    69  	if n.Err == nil {
    70  		panic("must not be called")
    71  	}
    72  	return n.Err
    73  }
    74  
    75  // StartUpload is part of gs.GoogleStorage interface.
    76  func (n NoopGoogleStorage) StartUpload(ctx context.Context, path string) (uploadURL string, err error) {
    77  	if n.Err == nil {
    78  		panic("must not be called")
    79  	}
    80  	return "", n.Err
    81  }
    82  
    83  // CancelUpload is part of gs.GoogleStorage interface.
    84  func (n NoopGoogleStorage) CancelUpload(ctx context.Context, uploadURL string) error {
    85  	if n.Err == nil {
    86  		panic("must not be called")
    87  	}
    88  	return n.Err
    89  }
    90  
    91  // Reader is part of gs.GoogleStorage interface.
    92  func (n NoopGoogleStorage) Reader(ctx context.Context, path string, gen int64) (gs.Reader, error) {
    93  	if n.Err == nil {
    94  		panic("must not be called")
    95  	}
    96  	return nil, n.Err
    97  }
    98  
    99  // MockGSReader implements gs.Reader on top of a regular io.ReaderAt.
   100  type MockGSReader struct {
   101  	io.ReaderAt
   102  
   103  	Len int64
   104  	Gen int64
   105  }
   106  
   107  // Size is part of gs.Reader interface.
   108  func (m *MockGSReader) Size() int64 { return m.Len }
   109  
   110  // Generation is part of gs.Reader interface.
   111  func (m *MockGSReader) Generation() int64 { return m.Gen }
   112  
   113  // NewMockGSReader constructs MockGSReader from a byte slice.
   114  func NewMockGSReader(data []byte) *MockGSReader {
   115  	return &MockGSReader{
   116  		ReaderAt: bytes.NewReader(data),
   117  		Len:      int64(len(data)),
   118  		Gen:      1,
   119  	}
   120  }