Skip to content

Commit

Permalink
test: add az fileshare storage integration tests with real az resourc…
Browse files Browse the repository at this point in the history
…es (#180)

* test: add az fileshare storage integration tests with real az resources

* pr-fix: don't use resource group for creating az fileshare

* pr-fix: update w/o additional az fileshare setup

* pr-add: integration test for az fileshare w existing folder

* pr-fix: use guid to generate unique file shares

* pr-fix: use guid to generate unique file shares

* pr-fix: use correct fileshare name

* pr-fix: still publish test results even when when test suite fails

* pr-add: integration test for uploading to az fileshare

* pr-add: integration test for non-existing az fileshare

* pr-fix: update with passing storage context

* pr-fix: update with correct assertion and arg names

* pr-fix: update with correct test args + revisit fileshare folder creation script

* pr-fix: update with forced fileshare deletion + correct mocked assertions

* pr-fix: update with correct ps storage account

* pr-fix: update with simpler integration test teardown

* pr-fix: unit & integration tests final

* Update run-pester-tests.yml

* Update Arcus.Scripting.Storage.FileShare.tests.ps1

* pr-fix: change filestorage feature docs with updated logging
  • Loading branch information
stijnmoreels authored Jun 28, 2021
1 parent 4c1ed00 commit b08c6e9
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 122 deletions.
15 changes: 8 additions & 7 deletions docs/preview/features/powershell/azure-storage-fileshare.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PS> Install-Module -Name Arcus.Scripting.Storage.FileShare
## Creating a folder on an Azure file share

Creates a new folder within the Azure File Share resource.
When a folder already exists with the provided name, it will be skipped. No exception will be thrown.

| Parameter | Mandatory | Description |
| -------------------- | --------- | ----------------------------------------------------------------------- |
Expand All @@ -32,8 +33,8 @@ Creates a new folder within the Azure File Share resource.

```powershell
PS> Create-AzFileShareStorageFolder -ResourceGroupName "shipping-resources" -StorageAccountName "tracking-account-storage" -FileShareName "returned" -FolderName "containers"
# Creating 'containers' directory in file share..
# Directory 'containers' has been created..
# Creating Azure FileShare storage folder 'containers' in file share 'returned'..
# Created Azure FileShare storage folder 'containers' in file share 'returned'
```

## Copying files to a folder on an Azure file share
Expand All @@ -53,8 +54,8 @@ Upload a set of files from a given folder, optionally matching a specific file m

```powershell
PS> Copy-AzFileShareStorageFiles -ResourceGroupName "shipping-resources" -StorageAccountName "tracking-account-storage" -FileShareName "returned" -SourceFolderPath "containers" -DestinationFolderName "containers"
# Upload files to file share...
# Uploaded the file to File Share: [fileName]
# Uploaded the file to File Share: [fileName]
# Files have been uploaded
```
# Upload files to Azure FileShare storage 'returned'...
# Uploaded the '[fileName]' file to Azure FileShare 'returned'
# Uploaded the '[fileName]' file to Azure FileShare 'returned'
# Files have been uploaded to Azure FileShare storage 'returned'
```
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,39 @@ param(
[Parameter(Mandatory = $false)][string] $FileMask = ""
)

function VerifyAzureFileShareExists
{
try
{
function VerifyAzureFileShareExists {
try {
$fileShare = Get-AzStorageShare -Context $context -Name $FileShareName -ErrorAction Stop
return $true
}
catch [Microsoft.Azure.Storage.StorageException]
{
if($Error[0].Exception.Message -like "*does not exist*")
{
} catch [Microsoft.Azure.Storage.StorageException] {
if ($Error[0].Exception.Message -like "*does not exist*") {
Write-Host "The given file-share '$FileShareName' does not seem to exist in storage account '$StorageAccountName'."
Write-Error "The given file-share '$FileShareName' does not seem to exist in storage account '$StorageAccountName'."
return $false
}
else
{
} else {
throw
}
}
}

try
{
Write-Host "Upload files to file share..."

## Get the storage account context
$context = (Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName).Context

## Get the file share
if(VerifyAzureFileShareExists)
{
## Loop all files in the source-folder
foreach($file in Get-ChildItem ("$SourceFolderPath") -File)
{
## Does the file match the FileMask
if($file.Name.EndsWith($FileMask,"CurrentCultureIgnoreCase"))
{
## Upload the file
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$context = $storageAccount.Context

Write-Verbose "Upload files to Azure FileShare storage '$FileShareName'..."

if (VerifyAzureFileShareExists) {
foreach ($file in Get-ChildItem ("$SourceFolderPath") -File) {
try {
if ($file.Name.EndsWith($FileMask, "CurrentCultureIgnoreCase")) {
Set-AzStorageFileContent -Context $context -ShareName $FileShareName -Source $file.FullName -Path $DestinationFolderName -Force
$fileName = $file.Name
Write-Host "Uploaded the file to File Share: $fileName"
Write-Host "Uploaded the '$($file.Name)' file to Azure FileShare '$FileShareName'"
}
} catch {
$ErrorMessage = $_.Exception.Message
Write-Error "Failed to upload files to directory '$DestinationFolderName' in file-share '$FileShareName'. Reason: $ErrorMessage"
}

Write-Host "Files have been uploaded"

}

Write-Host "Files have been uploaded to Azure FileShare storage '$FileShareName'"
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Error "Failed to upload files to directory '$DestinationFolderName' in file-share '$FileShareName'. Reason: $ErrorMessage"
return $null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,16 @@ param(
[Parameter(Mandatory = $true)][string] $FolderName = $(throw "Name of folder is required")
)

try{
Write-Host "Creating '$FolderName' directory in file share.."
$storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$fileShare = Get-AzStorageFile -Context $storageAccount.Context -ShareName $FileShareName
$fileShareFolders =
Get-AzStorageFile -ShareName $FileShareName -Context $storageAccount.Context |
where { $_.GetType().Name -eq "AzureStorageFileDirectory" }

## Get the storage account context
$ctx = (Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName).Context

## Create directory
Get-AzStorageShare -Context $ctx -Name $FileShareName | New-AzStorageDirectory -Path $FolderName -ErrorAction Stop

Write-Host "Directory '$FolderName' has been created.."
}
catch [Microsoft.Azure.Storage.StorageException]
{
if($Error[0].Exception.Message -like "*already exists*")
{
Write-Host "The specified folder already exists."
}
else
{
throw
}
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Error "Failed to create the directory '$FolderName' in file-share '$FileShareName'. Reason: $ErrorMessage"
return $null
}
if ($FolderName -in $fileShareFolders.Name) {
Write-Host "Azure FileShare storage folder '$FolderName' already exists, skipping"
} else {
Write-Verbose "Creating Azure FileShare storage folder '$FolderName' in file share '$FileShareName'.."
New-AzStorageDirectory -Context $storageAccount.Context -ShareName $FileShareName -Path $FolderName
Write-Host "Created Azure FileShare storage folder '$FolderName' in file share '$FileShareName'"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Import-Module Az.Storage
Import-Module -Name $PSScriptRoot\..\Arcus.Scripting.Storage.FileShare -ErrorAction Stop

InModuleScope Arcus.Scripting.Storage.FileShare {
Describe "Arcus Azure FileShare storage integration tests" {
BeforeEach {
$config = & $PSScriptRoot\Load-JsonAppsettings.ps1 -fileName "appsettings.json"
& $PSScriptRoot\Connect-AzAccountFromConfig.ps1 -config $config
$guid = [System.Guid]::NewGuid()
$fileShareName = "arcus-scripting-fileshare-$guid"
$storageAccount = Get-AzStorageAccount -ResourceGroupName $config.Arcus.ResourceGroupName -Name $config.Arcus.Storage.StorageAccount.Name
New-AzStorageShare -Context $storageAccount.Context -Name $fileShareName
}
Context "Create Azure FileShare storage folder" {
It "Creates a new Azure FileShare storage folder" {
# Arrange
$folderName = "new-arcus-fileshare-folder"

# Act
Create-AzFileShareStorageFolder `
-ResourceGroupName $config.Arcus.ResourceGroupName `
-StorageAccountName $config.Arcus.Storage.StorageAccount.Name `
-FileShareName $fileShareName `
-FolderName $folderName

# Assert
Get-AzStorageFile -ShareName $fileShareName -Context $storageAccount.Context |
where { $_.GetType().Name -eq "AzureStorageFileDirectory" } |
% { $_.Name } |
Should -Contain $folderName
}
It "Doesn't create a new Azure FileShare storage folder when already exists" {
# Arrange
$folderName = "already-existing-arcus-fileshare-folder"
New-AzStorageDirectory -Context $storageAccount.Context -ShareName $fileShareName -Path $folderName

# Act
Create-AzFileShareStorageFolder `
-ResourceGroupName $config.Arcus.ResourceGroupName `
-StorageAccountName $config.Arcus.Storage.StorageAccount.Name `
-FileShareName $fileShareName `
-FolderName $folderName

# Assert
Get-AzStorageFile -ShareName $fileShareName -Context $storageAccount.Context |
where { $_.GetType().Name -eq "AzureStorageFileDirectory" } |
% { $_.Name } |
Should -Contain $folderName
}
}
Context "Copy files into Azure FileShare storage folder" {
It "Uploads file into existing Azure FileShare storage" {
# Arrange
$folderName = "uploaded-arcus-fileshare-folder"
New-AzStorageDirectory -Context $storageAccount.Context -ShareName $fileShareName -Path $folderName

# Act
Copy-AzFileShareStorageFiles `
-ResourceGroupName $config.Arcus.ResourceGroupName `
-StorageAccountName $config.Arcus.Storage.StorageAccount.Name `
-FileShareName $fileShareName `
-SourceFolderPath "$PSScriptRoot\Blobs" `
-DestinationFolderName $folderName

# Assert
$tempLocation = "$PSScriptRoot\arcus.png"
try {
Get-AzStorageFileContent `
-Context $storageAccount.Context `
-ShareName $fileShareName `
-Path "$folderName/arcus.png" `
-Destination $tempLocation -Force
$file = Get-Item $tempLocation
$file.Length | Should -BeGreaterThan 0
} finally {
Remove-Item $tempLocation -Force
}
}
It "Uploads file into non-existing Azure FileShare storage" {
# Arrange
$folderName = "non-existing-arcus-fileshare-folder"
$nonExistingFileShareName = "non-existing-fileshare-storage"

# Act / Assert
{ Copy-AzFileShareStorageFiles `
-ResourceGroupName $config.Arcus.ResourceGroupName `
-StorageAccountName $config.Arcus.Storage.StorageAccount.Name `
-FileShareName $nonExistingFileShareName `
-SourceFolderPath "$PSScriptRoot\Blobs" `
-DestinationFolderName $folderName } |
Should -Throw
}
}
AfterEach {
Remove-AzStorageShare -Name $fileShareName -Context $storageAccount.Context -IncludeAllSnapshot -Force
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
<Compile Include="Arcus.Scripting.LogicApps.tests.ps1" />
<Compile Include="Arcus.Scripting.KeyVault.tests.ps1" />
<Compile Include="Arcus.Scripting.Storage.Blob.tests.ps1" />
<Compile Include="Arcus.Scripting.Storage.FileShare.tests.ps1" />
<Compile Include="Connect-AzAccountFromConfig.ps1" />
<Compile Include="Load-JsonAppsettings.ps1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Arcus.Scripting.ARM\Arcus.Scripting.ARM.pssproj">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
param(
[string]$fileName
)

$filePath = "$PSScriptRoot\$fileName"
[string]$appsettings = Get-Content $filePath
return $config = ConvertFrom-Json $appsettings
Loading

0 comments on commit b08c6e9

Please sign in to comment.