commit
e830fa4116
|
@ -19,11 +19,6 @@ type GSBackend struct {
|
|||
}
|
||||
|
||||
func NewGSReader(path string) (*GSBackend, error) {
|
||||
storageClient, err := storage.NewClient(context.Background())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bucketPath := strings.Split(path, "/")
|
||||
if len(bucketPath) < 2 {
|
||||
return nil, errors.Errorf("Unable to parse Google Storage path: %s. Must be BUCKET_NAME/PATH/TO/OBJECT", path)
|
||||
|
@ -32,14 +27,21 @@ func NewGSReader(path string) (*GSBackend, error) {
|
|||
key := strings.Join(bucketPath[1:], "/")
|
||||
|
||||
return &GSBackend{
|
||||
bucketName: bucketName,
|
||||
path: key,
|
||||
storageClient: storageClient,
|
||||
bucketName: bucketName,
|
||||
path: key,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *GSBackend) Read(p []byte) (int, error) {
|
||||
if s.reader == nil {
|
||||
if s.storageClient == nil {
|
||||
client, err := storage.NewClient(context.Background())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
s.storageClient = client
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
rc, err := s.storageClient.Bucket(s.bucketName).Object(s.path).NewReader(ctx)
|
||||
if err != nil {
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
|
@ -14,7 +13,7 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGSBackendInvalid(t *testing.T) {
|
||||
func TestGSBackend_NewGSReader(t *testing.T) {
|
||||
type args struct {
|
||||
path string
|
||||
}
|
||||
|
@ -24,6 +23,16 @@ func TestGSBackendInvalid(t *testing.T) {
|
|||
want *GSBackend
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "valid path",
|
||||
args: args{
|
||||
path: "bucket-1/path/to/terraform.tfstate",
|
||||
},
|
||||
want: &GSBackend{
|
||||
bucketName: "bucket-1",
|
||||
path: "path/to/terraform.tfstate",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid path",
|
||||
args: args{
|
||||
|
@ -35,12 +44,12 @@ func TestGSBackendInvalid(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_ = os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", "testdata/gcp_application_default_credentials.json")
|
||||
|
||||
got, err := NewGSReader(tt.args.path)
|
||||
if err.Error() != tt.wantErr.Error() {
|
||||
t.Errorf("NewGSReader() error = '%s', wantErr '%s'", err, tt.wantErr)
|
||||
if tt.wantErr != nil {
|
||||
assert.EqualError(t, err, tt.wantErr.Error())
|
||||
return
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewGSReader() got = %v, want %v", got, tt.want)
|
||||
|
@ -122,21 +131,23 @@ func TestGSBackend_Read(t *testing.T) {
|
|||
func TestGSBackend_Close(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
reader io.ReadCloser
|
||||
reader *MockReaderMock
|
||||
client *storage.Client
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "should fail to close reader",
|
||||
reader: func() io.ReadCloser {
|
||||
return nil
|
||||
reader: func() *MockReaderMock {
|
||||
m := &MockReaderMock{}
|
||||
m.On("Close").Return(errors.New("dummy error"))
|
||||
return m
|
||||
}(),
|
||||
client: &storage.Client{},
|
||||
wantErr: errors.New("Unable to close reader as nothing was opened"),
|
||||
wantErr: errors.New("dummy error"),
|
||||
},
|
||||
{
|
||||
name: "should close reader",
|
||||
reader: func() io.ReadCloser {
|
||||
reader: func() *MockReaderMock {
|
||||
m := &MockReaderMock{}
|
||||
m.On("Close").Return(nil)
|
||||
return m
|
||||
|
@ -156,6 +167,8 @@ func TestGSBackend_Close(t *testing.T) {
|
|||
} else {
|
||||
assert.EqualError(t, err, tt.wantErr.Error())
|
||||
}
|
||||
|
||||
tt.reader.AssertExpectations(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
{
|
||||
"client_id": "testdata.driftctl.apps.googleusercontent.com",
|
||||
"client_secret": "fake-secret",
|
||||
"quota_project_id": "fake-project-id",
|
||||
"refresh_token": "fake-token",
|
||||
"type": "authorized_user"
|
||||
}
|
Loading…
Reference in New Issue