github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/devserver/fake.go (about) 1 // Copyright 2018 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package devserver 6 7 import ( 8 "bytes" 9 "context" 10 "io" 11 "io/ioutil" 12 "net/url" 13 "os" 14 15 "go.chromium.org/tast/core/errors" 16 ) 17 18 // FakeClient is a fake implementation of devserver.Client suitable for unit tests. 19 type FakeClient struct { 20 files map[string][]byte // GS URL -> content 21 } 22 23 var _ Client = &FakeClient{} 24 25 // NewFakeClient constructs a FakeClient. files is a map from GS URL to content. 26 func NewFakeClient(files map[string][]byte) *FakeClient { 27 return &FakeClient{files} 28 } 29 30 // TearDown does nothing. 31 func (c *FakeClient) TearDown() error { 32 return nil 33 } 34 35 // Open simulates a download from Google Cloud Storage. 36 func (c *FakeClient) Open(ctx context.Context, gsURL string) (io.ReadCloser, error) { 37 data, ok := c.files[gsURL] 38 if !ok { 39 return nil, os.ErrNotExist 40 } 41 return ioutil.NopCloser(bytes.NewReader(data)), nil 42 } 43 44 // Stage simulates a getting a url to Google Cloud Storage. 45 func (c *FakeClient) Stage(ctx context.Context, gsURL string) (*url.URL, error) { 46 return nil, errors.New("FakeClient.Stage not implemented") 47 }