Skip to content

Commit

Permalink
Merge pull request #109 from tombuildsstuff/bugfix/resource-ids
Browse files Browse the repository at this point in the history
bugfix: don't assume a fixed number of segments in blob resource IDs
  • Loading branch information
manicminer authored Mar 18, 2024
2 parents 6142e37 + 25a1a6a commit 8efc5bb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 6 deletions.
4 changes: 2 additions & 2 deletions storage/2020-08-04/blob/blobs/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func ParseBlobID(input, domainSuffix string) (*BlobId, error) {

path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) != 2 {
return nil, fmt.Errorf("expected the path to contain 2 segments but got %d", len(segments))
if len(segments) < 2 {
return nil, fmt.Errorf("expected the path to contain at least 2 segments but got %d", len(segments))
}

containerName := segments[0]
Expand Down
64 changes: 64 additions & 0 deletions storage/2020-08-04/blob/blobs/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ func TestParseBlobIDStandard(t *testing.T) {
}
}

func TestParseNestedBlobIDStandard(t *testing.T) {
input := "https://example1.blob.core.windows.net/container1/more/deeply/nested/blob1.vhd"
expected := BlobId{
AccountId: accounts.AccountId{
AccountName: "example1",
SubDomainType: accounts.BlobSubDomainType,
DomainSuffix: "core.windows.net",
},
ContainerName: "container1",
BlobName: "more/deeply/nested/blob1.vhd",
}
actual, err := ParseBlobID(input, "core.windows.net")
if err != nil {
t.Fatalf(err.Error())
}
if actual.AccountId.AccountName != expected.AccountId.AccountName {
t.Fatalf("expected AccountName to be %q but got %q", expected.AccountId.AccountName, actual.AccountId.AccountName)
}
if actual.AccountId.SubDomainType != expected.AccountId.SubDomainType {
t.Fatalf("expected SubDomainType to be %q but got %q", expected.AccountId.SubDomainType, actual.AccountId.SubDomainType)
}
if actual.AccountId.DomainSuffix != expected.AccountId.DomainSuffix {
t.Fatalf("expected DomainSuffix to be %q but got %q", expected.AccountId.DomainSuffix, actual.AccountId.DomainSuffix)
}
if actual.ContainerName != expected.ContainerName {
t.Fatalf("expected ContainerName to be %q but got %q", expected.ContainerName, actual.ContainerName)
}
if actual.BlobName != expected.BlobName {
t.Fatalf("expected BlobName to be %q but got %q", expected.BlobName, actual.BlobName)
}
}

func TestParseBlobIDInADNSZone(t *testing.T) {
input := "https://example1.zone1.blob.storage.azure.net/container1/blob1.vhd"
expected := BlobId{
Expand Down Expand Up @@ -132,6 +164,38 @@ func TestFormatBlobIDStandard(t *testing.T) {
}
}

func TestFormatNestedBlobIDStandard(t *testing.T) {
actual := BlobId{
AccountId: accounts.AccountId{
AccountName: "example1",
SubDomainType: accounts.BlobSubDomainType,
DomainSuffix: "core.windows.net",
IsEdgeZone: false,
},
ContainerName: "container1",
BlobName: "more/deeply/nested/somefile.vhd",
}.ID()
expected := "https://example1.blob.core.windows.net/container1/more/deeply/nested/somefile.vhd"
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
}

func TestParseInvalidBlobIDStandard(t *testing.T) {
input := "https://example1.blob.core.windows.net/blobby.vhd"
actual, err := ParseBlobID(input, "core.windows.net")
if err == nil {
if actual != nil {
t.Fatalf("expected an error but got: %#v", *actual)
} else {
t.Fatalf("expected an error but got no error and a nil result")
}
}
if err.Error() != "expected the path to contain at least 2 segments but got 1" {
t.Fatalf("unexpected error received: %+v", err)
}
}

func TestFormatBlobIDInDNSZone(t *testing.T) {
actual := BlobId{
AccountId: accounts.AccountId{
Expand Down
2 changes: 1 addition & 1 deletion storage/2020-08-04/file/shares/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ParseShareID(input, domainSuffix string) (*ShareId, error) {
path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) == 0 {
return nil, fmt.Errorf("Expected the path to contain segments but got none")
return nil, fmt.Errorf("expected the path to contain segments but got none")
}

shareName := strings.TrimPrefix(uri.Path, "/")
Expand Down
4 changes: 2 additions & 2 deletions storage/2023-11-03/blob/blobs/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func ParseBlobID(input, domainSuffix string) (*BlobId, error) {

path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) != 2 {
return nil, fmt.Errorf("expected the path to contain 2 segments but got %d", len(segments))
if len(segments) < 2 {
return nil, fmt.Errorf("expected the path to contain at least 2 segments but got %d", len(segments))
}

containerName := segments[0]
Expand Down
64 changes: 64 additions & 0 deletions storage/2023-11-03/blob/blobs/resource_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,53 @@ func TestParseBlobIDStandard(t *testing.T) {
}
}

func TestParseNestedBlobIDStandard(t *testing.T) {
input := "https://example1.blob.core.windows.net/container1/more/deeply/nested/blob1.vhd"
expected := BlobId{
AccountId: accounts.AccountId{
AccountName: "example1",
SubDomainType: accounts.BlobSubDomainType,
DomainSuffix: "core.windows.net",
},
ContainerName: "container1",
BlobName: "more/deeply/nested/blob1.vhd",
}
actual, err := ParseBlobID(input, "core.windows.net")
if err != nil {
t.Fatalf(err.Error())
}
if actual.AccountId.AccountName != expected.AccountId.AccountName {
t.Fatalf("expected AccountName to be %q but got %q", expected.AccountId.AccountName, actual.AccountId.AccountName)
}
if actual.AccountId.SubDomainType != expected.AccountId.SubDomainType {
t.Fatalf("expected SubDomainType to be %q but got %q", expected.AccountId.SubDomainType, actual.AccountId.SubDomainType)
}
if actual.AccountId.DomainSuffix != expected.AccountId.DomainSuffix {
t.Fatalf("expected DomainSuffix to be %q but got %q", expected.AccountId.DomainSuffix, actual.AccountId.DomainSuffix)
}
if actual.ContainerName != expected.ContainerName {
t.Fatalf("expected ContainerName to be %q but got %q", expected.ContainerName, actual.ContainerName)
}
if actual.BlobName != expected.BlobName {
t.Fatalf("expected BlobName to be %q but got %q", expected.BlobName, actual.BlobName)
}
}

func TestParseInvalidBlobIDStandard(t *testing.T) {
input := "https://example1.blob.core.windows.net/blobby.vhd"
actual, err := ParseBlobID(input, "core.windows.net")
if err == nil {
if actual != nil {
t.Fatalf("expected an error but got: %#v", *actual)
} else {
t.Fatalf("expected an error but got no error and a nil result")
}
}
if err.Error() != "expected the path to contain at least 2 segments but got 1" {
t.Fatalf("unexpected error received: %+v", err)
}
}

func TestParseBlobIDInADNSZone(t *testing.T) {
input := "https://example1.zone1.blob.storage.azure.net/container1/blob1.vhd"
expected := BlobId{
Expand Down Expand Up @@ -132,6 +179,23 @@ func TestFormatBlobIDStandard(t *testing.T) {
}
}

func TestFormatNestedBlobIDStandard(t *testing.T) {
actual := BlobId{
AccountId: accounts.AccountId{
AccountName: "example1",
SubDomainType: accounts.BlobSubDomainType,
DomainSuffix: "core.windows.net",
IsEdgeZone: false,
},
ContainerName: "container1",
BlobName: "more/deeply/nested/somefile.vhd",
}.ID()
expected := "https://example1.blob.core.windows.net/container1/more/deeply/nested/somefile.vhd"
if actual != expected {
t.Fatalf("expected %q but got %q", expected, actual)
}
}

func TestFormatBlobIDInDNSZone(t *testing.T) {
actual := BlobId{
AccountId: accounts.AccountId{
Expand Down
2 changes: 1 addition & 1 deletion storage/2023-11-03/file/shares/resource_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ParseShareID(input, domainSuffix string) (*ShareId, error) {
path := strings.TrimPrefix(uri.Path, "/")
segments := strings.Split(path, "/")
if len(segments) == 0 {
return nil, fmt.Errorf("Expected the path to contain segments but got none")
return nil, fmt.Errorf("expected the path to contain segments but got none")
}

shareName := strings.TrimPrefix(uri.Path, "/")
Expand Down

0 comments on commit 8efc5bb

Please sign in to comment.