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 066b3b1 commit a6d453f
Show file tree
Hide file tree
Showing 4 changed files with 488 additions and 8 deletions.
20 changes: 12 additions & 8 deletions internal/provider/blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/openshift-pipelines/tekton-caches/internal/tar"
"gocloud.dev/blob"

// Adding the driver for gcs
// Adding the driver for gcs.
_ "gocloud.dev/blob/gcsblob"
// Adding the driver for s3
// Adding the driver for s3.
_ "gocloud.dev/blob/s3blob"
// If we want to add azure blob storage, we can use this import
// _ "gocloud.dev/blob/azureblob"
// If we want to add azure blob storage, we can use this import.
// _ "gocloud.dev/blob/azureblob" .
)

const (
Expand All @@ -24,6 +24,10 @@ const (

var (
queryParams string
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 init() {

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

View workflow job for this annotation

GitHub Actions / go

don't use `init` function (gochecknoinits)
Expand All @@ -39,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, clean, err := openBucket(ctx, url.String())
if err != nil {
log.Printf("error opening bucket: %s", err)
return err
}
defer bucket.Close()
defer clean()

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 @@ -79,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, clean, err := openBucket(ctx, url.String())
if err != nil {
return err
}
defer bucket.Close()
defer clean()

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"), 0o644); 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(_ context.Context, _ string) (*blob.Bucket, func() error, error) {
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 a6d453f

Please sign in to comment.