Start-IpsAwsPrepareJob.Tests.ps1

BeforeAll {
    Add-PSSnapin Citrix.*
    . $PSScriptRoot\Start-IpsAwsPrepareJob.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, [int32]$count, [bool]$dryrunExpected, [bool]$dryrunActual)

        $dryrunActual | Should -Be $dryrunExpected

        $body.platformCredentialId | Should -Be "aws-credential"
        $body.provisioningType | Should -Be "Mcs"
        $body.securityGroupIds -is [Array] | Should -Be $true
        $body.securityGroupIds | Should -HaveCount $count
        $body.xdReconfigure -is [Array] | Should -Be $true
        $body.xdReconfigure | Should -HaveCount $count
        $body.xdReconfigure[0].ParameterName | Should -Be "controllers"
        $body.xdReconfigure[0].ParameterValue | Should -Be "cloudy-cc1.wse2edev.cloudy"
        if ($count -eq 2) {
            $body.xdReconfigure[1].ParameterName | Should -Be "portnumber"
            $body.xdReconfigure[1].ParameterValue | Should -Be "80"
        }

        $tags = Convert-Object $body.tags
        $tags.Count | Should -Be $count
        $tags.ContainsKey("ctx-user") | Should -Be $true
        if ($count -eq 2) {
            $tags.user | Should -Be "foo"
        }
    }

    $tests = @(
        @{
            name = "config"
            config = @'
{
    "Deployment": "api.test.cloud.com",
    "CustomerId": "qvj487ll7e68",
    "CloudPlatform": "aws",
    "CloudProvisioningType": "Mcs",
    "ResourceLocationId": "52509414-3c19-4a8e-a711-01837da5a1c8",
    "XdReconfigure": [
        {"ParameterName": "controllers", "ParameterValue": "cloudy-cc1.wse2edev.cloudy"}
    ],
    "AwsRegion": "us-east-2",
    "AwsTargetSnapshotId": "snap-087af7cd8fdfa545f",
    "AwsSubnetId": "subnet-0df5f0041b33a47ea",
    "AwsSecurityGroupIds": ["sg-0e38453a02de64d9d"],
    "UsePublicIP": true,
    "CloudCwSecretId": "aws-credential",
    "DryRun": true
}
'@

            count = 1
            dryrun = $true
            validate = $validate
        },
        @{
            name = "config with 2 array elements"
            config = @'
{
    "Deployment": "api.test.cloud.com",
    "CustomerId": "qvj487ll7e68",
    "CloudPlatform": "aws",
    "CloudProvisioningType": "Mcs",
    "ResourceLocationId": "52509414-3c19-4a8e-a711-01837da5a1c8",
    "XdReconfigure": [
        {"ParameterName": "controllers", "ParameterValue": "cloudy-cc1.wse2edev.cloudy"},
        {"ParameterName": "portnumber", "ParameterValue": "80"}
    ],
    "AwsRegion": "us-east-2",
    "AwsTargetSnapshotId": "snap-087af7cd8fdfa545f",
    "AwsSubnetId": "subnet-0df5f0041b33a47ea",
    "AwsSecurityGroupIds": ["sg-0e38453a02de64d9d", "sg-1fffff3a02de64d9d"],
    "CloudCwSecretId": "aws-credential",
    "Tags": {"user": "foo"}
}
'@

            count = 2
            dryrun = $false
            validate = $validate
        },
        @{
            name = "splat"
            params = @{
                Deployment = 'api.test.cloud.com'
                CustomerId = 'qvj487ll7e68'
                ResourceLocationId = '52509414-3c19-4a8e-a711-01837da5a1c8'
                ProvisioningType = 'Mcs'
                XdReconfigure = @(
                    @{ParameterName = "controllers"; ParameterValue = "cloudy-cc1.wse2edev.cloudy"},
                    @{ParameterName = "portnumber"; ParameterValue = "80"}
                )
                AwsRegion = 'us-east-2'
                AwsTargetSnapshotId = 'snap-087af7cd8fdfa545f'
                AwsSubnetId = 'subnet-0df5f0041b33a47ea'
                AwsSecurityGroupIds = @("sg-0e38453a02de64d9d", "sg-1fffff3a02de64d9d")
                AwsCwSecretId = 'aws-credential'
                Tags = @{ user = "foo" }
            }
            count = 2
            dryrun = $false
            validate = $validate
        },
        @{
            name = "splat with aliases"
            params = @{
                Deployment = 'api.test.cloud.com'
                CustomerId = 'qvj487ll7e68'
                ResourceLocationId = '52509414-3c19-4a8e-a711-01837da5a1c8'
                CloudProvisioningType = 'Mcs'  # alias for ProvisioningType
                XdReconfigure = @(
                    @{ParameterName = "controllers"; ParameterValue = "cloudy-cc1.wse2edev.cloudy"},
                    @{ParameterName = "portnumber"; ParameterValue = "80"}
                )
                AwsRegion = 'us-east-2'
                AwsTargetSnapshotId = 'snap-087af7cd8fdfa545f'
                AwsSubnetId = 'subnet-0df5f0041b33a47ea'
                AwsSecurityGroupIds = @("sg-0e38453a02de64d9d", "sg-1fffff3a02de64d9d")
                CloudCwSecretId = 'aws-credential'  # alias for AwsCwSecretId
                Tags = @{ user = "foo" }
            }
            count = 2
            dryrun = $false
            validate = $validate
        }
    )

    It 'Test file <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-IpsAwsPrepareJob -ConfigJsonFile $configFile.FullName
        }
        else
        {
            Start-IpsAwsPrepareJob @params
        }

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

        Invoke-Command -ScriptBlock $validate -ArgumentList $body, $count, $dryrun, $restQuery.dryRun
    }
}