Public/Set-CloudLoadTestDrop.Tests.ps1

$sut = $MyInvocation.MyCommand.Name -replace ".Tests", ""
. "$PSScriptRoot\$sut"

Describe "Set-CloudLoadTestDrop" {

    $Headers = @{
        Authorization = "Basic IDp0ZXN0"
        'Content-Type' = "application/json; charset=utf-8"
        Accept = "application/json; api-version=1.0"
    }
    $BaseUri = "https://test.vsclt.visualstudio.com"
    $TestDrop = @{
            ContainerName = "ets-containerfor-b4e51292-6cd7-4631-a1eb-caeaf4031abb"
            Id = "ed45ed32-dafb-4178-ba7e-ad25755348f8"
            dropContainerUrl = "https://myaccount.blob.core.windows.net/ets-containerfor-b4e51292-6cd7-4631-a1eb-caeaf4031abb/ed45ed32-dafb-4178-ba7e-ad25755348f8"
            StorageAccountName = "myaccount"
            SasToken = "?sv=2012-02-12&se=2014-06-23T08%3A13%3A34Z&sr=c&si=sas_tenant_policyb4e51292-6cd7-4631-a1eb-caeaf4031abb&sig=zigGSss1xVwz6qDJzmwiR8KzWF%2Bq%2FTiyNegCV%2FCKfrg%3D"
    }
    $TestDirectoryPath = "TestDrive:\LoadTests"
    $LoadTestFileName = "test.loadtest"

    Mock New-AzureStorageContext {
        Return @{
            StorageAccountName = "myaccount"
            BlobEndPoint = "https://myaccount.blob.core.windows.net/"
            TableEndPoint = "https://myaccount.table.core.windows.net/"
            QueueEndPoint = "https://myaccount.queue.core.windows.net/"
            FileEndPoint  = "https://myaccount.file.core.windows.net/"
            Context = "Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext"
            Name = $null
        }
    }
    Mock Set-AzureStorageBlobContent { 
        Return @{
            ICloudBlob        = "Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"
            BlobType          = "BlockBlob"
            Length            = "5"
            ContentType       = "application/octet-stream"
            LastModified      = "19/07/2017 21:49:13 +00:00"
            Context           = "Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext"
            Name              = $BlobName
        }
    }

    Context "Parameter validation" {

        It "Should throw if parameter BaseUri value is not a valid uri" {
            {Set-CloudLoadTestDrop -BaseUri "" -Headers $Headers} | Should throw "Cannot validate argument on parameter 'BaseUri'. Parameter value is not valid ''"
        }

        It "Should throw if parameter Headers value is null or empty" {
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers ""} | Should throw "Cannot process argument transformation on parameter 'Headers'. Cannot convert the `"`" value of type `"System.String`" to type `"System.Collections.Hashtable`"."
        }

        It "Should throw if parameter TestDirectoryPath value is not a valid path format" {
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath ""} | Should throw "Cannot validate argument on parameter 'TestDirectoryPath'. Cannot bind argument to parameter 'Path' because it is an empty string."
        }

        It "Should throw if parameter TestDirectoryPath value is null or empty" {
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath ""} | Should throw "Cannot validate argument on parameter 'TestDirectoryPath'. Cannot bind argument to parameter 'Path' because it is an empty string."
        }

        It "Should throw if parameter TestDirectoryPath value directory does not exist" {
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath $TestDirectoryPath} | Should throw "Cannot validate argument on parameter 'TestDirectoryPath'. Invalid directory path 'TestDrive:\LoadTests'"
        }

        It "Should throw if parameter LoadTestFileName value is null or empty" {
            New-Item -Path $TestDirectoryPath -ItemType Directory
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath $TestDirectoryPath -LoadTestFileName ""} | Should throw "Cannot bind argument to parameter 'LoadTestFileName' because it is an empty string."
            Remove-Item -Path $TestDirectoryPath -Recurse -Force
        }

        It "Should throw if parameter LoadTestFileName value file does not exist" {
            New-Item -Path $TestDirectoryPath -ItemType Directory
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath $TestDirectoryPath -LoadTestFileName $LoadTestFileName} | Should throw "Load test file 'test.loadtest' not found in directory 'TestDrive:\LoadTests'"
            Remove-Item -Path $TestDirectoryPath -Recurse -Force
            
        }
    }


    Context "Test drop" {

        Mock Set-AzureStorageBlobContent{ 
        Return @{
            ICloudBlob        = "Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob"
            BlobType          = "BlockBlob"
            Length            = "5"
            ContentType       = "application/octet-stream"
            LastModified      = "19/07/2017 21:49:13 +00:00"
            Context           = "Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext"
            Name              = $BlobName
        }
    }

        It "Should publish files to the test drop" {
            New-Item -Path $TestDirectoryPath -ItemType Directory
            New-Item -Path $TestDirectoryPath -Name $LoadTestFileName -ItemType File
            {Set-CloudLoadTestDrop -BaseUri $BaseUri -Headers $Headers -TestDrop $TestDrop -TestDirectoryPath $TestDirectoryPath -LoadTestFileName $LoadTestFileName} | Should not throw
            Assert-MockCalled -CommandName New-AzureStorageContext -Times 1
            Assert-MockCalled -CommandName Set-AzureStorageBlobContent -Times 1
        }

    }

}