functions/New-LabPesterTest.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
function New-LabPesterTest
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [string[]]
        $Role,

        [Parameter(Mandatory)]
        [string]
        $Path,

        [Parameter()]
        [switch]
        $IsCustomRole
    )

    foreach ($r in $Role)
    {
        $line = if ($IsCustomRole.IsPresent)
        {
            "(Get-LabVM).Where({`$_.PreInstallationActivity.Where({`$_.IsCustomRole}).RoleName -contains '$r' -or `$_.PostInstallationActivity.Where({`$_.IsCustomRole}).RoleName -contains '$r'})"
        }
        else
        {
            "(Get-LabVm -Role $r).Count | Should -Be `$(Get-Lab).Machines.Where({`$_.Roles.Name -contains '$r'}).Count"
        }

        $fileContent = @"
Describe "[`$((Get-Lab).Name)] $r" -Tag $r {
    Context "Role deployment successful" {
        It "[$r] Should return the correct amount of machines" {
            $line
        }
    }
}
"@


        if (Test-Path -Path (Join-Path -Path $Path -ChildPath "$r.tests.ps1"))
        {
            continue
        }

        Set-Content -Path (Join-Path -Path $Path -ChildPath "$r.tests.ps1") -Value $fileContent
    }
}