Skip to content

Commit

Permalink
report error instead of panic when use a empty file in storage_share_…
Browse files Browse the repository at this point in the history
…file (#24179)
  • Loading branch information
wuxu92 authored Dec 11, 2023
1 parent 7307d9e commit 4f1d4ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/services/storage/storage_share_file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ func resourceStorageShareFileCreate(d *pluginsdk.ResourceData, meta interface{})
return fmt.Errorf("'stat'-ing File %q (File Share %q / Account %q): %+v", fileName, storageShareID.Name, storageShareID.AccountName, err)
}

if info.Size() == 0 {
return fmt.Errorf("file %q (File Share %q / Account %q) is empty", fileName, storageShareID.Name, storageShareID.AccountName)
}

input.ContentLength = info.Size()
}

Expand Down
18 changes: 18 additions & 0 deletions internal/services/storage/storage_share_file_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
Expand Down Expand Up @@ -118,6 +119,23 @@ func TestAccAzureRMStorageShareFile_withFile(t *testing.T) {
})
}

func TestAccAzureRMStorageShareFile_withEmptyFile(t *testing.T) {
sourceBlob, err := os.CreateTemp("", "")
if err != nil {
t.Fatalf("Failed to create local source blob file")
}

data := acceptance.BuildTestData(t, "azurerm_storage_share_file", "test")
r := StorageShareFileResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withFile(data, sourceBlob.Name()),
ExpectError: regexp.MustCompile(`Error: file .* is empty`),
},
})
}

func (StorageShareFileResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := files.ParseResourceID(state.ID)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/storage_share_file.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ The following arguments are supported:

* `source` - (Optional) An absolute path to a file on the local system. Changing this forces a new resource to be created.

~> **Note** The file specified with `source` can not be empty.

* `content_type` - (Optional) The content type of the share file. Defaults to `application/octet-stream`.

* `content_md5` - (Optional) The MD5 sum of the file contents. Changing this forces a new resource to be created.
Expand Down

0 comments on commit 4f1d4ff

Please sign in to comment.