Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.27k stars 3.87k forks source link

Integrate Storage data plane scenario tests #5314

Open MiYanni opened 6 years ago

MiYanni commented 6 years ago
blueww commented 6 years ago

Add the updated script here. To run the script, need provide the account name,key, and a local file which is 512 byte aligned.

$ctx = New-AzureStorageContext -StorageAccountName [accountName] -StorageAccountKey [key]
$localSrcFile = "C:\temp\temp.psd1" #The file need exist before test, and should be 512 bytes aligned
$localDestFile = "C:\temp\test1.txt "
$containerName = "weitest1"

    #Blob. Container
    $objectName1 = "test1.txt"
    $objectName2 = "test2.txt"
    $objectName3 = "test3.txt"
    $ContentType = "image/jpeg"
    $ContentMD5 = "i727sP7HigloQDsqadNLHw=="
    New-AzureStorageContainer $containerName -Context $ctx

    $sas = New-AzureStorageAccountSASToken -Service Blob -ResourceType Service,Container -Permission rl -Context $ctx
    $ctxsas = New-AzureStorageContext -StorageAccountName $ctx.StorageAccountName -SasToken $sas
    $container = Get-AzureStorageContainer -Context $ctxsas
    Assert-True {$container.Count -ge 1}

    #Blob. Upload
    Set-AzureStorageBlobContent -File $localSrcFile -Container $containerName -Blob $objectName1 -Force -Properties @{"ContentType" = $ContentType; "ContentMD5" = $ContentMD5} -Context $ctx
    $blob = Get-AzureStorageContainer -Name $containerName -Context $ctx |Get-AzureStorageBlob
    Assert-AreEqual $blob.Count 1
    Assert-AreEqual $blob.Name $objectName1
    Assert-AreEqual $blob.ICloudBlob.Properties.ContentType $ContentType
    Assert-AreEqual $blob.ICloudBlob.Properties.ContentMD5 $ContentMD5

    #Blob. Copy
    Start-AzureStorageBlobCopy -srcContainer $containerName -SrcBlob $objectName1 -DestContainer $containerName -DestBlob $objectName2 -Context $ctx -DestContext $ctx
    Get-AzureStorageBlobCopyState -Container $containerName -Blob $objectName2 -Context $ctx
    $blob = Get-AzureStorageBlob -Container $containerName -Context $ctx
    Assert-AreEqual $blob.Count 2
    Assert-AreEqual $blob[0].Name $objectName1
    Assert-AreEqual $blob[1].Name $objectName2

    #Blob. download
    Get-AzureStorageBlobContent -Container $containerName -Blob $objectName2 -Destination $localDestFile -Force -Context $ctx
    Assert-AreEqual (Get-FileHash -Path $localDestFile -Algorithm MD5).Hash (Get-FileHash -Path $localSrcFile -Algorithm MD5).Hash

    #Blob. Delete
    Remove-AzureStorageBlob -Container $containerName -Blob $objectName2 -Force -Context $ctx
    $blob = Get-AzureStorageBlob -Container $containerName -Context $ctx
    Assert-AreEqual $blob.Count 1
    Assert-AreEqual $blob[0].Name $objectName1

    #Blob. Snapshot and incrementalCopy
    $pageBlobName1 = "page1"
    $pageBlobName2 = "page2"
    $b = Set-AzureStorageBlobContent -File $localSrcFile -Container $containerName -Blob $pageBlobName1 -Force -BlobType page -Context $ctx
    # .net can use sync call
    # $snapshot = $b.ICloudBlob.Snapshot()
    $task = $b.ICloudBlob.SnapshotAsync() 
    $snapshot = $task.Result  
    $blob = Get-AzureStorageBlob -Container $containerName -Context $ctx | where {$_.Name -eq $pageBlobName1}
    Assert-AreEqual $blob.Count 2
    Assert-AreEqual $blob[0].ICloudBlob.IsSnapshot $true
    Assert-AreEqual $blob[1].ICloudBlob.IsSnapshot $false
    Start-AzureStorageBlobIncrementalCopy -srcContainer $containerName -SrcBlob $pageBlobName1 -SrcBlobSnapshotTime $snapshot.SnapshotTime -DestContainer $containerName -DestBlob $pageBlobName2 -Context $ctx -DestContext $ctx
    $blob = Get-AzureStorageBlob -Container $containerName -Context $ctx | where {$_.Name -eq $pageBlobName2}
    Assert-AreEqual $blob.Count 2
    Assert-AreEqual $blob[0].ICloudBlob.IsSnapshot $true
    Assert-AreEqual $blob[1].ICloudBlob.IsSnapshot $false

    #File. share . Create, snapshot, delete
    New-AzureStorageShare $containerName -Context $ctx
    $Share = Get-AzureStorageShare -Name $containerName -Context $ctx
    Assert-AreEqual $Share.Count 1
    Assert-AreEqual $Share[0].Name $containerName
    $task = $Share.SnapshotAsync() 
    $snapshot = $task.Result  
    $Shares = Get-AzureStorageShare -prefix $containerName -Context $ctx
    Assert-AreEqual $Shares.Count 2
    $snapshot.DeleteAsync()

    #File.Upload
    Set-AzureStorageFileContent -source $localSrcFile -ShareName $containerName -Path $objectName1 -Force -Context $ctx
    $file = Get-AzureStorageFile -ShareName $containerName -Context $ctx
    Assert-AreEqual $file.Count 1
    Assert-AreEqual $file[0].Name $objectName1

    #File.Copy
    Start-AzureStorageFileCopy -SrcShareName $containerName -SrcFilePath $objectName1 -DestShareName $containerName -DestFilePath $objectName2 -Force -Context $ctx -DestContext $ctx
    Get-AzureStorageFileCopyState -ShareName $containerName -FilePath $objectName2 -Context $ctx -WaitForComplete
    $file = Get-AzureStorageFile -ShareName $containerName -Context $ctx
    Assert-AreEqual $file.Count 2
    Assert-AreEqual $file[0].Name $objectName1
    Assert-AreEqual $file[1].Name $objectName2

    #File.Download
    Get-AzureStorageFileContent -ShareName $containerName -Path $objectName1 -Destination $localDestFile -Force -Context $ctx    
    Assert-AreEqual (Get-FileHash -Path $localDestFile -Algorithm MD5).Hash (Get-FileHash -Path $localSrcFile -Algorithm MD5).Hash

    #File. Delete
    Remove-AzureStorageFile -ShareName $containerName -Path $objectName1 -Context $ctx
    $file = Get-AzureStorageFile -ShareName $containerName -Context $ctx
    Assert-AreEqual $file.Count 1
    Assert-AreEqual $file[0].Name $objectName2

    #File. Directory
    $dirName = "testdir"
    New-AzureStorageDirectory -ShareName $containerName -Path $dirName -Context $ctx    
    $file = Get-AzureStorageFile -ShareName $containerName -Context $ctx
    Assert-AreEqual $file.Count 2
    Assert-AreEqual $file[0].Name $objectName2
    Assert-AreEqual $file[0].GetType().Name "CloudFile"
    Assert-AreEqual $file[1].Name $dirName
    Assert-AreEqual $file[1].GetType().Name "CloudFileDirectory"
    Remove-AzureStorageDirectory -ShareName $containerName -Path $dirName -Context $ctx  
    $file = Get-AzureStorageFile -ShareName $containerName -Context $ctx
    Assert-AreEqual $file.Count 1
    Assert-AreEqual $file[0].Name $objectName2
    Assert-AreEqual $file[0].GetType().Name "CloudFile"

    #blob<->File Copy
    Start-AzureStorageBlobCopy  -SrcShareName $containerName -SrcFilePath $objectName2 -DestContainer $containerName -DestBlob $objectName3 -Force -Context $ctx -DestContext $ctx
    Get-AzureStorageBlobCopyState -Container $containerName -Blob $objectName3 -Context $ctx    
    $blob = Get-AzureStorageBlob -Container $containerName -Blob $objectName3 -Context $ctx
    Assert-AreEqual $blob.Count 1
    Assert-AreEqual $blob[0].Name $objectName3

    Start-AzureStorageFileCopy  -SrcContainerName $containerName -SrcBlobName $objectName1  -DestShareName $containerName -DestFilePath $objectName3 -Force -Context $ctx -DestContext $ctx
    Get-AzureStorageFileCopyState -ShareName $containerName -FilePath $objectName3 -Context $ctx    
    $file = Get-AzureStorageFile -ShareName $containerName -Path $objectName3 -Context $ctx
    Assert-AreEqual $file.Count 1
    Assert-AreEqual $file[0].Name $objectName3

    Remove-AzureStorageShare -Name $containerName -Force -Context $ctx
    Remove-AzureStorageContainer -Name $containerName -Force -Context $ctx

    #Table
    New-AzureStorageTable -Name $containerName -Context $ctx
    $table =Get-AzureStorageTable -Name $containerName -Context $ctx
    Assert-AreEqual $table.Count 1
    Assert-AreEqual $table[0].Name $containerName

    $tableCount1 = (Get-AzureStorageTable -Context $ctx).Count
    Remove-AzureStorageTable -Name $containerName -Force -Context $ctx
    $table2 = Get-AzureStorageTable -Context $ctx
    if ($table2 -eq $null) { 
        $tableCount2 = 0
    }
    else {
        $tableCount2 = $table2.Count
    }    
    Assert-AreEqual ($tableCount1-$tableCount2) 1

    #Queue
    New-AzureStorageQueue -Name $containerName -Context $ctx
    $queue = Get-AzureStorageQueue -Name $containerName -Context $ctx
    Assert-AreEqual $queue.Count 1
    Assert-AreEqual $queue[0].Name $containerName

    $queueCount1 = (Get-AzureStorageQueue -Context $ctx).Count
    Remove-AzureStorageQueue -Name $containerName -Force -Context $ctx
    Get-AzureStorageQueue -Context $ctx    
    if ($queue2 -eq $null) { 
        $queueCount2 = 0
    }
    else {
        $queueCount2 = $queue2.Count
    }    
    Assert-AreEqual ($queueCount1-$queueCount2) 1

    #common
    $version = "1.0"
    $retentionDays = 2
    $LoggingOperations = "All"

    Set-AzureStorageServiceLoggingProperty -ServiceType blob -RetentionDays $retentionDays -Version $version -LoggingOperations $LoggingOperations -Context $ctx
    $property = Get-AzureStorageServiceLoggingProperty -ServiceType blob -Context $ctx
    Assert-AreEqual $property.LoggingOperations.ToString() $LoggingOperations
    Assert-AreEqual $property.Version $version
    Assert-AreEqual $property.RetentionDays $retentionDays

    $MetricsLevel = "Service"
    Set-AzureStorageServiceMetricsProperty -ServiceType blob -Version $version -MetricsType Hour -RetentionDays $retentionDays -MetricsLevel $MetricsLevel -Context $ctx
    $property = Get-AzureStorageServiceMetricsProperty -ServiceType Blob -MetricsType Hour -Context $ctx
    Assert-AreEqual $property.MetricsLevel.ToString() $MetricsLevel
    Assert-AreEqual $property.Version $version
    Assert-AreEqual $property.RetentionDays $retentionDays

    Set-AzureStorageCORSRule -ServiceType blob -Context $ctx -CorsRules (@{
        AllowedHeaders=@("x-ms-blob-content-type","x-ms-blob-content-disposition");
        AllowedOrigins=@("*");
        MaxAgeInSeconds=30;
        AllowedMethods=@("Get","Connect")},
        @{
        AllowedOrigins=@("http://www.fabrikam.com","http://www.contoso.com"); 
        ExposedHeaders=@("x-ms-meta-data*","x-ms-meta-customheader"); 
        AllowedHeaders=@("x-ms-meta-target*","x-ms-meta-customheader");
        MaxAgeInSeconds=30;
        AllowedMethods=@("Put")})
    $cors = Get-AzureStorageCORSRule -ServiceType blob -Context $ctx
    Assert-AreEqual $cors.Count 2
    Remove-AzureStorageCORSRule -ServiceType blob -Context $ctx
    $cors = Get-AzureStorageCORSRule -ServiceType blob -Context $ctx
    Assert-AreEqual $cors.Count 0

    Enable-AzureStorageDeleteRetentionPolicy -RetentionDays 3 -Context $ctx
    $property = Get-AzureStorageServiceProperty -ServiceType blob -Context $ctx
    Assert-AreEqual $property.DeleteRetentionPolicy.Enabled $true
    Assert-AreEqual $property.DeleteRetentionPolicy.RetentionDays 3

    Disable-AzureStorageDeleteRetentionPolicy  -Context $ctx
    $property = Get-AzureStorageServiceProperty -ServiceType blob -Context $ctx
    Assert-AreEqual $property.DeleteRetentionPolicy.Enabled $false