| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package azureblob |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"encoding/base64" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
"net/http" |
| 23 |
"net/url" |
| 24 |
"os" |
| 25 |
"strings" |
| 26 |
"testing" |
| 27 |
|
| 28 |
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" |
| 29 |
azblobblob "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob" |
| 30 |
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container" |
| 31 |
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/sas" |
| 32 |
|
| 33 |
"github.com/google/go-cmp/cmp" |
| 34 |
"gocloud.dev/blob" |
| 35 |
"gocloud.dev/blob/driver" |
| 36 |
"gocloud.dev/blob/drivertest" |
| 37 |
"gocloud.dev/internal/testing/setup" |
| 38 |
) |
| 39 |
|
| 40 |
// Prerequisites for -record mode |
| 41 |
// 1. Sign-in to your Azure Subscription at http://portal.azure.com. |
| 42 |
// |
| 43 |
// 2. Create a Storage Account. |
| 44 |
// |
| 45 |
// 3. Locate the Access Key (Primary or Secondary) under your Storage Account > Settings > Access Keys. |
| 46 |
// |
| 47 |
// 4. Set the environment variables AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_KEY to |
| 48 |
// the storage account name and your access key. |
| 49 |
// |
| 50 |
// 5. Create a container in your Storage Account > Blob. Update the bucketName |
| 51 |
// constant to your container name. |
| 52 |
// |
| 53 |
// Here is a step-by-step walkthrough using the Azure Portal |
| 54 |
// https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-portal |
| 55 |
// |
| 56 |
// 5. Run the tests with -record. |
| 57 |
|
| 58 |
const ( |
| 59 |
bucketName = "go-cloud-bucket" |
| 60 |
accountName = "gocloudblobtests" |
| 61 |
) |
| 62 |
|
| 63 |
type harness struct { |
| 64 |
clientFn func(bucketName string) (*container.Client, error) |
| 65 |
closer func() |
| 66 |
httpClient *http.Client |
| 67 |
} |
| 68 |
|
| 69 |
func newHarness(ctx context.Context, t *testing.T) (drivertest.Harness, error) { |
| 70 |
t.Helper() |
| 71 |
|
| 72 |
var key string |
| 73 |
if *setup.Record { |
| 74 |
name := os.Getenv("AZURE_STORAGE_ACCOUNT") |
| 75 |
if name != accountName { |
| 76 |
t.Fatalf("Please update the accountName constant to match your settings file so future records work (%q vs %q)", name, accountName) |
| 77 |
} |
| 78 |
key = os.Getenv("AZURE_STORAGE_KEY") |
| 79 |
} else { |
| 80 |
// In replay mode, we use fake credentials. |
| 81 |
key = base64.StdEncoding.EncodeToString([]byte("FAKECREDS")) |
| 82 |
} |
| 83 |
credential, err := azblob.NewSharedKeyCredential(accountName, key) |
| 84 |
if err != nil { |
| 85 |
return nil, err |
| 86 |
} |
| 87 |
httpClient, done := setup.NewAzureTestBlobClient(ctx, t) |
| 88 |
// Hack to work around the fact that SignedURLs for PUTs are not fully |
| 89 |
// portable; they require a "x-ms-blob-type" header. Intercept all |
| 90 |
// requests, and insert that header where needed. |
| 91 |
httpClient.Transport = &requestInterceptor{httpClient.Transport} |
| 92 |
clientOptions := container.ClientOptions{} |
| 93 |
clientOptions.Transport = httpClient |
| 94 |
serviceURL := fmt.Sprintf("https://%s.blob.core.windows.net", accountName) |
| 95 |
clientFn := func(bucketName string) (*container.Client, error) { |
| 96 |
return container.NewClientWithSharedKeyCredential(serviceURL+"/"+bucketName, credential, &clientOptions) |
| 97 |
} |
| 98 |
return &harness{clientFn: clientFn, closer: done, httpClient: httpClient}, nil |
| 99 |
} |
| 100 |
|
| 101 |
// requestInterceptor implements a hack for the lack of portability for |
| 102 |
// SignedURLs for PUT. It adds the required "x-ms-blob-type" header where |
| 103 |
// Azure requires it. |
| 104 |
type requestInterceptor struct { |
| 105 |
base http.RoundTripper |
| 106 |
} |
| 107 |
|
| 108 |
func (ri *requestInterceptor) RoundTrip(req *http.Request) (*http.Response, error) { |
| 109 |
if req.Method == http.MethodPut && strings.Contains(req.URL.Path, "blob-for-signing") { |
| 110 |
reqClone := *req |
| 111 |
reqClone.Header.Add("x-ms-blob-type", "BlockBlob") |
| 112 |
req = &reqClone |
| 113 |
} |
| 114 |
return ri.base.RoundTrip(req) |
| 115 |
} |
| 116 |
|
| 117 |
func (h *harness) HTTPClient() *http.Client { |
| 118 |
return h.httpClient |
| 119 |
} |
| 120 |
|
| 121 |
func (h *harness) MakeDriver(ctx context.Context) (driver.Bucket, error) { |
| 122 |
client, err := h.clientFn(bucketName) |
| 123 |
if err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
return openBucket(ctx, client, nil) |
| 127 |
} |
| 128 |
|
| 129 |
func (h *harness) MakeDriverForNonexistentBucket(ctx context.Context) (driver.Bucket, error) { |
| 130 |
client, err := h.clientFn("bucket-does-not-exist") |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
return openBucket(ctx, client, nil) |
| 135 |
} |
| 136 |
|
| 137 |
func (h *harness) Close() { |
| 138 |
h.closer() |
| 139 |
} |
| 140 |
|
| 141 |
func TestConformance(t *testing.T) { |
| 142 |
// See setup instructions above for more details. |
| 143 |
// Disable the conformance tests, we don't have an Azure account anymore. |
| 144 |
// drivertest.RunConformanceTests(t, newHarness, []drivertest.AsTest{verifyContentLanguage{}}) |
| 145 |
} |
| 146 |
|
| 147 |
func BenchmarkAzureblob(b *testing.B) { |
| 148 |
name := os.Getenv("AZURE_STORAGE_ACCOUNT") |
| 149 |
key := os.Getenv("AZURE_STORAGE_KEY") |
| 150 |
credential, err := azblob.NewSharedKeyCredential(name, key) |
| 151 |
if err != nil { |
| 152 |
b.Fatal(err) |
| 153 |
} |
| 154 |
containerURL := fmt.Sprintf("https://%s.blob.core.windows.net/%s", accountName, bucketName) |
| 155 |
client, err := container.NewClientWithSharedKeyCredential(containerURL, credential, nil) |
| 156 |
if err != nil { |
| 157 |
b.Fatal(err) |
| 158 |
} |
| 159 |
bkt, err := OpenBucket(context.Background(), client, nil) |
| 160 |
if err != nil { |
| 161 |
b.Fatal(err) |
| 162 |
} |
| 163 |
drivertest.RunBenchmarks(b, bkt) |
| 164 |
} |
| 165 |
|
| 166 |
var language = "nl" |
| 167 |
|
| 168 |
// verifyContentLanguage uses As to access the underlying Azure types and |
| 169 |
// read/write the ContentLanguage field. |
| 170 |
type verifyContentLanguage struct{} |
| 171 |
|
| 172 |
func (verifyContentLanguage) Name() string { |
| 173 |
return "verify ContentLanguage can be written and read through As" |
| 174 |
} |
| 175 |
|
| 176 |
func (verifyContentLanguage) BucketCheck(b *blob.Bucket) error { |
| 177 |
var u *container.Client |
| 178 |
if !b.As(&u) { |
| 179 |
return errors.New("Bucket.As failed") |
| 180 |
} |
| 181 |
return nil |
| 182 |
} |
| 183 |
|
| 184 |
func (verifyContentLanguage) ErrorCheck(b *blob.Bucket, err error) error { |
| 185 |
return nil |
| 186 |
} |
| 187 |
|
| 188 |
func (verifyContentLanguage) BeforeRead(as func(any) bool) error { |
| 189 |
var u *azblob.DownloadStreamOptions |
| 190 |
if !as(&u) { |
| 191 |
return fmt.Errorf("BeforeRead As failed to get %T", u) |
| 192 |
} |
| 193 |
return nil |
| 194 |
} |
| 195 |
|
| 196 |
func (verifyContentLanguage) BeforeWrite(as func(any) bool) error { |
| 197 |
var azOpts *azblob.UploadStreamOptions |
| 198 |
if !as(&azOpts) { |
| 199 |
return errors.New("Writer.As failed") |
| 200 |
} |
| 201 |
azOpts.HTTPHeaders.BlobContentLanguage = &language |
| 202 |
return nil |
| 203 |
} |
| 204 |
|
| 205 |
func (verifyContentLanguage) BeforeCopy(as func(any) bool) error { |
| 206 |
var co *azblobblob.StartCopyFromURLOptions |
| 207 |
if !as(&co) { |
| 208 |
return errors.New("BeforeCopy.As failed") |
| 209 |
} |
| 210 |
return nil |
| 211 |
} |
| 212 |
|
| 213 |
func (verifyContentLanguage) BeforeList(as func(any) bool) error { |
| 214 |
var azOpts *container.ListBlobsHierarchyOptions |
| 215 |
if !as(&azOpts) { |
| 216 |
return errors.New("BeforeList.As failed") |
| 217 |
} |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
func (verifyContentLanguage) BeforeSign(as func(any) bool) error { |
| 222 |
var azOpts *sas.BlobPermissions |
| 223 |
if !as(&azOpts) { |
| 224 |
return errors.New("BeforeSign.As failed") |
| 225 |
} |
| 226 |
return nil |
| 227 |
} |
| 228 |
|
| 229 |
func (verifyContentLanguage) AttributesCheck(attrs *blob.Attributes) error { |
| 230 |
var resp azblobblob.GetPropertiesResponse |
| 231 |
if !attrs.As(&resp) { |
| 232 |
return errors.New("Attributes.As returned false") |
| 233 |
} |
| 234 |
if got := *resp.ContentLanguage; got != language { |
| 235 |
return fmt.Errorf("got %q want %q", got, language) |
| 236 |
} |
| 237 |
return nil |
| 238 |
} |
| 239 |
|
| 240 |
func (verifyContentLanguage) ReaderCheck(r *blob.Reader) error { |
| 241 |
var resp azblobblob.DownloadStreamResponse |
| 242 |
if !r.As(&resp) { |
| 243 |
return errors.New("Reader.As returned false") |
| 244 |
} |
| 245 |
if got := *resp.ContentLanguage; got != language { |
| 246 |
return fmt.Errorf("got %q want %q", got, language) |
| 247 |
} |
| 248 |
return nil |
| 249 |
} |
| 250 |
|
| 251 |
func (verifyContentLanguage) ListObjectCheck(o *blob.ListObject) error { |
| 252 |
if o.IsDir { |
| 253 |
var prefix container.BlobPrefix |
| 254 |
if !o.As(&prefix) { |
| 255 |
return errors.New("ListObject.As for dir returned false") |
| 256 |
} |
| 257 |
return nil |
| 258 |
} |
| 259 |
var item container.BlobItem |
| 260 |
if !o.As(&item) { |
| 261 |
return errors.New("ListObject.As for object returned false") |
| 262 |
} |
| 263 |
if got := *item.Properties.ContentLanguage; got != language { |
| 264 |
return fmt.Errorf("got %q want %q", got, language) |
| 265 |
} |
| 266 |
return nil |
| 267 |
} |
| 268 |
|
| 269 |
func TestOpenBucket(t *testing.T) { |
| 270 |
tests := []struct { |
| 271 |
description string |
| 272 |
nilClient bool |
| 273 |
accountName string |
| 274 |
want string |
| 275 |
wantErr bool |
| 276 |
}{ |
| 277 |
{ |
| 278 |
description: "nil client results in error", |
| 279 |
nilClient: true, |
| 280 |
accountName: "myaccount", |
| 281 |
wantErr: true, |
| 282 |
}, |
| 283 |
{ |
| 284 |
description: "success", |
| 285 |
accountName: "myaccount", |
| 286 |
want: "foo", |
| 287 |
}, |
| 288 |
} |
| 289 |
|
| 290 |
ctx := context.Background() |
| 291 |
for _, test := range tests { |
| 292 |
t.Run(test.description, func(t *testing.T) { |
| 293 |
var client *container.Client |
| 294 |
var err error |
| 295 |
if !test.nilClient { |
| 296 |
client, err = container.NewClientWithNoCredential("", nil) |
| 297 |
if err != nil { |
| 298 |
t.Fatal(err) |
| 299 |
} |
| 300 |
} |
| 301 |
// Create portable type. |
| 302 |
b, err := OpenBucket(ctx, client, nil) |
| 303 |
if b != nil { |
| 304 |
defer b.Close() |
| 305 |
} |
| 306 |
if (err != nil) != test.wantErr { |
| 307 |
t.Errorf("got err %v want error %v", err, test.wantErr) |
| 308 |
} |
| 309 |
}) |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
func TestOpenerFromEnv(t *testing.T) { |
| 314 |
tests := []struct { |
| 315 |
accountName string |
| 316 |
accountKey string |
| 317 |
sasToken string |
| 318 |
connectionString string |
| 319 |
connectionString2 string |
| 320 |
domain string |
| 321 |
protocol string |
| 322 |
isCDN bool |
| 323 |
isLocalEmulator bool |
| 324 |
|
| 325 |
want *credInfoT |
| 326 |
wantOpts *ServiceURLOptions |
| 327 |
}{ |
| 328 |
{ |
| 329 |
// Shared key. |
| 330 |
accountName: "myaccount", |
| 331 |
accountKey: "fakecreds", |
| 332 |
want: &credInfoT{ |
| 333 |
CredType: credTypeSharedKey, |
| 334 |
AccountName: "myaccount", |
| 335 |
AccountKey: "fakecreds", |
| 336 |
}, |
| 337 |
wantOpts: &ServiceURLOptions{ |
| 338 |
AccountName: "myaccount", |
| 339 |
}, |
| 340 |
}, |
| 341 |
{ |
| 342 |
// SAS Token. |
| 343 |
accountName: "myaccount", |
| 344 |
sasToken: "a-sas-token", |
| 345 |
want: &credInfoT{ |
| 346 |
CredType: credTypeSASViaNone, |
| 347 |
AccountName: "myaccount", |
| 348 |
}, |
| 349 |
wantOpts: &ServiceURLOptions{ |
| 350 |
AccountName: "myaccount", |
| 351 |
SASToken: "a-sas-token", |
| 352 |
}, |
| 353 |
}, |
| 354 |
{ |
| 355 |
// Connection string. |
| 356 |
accountName: "myaccount", |
| 357 |
connectionString: "a-connection-string", |
| 358 |
want: &credInfoT{ |
| 359 |
CredType: credTypeConnectionString, |
| 360 |
AccountName: "myaccount", |
| 361 |
ConnectionString: "a-connection-string", |
| 362 |
}, |
| 363 |
wantOpts: &ServiceURLOptions{ |
| 364 |
AccountName: "myaccount", |
| 365 |
}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
// Connection string provides default protocol and account name. |
| 369 |
connectionString: "DefaultEndpointsProtocol=https;AccountName=another-account", |
| 370 |
want: &credInfoT{ |
| 371 |
CredType: credTypeConnectionString, |
| 372 |
ConnectionString: "DefaultEndpointsProtocol=https;AccountName=another-account", |
| 373 |
}, |
| 374 |
wantOpts: &ServiceURLOptions{ |
| 375 |
AccountName: "another-account", |
| 376 |
Protocol: "https", |
| 377 |
}, |
| 378 |
}, |
| 379 |
{ |
| 380 |
// Alternate connection string. |
| 381 |
accountName: "myaccount", |
| 382 |
connectionString2: "a-connection-string", |
| 383 |
want: &credInfoT{ |
| 384 |
CredType: credTypeConnectionString, |
| 385 |
AccountName: "myaccount", |
| 386 |
ConnectionString: "a-connection-string", |
| 387 |
}, |
| 388 |
wantOpts: &ServiceURLOptions{ |
| 389 |
AccountName: "myaccount", |
| 390 |
}, |
| 391 |
}, |
| 392 |
{ |
| 393 |
// Default. |
| 394 |
accountName: "anotheraccount", |
| 395 |
want: &credInfoT{ |
| 396 |
CredType: credTypeDefault, |
| 397 |
AccountName: "anotheraccount", |
| 398 |
}, |
| 399 |
wantOpts: &ServiceURLOptions{ |
| 400 |
AccountName: "anotheraccount", |
| 401 |
}, |
| 402 |
}, |
| 403 |
{ |
| 404 |
// Setting protocol and domain. |
| 405 |
accountName: "myaccount", |
| 406 |
protocol: "http", |
| 407 |
domain: "foo.bar.com", |
| 408 |
want: &credInfoT{ |
| 409 |
CredType: credTypeDefault, |
| 410 |
AccountName: "myaccount", |
| 411 |
}, |
| 412 |
wantOpts: &ServiceURLOptions{ |
| 413 |
AccountName: "myaccount", |
| 414 |
Protocol: "http", |
| 415 |
StorageDomain: "foo.bar.com", |
| 416 |
}, |
| 417 |
}, |
| 418 |
{ |
| 419 |
// Local emulator. |
| 420 |
accountName: "myaccount", |
| 421 |
isLocalEmulator: true, |
| 422 |
want: &credInfoT{ |
| 423 |
CredType: credTypeDefault, |
| 424 |
AccountName: "myaccount", |
| 425 |
}, |
| 426 |
wantOpts: &ServiceURLOptions{ |
| 427 |
AccountName: "myaccount", |
| 428 |
IsLocalEmulator: true, |
| 429 |
}, |
| 430 |
}, |
| 431 |
} |
| 432 |
for _, test := range tests { |
| 433 |
t.Setenv("AZURE_STORAGE_ACCOUNT", test.accountName) |
| 434 |
t.Setenv("AZURE_STORAGE_KEY", test.accountKey) |
| 435 |
t.Setenv("AZURE_STORAGE_SAS_TOKEN", test.sasToken) |
| 436 |
t.Setenv("AZURE_STORAGE_CONNECTION_STRING", test.connectionString) |
| 437 |
t.Setenv("AZURE_STORAGEBLOB_CONNECTIONSTRING", test.connectionString2) |
| 438 |
t.Setenv("AZURE_STORAGE_DOMAIN", test.domain) |
| 439 |
t.Setenv("AZURE_STORAGE_PROTOCOL", test.protocol) |
| 440 |
if test.isCDN { |
| 441 |
t.Setenv("AZURE_STORAGE_IS_CDN", "true") |
| 442 |
} else { |
| 443 |
t.Setenv("AZURE_STORAGE_IS_CDN", "") |
| 444 |
} |
| 445 |
if test.isLocalEmulator { |
| 446 |
t.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "true") |
| 447 |
} else { |
| 448 |
t.Setenv("AZURE_STORAGE_IS_LOCAL_EMULATOR", "") |
| 449 |
} |
| 450 |
|
| 451 |
got := newCredInfoFromEnv() |
| 452 |
if diff := cmp.Diff(got, test.want); diff != "" { |
| 453 |
t.Errorf("unexpected diff in credInfo: %s", diff) |
| 454 |
} |
| 455 |
gotOpts := NewDefaultServiceURLOptions() |
| 456 |
if diff := cmp.Diff(gotOpts, test.wantOpts); diff != "" { |
| 457 |
t.Errorf("unexpected diff in Options: %s", diff) |
| 458 |
} |
| 459 |
|
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
func TestNewServiceURL(t *testing.T) { |
| 464 |
tests := []struct { |
| 465 |
opts ServiceURLOptions |
| 466 |
query url.Values |
| 467 |
want ServiceURL |
| 468 |
wantErrOverrides bool |
| 469 |
wantErrURL bool |
| 470 |
}{ |
| 471 |
{ |
| 472 |
// Unknown query parameter. |
| 473 |
opts: ServiceURLOptions{ |
| 474 |
AccountName: "myaccount", |
| 475 |
}, |
| 476 |
query: url.Values{ |
| 477 |
"foo": {"bar"}, |
| 478 |
}, |
| 479 |
wantErrOverrides: true, |
| 480 |
}, |
| 481 |
{ |
| 482 |
// Duplicate query parameter. |
| 483 |
opts: ServiceURLOptions{ |
| 484 |
AccountName: "myaccount", |
| 485 |
}, |
| 486 |
query: url.Values{ |
| 487 |
"domain": {"blob.core.usgovcloudapi.net", "blob.core.windows.net"}, |
| 488 |
}, |
| 489 |
wantErrOverrides: true, |
| 490 |
}, |
| 491 |
{ |
| 492 |
// Missing account name. |
| 493 |
opts: ServiceURLOptions{}, |
| 494 |
wantErrURL: true, |
| 495 |
}, |
| 496 |
{ |
| 497 |
// Account name set in the query |
| 498 |
opts: ServiceURLOptions{}, |
| 499 |
query: url.Values{ |
| 500 |
"storage_account": {"testaccount"}, |
| 501 |
}, |
| 502 |
want: "https://testaccount.blob.core.windows.net", |
| 503 |
}, |
| 504 |
{ |
| 505 |
// Basic working case. |
| 506 |
opts: ServiceURLOptions{ |
| 507 |
AccountName: "myaccount", |
| 508 |
}, |
| 509 |
want: "https://myaccount.blob.core.windows.net", |
| 510 |
}, |
| 511 |
{ |
| 512 |
// SASToken. |
| 513 |
opts: ServiceURLOptions{ |
| 514 |
AccountName: "myaccount", |
| 515 |
SASToken: "my-sas-token", |
| 516 |
}, |
| 517 |
want: "https://myaccount.blob.core.windows.net?my-sas-token", |
| 518 |
}, |
| 519 |
{ |
| 520 |
// Setting domain from ServiceURLOptions. |
| 521 |
opts: ServiceURLOptions{ |
| 522 |
AccountName: "myaccount", |
| 523 |
StorageDomain: "blob.core.usgovcloudapi.net", |
| 524 |
}, |
| 525 |
want: "https://myaccount.blob.core.usgovcloudapi.net", |
| 526 |
}, |
| 527 |
{ |
| 528 |
// Setting domain from the URL. |
| 529 |
opts: ServiceURLOptions{ |
| 530 |
AccountName: "myaccount", |
| 531 |
StorageDomain: "overridden", |
| 532 |
}, |
| 533 |
query: url.Values{ |
| 534 |
"domain": {"blob.core.usgovcloudapi.net"}, |
| 535 |
}, |
| 536 |
want: "https://myaccount.blob.core.usgovcloudapi.net", |
| 537 |
}, |
| 538 |
{ |
| 539 |
// Setting protocol from ServiceURLOptions. |
| 540 |
opts: ServiceURLOptions{ |
| 541 |
AccountName: "myaccount", |
| 542 |
Protocol: "http", |
| 543 |
}, |
| 544 |
want: "http://myaccount.blob.core.windows.net", |
| 545 |
}, |
| 546 |
{ |
| 547 |
// Setting protocol from the URL. |
| 548 |
opts: ServiceURLOptions{ |
| 549 |
AccountName: "myaccount", |
| 550 |
Protocol: "https", |
| 551 |
}, |
| 552 |
query: url.Values{ |
| 553 |
"protocol": {"http"}, |
| 554 |
}, |
| 555 |
want: "http://myaccount.blob.core.windows.net", |
| 556 |
}, |
| 557 |
{ |
| 558 |
// Setting IsCDN from ServiceURLOptions. |
| 559 |
opts: ServiceURLOptions{ |
| 560 |
AccountName: "myaccount", |
| 561 |
IsCDN: true, |
| 562 |
}, |
| 563 |
want: "https://blob.core.windows.net", |
| 564 |
}, |
| 565 |
{ |
| 566 |
// Setting IsCDN from the URL. |
| 567 |
opts: ServiceURLOptions{ |
| 568 |
AccountName: "myaccount", |
| 569 |
}, |
| 570 |
query: url.Values{ |
| 571 |
"cdn": {"true"}, |
| 572 |
}, |
| 573 |
want: "https://blob.core.windows.net", |
| 574 |
}, |
| 575 |
{ |
| 576 |
// Local emulator, implicit from domain. |
| 577 |
opts: ServiceURLOptions{ |
| 578 |
AccountName: "myaccount", |
| 579 |
Protocol: "http", |
| 580 |
StorageDomain: "localhost:10001", |
| 581 |
}, |
| 582 |
want: "http://localhost:10001/myaccount", |
| 583 |
}, |
| 584 |
{ |
| 585 |
// Local emulator, implicit from domain through URL parameter. |
| 586 |
opts: ServiceURLOptions{ |
| 587 |
AccountName: "myaccount", |
| 588 |
}, |
| 589 |
query: url.Values{ |
| 590 |
"protocol": {"http"}, |
| 591 |
"domain": {"127.0.0.1:10001"}, |
| 592 |
}, |
| 593 |
want: "http://127.0.0.1:10001/myaccount", |
| 594 |
}, |
| 595 |
{ |
| 596 |
// Local emulator, explicit through ServiceURLOptions. |
| 597 |
opts: ServiceURLOptions{ |
| 598 |
AccountName: "myaccount", |
| 599 |
StorageDomain: "mylocalemulator", |
| 600 |
IsLocalEmulator: true, |
| 601 |
}, |
| 602 |
want: "https://mylocalemulator/myaccount", |
| 603 |
}, |
| 604 |
{ |
| 605 |
// Local emulator, explicit through URL parameter. |
| 606 |
opts: ServiceURLOptions{ |
| 607 |
AccountName: "myaccount", |
| 608 |
StorageDomain: "mylocalemulator", |
| 609 |
}, |
| 610 |
query: url.Values{ |
| 611 |
"localemu": {"true"}, |
| 612 |
}, |
| 613 |
want: "https://mylocalemulator/myaccount", |
| 614 |
}, |
| 615 |
} |
| 616 |
|
| 617 |
for _, test := range tests { |
| 618 |
opts, err := test.opts.withOverrides(test.query) |
| 619 |
if (err != nil) != test.wantErrOverrides { |
| 620 |
t.Fatalf("withOverrides got err %v want error %v", err, test.wantErrOverrides) |
| 621 |
} |
| 622 |
if err != nil { |
| 623 |
continue |
| 624 |
} |
| 625 |
got, err := NewServiceURL(opts) |
| 626 |
if (err != nil) != test.wantErrURL { |
| 627 |
t.Errorf("NewServiceURL got err %v want error %v", err, test.wantErrURL) |
| 628 |
} |
| 629 |
if got != test.want { |
| 630 |
t.Errorf("got %q want %q", got, test.want) |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
func TestOpenBucketFromURL(t *testing.T) { |
| 636 |
t.Setenv("AZURE_STORAGE_ACCOUNT", "my-account") |
| 637 |
t.Setenv("AZURE_STORAGE_KEY", "bXlrZXk=") // mykey base64 encoded |
| 638 |
|
| 639 |
tests := []struct { |
| 640 |
URL string |
| 641 |
WantErr bool |
| 642 |
}{ |
| 643 |
// OK. |
| 644 |
{"azblob://mybucket", false}, |
| 645 |
// With storage domain. |
| 646 |
{"azblob://mybucket?domain=blob.core.usgovcloudapi.net", false}, |
| 647 |
// With duplicate storage domain. |
| 648 |
{"azblob://mybucket?domain=blob.core.usgovcloudapi.net&domain=blob.core.windows.net", true}, |
| 649 |
// With protocol. |
| 650 |
{"azblob://mybucket?protocol=http", false}, |
| 651 |
// With invalid protocol. |
| 652 |
{"azblob://mybucket?protocol=ftp", true}, |
| 653 |
// With Account. |
| 654 |
{"azblob://mybucket?storage_account=test", false}, |
| 655 |
// With CDN. |
| 656 |
{"azblob://mybucket?cdn=true", false}, |
| 657 |
// With invalid CDN. |
| 658 |
{"azblob://mybucket?cdn=42", true}, |
| 659 |
// With local emulator. |
| 660 |
{"azblob://mybucket?localemu=true", false}, |
| 661 |
// With invalid local emulator. |
| 662 |
{"azblob://mybucket?localemu=42", true}, |
| 663 |
// Invalid parameter. |
| 664 |
{"azblob://mybucket?param=value", true}, |
| 665 |
} |
| 666 |
|
| 667 |
ctx := context.Background() |
| 668 |
for _, test := range tests { |
| 669 |
b, err := blob.OpenBucket(ctx, test.URL) |
| 670 |
if b != nil { |
| 671 |
defer b.Close() |
| 672 |
} |
| 673 |
if (err != nil) != test.WantErr { |
| 674 |
t.Errorf("%s: got error %v, want error %v", test.URL, err, test.WantErr) |
| 675 |
} |
| 676 |
} |
| 677 |
} |