Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Leavrth <[email protected]>
  • Loading branch information
Leavrth authored and ti-chi-bot committed Dec 9, 2022
1 parent 92e2558 commit 1969647
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions br/pkg/storage/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ go_library(
"@com_github_aws_aws_sdk_go//service/s3",
"@com_github_aws_aws_sdk_go//service/s3/s3iface",
"@com_github_aws_aws_sdk_go//service/s3/s3manager",
"@com_github_azure_azure_sdk_for_go_sdk_azcore//policy",
"@com_github_azure_azure_sdk_for_go_sdk_azidentity//:azidentity",
"@com_github_azure_azure_sdk_for_go_sdk_storage_azblob//:azblob",
"@com_github_golang_snappy//:snappy",
Expand Down
53 changes: 53 additions & 0 deletions br/pkg/storage/azblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ package storage

import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"sync"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
Expand Down Expand Up @@ -298,3 +302,52 @@ func TestNewAzblobStorage(t *testing.T) {
require.Equal(t, "http://127.0.0.1:1000", b.serviceURL)
}
}

type fakeClientBuilder struct {
Endpoint string
}

func (b *fakeClientBuilder) GetServiceClient() (azblob.ServiceClient, error) {
connStr := fmt.Sprintf("DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;BlobEndpoint=%s/devstoreaccount1;", b.Endpoint)
return azblob.NewServiceClientFromConnectionString(connStr, getDefaultClientOptions())
}

func (b *fakeClientBuilder) GetAccountName() string {
return "devstoreaccount1"
}

func TestDownloadRetry(t *testing.T) {
var count int32 = 0
var lock sync.Mutex
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Log(r.URL)
if strings.Contains(r.URL.String(), "restype=container") {
w.WriteHeader(201)
return
}
lock.Lock()
count += 1
lock.Unlock()
header := w.Header()
header.Add("Etag", "0x1")
header.Add("Content-Length", "5")
w.WriteHeader(200)
w.Write([]byte("1234567"))
}))

defer server.Close()
t.Log(server.URL)

options := &backuppb.AzureBlobStorage{
Bucket: "test",
Prefix: "a/b/",
}

ctx := context.Background()
builder := &fakeClientBuilder{Endpoint: server.URL}
s, err := newAzureBlobStorageWithClientBuilder(ctx, options, builder)
require.NoError(t, err)
_, err = s.ReadFile(ctx, "c")
require.Error(t, err)
require.Less(t, azblobRetryTimes, count)
}

0 comments on commit 1969647

Please sign in to comment.