TestCases and assigning multiple variables

Hi,

I’m currently working on a project where I’m auditing properties of Azure settings.

for example get-azureRmSqlDatabaseThreatDetectionPolicy, ThreatDetectionState is enabled or not.

I’m trying to write my test and code correctly but I’m confused as to how to load the test cases up with the information I need, or I’m not understanding how to use the test cases correctly. I need some advice please.

Rather than just adding the servername to the testcase I would like to have the resourcegroup added also and then be able to refer to it as a param? Is this possible.

Here is my code - doesn’t work

$sqlservername = Get-azurermsqlserver |Select-Object servername, ResourceGroupName
$TestCases = @()
$sqlservername.ForEach{$TestCases += @{sqlservername = $_.Servername}}
Describe ‘Testing user databases’ {

Create a Context for each Instance.

it “Testing Threatprotectionpolicy on $($_)” -TestCases $TestCases {

Loop through the sql server on the instance

Param($sqlservername)
(Get-AzureRmSqlServerThreatDetectionPolicy -resourcegroupname $sqlservername.resourcegroupname -servername $sqlservername ).ThreatDetectionState | Should -Be “Enabled”

}

}

Below is an alternate way I have got it working but I really wanted to use the testcases if possible.

$sqlservername = Get-azurermsqlserver |Select-Object servername, ResourceGroupName
Describe ‘Testing user databases’ {

Loop through the instances

$sqlservername.ForEach{

Create a Context for each Instance.

Context “Testing Threatprotectionpolicy on $($_)” {

Loop through the sql server on the instance

(Get-AzureRmSqlServerThreatDetectionPolicy -resourcegroupname $.resourcegroupname -servername $.servername ).ForEach{

It “SQL Server ThreatDetectionState should be Enabled” {
$_.ThreatDetectionState | Should -Be “Enabled”
}
}
}
}
}

Many Thanks Russ

You are almost, Just add resourcegroup key as well in the hastable.

$sqlservername = Get-azurermsqlserver |Select-Object servername, ResourceGroupName
$TestCases = @()
$sqlservername.ForEach{$TestCases += @{sqlservername = $_.Servername ; resourcegroupname = $_.resourcegroupname }}
Describe 'Testing user databases' {
    # Create a Context for each Instance.
    it "Testing Threatprotectionpolicy on $($_)" -TestCases $TestCases {
        # Loop through the sql server on the instance
        Param($sqlservername, $resourcegroupname)
        (Get-AzureRmSqlServerThreatDetectionPolicy -resourcegroupname $resourcegroupname -servername $sqlservername ).ThreatDetectionState | Should -Be "Enabled"
    }
}

Ahhh,I see thank you.

So to add to this I have two the situation with this threatdetectionpolicy that it can be enabled at the server level (get-azureRmSqlServerThreatDetectionPolicy state =enabled) and at the database level (get-azurermsqldatabasethreatdetectionpolicy state =enabled) I would like the test to check the server level first , if’s not enabled, then run the database level test. Can I do this with a test case and do I require an if statement or is there a way to get the pester It to do it?

Many Thanks

Russell

You can’t do it inside TestCases, you will have to use two It blocks for each level and use -Skip switch of the It function