Skip to content

Commit e6e1faf

Browse files
Added a few service client tests (Azure#19924)
* Added a few service client tests and some changes to testcommon * Fixed type of arg * Addressed comments from PR * Modified some tests and uploaded some assets * make sas unrecorded * fix ineffassign * Removing deprecated import * Ignoring unused lint --------- Co-authored-by: siminsavani-msft <77068571+siminsavani-msft@users.noreply.github.com>
1 parent a47082f commit e6e1faf

File tree

9 files changed

+176
-57
lines changed

9 files changed

+176
-57
lines changed

sdk/storage/azblob/appendblob/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func (s *AppendBlobUnrecordedTestsSuite) TestAppendBlockFromURL() {
263263
// Get source abClient URL with SAS for AppendBlockFromURL.
264264
srcBlobParts, _ := blob.ParseURL(srcBlob.URL())
265265

266-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
266+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
267267
_require.Nil(err)
268268
perms := sas.BlobPermissions{Read: true}
269269

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

348-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
348+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
349349
_require.Nil(err)
350350
perms := sas.BlobPermissions{Read: true}
351351

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

1766-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
1766+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
17671767
_require.Nil(err)
17681768

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

1875-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
1875+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
18761876
_require.Nil(err)
18771877

18781878
// Set Account SAS

sdk/storage/azblob/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "go",
44
"TagPrefix": "go/storage/azblob",
5-
"Tag": "go/storage/azblob_46e572d43a"
5+
"Tag": "go/storage/azblob_658d0bdec9"
66
}

sdk/storage/azblob/blob/client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (s *BlobUnrecordedTestsSuite) TestCreateBlobClientWithSnapshotAndSAS() {
111111
currentTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
112112
_require.Nil(err)
113113

114-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
114+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
115115
_require.Nil(err)
116116

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

155-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
155+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
156156
_require.Nil(err)
157157
sasQueryParams, err := sas.AccountSignatureValues{
158158
Protocol: sas.ProtocolHTTPS,
@@ -418,7 +418,7 @@ func (s *BlobRecordedTestsSuite) TestBlobStartCopySourcePrivate() {
418418
// bbClient := testcommon.CreateNewBlockBlob(context.Background(), _require, blockBlobName, containerClient)
419419
//
420420
// // Create sas values for the source blob
421-
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
421+
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
422422
// if err != nil {
423423
// s.T().Fatal("Couldn't fetch credential because " + err.Error())
424424
// }
@@ -3022,7 +3022,7 @@ func (s *BlobRecordedTestsSuite) TestPermanentDelete() {
30223022
parts, err := sas.ParseURL(bbClient.URL()) // Get parts for BlobURL
30233023
_require.Nil(err)
30243024

3025-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
3025+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
30263026
_require.Nil(err)
30273027

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

3135-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
3135+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
31363136
_require.Nil(err)
31373137

31383138
// Set Account SAS

sdk/storage/azblob/blob/retry_reader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ package blob
88

99
import (
1010
"context"
11+
"crypto/rand"
1112
"errors"
1213
"fmt"
1314
"io"
14-
"math/rand"
1515
"net"
1616
"net/http"
1717
"testing"

sdk/storage/azblob/blockblob/client_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type BlockBlobUnrecordedTestsSuite struct {
190190
// // Get source blob url with SAS for StageFromURL.
191191
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
192192
//
193-
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
193+
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
194194
// _require.Nil(err)
195195
//
196196
// srcBlobParts.SAS, err = BlobSASSignatureValues{
@@ -287,7 +287,7 @@ type BlockBlobUnrecordedTestsSuite struct {
287287
// // Get source blob url with SAS for StageFromURL.
288288
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
289289
//
290-
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
290+
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
291291
// _require.Nil(err)
292292
//
293293
// srcBlobParts.SAS, err = BlobSASSignatureValues{
@@ -387,7 +387,7 @@ type BlockBlobUnrecordedTestsSuite struct {
387387
// contentLanguageVal := "content-language-override"
388388
// contentTypeVal := "content-type-override"
389389
//
390-
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
390+
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
391391
// _require.Nil(err)
392392
// // Append User Delegation SAS token to URL
393393
// blobParts.SAS, err = BlobSASSignatureValues{
@@ -1308,7 +1308,7 @@ func (s *BlockBlobUnrecordedTestsSuite) TestSetTierOnCopyBlockBlobFromURL() {
13081308
expiryTime, err := time.Parse(time.UnixDate, "Fri Jun 11 20:00:00 UTC 2049")
13091309
_require.Nil(err)
13101310

1311-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
1311+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
13121312
if err != nil {
13131313
s.T().Fatal("Couldn't fetch credential because " + err.Error())
13141314
}
@@ -1369,7 +1369,7 @@ func (s *BlockBlobUnrecordedTestsSuite) TestSetTierOnCopyBlockBlobFromURL() {
13691369
//
13701370
// // Get source blob url with SAS for StageFromURL.
13711371
// srcBlobParts, _ := NewBlobURLParts(srcBlob.URL())
1372-
// credential, err := testcommon.GetGenericCredential(nil, testcommon.TestAccountDefault)
1372+
// credential, err := testcommon.GetGenericSharedKeyCredential(nil, testcommon.TestAccountDefault)
13731373
// _require.Nil(err)
13741374
// srcBlobParts.SAS, err = blob.SASSignatureValues{
13751375
// Protocol: SASProtocolHTTPS, // Users MUST use HTTPS (not HTTP)

sdk/storage/azblob/internal/testcommon/clients_auth.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var SpecialCharBlobTagsMap = map[string]string{
8686
"GO ": ".Net",
8787
}
8888

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

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

102-
setClientOptions(t, &options.ClientOptions)
102+
SetClientOptions(t, &options.ClientOptions)
103103

104-
cred, err := GetGenericCredential(accountType)
104+
cred, err := GetGenericSharedKeyCredential(accountType)
105105
if err != nil {
106106
return nil, err
107107
}
@@ -116,9 +116,9 @@ func GetServiceClient(t *testing.T, accountType TestAccountType, options *servic
116116
options = &service.ClientOptions{}
117117
}
118118

119-
setClientOptions(t, &options.ClientOptions)
119+
SetClientOptions(t, &options.ClientOptions)
120120

121-
cred, err := GetGenericCredential(accountType)
121+
cred, err := GetGenericSharedKeyCredential(accountType)
122122
if err != nil {
123123
return nil, err
124124
}
@@ -128,48 +128,62 @@ func GetServiceClient(t *testing.T, accountType TestAccountType, options *servic
128128
return serviceClient, err
129129
}
130130

131-
func GetAccountInfo(accountType TestAccountType) (string, string) {
131+
func GetServiceClientNoCredential(t *testing.T, sasUrl string, options *service.ClientOptions) (*service.Client, error) {
132+
if options == nil {
133+
options = &service.ClientOptions{}
134+
}
135+
136+
SetClientOptions(t, &options.ClientOptions)
137+
138+
serviceClient, err := service.NewClientWithNoCredential(sasUrl, options)
139+
140+
return serviceClient, err
141+
}
142+
143+
func GetGenericAccountInfo(accountType TestAccountType) (string, string) {
144+
if recording.GetRecordMode() == recording.PlaybackMode {
145+
return FakeStorageAccount, "ZmFrZQ=="
146+
}
132147
accountNameEnvVar := string(accountType) + AccountNameEnvVar
133148
accountKeyEnvVar := string(accountType) + AccountKeyEnvVar
134149
accountName, _ := GetRequiredEnv(accountNameEnvVar)
135150
accountKey, _ := GetRequiredEnv(accountKeyEnvVar)
136151
return accountName, accountKey
137152
}
138153

139-
func GetGenericCredential(accountType TestAccountType) (*azblob.SharedKeyCredential, error) {
140-
if recording.GetRecordMode() == recording.PlaybackMode {
141-
return azblob.NewSharedKeyCredential(FakeStorageAccount, "ZmFrZQ==")
142-
}
143-
144-
accountName, accountKey := GetAccountInfo(accountType)
154+
func GetGenericSharedKeyCredential(accountType TestAccountType) (*azblob.SharedKeyCredential, error) {
155+
accountName, accountKey := GetGenericAccountInfo(accountType)
145156
if accountName == "" || accountKey == "" {
146157
return nil, errors.New(string(accountType) + AccountNameEnvVar + " and/or " + string(accountType) + AccountKeyEnvVar + " environment variables not specified.")
147158
}
148159
return azblob.NewSharedKeyCredential(accountName, accountKey)
149160
}
150161

151-
func GetConnectionString(accountType TestAccountType) string {
152-
accountName, accountKey := GetAccountInfo(accountType)
162+
func GetGenericConnectionString(accountType TestAccountType) (*string, error) {
163+
accountName, accountKey := GetGenericAccountInfo(accountType)
164+
if accountName == "" || accountKey == "" {
165+
return nil, errors.New(string(accountType) + AccountNameEnvVar + " and/or " + string(accountType) + AccountKeyEnvVar + " environment variables not specified.")
166+
}
153167
connectionString := fmt.Sprintf("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net/",
154168
accountName, accountKey)
155-
return connectionString
169+
return &connectionString, nil
156170
}
157171

158172
func GetServiceClientFromConnectionString(t *testing.T, accountType TestAccountType, options *service.ClientOptions) (*service.Client, error) {
159173
if options == nil {
160174
options = &service.ClientOptions{}
161175
}
176+
SetClientOptions(t, &options.ClientOptions)
162177

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

167-
if recording.GetRecordMode() == recording.PlaybackMode {
168-
return service.NewClientWithNoCredential(FakeStorageURL, options)
182+
cred, err := GetGenericConnectionString(accountType)
183+
if err != nil {
184+
return nil, err
169185
}
170-
171-
connectionString := GetConnectionString(accountType)
172-
svcClient, err := service.NewClientFromConnectionString(connectionString, options)
186+
svcClient, err := service.NewClientFromConnectionString(*cred, options)
173187
return svcClient, err
174188
}
175189

sdk/storage/azblob/pageblob/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,7 +4029,7 @@ func (s *PageBlobRecordedTestsSuite) TestPageBlockPermanentDelete() {
40294029
parts, err := sas.ParseURL(pbClient.URL()) // Get parts for BlobURL
40304030
_require.Nil(err)
40314031

4032-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
4032+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
40334033
_require.Nil(err)
40344034

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

4149-
credential, err := testcommon.GetGenericCredential(testcommon.TestAccountDefault)
4149+
credential, err := testcommon.GetGenericSharedKeyCredential(testcommon.TestAccountDefault)
41504150
_require.Nil(err)
41514151

41524152
// Set Account SAS

sdk/storage/azblob/service/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// ClientOptions contains the optional parameters when creating a Client.
2929
type ClientOptions struct {
30-
azcore.ClientOptions
30+
azcore.ClientOptions //nolint:unused
3131
}
3232

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

0 commit comments

Comments
 (0)