Skip to content

Commit

Permalink
[Storage] Add live test to cover File/Table, and cover StorageContext…
Browse files Browse the repository at this point in the history
… generate in different ways (#26069) (#26226)

* [Storage] Add live test to cover File/Table, and cover StorageContext generate in different ways

* remove change log

Co-authored-by: Wei Wei <[email protected]>
  • Loading branch information
VeryEarly and blueww authored Oct 8, 2024
1 parent b11e991 commit 9a89841
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 42 deletions.
16 changes: 9 additions & 7 deletions src/Storage/LiveTests/Storage.Management/BlobTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
$ContentMD5 = "i727sP7HigloQDsqadNLHw=="
$testfile512path = "$PSScriptRoot\TestFiles\testfile512"

$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowBlobPublicAccess $true
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowBlobPublicAccess $true -AllowSharedKeyAccess $true -Tag @{"Az.Sec.DisableAllowSharedKeyAccess::Skip" = "For Powershell test."}
$ctx = $account.Context
$ctx1 = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)[0].Value

$container = New-AzStorageContainer -Name $containerName -Context $ctx
$containerSAS = New-AzStorageContainerSASToken -Name $containerName -Permission radwl -ExpiryTime 5000-01-01 -Context $ctx
$sasCtx = New-AzStorageContext -SasToken $containerSAS -StorageAccountName $storageAccountName
Expand Down Expand Up @@ -50,13 +52,13 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
$b.FetchAttributes()
Assert-AreEqual "Hot" $b.BlobProperties.AccessTier

$b2 = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName2 -Force -Context $ctx
$b2 = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName2 -Force -Context $ctx1
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
Assert-AreEqual 2 $blobs.Count
$b3 = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Force -Context $sasCtx
$b2.BlobBaseClient.CreateSnapshot()

$blobs = Get-AzStorageBlob -Container $containerName -Prefix "blob3" -Context $ctx
$blobs = Get-AzStorageBlob -Container $containerName -Prefix "blob3" -Context $ctx1
Assert-AreEqual 1 $blobs.Count
Assert-AreEqual $blobName3 $blobs[0].Name

Expand All @@ -66,7 +68,7 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
Assert-AreEqual $blobName2 $blob.Name
Assert-AreEqual $snapshotblob.SnapshotTime $blob.SnapshotTime

$blob = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Tag @{"Tag1" = "Value2"; "Tag2" = "Value2" } -Context $ctx -Force
$blob = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Tag @{"Tag1" = "Value2"; "Tag2" = "Value2" } -Context $ctx1 -Force
$blob = Get-AzStorageBlob -Blob $blobName3 -Container $containerName -IncludeTag -Context $ctx
Assert-AreEqual $blobName3 $blob.Name
Assert-AreEqual 2 $blob.TagCount
Expand All @@ -77,18 +79,18 @@ Invoke-LiveTestScenario -Name "Blob basics" -Description "Test blob basic operat
$blob = Set-AzStorageBlobContent -File $testfile512path -Container $containerName -Blob $blobName3 -Context $sasctx -Force
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx -IncludeVersion
$versionBlob = $blobs | ? { $_.VersionId -ne $null } | Select-Object -First 1
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName3 -VersionId $versionBlob.VersionId -Context $ctx
$blob = Get-AzStorageBlob -Container $containerName -Blob $blobName3 -VersionId $versionBlob.VersionId -Context $ctx1
Assert-AreEqual $blobName3 $blob.Name
Assert-AreEqual $versionBlob.VersionId $blob.VersionId

$b2 | Remove-AzStorageBlob -Force
$blobs = Get-AzStorageBlob -Container $containerName -Context $ctx
Assert-AreEqual 2 $blobs.Count

$container = Get-AzStorageContainer -Name $containerName -Context $ctx
$container = Get-AzStorageContainer -Name $containerName -Context $ctx1
$containerProperties = $container.BlobContainerClient.GetProperties().Value
Assert-AreEqual $container.BlobContainerProperties.ETag $containerProperties.ETag
Set-AzStorageContainerAcl -Name $containerName -Permission Blob -Context $ctx
Set-AzStorageContainerAcl -Name $containerName -Permission Blob -Context $ctx1
$containerProperties = $container.BlobContainerClient.GetProperties().Value
Assert-AreNotEqual $container.BlobContainerProperties.ETag $containerProperties.ETag
$container.FetchAttributes()
Expand Down
149 changes: 149 additions & 0 deletions src/Storage/LiveTests/Storage.Management/FileTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
Invoke-LiveTestScenario -Name "File basics" -Description "Test File basic operation" -ScenarioScript `
{
param ($rg)

$rgName = $rg.ResourceGroupName
$storageAccountName = New-LiveTestStorageAccountName
$shareName = New-LiveTestResourceName
$testfile512path = "$PSScriptRoot\TestFiles\testfile512"
$localDestFile = "$PSScriptRoot\TestFiles\dest"
$location = $rg.Location
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowSharedKeyAccess $true -Tag @{"Az.Sec.DisableAllowSharedKeyAccess::Skip" = "For Powershell test."}
$ctx = $account.Context
$ctx1 = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)[0].Value

$objectName1 = "filetest1.txt."
$objectName2 = "filetest2.txt"

#Create a file share
New-AzStorageShare $shareName -Context $ctx
$Share = Get-AzStorageShare -Name $shareName -Context $ctx
Assert-AreEqual $Share.Count 1
Assert-AreEqual $Share[0].Name $shareName

# upload file
$t = Set-AzStorageFileContent -source $testfile512path -ShareName $shareName -Path $objectName1 -Force -Context $ctx -asjob
$t | wait-job
Assert-AreEqual $t.State "Completed"
Assert-AreEqual $t.Error $null

# upload/remove file/dir with -DisAllowTrailingDot
$dirName1WithTrailingDot = "testdir1.."
$dirName1WithOutTrailingDot = "testdir1"
$objectPathWithoutTrailingDot = "testdir1/filetest1.txt"
New-AzStorageDirectory -ShareName $shareName -Path $dirName1WithTrailingDot -Context $ctx1 -DisAllowTrailingDot
$file11 = Set-AzStorageFileContent -source $testfile512path -ShareName $shareName -Path "$($dirName1WithTrailingDot)/$($objectName1)" -Force -Context $ctx1 -DisAllowTrailingDot
$file = Get-AzStorageFile -ShareName $shareName -Path $objectPathWithoutTrailingDot -Context $ctx1 -DisAllowTrailingDot
Assert-AreEqual $file.Count 1
Assert-AreEqual $file[0].ShareFileClient.Path $objectPathWithoutTrailingDot
Remove-AzStorageFile -ShareName $shareName -Path "$($dirName1WithTrailingDot)/$($objectName1)" -Context $ctx1 -DisAllowTrailingDot
Remove-AzStorageDirectory -ShareName $shareName -Path $dirName1WithTrailingDot -Context $ctx1 -DisAllowTrailingDot

# list file
$file = Get-AzStorageFile -ShareName $shareName -Context $ctx
Assert-AreEqual $file.Count 1
Assert-AreEqual $file[0].Name $objectName1
Assert-NotNull $file[0].ListFileProperties.Properties.ETag

if ($Env:OS -eq "Windows_NT")
{
Set-AzStorageFileContent -source $testfile512path -ShareName $shareName -Path $objectName1 -PreserveSMBAttribute -Force -Context $ctx
}
else
{
Set-AzStorageFileContent -source $testfile512path -ShareName $shareName -Path $objectName1 -Force -Context $ctx
}
$file = Get-AzStorageFile -ShareName $shareName -Context $ctx
Assert-AreEqual $file.Count 1
Assert-AreEqual $file[0].Name $objectName1
Assert-NotNull $file[0].ListFileProperties.Properties.ETag
if ($Env:OS -eq "Windows_NT")
{
$localFileProperties = Get-ItemProperty $testfile512path
Assert-AreEqual $localFileProperties.CreationTime.ToUniversalTime().Ticks $file[0].ListFileProperties.Properties.CreatedOn.ToUniversalTime().Ticks
Assert-AreEqual $localFileProperties.LastWriteTime.ToUniversalTime().Ticks $file[0].ListFileProperties.Properties.LastWrittenOn.ToUniversalTime().Ticks
Assert-AreEqual $localFileProperties.Attributes.ToString() $file[0].ListFileProperties.FileAttributes.ToString()
}

Start-AzStorageFileCopy -SrcShareName $shareName -SrcFilePath $objectName1 -DestShareName $shareName -DestFilePath $objectName2 -Force -Context $ctx -DestContext $ctx
Get-AzStorageFileCopyState -ShareName $shareName -FilePath $objectName2 -Context $ctx -WaitForComplete
$file = Get-AzStorageFile -ShareName $shareName -Context $ctx
Assert-AreEqual $file.Count 2
Assert-AreEqual $file[0].Name $objectName1
Assert-AreEqual $file[1].Name $objectName2

$t = Get-AzStorageFileContent -ShareName $shareName -Path $objectName1 -Destination $localDestFile -Force -Context $ctx -asjob
$t | wait-job
Assert-AreEqual $t.State "Completed"
Assert-AreEqual $t.Error $null
Assert-AreEqual (Get-FileHash -Path $localDestFile -Algorithm MD5).Hash (Get-FileHash -Path $testfile512path -Algorithm MD5).Hash

if ($Env:OS -eq "Windows_NT")
{
Get-AzStorageFileContent -ShareName $shareName -Path $objectName1 -Destination $localDestFile -PreserveSMBAttribute -Force -Context $ctx1
}
else
{
Get-AzStorageFileContent -ShareName $shareName -Path $objectName1 -Destination $localDestFile -Force -Context $ctx
}
Assert-AreEqual (Get-FileHash -Path $localDestFile -Algorithm MD5).Hash (Get-FileHash -Path $testfile512path -Algorithm MD5).Hash
if ($Env:OS -eq "Windows_NT")
{
$file = Get-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $ctx1
$localFileProperties = Get-ItemProperty $testfile512path
Assert-AreEqual $localFileProperties.CreationTime.ToUniversalTime().Ticks $file[0].FileProperties.SmbProperties.FileCreatedOn.ToUniversalTime().Ticks
Assert-AreEqual $localFileProperties.LastWriteTime.ToUniversalTime().Ticks $file[0].FileProperties.SmbProperties.FileLastWrittenOn.ToUniversalTime().Ticks
Assert-AreEqual $localFileProperties.Attributes.ToString() $file[0].FileProperties.SmbProperties.FileAttributes.ToString()
}

$fileName1 = "new" + $objectName1
$file = Get-AzStorageFile -ShareName $shareName -Path $objectName1 -Context $ctx

$file2 = Rename-AzStorageFile -ShareName $shareName -SourcePath $objectName1 -DestinationPath $fileName1 -Context $ctx
Assert-AreEqual $file2.Name $fileName1
Assert-AreEqual $file.FileProperties.ContentType $file2.FileProperties.ContentType
Assert-AreEqual $file.FileProperties.ContentLength $file2.FileProperties.ContentLength

$file3 = $file2 | Rename-AzStorageFile -DestinationPath $fileName1 -Context $ctx1 -Force
Assert-AreEqual $file3.Name $fileName1
Assert-AreEqual $file2.FileProperties.ContentType $file3.FileProperties.ContentType
Assert-AreEqual $file2.FileProperties.ContentLength $file3.FileProperties.ContentLength

Remove-AzStorageFile -ShareName $shareName -Path $fileName1 -Context $ctx
$file = Get-AzStorageFile -ShareName $shareName -Context $ctx
Assert-AreEqual $file.Count 1
Assert-AreEqual $file[0].Name $objectName2

$dirName = "filetestdir"
New-AzStorageDirectory -ShareName $shareName -Path $dirName -Context $ctx
$file = Get-AzStorageShare -Name $shareName -Context $ctx1 | Get-AzStorageFile -ExcludeExtendedInfo
Assert-AreEqual $file.Count 2
Assert-AreEqual $file[0].Name $dirName
Assert-AreEqual $file[0].GetType().Name "AzureStorageFileDirectory"
Assert-Null $file[0].ListFileProperties.Properties.ETag
Assert-AreEqual $file[1].Name $objectName2
Assert-AreEqual $file[1].GetType().Name "AzureStorageFile"
Assert-Null $file[1].ListFileProperties.Properties.ETag

$newDir = "new" + $dirName + ".."
$dir = Get-AzStorageFile -ShareName $shareName -Path $dirName -Context $ctx
$dir2 = Rename-AzStorageDirectory -ShareName $shareName -SourcePath $dirName -DestinationPath $newDir -Context $ctx1
Assert-AreEqual $newDir $dir2.Name
Assert-AreEqual $dir.ListFileProperties.IsDirectory $dir2.ListFileProperties.IsDirectory
Assert-AreEqual $dir.ListFileProperties.FileAttributes $dir2.ListFileProperties.FileAttributes

$newDir2 = "new2" + $dirName
$dir3 = $dir2 | Rename-AzStorageDirectory -DestinationPath $newDir2 -Context $ctx1
Assert-AreEqual $newDir2 $dir3.Name
Assert-AreEqual $dir2.ListFileProperties.IsDirectory $dir3.ListFileProperties.IsDirectory
Assert-AreEqual $dir2.ListFileProperties.FileAttributes $dir3.ListFileProperties.FileAttributes

$dir3 | Remove-AzStorageDirectory
$file = Get-AzStorageFile -ShareName $shareName -Context $ctx
Assert-AreEqual $file.Count 1
Assert-AreEqual $file[0].Name $objectName2
Assert-AreEqual $file[0].GetType().Name "AzureStorageFile"

# Clean Storage Account
Remove-AzStorageShare -Name $shareName -Force -Context $ctx
}
13 changes: 7 additions & 6 deletions src/Storage/LiveTests/Storage.Management/QueueTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Invoke-LiveTestScenario -Name "Queue basics" -Description "Test queue basic oper
$queueName = New-LiveTestResourceName
$queueName2 = New-LiveTestResourceName
$location = $rg.Location
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowSharedKeyAccess $true -Tag @{"Az.Sec.DisableAllowSharedKeyAccess::Skip" = "For Powershell test."}
$ctx = $account.Context
$ctx1 = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)[0].Value

$q = New-AzStorageQueue -Name $queueName -Context $ctx
Assert-AreEqual $queueName $q.Name
Expand All @@ -17,16 +18,16 @@ Invoke-LiveTestScenario -Name "Queue basics" -Description "Test queue basic oper
Assert-AreEqual 0 $q.ApproximateMessageCount
Assert-AreEqual 0 $q.QueueProperties.ApproximateMessagesCount

$q = New-AzStorageQueue -Name $queueName2 -Context $ctx
$qs = Get-AzStorageQueue -Context $ctx
$q = New-AzStorageQueue -Name $queueName2 -Context $ctx1
$qs = Get-AzStorageQueue -Context $ctx1
Assert-AreEqual 2 $qs.Count

$sas = New-AzStorageAccountSASToken -Service Queue -ResourceType Container,Object,Service -Permission rwdl -ExpiryTime 3000-01-01 -Context $ctx
$ctxaccountsas = New-AzStorageContext -StorageAccountName $storageAccountName -SasToken $sas
$qs = Get-AzStorageQueue -Context $ctxaccountsas
Assert-AreEqual 2 $qs.Count

$sas = New-AzStorageQueueSASToken -Name $queueName -Context $ctx -Permission ruap
$sas = New-AzStorageQueueSASToken -Name $queueName -Context $ctx1 -Permission ruap
$sasctx = New-AzStorageContext -StorageAccountName $storageAccountName -SasToken $sas
$q = Get-AzStorageQueue -Name $queueName -Context $sasctx
Assert-AreEqual $queueName $q.Name
Expand All @@ -53,10 +54,10 @@ Invoke-LiveTestScenario -Name "Queue basics" -Description "Test queue basic oper
Assert-Null $p.StartTime
Assert-AreEqual "rau" $p.Permissions

$p = New-AzStorageQueueStoredAccessPolicy -Queue $queueName -Policy $policyName2 -Permission ruap -Context $ctx
$p = New-AzStorageQueueStoredAccessPolicy -Queue $queueName -Policy $policyName2 -Permission ruap -Context $ctx1
$p = Get-AzStorageQueueStoredAccessPolicy -Queue $queueName -Context $ctx
Assert-AreEqual 2 $p.Count
$p = Get-AzStorageQueueStoredAccessPolicy -Queue $queueName -Policy $policyName1 -Context $ctx
$p = Get-AzStorageQueueStoredAccessPolicy -Queue $queueName -Policy $policyName1 -Context $ctx1
Assert-AreEqual $policyName1 $p.Policy

Remove-AzStorageQueueStoredAccessPolicy -Queue $queueName -Policy $policyName1 -Context $ctx
Expand Down
57 changes: 57 additions & 0 deletions src/Storage/LiveTests/Storage.Management/TableTests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Invoke-LiveTestScenario -Name "Table basics" -Description "Test Table basic operation" -ScenarioScript `
{
param ($rg)

$rgName = $rg.ResourceGroupName
$storageAccountName = New-LiveTestStorageAccountName
$tableName = New-LiveTestResourceName
$tableName2 = New-LiveTestResourceName
$location = $rg.Location
$account = New-AzStorageAccount -ResourceGroupName $rgName -Name $storageAccountName -Location $location -SkuName Standard_GRS -AllowSharedKeyAccess $true -Tag @{"Az.Sec.DisableAllowSharedKeyAccess::Skip" = "For Powershell test."}
$ctx = $account.Context
$ctx1 = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey (Get-AzStorageAccountKey -ResourceGroupName $rgName -Name $storageAccountName)[0].Value

# Create Table
New-AzStorageTable -Name $tableName -Context $ctx
$table =Get-AzStorageTable -Name $tableName -Context $ctx
Assert-AreEqual $table.Count 1
Assert-AreEqual $table[0].Name $tableName
New-AzStorageTable -Name $tableName2 -Context $ctx1
$table2 =Get-AzStorageTable -Name $tableName2 -Context $ctx1
Assert-AreEqual $table2.Count 1
Assert-AreEqual $table2[0].Name $tableName2

#Test run Table query - Insert Entity
$partitionKey = "p123"
$rowKey = "row123"
$tableRow = [Azure.Data.Tables.TableEntity]::new($partitionKey, $rowKey)
$tableRow.Add("Name", "name1")
$tableRow.Add("ID", 4567)
$methodAddEntity = [Azure.Data.Tables.TableClient].GetMethods() | Where-Object Name -eq "AddEntity"
$methodAddEntity = $methodAddEntity.MakeGenericMethod([Azure.Data.Tables.TableEntity])
$methodAddEntity.Invoke($table.TableClient, @([Azure.Data.Tables.TableEntity]$tableRow, $null))

# Create Table Object - which reference to exist Table with SAS
$tableSASUri = New-AzStorageTableSASToken -Name $tablename -Permission "raud" -ExpiryTime (([DateTime]::UtcNow.AddDays(10))) -FullUri -Context $ctx
$uri = [System.Uri]$tableSASUri
$sasTable = New-Object -TypeName Azure.Data.Tables.TableClient $uri

#Get Entity
$methodGetEntity = [Azure.Data.Tables.TableClient].GetMethods() | Where-Object Name -eq "GetEntity"
$methodGetEntity = $methodGetEntity.MakeGenericMethod([Azure.Data.Tables.TableEntity])
$entity = $methodGetEntity.Invoke($sasTable, @($partitionKey,$rowKey, $null, $null)).Value
Assert-AreEqual $partitionKey $entity["PartitionKey"]
Assert-AreEqual $rowKey $entity["RowKey"]
Assert-AreEqual "name1" $entity["Name"]
Assert-AreEqual 4567 $entity["ID"]

# Get/Remove Table
$tableCount1 = (Get-AzStorageTable -Context $ctx).Count
Assert-AreEqual $tableCount1 2
Remove-AzStorageTable -Name $tableName -Force -Context $ctx
$tableCount2 = (Get-AzStorageTable -Context $ctx1).Count
Assert-AreEqual $tableCount2 1
$table2 | Remove-AzStorageTable -Force
$tableCount3 = (Get-AzStorageTable -Context $ctx1).Count
Assert-AreEqual $tableCount3 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ Invoke-LiveTestScenario -Name "Removes a Storage account" -Description "Test rem
}

& "$PSScriptRoot\QueueTests.ps1"
& "$PSScriptRoot\BlobTests.ps1"
& "$PSScriptRoot\BlobTests.ps1"
& "$PSScriptRoot\TableTests.ps1"
& "$PSScriptRoot\FileTests.ps1"
Loading

0 comments on commit 9a89841

Please sign in to comment.