From b08c6e99300cc1d5c6880872e1080e11ac7d3226 Mon Sep 17 00:00:00 2001 From: stijnmoreels <9039753+stijnmoreels@users.noreply.github.com> Date: Mon, 28 Jun 2021 09:36:11 +0200 Subject: [PATCH] test: add az fileshare storage integration tests with real az resources (#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 --- .../powershell/azure-storage-fileshare.md | 15 +- .../Arcus.Scripting.Storage.FileShare.psd1 | Bin 7826 -> 7794 bytes .../Scripts/Copy-AzFileShareStorageFiles.ps1 | 60 +++----- .../Create-AzFileShareStorageFolder.ps1 | 39 ++---- ...rcus.Scripting.Storage.FileShare.tests.ps1 | 98 +++++++++++++ .../Arcus.Scripting.Tests.Integration.pssproj | 2 + .../Load-JsonAppsettings.ps1 | 7 + ...rcus.Scripting.Storage.FileShare.tests.ps1 | 132 +++++++++++------- 8 files changed, 231 insertions(+), 122 deletions(-) create mode 100644 src/Arcus.Scripting.Tests.Integration/Arcus.Scripting.Storage.FileShare.tests.ps1 create mode 100644 src/Arcus.Scripting.Tests.Integration/Load-JsonAppsettings.ps1 diff --git a/docs/preview/features/powershell/azure-storage-fileshare.md b/docs/preview/features/powershell/azure-storage-fileshare.md index f953d483..7b3e2859 100644 --- a/docs/preview/features/powershell/azure-storage-fileshare.md +++ b/docs/preview/features/powershell/azure-storage-fileshare.md @@ -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 | | -------------------- | --------- | ----------------------------------------------------------------------- | @@ -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 @@ -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' +``` \ No newline at end of file diff --git a/src/Arcus.Scripting.Storage.FileShare/Arcus.Scripting.Storage.FileShare.psd1 b/src/Arcus.Scripting.Storage.FileShare/Arcus.Scripting.Storage.FileShare.psd1 index 22124bc9a4ab5fb61b877017dc87fedefa2f02df..f3a11b83cda7bd3decf3ed21e23d77a82c994b63 100644 GIT binary patch delta 18 ZcmbPa`^jcQ6ceMt + + diff --git a/src/Arcus.Scripting.Tests.Integration/Load-JsonAppsettings.ps1 b/src/Arcus.Scripting.Tests.Integration/Load-JsonAppsettings.ps1 new file mode 100644 index 00000000..c6d3362e --- /dev/null +++ b/src/Arcus.Scripting.Tests.Integration/Load-JsonAppsettings.ps1 @@ -0,0 +1,7 @@ +param( + [string]$fileName +) + +$filePath = "$PSScriptRoot\$fileName" +[string]$appsettings = Get-Content $filePath + return $config = ConvertFrom-Json $appsettings \ No newline at end of file diff --git a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Storage.FileShare.tests.ps1 b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Storage.FileShare.tests.ps1 index bab40500..ec1a9834 100644 --- a/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Storage.FileShare.tests.ps1 +++ b/src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.Storage.FileShare.tests.ps1 @@ -1,6 +1,18 @@ Describe "Arcus" { Context "File Share" { InModuleScope Arcus.Scripting.Storage.FileShare { + BeforeEach { + # Test values, not really pointing to anything + $storageAccount = New-Object -TypeName Microsoft.Azure.Management.Storage.Models.StorageAccount + $testSasToken = "?st=2013-09-03T04%3A12%3A15Z&se=2013-09-03T05%3A12%3A15Z&sr=c&sp=r&sig=fN2NPxLK99tR2%2BWnk48L3lMjutEj7nOwBo7MXs2hEV8%3D" + $testEndpoint = "http://storageaccountname.blob.core.windows.net" + $testConnection = [System.String]::Format("BlobEndpoint={0};QueueEndpoint={0};TableEndpoint={0};SharedAccessSignature={1}", $testEndpoint, $testSasToken) + $storageAccount = New-Object -TypeName Microsoft.Azure.Management.Storage.Models.StorageAccount + $psStorageAccount = New-Object -TypeName Microsoft.Azure.Commands.Management.Storage.Models.PSStorageAccount -ArgumentList $storageAccount + + $cloudShare = New-Object -TypeName Microsoft.Azure.Storage.File.CloudFileShare -ArgumentList (New-Object -TypeName System.Uri "https://something") + $fileShare = New-Object -TypeName Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare -ArgumentList $cloudShare, $storageContext + } It "Create folder on Azure File Share" { # Arrange $resourceGroup = "stock" @@ -8,79 +20,106 @@ Describe "Arcus" { $fileShareName = "shipped-file" $storageAccountName = "admin" $tableName = "products" - - # Test values, not really pointing to anything - $storageAccount = New-Object -TypeName Microsoft.Azure.Management.Storage.Models.StorageAccount - $testSasToken = "?st=2013-09-03T04%3A12%3A15Z&se=2013-09-03T05%3A12%3A15Z&sr=c&sp=r&sig=fN2NPxLK99tR2%2BWnk48L3lMjutEj7nOwBo7MXs2hEV8%3D" - $testEndpoint = "http://storageaccountname.blob.core.windows.net" - $testConnection = [System.String]::Format("BlobEndpoint={0};QueueEndpoint={0};TableEndpoint={0};SharedAccessSignature={1}", $testEndpoint, $testSasToken) - $storageAccount = [Microsoft.Azure.Storage.CloudStorageAccount]::Parse($testConnection) - $storageContext = New-Object -TypeName Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext -ArgumentList $storageAccount - $cloudShare = New-Object -TypeName Microsoft.Azure.Storage.File.CloudFileShare -ArgumentList (New-Object -TypeName System.Uri "https://something") - $fileShare = New-Object -TypeName Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare -ArgumentList $cloudShare, $storageContext - Mock Get-AzStorageAccount { $ResourceGroupName | Should -Be $resourceGroup $Name | Should -Be $storageAccountName return $psStorageAccount } -Verifiable - Mock Get-AzStorageShare { - $Context | Should -Be $psStorageAccount - $Name | Should -Be $fileShareName - return $fileShare } -Verifiable + Mock Get-AzStorageFile { return @() } Mock New-AzStorageDirectory { - $Share | Should -Not -Be $null + $ShareName | Should -Be $fileShareName $Path | Should -Be $folderName } # Act - Create-AzFileShareStorageFolder -ResourceGroupName $resourceGroup -StorageAccountName $storageAccountName -FileShareName $fileShareName -FolderName $folderName + Create-AzFileShareStorageFolder ` + -ResourceGroupName $resourceGroup ` + -StorageAccountName $storageAccountName ` + -FileShareName $fileShareName ` + -FolderName $folderName # Assert Assert-VerifiableMock Assert-MockCalled Get-AzStorageAccount -Times 1 - Assert-MockCalled Get-AzStorageShare -Times 1 + Assert-MockCalled Get-AzStorageFile -Times 1 Assert-MockCalled New-AzStorageDirectory -Times 1 } - It "Copy files to Azure File Share" { + It "Creates duplicate folder on Azure FileShare" { + # Arrange + $resourceGroup = "stock" + $folderName = "shipped" + $fileShareName = "shipped-file" + $storageAccountName = "admin" + $tableName = "products" + $fileAddress = "http://test.file.core.windows.net/$fileShareName/$folderName" + Write-Host $fileAddress + + $storageUri = New-Object -TypeName System.Uri -ArgumentList $fileAddress + $cloudFileDirectory = New-Object -TypeName Microsoft.Azure.Storage.File.CloudFileDirectory -ArgumentList $storageUri + $fileShareDirectory = New-Object -TypeName Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileDirectory -ArgumentList $cloudFileDirectory, $psStorageAccount.Context + Write-Host "Name: " $fileShareDirectory.Name + + Mock Get-AzStorageAccount { + $ResourceGroupName | Should -Be $resourceGroup + $Name | Should -Be $storageAccountName + return $psStorageAccount } -Verifiable + Mock Get-AzStorageFile { + $ShareName | Should -Be $fileShareName + $Context | Should -Be $psStorageAccount.Context + return @($fileShareDirectory) } + Mock New-AzStorageDirectory { } + + # Act + Create-AzFileShareStorageFolder ` + -ResourceGroupName $resourceGroup ` + -StorageAccountName $storageAccountName ` + -FileShareName $fileShareName ` + -FolderName $folderName + + # Assert + Assert-VerifiableMock + Assert-MockCalled Get-AzStorageFile -Times 1 + Assert-MockCalled Get-AzStorageAccount -Times 1 + Assert-MockCalled New-AzStorageDirectory -Times 0 + } + It "Copy files to existing Azure File Share" { # Arrange $resourceGroup = "stock" $storageAccountName = "admin" $fileShareName = "shipped-file" - $sourceFolderPath = "shipped" - $destinationFolderName = "shipped" + $sourceFolderPath = "/shipped" + $destinationFolderName = "/shipped" $fileMask = "-suffix" - # Test values, not really pointing to anything - $storageAccount = New-Object -TypeName Microsoft.Azure.Management.Storage.Models.StorageAccount - $testSasToken = "?st=2013-09-03T04%3A12%3A15Z&se=2013-09-03T05%3A12%3A15Z&sr=c&sp=r&sig=fN2NPxLK99tR2%2BWnk48L3lMjutEj7nOwBo7MXs2hEV8%3D" - $testEndpoint = "http://storageaccountname.blob.core.windows.net" - $testConnection = [System.String]::Format("BlobEndpoint={0};QueueEndpoint={0};TableEndpoint={0};SharedAccessSignature={1}", $testEndpoint, $testSasToken) - $storageAccount = [Microsoft.Azure.Storage.CloudStorageAccount]::Parse($testConnection) - $storageContext = New-Object -TypeName Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext -ArgumentList $storageAccount - - $cloudShare = New-Object -TypeName Microsoft.Azure.Storage.File.CloudFileShare -ArgumentList (New-Object -TypeName System.Uri "https://something") - $fileShare = New-Object -TypeName Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare -ArgumentList $cloudShare, $storageContext - $files = @( [pscustomobject]@{ Name = "Container 1$fileMask"; FullName = "Container 1-full" }, [pscustomobject]@{ Name = "Container 2"; FullName = "Container 2-full" }) + $files = @( + [pscustomobject]@{ Name = "Container 1$fileMask"; FullName = "Container 1-full" }, + [pscustomobject]@{ Name = "Container 2"; FullName = "Container 2-full" } + ) Mock Get-AzStorageAccount { $ResourceGroupName | Should -Be $resourceGroup $Name | Should -Be $storageAccountName - return $psStorageAccount } -Verifiable + return $psStorageAccount } -Verifiable Mock Get-AzStorageShare { - $Context | Should -Be $psStorageAccount + $Context | Should -Be $psStorageAccount.Context $Name | Should -Be $fileShareName return $fileShare } -Verifiable Mock Get-ChildItem { $Path | Should -Be $sourceFolderPath return $files } -Verifiable Mock Set-AzStorageFileContent { - $Context | Should -Be $psStorageAccount + $Context | Should -Be $psStorageAccount.Context $ShareName | Should -Be $fileShareName $Source | Should -BeIn ($files | % { $_.FullName }) $Path | Should -Be $destinationFolderName } -Verifiable # Act - Copy-AzFileShareStorageFiles -ResourceGroupName $resourceGroup -StorageAccountName $storageAccountName -FileShareName $fileShareName -SourceFolderPath $sourceFolderPath -DestinationFolderName $destinationFolderName -FileMask $fileMask + Copy-AzFileShareStorageFiles ` + -ResourceGroupName $resourceGroup ` + -StorageAccountName $storageAccountName ` + -FileShareName $fileShareName ` + -SourceFolderPath $sourceFolderPath ` + -DestinationFolderName $destinationFolderName ` + -FileMask $fileMask # Assert Assert-VerifiableMock @@ -98,30 +137,25 @@ Describe "Arcus" { $destinationFolderName = "shipped" $fileMask = "-suffix" - # Test values, not really pointing to anything - $storageAccount = New-Object -TypeName Microsoft.Azure.Management.Storage.Models.StorageAccount - $testSasToken = "?st=2013-09-03T04%3A12%3A15Z&se=2013-09-03T05%3A12%3A15Z&sr=c&sp=r&sig=fN2NPxLK99tR2%2BWnk48L3lMjutEj7nOwBo7MXs2hEV8%3D" - $testEndpoint = "http://storageaccountname.blob.core.windows.net" - $testConnection = [System.String]::Format("BlobEndpoint={0};QueueEndpoint={0};TableEndpoint={0};SharedAccessSignature={1}", $testEndpoint, $testSasToken) - $storageAccount = [Microsoft.Azure.Storage.CloudStorageAccount]::Parse($testConnection) - $storageContext = New-Object -TypeName Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext -ArgumentList $storageAccount - - $cloudShare = New-Object -TypeName Microsoft.Azure.Storage.File.CloudFileShare -ArgumentList (New-Object -TypeName System.Uri "https://something") - $fileShare = New-Object -TypeName Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare -ArgumentList $cloudShare, $storageContext - Mock Get-AzStorageAccount { $ResourceGroupName | Should -Be $resourceGroup $Name | Should -Be $storageAccountName return $psStorageAccount } -Verifiable Mock Get-AzStorageShare { - $Context | Should -Be $psStorageAccount + $Context | Should -Be $psStorageAccount.Context $Name | Should -Be $fileShareName throw [Microsoft.Azure.Storage.StorageException] "Sabotage does not exist getting file share" } Mock Get-ChildItem { } Mock Set-AzStorageFileContent { } # Act - { Copy-AzFileShareStorageFiles -ResourceGroupName $resourceGroup -StorageAccountName $storageAccountName -FileShareName $fileShareName -SourceFolderPath $sourceFolderPath -DestinationFolderName $destinationFolderName -FileMask $fileMask } | + { Copy-AzFileShareStorageFiles ` + -ResourceGroupName $resourceGroup ` + -StorageAccountName $storageAccountName ` + -FileShareName $fileShareName ` + -SourceFolderPath $sourceFolderPath ` + -DestinationFolderName $destinationFolderName ` + -FileMask $fileMask } | Should -Throw # Assert