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

Added a few service client tests #19924

Merged
merged 10 commits into from
Feb 10, 2023
8 changes: 4 additions & 4 deletions sdk/storage/azblob/appendblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (s *AppendBlobUnrecordedTestsSuite) TestAppendBlockFromURL() {
// Get source abClient URL with SAS for AppendBlockFromURL.
srcBlobParts, _ := blob.ParseURL(srcBlob.URL())

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)
perms := sas.BlobPermissions{Read: true}

Expand Down Expand Up @@ -345,7 +345,7 @@ func (s *AppendBlobUnrecordedTestsSuite) TestAppendBlockFromURLWithMD5() {
// Get source abClient URL with SAS for AppendBlockFromURL.
srcBlobParts, _ := blob.ParseURL(srcBlob.URL())

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)
perms := sas.BlobPermissions{Read: true}

Expand Down Expand Up @@ -1763,7 +1763,7 @@ func (s *AppendBlobRecordedTestsSuite) TestAppendBlockPermanentDelete() {
parts, err := sas.ParseURL(abClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS and set Permanent Delete to true
Expand Down Expand Up @@ -1872,7 +1872,7 @@ func (s *AppendBlobRecordedTestsSuite) TestAppendBlockPermanentDeleteWithoutPerm
parts, err := sas.ParseURL(abClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azblob/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "go",
"TagPrefix": "go/storage/azblob",
"Tag": "go/storage/azblob_46e572d43a"
"Tag": "go/storage/azblob_658d0bdec9"
}
10 changes: 5 additions & 5 deletions sdk/storage/azblob/blob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (s *BlobUnrecordedTestsSuite) TestCreateBlobClientWithSnapshotAndSAS() {
currentTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

sasQueryParams, err := sas.AccountSignatureValues{
Expand Down Expand Up @@ -152,7 +152,7 @@ func (s *BlobUnrecordedTestsSuite) TestCreateBlobClientWithSnapshotAndSASUsingCo
currentTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)
sasQueryParams, err := sas.AccountSignatureValues{
Protocol: sas.ProtocolHTTPS,
Expand Down Expand Up @@ -418,7 +418,7 @@ func (s *BlobRecordedTestsSuite) TestBlobStartCopySourcePrivate() {
// bbClient := testcommon.CreateNewBlockBlob(context.Background(), _require, blockBlobName, containerClient)
//
// // Create sas values for the source blob
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
// if err != nil {
// s.T().Fatal("Couldn't fetch credential because " + err.Error())
// }
Expand Down Expand Up @@ -3022,7 +3022,7 @@ func (s *BlobRecordedTestsSuite) TestPermanentDelete() {
parts, err := sas.ParseURL(bbClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS and set Permanent Delete to true
Expand Down Expand Up @@ -3132,7 +3132,7 @@ func (s *BlobRecordedTestsSuite) TestPermanentDeleteWithoutPermission() {
parts, err := sas.ParseURL(bbClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azblob/blob/retry_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ package blob

import (
"context"
"crypto/rand"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"testing"
Expand Down
10 changes: 5 additions & 5 deletions sdk/storage/azblob/blockblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ type BlockBlobUnrecordedTestsSuite struct {
// // Get source blob url with SAS for StageFromURL.
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
//
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
// _require.Nil(err)
//
// srcBlobParts.SAS, err = BlobSASSignatureValues{
Expand Down Expand Up @@ -287,7 +287,7 @@ type BlockBlobUnrecordedTestsSuite struct {
// // Get source blob url with SAS for StageFromURL.
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
//
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
// _require.Nil(err)
//
// srcBlobParts.SAS, err = BlobSASSignatureValues{
Expand Down Expand Up @@ -387,7 +387,7 @@ type BlockBlobUnrecordedTestsSuite struct {
// contentLanguageVal := "content-language-override"
// contentTypeVal := "content-type-override"
//
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
// _require.Nil(err)
// // Append User Delegation SAS token to URL
// blobParts.SAS, err = BlobSASSignatureValues{
Expand Down Expand Up @@ -1308,7 +1308,7 @@ func (s *BlockBlobUnrecordedTestsSuite) TestSetTierOnCopyBlockBlobFromURL() {
expiryTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
if err != nil {
s.T().Fatal("Couldn't fetch credential because " + err.Error())
}
Expand Down Expand Up @@ -1369,7 +1369,7 @@ func (s *BlockBlobUnrecordedTestsSuite) TestSetTierOnCopyBlockBlobFromURL() {
//
// // Get source blob url with SAS for StageFromURL.
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
// _require.Nil(err)
// srcBlobParts.SAS, err = blob.SASSignatureValues{
// Protocol: SASProtocolHTTPS, // Users MUST use HTTPS (not HTTP)
Expand Down
54 changes: 34 additions & 20 deletions sdk/storage/azblob/internal/testcommon/clients_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var SpecialCharBlobTagsMap = map[string]string{
"GO ": ".Net",
}

func setClientOptions(t *testing.T, opts *azcore.ClientOptions) {
func SetClientOptions(t *testing.T, opts *azcore.ClientOptions) {
opts.Logging.AllowedHeaders = append(opts.Logging.AllowedHeaders, "X-Request-Mismatch", "X-Request-Mismatch-Error")

transport, err := recording.NewRecordingHTTPClient(t, nil)
Expand All @@ -99,9 +99,9 @@ func GetClient(t *testing.T, accountType TestAccountType, options *azblob.Client
options = &azblob.ClientOptions{}
}

setClientOptions(t, &options.ClientOptions)
SetClientOptions(t, &options.ClientOptions)

cred, err := GetGenericCredential(accountType)
cred, err := GetGenericSharedKeyCredential(accountType)
if err != nil {
return nil, err
}
Expand All @@ -116,9 +116,9 @@ func GetServiceClient(t *testing.T, accountType TestAccountType, options *servic
options = &service.ClientOptions{}
}

setClientOptions(t, &options.ClientOptions)
SetClientOptions(t, &options.ClientOptions)

cred, err := GetGenericCredential(accountType)
cred, err := GetGenericSharedKeyCredential(accountType)
if err != nil {
return nil, err
}
Expand All @@ -128,48 +128,62 @@ func GetServiceClient(t *testing.T, accountType TestAccountType, options *servic
return serviceClient, err
}

func GetAccountInfo(accountType TestAccountType) (string, string) {
func GetServiceClientNoCredential(t *testing.T, sasUrl string, options *service.ClientOptions) (*service.Client, error) {
if options == nil {
options = &service.ClientOptions{}
}

SetClientOptions(t, &options.ClientOptions)

serviceClient, err := service.NewClientWithNoCredential(sasUrl, options)

return serviceClient, err
}

func GetGenericAccountInfo(accountType TestAccountType) (string, string) {
if recording.GetRecordMode() == recording.PlaybackMode {
return FakeStorageAccount, "ZmFrZQ=="
gapra-msft marked this conversation as resolved.
Show resolved Hide resolved
}
accountNameEnvVar := string(accountType) + AccountNameEnvVar
accountKeyEnvVar := string(accountType) + AccountKeyEnvVar
accountName, _ := GetRequiredEnv(accountNameEnvVar)
accountKey, _ := GetRequiredEnv(accountKeyEnvVar)
return accountName, accountKey
}

func GetGenericCredential(accountType TestAccountType) (*azblob.SharedKeyCredential, error) {
if recording.GetRecordMode() == recording.PlaybackMode {
return azblob.NewSharedKeyCredential(FakeStorageAccount, "ZmFrZQ==")
}

accountName, accountKey := GetAccountInfo(accountType)
func GetGenericSharedKeyCredential(accountType TestAccountType) (*azblob.SharedKeyCredential, error) {
accountName, accountKey := GetGenericAccountInfo(accountType)
if accountName == "" || accountKey == "" {
return nil, errors.New(string(accountType) + AccountNameEnvVar + " and/or " + string(accountType) + AccountKeyEnvVar + " environment variables not specified.")
}
return azblob.NewSharedKeyCredential(accountName, accountKey)
}

func GetConnectionString(accountType TestAccountType) string {
accountName, accountKey := GetAccountInfo(accountType)
func GetGenericConnectionString(accountType TestAccountType) (*string, error) {
accountName, accountKey := GetGenericAccountInfo(accountType)
if accountName == "" || accountKey == "" {
return nil, errors.New(string(accountType) + AccountNameEnvVar + " and/or " + string(accountType) + AccountKeyEnvVar + " environment variables not specified.")
}
connectionString := fmt.Sprintf("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net/",
accountName, accountKey)
return connectionString
return &connectionString, nil
}

func GetServiceClientFromConnectionString(t *testing.T, accountType TestAccountType, options *service.ClientOptions) (*service.Client, error) {
if options == nil {
options = &service.ClientOptions{}
}
SetClientOptions(t, &options.ClientOptions)

transport, err := recording.NewRecordingHTTPClient(t, nil)
require.NoError(t, err)
options.Transport = transport

if recording.GetRecordMode() == recording.PlaybackMode {
return service.NewClientWithNoCredential(FakeStorageURL, options)
cred, err := GetGenericConnectionString(accountType)
if err != nil {
return nil, err
}

connectionString := GetConnectionString(accountType)
svcClient, err := service.NewClientFromConnectionString(connectionString, options)
svcClient, err := service.NewClientFromConnectionString(*cred, options)
return svcClient, err
}

Expand Down
4 changes: 2 additions & 2 deletions sdk/storage/azblob/pageblob/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4029,7 +4029,7 @@ func (s *PageBlobRecordedTestsSuite) TestPageBlockPermanentDelete() {
parts, err := sas.ParseURL(pbClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS and set Permanent Delete to true
Expand Down Expand Up @@ -4146,7 +4146,7 @@ func (s *PageBlobRecordedTestsSuite) TestPageBlockPermanentDeleteWithoutPermissi
parts, err := sas.ParseURL(pbClient.URL()) // Get parts for BlobURL
_require.Nil(err)

credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
_require.Nil(err)

// Set Account SAS
Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azblob/service/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// ClientOptions contains the optional parameters when creating a Client.
type ClientOptions struct {
azcore.ClientOptions
azcore.ClientOptions //nolint:unused
}

// Client represents a URL to the Azure Blob Storage service allowing you to manipulate blob containers.
Expand Down
Loading