go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/appengine/coordinator/coordinatorTest/gs.go (about)

     1  // Copyright 2015 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 coordinatorTest
    16  
    17  import (
    18  	"errors"
    19  	"io"
    20  
    21  	storage "cloud.google.com/go/storage"
    22  	"go.chromium.org/luci/common/gcloud/gs"
    23  )
    24  
    25  // GSClient is a testing Google Storage client implementation.
    26  type GSClient map[gs.Path][]byte
    27  
    28  // Put sets the data at a given path.
    29  func (c GSClient) Put(path gs.Path, d []byte) {
    30  	c[path] = d
    31  }
    32  
    33  // Get retrieves the data at the specific path.
    34  func (c GSClient) Get(path gs.Path) []byte {
    35  	return c[path]
    36  }
    37  
    38  // Attrs implements gs.Client.
    39  func (c GSClient) Attrs(path gs.Path) (*storage.ObjectAttrs, error) { return nil, nil }
    40  
    41  // Objects implements gs.Client.
    42  func (c GSClient) Objects(path gs.Path) ([]*storage.ObjectAttrs, error) { return nil, nil }
    43  
    44  // SignedURL implements gs.Client.
    45  func (c GSClient) SignedURL(p gs.Path, opts *storage.SignedURLOptions) (string, error) {
    46  	return "", nil
    47  }
    48  
    49  // Close implements gs.Client.
    50  func (c GSClient) Close() error { return nil }
    51  
    52  // NewWriter implements gs.Client.
    53  func (c GSClient) NewWriter(gs.Path) (gs.Writer, error) {
    54  	return nil, errors.New("not implemented")
    55  }
    56  
    57  // Rename implements gs.Client.
    58  func (c GSClient) Rename(gs.Path, gs.Path) error { return errors.New("not implemented") }
    59  
    60  // Delete implements gs.Client.
    61  func (c GSClient) Delete(gs.Path) error { return errors.New("not implemented") }
    62  
    63  // NewReader implements gs.Client.
    64  func (c GSClient) NewReader(path gs.Path, offset int64, length int64) (io.ReadCloser, error) {
    65  	if d, ok := c["error"]; ok {
    66  		return nil, errors.New(string(d))
    67  	}
    68  
    69  	d, ok := c[path]
    70  	if !ok {
    71  		return nil, errors.New("does not exist")
    72  	}
    73  
    74  	// Determine the slice of data to return.
    75  	if offset < 0 {
    76  		offset = 0
    77  	}
    78  	end := int64(len(d))
    79  	if length >= 0 {
    80  		if v := offset + length; v < end {
    81  			end = v
    82  		}
    83  	}
    84  	d = d[offset:end]
    85  
    86  	r := make([]byte, len(d))
    87  	copy(r, d)
    88  	gsr := testGSReader(r)
    89  	return &gsr, nil
    90  }
    91  
    92  type testGSReader []byte
    93  
    94  func (r *testGSReader) Read(d []byte) (int, error) {
    95  	if len(*r) == 0 {
    96  		return 0, io.EOF
    97  	}
    98  
    99  	amt := copy(d, *r)
   100  	*r = (*r)[amt:]
   101  	return amt, nil
   102  }
   103  
   104  func (r *testGSReader) Close() error { return nil }