TestCases and assigning multiple variables

Welcome Forums Pester TestCases and assigning multiple variables

Viewing 3 reply threads
  • Author
    Posts
    • #112447
      Participant
      Topics: 4
      Replies: 6
      Points: 35
      Rank: Member

      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

    • #112459
      Senior Moderator
      Topics: 9
      Replies: 1265
      Points: 4,582
      Helping Hand
      Rank: Community Hero

      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"
          }
      }
      
      
    • #112493
      Participant
      Topics: 4
      Replies: 6
      Points: 35
      Rank: Member

      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

    • #112496
      Senior Moderator
      Topics: 9
      Replies: 1265
      Points: 4,582
      Helping Hand
      Rank: Community Hero

      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

Viewing 3 reply threads
  • The topic ‘TestCases and assigning multiple variables’ is closed to new replies.