Public/Set-CloudLoadTestDrop.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
$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
        }

    }

}