From 2afb82530f296116589903337bd8b078107da6a5 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Fri, 25 Oct 2024 14:14:10 +0200 Subject: [PATCH] new batch --- tests/Add-DbaAgDatabase.Tests.ps1 | 62 +++++++++++---- tests/Add-DbaAgListener.Tests.ps1 | 59 +++++++++++---- tests/Add-DbaAgReplica.Tests.ps1 | 88 +++++++++++++++++----- tests/Add-DbaComputerCertificate.Tests.ps1 | 53 +++++++++---- 4 files changed, 202 insertions(+), 60 deletions(-) diff --git a/tests/Add-DbaAgDatabase.Tests.ps1 b/tests/Add-DbaAgDatabase.Tests.ps1 index 59717a15f6..92e8af11aa 100644 --- a/tests/Add-DbaAgDatabase.Tests.ps1 +++ b/tests/Add-DbaAgDatabase.Tests.ps1 @@ -1,19 +1,41 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") -Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} +param($ModuleName = "dbatools") $global:TestConfig = Get-TestConfig -Describe "$CommandName Unit Tests" -Tag 'UnitTests' { - Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'AvailabilityGroup', 'Database', 'Secondary', 'SecondarySqlCredential', 'InputObject', 'SeedingMode', 'SharedPath', 'UseLastBackup', 'AdvancedBackupParams', 'EnableException' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters - It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 +Describe "Add-DbaAgDatabase" -Tag "UnitTests" { + Context "Parameter validation" { + BeforeAll { + $command = Get-Command Add-DbaAgDatabase + $expectedParameters = $TestConfig.CommonParameters + + $expectedParameters += @( + "SqlInstance", + "SqlCredential", + "AvailabilityGroup", + "Database", + "Secondary", + "SecondarySqlCredential", + "InputObject", + "SeedingMode", + "SharedPath", + "UseLastBackup", + "AdvancedBackupParams", + "EnableException" + ) + } + + It "Should have exactly the expected parameters" { + $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" } + Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty + } + + It "Has parameter: <_>" -ForEach $expectedParameters { + $command | Should -HaveParameter $PSItem } } } -Describe "$commandname Integration Tests" -Tag "IntegrationTests" { +Describe "Add-DbaAgDatabase" -Tag "IntegrationTests" { BeforeAll { $null = Get-DbaProcess -SqlInstance $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue $server = Connect-DbaInstance -SqlInstance $TestConfig.instance3 @@ -22,17 +44,31 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" { $newdbname = "dbatoolsci_addag_agroupdb_2" $server.Query("create database $dbname") $backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbname | Backup-DbaDatabase - $ag = New-DbaAvailabilityGroup -Primary $TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Database $dbname -Confirm:$false -Certificate dbatoolsci_AGCert + $splatNewAg = @{ + Primary = $TestConfig.instance3 + Name = $agname + ClusterType = "None" + FailoverMode = "Manual" + Database = $dbname + Confirm = $false + Certificate = "dbatoolsci_AGCert" + } + $ag = New-DbaAvailabilityGroup @splatNewAg } + AfterAll { $null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname -Confirm:$false $null = Remove-DbaDatabase -SqlInstance $server -Database $dbname, $newdbname -Confirm:$false } - Context "adds ag db" { - It "returns proper results" { + + Context "When adding AG database" { + BeforeAll { $server.Query("create database $newdbname") $backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $newdbname | Backup-DbaDatabase $results = Add-DbaAgDatabase -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agname -Database $newdbname -Confirm:$false + } + + It "Returns proper results" { $results.AvailabilityGroup | Should -Be $agname $results.Name | Should -Be $newdbname $results.IsJoined | Should -Be $true diff --git a/tests/Add-DbaAgListener.Tests.ps1 b/tests/Add-DbaAgListener.Tests.ps1 index bb709e865b..8acdaf329a 100644 --- a/tests/Add-DbaAgListener.Tests.ps1 +++ b/tests/Add-DbaAgListener.Tests.ps1 @@ -1,32 +1,65 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") -Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} +param($ModuleName = "dbatools") $global:TestConfig = Get-TestConfig -Describe "$CommandName Unit Tests" -Tag 'UnitTests' { - Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'AvailabilityGroup', 'Name', 'IPAddress', 'SubnetIP', 'SubnetMask', 'Port', 'Dhcp', 'Passthru', 'InputObject', 'EnableException' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters - It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0 +Describe "Add-DbaAgListener" -Tag "UnitTests" { + Context "Parameter validation" { + BeforeAll { + $command = Get-Command Add-DbaAgListener + $expectedParameters = $TestConfig.CommonParameters + + $expectedParameters += @( + "SqlInstance", + "SqlCredential", + "AvailabilityGroup", + "Name", + "IPAddress", + "SubnetIP", + "SubnetMask", + "Port", + "Dhcp", + "Passthru", + "InputObject", + "EnableException" + ) + } + + It "Should have exactly the expected parameters" { + $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" } + Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty + } + + It "Has parameter: <_>" -ForEach $expectedParameters { + $command | Should -HaveParameter $PSItem } } } -Describe "$commandname Integration Tests" -Tag "IntegrationTests" { +Describe "Add-DbaAgListener" -Tag "IntegrationTests" { BeforeAll { $agname = "dbatoolsci_ag_newlistener" $listenerName = 'dbatoolsci_listener' - $ag = New-DbaAvailabilityGroup -Primary $TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Confirm:$false -Certificate dbatoolsci_AGCert + $splatPrimary = @{ + Primary = $TestConfig.instance3 + Name = $agname + ClusterType = "None" + FailoverMode = "Manual" + Certificate = "dbatoolsci_AGCert" + Confirm = $false + } + $ag = New-DbaAvailabilityGroup @splatPrimary } + AfterEach { $null = Remove-DbaAgListener -SqlInstance $TestConfig.instance3 -Listener $listenerName -AvailabilityGroup $agname -Confirm:$false } + AfterAll { $null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agname -Confirm:$false } - Context "creates a listener" { - It "returns results with proper data" { + + Context "When creating a listener" { + It "Returns results with proper data" { $results = $ag | Add-DbaAgListener -Name $listenerName -IPAddress 127.0.20.1 -Port 14330 -Confirm:$false $results.PortNumber | Should -Be 14330 } diff --git a/tests/Add-DbaAgReplica.Tests.ps1 b/tests/Add-DbaAgReplica.Tests.ps1 index 085d9329bc..b8e1494da2 100644 --- a/tests/Add-DbaAgReplica.Tests.ps1 +++ b/tests/Add-DbaAgReplica.Tests.ps1 @@ -1,40 +1,91 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") -Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} +param($ModuleName = "dbatools") $global:TestConfig = Get-TestConfig -Describe "$commandname Unit Tests" -Tag 'UnitTests' { - Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } - [object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'ClusterType', 'AvailabilityMode', 'FailoverMode', 'BackupPriority', 'ConnectionModeInPrimaryRole', 'ConnectionModeInSecondaryRole', 'SeedingMode', 'Endpoint', 'EndpointUrl', 'Passthru', 'ReadOnlyRoutingList', 'ReadonlyRoutingConnectionUrl', 'Certificate', 'ConfigureXESession', 'SessionTimeout', 'InputObject', 'EnableException' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters - It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0 +Describe "Add-DbaAgReplica" -Tag "UnitTests" { + Context "Parameter validation" { + BeforeAll { + $command = Get-Command Add-DbaAgReplica + $expectedParameters = $TestConfig.CommonParameters + + $expectedParameters += @( + "SqlInstance", + "SqlCredential", + "Name", + "ClusterType", + "AvailabilityMode", + "FailoverMode", + "BackupPriority", + "ConnectionModeInPrimaryRole", + "ConnectionModeInSecondaryRole", + "SeedingMode", + "Endpoint", + "EndpointUrl", + "Passthru", + "ReadOnlyRoutingList", + "ReadonlyRoutingConnectionUrl", + "Certificate", + "ConfigureXESession", + "SessionTimeout", + "InputObject", + "EnableException" + ) + } + + It "Should have exactly the expected parameters" { + $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" } + Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty + } + + It "Has parameter: <_>" -ForEach $expectedParameters { + $command | Should -HaveParameter $PSItem } } } -Describe "$commandname Integration Tests" -Tag "IntegrationTests" { + +Describe "Add-DbaAgReplica" -Tag "IntegrationTests" { BeforeAll { $agname = "dbatoolsci_agroup" - $ag = New-DbaAvailabilityGroup -Primary $TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false + $splatPrimary = @{ + Primary = $TestConfig.instance3 + Name = $agname + ClusterType = "None" + FailoverMode = "Manual" + Certificate = "dbatoolsci_AGCert" + Confirm = $false + } + $ag = New-DbaAvailabilityGroup @splatPrimary $replicaName = $ag.PrimaryReplica } + AfterAll { $null = Remove-DbaAvailabilityGroup -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agname -Confirm:$false } - Context "gets ag replicas" { - # the only way to test, really, is to call New-DbaAvailabilityGroup which calls Add-DbaAgReplica - $agname = "dbatoolsci_add_replicagroup" - $ag = New-DbaAvailabilityGroup -Primary $TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false - $replicaName = $ag.PrimaryReplica - It "returns results with proper data" { + Context "When adding AG replicas" { + BeforeAll { + $agname = "dbatoolsci_add_replicagroup" + $splatNewAg = @{ + Primary = $TestConfig.instance3 + Name = $agname + ClusterType = "None" + FailoverMode = "Manual" + Certificate = "dbatoolsci_AGCert" + Confirm = $false + } + $ag = New-DbaAvailabilityGroup @splatNewAg + $replicaName = $ag.PrimaryReplica + } + + It "Returns results with proper data" { $results = Get-DbaAgReplica -SqlInstance $TestConfig.instance3 $results.AvailabilityGroup | Should -Contain $agname $results.Role | Should -Contain 'Primary' $results.AvailabilityMode | Should -Contain 'SynchronousCommit' $results.FailoverMode | Should -Contain 'Manual' } - It "returns just one result" { + + It "Returns just one result" { $results = Get-DbaAgReplica -SqlInstance $TestConfig.instance3 -Replica $replicaName -AvailabilityGroup $agname $results.AvailabilityGroup | Should -Be $agname $results.Role | Should -Be 'Primary' @@ -43,4 +94,3 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" { } } } #$TestConfig.instance2 for appveyor - diff --git a/tests/Add-DbaComputerCertificate.Tests.ps1 b/tests/Add-DbaComputerCertificate.Tests.ps1 index df9646380c..f0875493f6 100644 --- a/tests/Add-DbaComputerCertificate.Tests.ps1 +++ b/tests/Add-DbaComputerCertificate.Tests.ps1 @@ -1,30 +1,53 @@ -$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") -Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan +#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"} +param($ModuleName = "dbatools") $global:TestConfig = Get-TestConfig -Describe "$CommandName Unit Tests" -Tag 'UnitTests' { - Context "Validate parameters" { - [object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')} - [object[]]$knownParameters = 'ComputerName', 'Credential', 'SecurePassword', 'Certificate', 'Path', 'Store', 'Folder', 'Flag', 'EnableException' - $knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters - It "Should only contain our specific parameters" { - (@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0 +Describe "Add-DbaComputerCertificate" -Tag "UnitTests" { + Context "Parameter validation" { + BeforeAll { + $command = Get-Command Add-DbaComputerCertificate + $expectedParameters = $TestConfig.CommonParameters + + $expectedParameters += @( + "ComputerName", + "Credential", + "SecurePassword", + "Certificate", + "Path", + "Store", + "Folder", + "Flag", + "EnableException" + ) + } + + It "Should have exactly the expected parameters" { + $actualParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin "WhatIf", "Confirm" } + Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $actualParameters | Should -BeNullOrEmpty + } + + It "Has parameter: <_>" -ForEach $expectedParameters { + $command | Should -HaveParameter $PSItem } } } -Describe "$commandname Integration Tests" -Tags "IntegrationTests" { +Describe "Add-DbaComputerCertificate" -Tag "IntegrationTests" { Context "Certificate is added properly" { - $results = Add-DbaComputerCertificate -Path "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt" -Confirm:$false + BeforeAll { + $results = Add-DbaComputerCertificate -Path "$($TestConfig.appveyorlabrepo)\certificates\localhost.crt" -Confirm:$false + } + + AfterAll { + Remove-DbaComputerCertificate -Thumbprint 29C469578D6C6211076A09CEE5C5797EEA0C2713 -Confirm:$false + } It "Should show the proper thumbprint has been added" { - $results.Thumbprint | Should Be "29C469578D6C6211076A09CEE5C5797EEA0C2713" + $results.Thumbprint | Should -Be "29C469578D6C6211076A09CEE5C5797EEA0C2713" } It "Should be in LocalMachine\My Cert Store" { - $results.PSParentPath | Should Be "Microsoft.PowerShell.Security\Certificate::LocalMachine\My" + $results.PSParentPath | Should -Be "Microsoft.PowerShell.Security\Certificate::LocalMachine\My" } - - Remove-DbaComputerCertificate -Thumbprint 29C469578D6C6211076A09CEE5C5797EEA0C2713 -Confirm:$false } }