Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add mock snippet operations API for tests [PLE-2575] #179

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions storage/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ func (s *GoogleCloudStorageClient) UploadDir(bucket, src, dst string) error {
if file.IsDir() {
err = s.UploadDir(bucket, filepath.Join(src, file.Name()), filepath.Join(dst, file.Name()))
if err != nil {
log.Println(err)
return err
}
} else {
err = s.UploadObject(bucket, filepath.Join(src, file.Name()), filepath.Join(dst, file.Name()))
if err != nil {
log.Println(err)
return err
}
}
Expand All @@ -50,6 +52,7 @@ func (s *GoogleCloudStorageClient) UploadDir(bucket, src, dst string) error {
func (s *GoogleCloudStorageClient) UploadObject(bucket, src, dst string) (err error) {
file, err := os.ReadFile(src)
if err != nil {
log.Println(err)
return err
}

Expand Down
42 changes: 42 additions & 0 deletions storage/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package storage

import (
"context"
"io"
"log"

"cloud.google.com/go/storage"
"google.golang.org/api/option"
)

type MockCloudStorageClient struct {
client *storage.Client
}

func NewMockCloudStorageClient(ctx context.Context, credentialsJSON []byte) (*MockCloudStorageClient, error) {
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(credentialsJSON))
if err != nil {
return nil, err
}
return &MockCloudStorageClient{client}, nil
}

func (*MockCloudStorageClient) UploadDir(_, _, _ string) error {
log.Println("not uploading object to upstream since the client is being run in tests")
return nil
}

func (*MockCloudStorageClient) UploadObject(_, _, _ string) (err error) {
log.Println("not uploading object to upstream since the client is being run in tests")
return
}

func (*MockCloudStorageClient) GetObjects(_ string, _ string, _ ...string) error {
log.Println("mock GetObjects() called, returning successful response")
return nil
}

func (s *MockCloudStorageClient) NewReader(ctx context.Context, bucket string, path string) (io.ReadCloser, error) {
obj := s.client.Bucket(bucket).Object(path)
return obj.NewReader(ctx)
}
2 changes: 2 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func NewStorageClient(ctx context.Context, storageType string, credentials []byt
return NewGoogleCloudStorageClient(ctx, credentials)
case "s3":
return NewS3StorageClient(ctx, credentials)
case "test":
return NewMockCloudStorageClient(ctx, credentials)
default:
return &GoogleCloudStorageClient{}, fmt.Errorf("expected storageType to be 'gcs' or 's3'. Received %s", storageType)
}
Expand Down