Skip to content

Commit

Permalink
Add Unit Test for blob
Browse files Browse the repository at this point in the history
  • Loading branch information
khrm committed Oct 7, 2024
1 parent 4cf0e53 commit 451bd58
Show file tree
Hide file tree
Showing 4 changed files with 487 additions and 4 deletions.
15 changes: 11 additions & 4 deletions internal/provider/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ func init() {
queryParams = os.Getenv("BLOB_QUERY_PARAMS")
}

var (
openBucket = func(ctx context.Context, urlString string) (*blob.Bucket, func() error, error) {
bucket, err := blob.OpenBucket(ctx, urlString+queryParams)
return bucket, bucket.Close, err
}
)

func Fetch(ctx context.Context, url url.URL, folder string) error {
log.Printf("Downloading cache from %s to %s", url.String(), folder)
file, err := os.CreateTemp("", cacheFile)
Expand All @@ -36,12 +43,12 @@ func Fetch(ctx context.Context, url url.URL, folder string) error {
}
defer os.Remove(file.Name())

bucket, err := blob.OpenBucket(ctx, url.String()+queryParams)
bucket, close, err := openBucket(ctx, url.String())

Check failure on line 46 in internal/provider/blob/blob.go

View workflow job for this annotation

GitHub Actions / go

redefines-builtin-id: redefinition of the built-in function close (revive)
if err != nil {
log.Printf("error opening bucket: %s", err)
return err
}
defer bucket.Close()
defer close()

Check failure on line 51 in internal/provider/blob/blob.go

View workflow job for this annotation

GitHub Actions / go

Error return value is not checked (errcheck)

rc, err := bucket.NewReader(ctx, url.Path[1:], nil)
if err != nil {
Expand Down Expand Up @@ -76,11 +83,11 @@ func Upload(ctx context.Context, url url.URL, folder string) error {
return err
}

bucket, err := blob.OpenBucket(ctx, url.String()+queryParams)
bucket, close, err := openBucket(ctx, url.String())

Check failure on line 86 in internal/provider/blob/blob.go

View workflow job for this annotation

GitHub Actions / go

redefines-builtin-id: redefinition of the built-in function close (revive)
if err != nil {
return err
}
defer bucket.Close()
defer close()

Check failure on line 90 in internal/provider/blob/blob.go

View workflow job for this annotation

GitHub Actions / go

Error return value is not checked (errcheck)

writer, err := bucket.NewWriter(ctx, url.Path[1:], nil)
if err != nil {
Expand Down
79 changes: 79 additions & 0 deletions internal/provider/blob/blob_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package blob

import (
"context"
"net/url"
"os"
"path/filepath"
"testing"

"gocloud.dev/blob"
"gocloud.dev/blob/memblob"
)

func TestFetchAndUpload(t *testing.T) {
// Create a temporary directory for the test
tempDir, err := os.MkdirTemp("", "blob_test")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)

// Create a test file
testFile := filepath.Join(tempDir, "test.txt")
if err := os.WriteFile(testFile, []byte("test content"), 0644); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}

// Create a mock bucket
bucket := memblob.OpenBucket(nil)
defer bucket.Close()

// Override the openBucket function for testing
originalOpenBucket := openBucket
defer func() { openBucket = originalOpenBucket }()
openBucket = func(ctx context.Context, urlString string) (*blob.Bucket, func() error, error) {

Check failure on line 35 in internal/provider/blob/blob_test.go

View workflow job for this annotation

GitHub Actions / go

unused-parameter: parameter 'ctx' seems to be unused, consider removing or renaming it as _ (revive)
return bucket, func() error { return nil }, nil
}

ctx := context.Background()
testURL, _ := url.Parse("mem://test-bucket/test-object")

// Test Upload
if err := Upload(ctx, *testURL, tempDir); err != nil {
t.Fatalf("Upload failed: %v", err)
}

// Verify the uploaded content
data, err := bucket.ReadAll(ctx, "test-object")
if err != nil {
t.Fatalf("Failed to read uploaded data: %v", err)
}
if len(data) == 0 {
t.Errorf("Uploaded data is empty")
}

// Clean up the temp directory
os.RemoveAll(tempDir)

// Create a new temp directory for Fetch
tempDir, err = os.MkdirTemp("", "blob_test_fetch")
if err != nil {
t.Fatalf("Failed to create temp dir for fetch: %v", err)
}
defer os.RemoveAll(tempDir)

// Test Fetch
if err := Fetch(ctx, *testURL, tempDir); err != nil {
t.Fatalf("Fetch failed: %v", err)
}

// Verify the fetched content
fetchedFiles, err := os.ReadDir(tempDir)
if err != nil {
t.Fatalf("Failed to read fetched directory: %v", err)
}
if len(fetchedFiles) == 0 {
t.Errorf("No files were fetched")
}
}
Loading

0 comments on commit 451bd58

Please sign in to comment.