Start-IpsGcpPrepareJob.Tests.ps1

BeforeAll {
    Add-PSSnapin Citrix.*
    . $PSScriptRoot\Start-IpsGcpPrepareJob.ps1
    . $PSScriptRoot\JobParamMethods.ps1
    . $PSScriptRoot\PlatformMethods.ps1
    . $PSScriptRoot\PrepareMethods.ps1
    . $PSScriptRoot\Auth.ps1
    . $PSScriptRoot\CCRestMethod.ps1
    . $PSScriptRoot\Log.ps1
    . $PSScriptRoot\VersionCheck.ps1
}

Describe 'Test parameter handling' {

    $validate = {
        Param([PSCustomObject]$body)
        $body.prefix | Should -Be "ips"
        $body.provisioningType | Should -Be "Mcs"
        $body.platformCredentialId | Should -Be "nightly-gcp-1"
        $body.resourceLocationId | Should -Be "52509414-3c19-4a8e-a711-01837da5a1c8"
        $body.targetImageName | Should -Be "nightly-mia-win10"
        $body.outputDiskName | Should -Be "nightly-mia-win10-out"
        $body.xdReconfigure -is [Array] | Should -Be $true
        $body.xdReconfigure | Should -HaveCount 2
        $body.xdReconfigure[0].ParameterName | Should -Be "controllers"
        $body.xdReconfigure[0].ParameterValue | Should -Be "cloudy-cc1.wse2edev.cloudy"
        $body.xdReconfigure[1].ParameterName | Should -Be "portnumber"
        $body.xdReconfigure[1].ParameterValue | Should -Be "80"
        $body.networkTags -is [Array] | Should -Be $true
        $body.networkTags | Should -HaveCount 1
        $body.networkTags[0] | Should -Be "cloudy"

        $tags = Convert-Object $body.tags
        $tags.Count | Should -Be 2
        $tags.ContainsKey("ctx-user") | Should -Be $true
        $tags.test | Should -Be "abcdef"
    }

    $tests = @(
        @{
            name = "file config"
            config = @'
{
    "Deployment": "api.test.cloud.com",
    "CustomerId": "wse2edev",
    "Prefix": "ips",
    "CloudPlatform": "gcp",
    "CloudCwSecretId": "nightly-gcp-1",
    "CloudProvisioningType": "Mcs",
    "CloudDiskName": "nightly-mia-win10",
    "OutputDiskName": "nightly-mia-win10-out",
    "XdReconfigure": [
        {"ParameterName": "controllers", "ParameterValue": "cloudy-cc1.wse2edev.cloudy"},
        {"ParameterName": "portnumber", "ParameterValue": "80"}
    ],
    "ResourceLocationId": "52509414-3c19-4a8e-a711-01837da5a1c8",
    "GcpZone": "us-east4-a",
    "VpcNetworkName": "cloudy-ce",
    "VpcNetworkSubnetName": "regions/us-east4/subnetworks/cloudy-ce-us-east4",
    "NetworkTags": ["cloudy"],
    "Tags": {"test": "abcdef"}
}
'@

            validate = $validate
        },
        @{
            name = "splat"
            params = @{
                Deployment = 'api.test.cloud.com'
                CustomerId = 'qvj487ll7e68'
                ResourceLocationId = '52509414-3c19-4a8e-a711-01837da5a1c8'
                CloudProvisioningType = 'Mcs'
                CloudDiskName = 'nightly-mia-win10'
                GcpCwSecretId = 'nightly-gcp-1'
                GcpZone = 'us-east4-a'
                VpcNetworkName = "cloudy-ce"
                VpcNetworkSubnetName = "regions/us-east4/subnetworks/cloudy-ce-us-east4"
                XdReconfigure = @(
                    @{ParameterName = "controllers"; ParameterValue = "cloudy-cc1.wse2edev.cloudy"}
                )
                NetworkTags = @("cloudy")
                Tags = @{ test = "abcdef" }
            }
            validate = {
                Param([PSCustomObject]$body)
                $body.provisioningType | Should -Be "Mcs"
                $body.platformCredentialId | Should -Be "nightly-gcp-1"
                $body.targetImageName | Should -Be "nightly-mia-win10"
                $body.resourceLocationId | Should -Be "52509414-3c19-4a8e-a711-01837da5a1c8"
                $body.xdReconfigure -is [Array] | Should -Be $true
                $body.xdReconfigure | Should -HaveCount 1
                $body.xdReconfigure[0].ParameterName | Should -Be "controllers"
                $body.xdReconfigure[0].ParameterValue | Should -Be "cloudy-cc1.wse2edev.cloudy"
                $body.networkTags -is [Array] | Should -Be $true
                $body.networkTags | Should -HaveCount 1
                $body.networkTags[0] | Should -Be "cloudy"

                $tags = Convert-Object $body.tags
                $tags.Count | Should -Be 2
                $tags.ContainsKey("ctx-user") | Should -Be $true
                $tags.test | Should -Be "abcdef"
            }
        }
    )

    It 'Test <name>' -ForEach $tests {

        Mock Add-PSSnapin { }
        Mock AuthToCitrixCloud {return @{}}
        Mock Clear-XDCredentials { }
        Mock VersionCheck { }
        Mock Invoke-CCRestMethod { $Global:RestBody = $json; $Global:RestQuery = $query }

        if ($null -ne $config)
        {
            $configFile = New-TemporaryFile
            $config | Set-Content -Path $configFile.FullName

            Start-IpsGcpPrepareJob -ConfigJsonFile $configFile.FullName
        }
        else
        {
            Start-IpsGcpPrepareJob @params -Force -DryRun $true
        }

        $body = ConvertFrom-Json $restBody
        Write-Host $restBody

        Invoke-Command -ScriptBlock $validate -ArgumentList $body
    }
}