Skip to content

Commit

Permalink
add first batch of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Oct 16, 2024
1 parent 4abfbe3 commit 3c80231
Show file tree
Hide file tree
Showing 43 changed files with 2,836 additions and 1,533 deletions.
93 changes: 65 additions & 28 deletions tests/Add-DbaAgDatabase.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,35 +1,72 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
param($ModuleName = 'dbatools')

Describe "Add-DbaAgDatabase" {
BeforeAll {
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
}

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
BeforeAll {
$CommandUnderTest = Get-Command Add-DbaAgDatabase
}
It "Should have SqlInstance as a non-mandatory parameter of type DbaInstanceParameter" {
$CommandUnderTest | Should -HaveParameter SqlInstance -Type DbaInstanceParameter -Not -Mandatory
}
It "Should have SqlCredential as a non-mandatory parameter of type PSCredential" {
$CommandUnderTest | Should -HaveParameter SqlCredential -Type PSCredential -Not -Mandatory
}
It "Should have AvailabilityGroup as a non-mandatory parameter of type String" {
$CommandUnderTest | Should -HaveParameter AvailabilityGroup -Type String -Not -Mandatory
}
It "Should have Database as a non-mandatory parameter of type String[]" {
$CommandUnderTest | Should -HaveParameter Database -Type String[] -Not -Mandatory
}
It "Should have Secondary as a non-mandatory parameter of type DbaInstanceParameter[]" {
$CommandUnderTest | Should -HaveParameter Secondary -Type DbaInstanceParameter[] -Not -Mandatory
}
It "Should have SecondarySqlCredential as a non-mandatory parameter of type PSCredential" {
$CommandUnderTest | Should -HaveParameter SecondarySqlCredential -Type PSCredential -Not -Mandatory
}
It "Should have InputObject as a non-mandatory parameter of type Database[]" {
$CommandUnderTest | Should -HaveParameter InputObject -Type Database[] -Not -Mandatory
}
It "Should have SeedingMode as a non-mandatory parameter of type String" {
$CommandUnderTest | Should -HaveParameter SeedingMode -Type String -Not -Mandatory
}
It "Should have SharedPath as a non-mandatory parameter of type String" {
$CommandUnderTest | Should -HaveParameter SharedPath -Type String -Not -Mandatory
}
It "Should have UseLastBackup as a non-mandatory switch parameter" {
$CommandUnderTest | Should -HaveParameter UseLastBackup -Type Switch -Not -Mandatory
}
It "Should have AdvancedBackupParams as a non-mandatory parameter of type Hashtable" {
$CommandUnderTest | Should -HaveParameter AdvancedBackupParams -Type Hashtable -Not -Mandatory
}
It "Should have EnableException as a non-mandatory switch parameter" {
$CommandUnderTest | Should -HaveParameter EnableException -Type Switch -Not -Mandatory
}
}
}

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $script:instance3
$agname = "dbatoolsci_addagdb_agroup"
$dbname = "dbatoolsci_addagdb_agroupdb"
$newdbname = "dbatoolsci_addag_agroupdb_2"
$server.Query("create database $dbname")
$backup = Get-DbaDatabase -SqlInstance $script:instance3 -Database $dbname | Backup-DbaDatabase
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Database $dbname -Confirm:$false -Certificate dbatoolsci_AGCert
}
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 "Command usage" {
BeforeAll {
$null = Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $script:instance3
$agname = "dbatoolsci_addagdb_agroup"
$dbname = "dbatoolsci_addagdb_agroupdb"
$newdbname = "dbatoolsci_addag_agroupdb_2"
$server.Query("create database $dbname")
$backup = Get-DbaDatabase -SqlInstance $script:instance3 -Database $dbname | Backup-DbaDatabase
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Database $dbname -Confirm:$false -Certificate dbatoolsci_AGCert
}

AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname -Confirm:$false
$null = Remove-DbaDatabase -SqlInstance $server -Database $dbname, $newdbname -Confirm:$false
}

It "adds ag db and returns proper results" {
$server.Query("create database $newdbname")
$backup = Get-DbaDatabase -SqlInstance $script:instance3 -Database $newdbname | Backup-DbaDatabase
$results = Add-DbaAgDatabase -SqlInstance $script:instance3 -AvailabilityGroup $agname -Database $newdbname -Confirm:$false
Expand All @@ -38,4 +75,4 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
$results.IsJoined | Should -Be $true
}
}
} #$script:instance2 for appveyor
} #$script:instance2 for appveyor
85 changes: 60 additions & 25 deletions tests/Add-DbaAgListener.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,34 +1,69 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
param($ModuleName = 'dbatools')

Describe "Add-DbaAgListener" {
BeforeAll {
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
}

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
BeforeAll {
$CommandUnderTest = Get-Command Add-DbaAgListener
}
It "Should have SqlInstance as a non-mandatory parameter of type DbaInstanceParameter[]" {
$CommandUnderTest | Should -HaveParameter SqlInstance -Type DbaInstanceParameter[] -Not -Mandatory
}
It "Should have SqlCredential as a non-mandatory parameter of type PSCredential" {
$CommandUnderTest | Should -HaveParameter SqlCredential -Type PSCredential -Not -Mandatory
}
It "Should have AvailabilityGroup as a non-mandatory parameter of type String[]" {
$CommandUnderTest | Should -HaveParameter AvailabilityGroup -Type String[] -Not -Mandatory
}
It "Should have Name as a non-mandatory parameter of type String" {
$CommandUnderTest | Should -HaveParameter Name -Type String -Not -Mandatory
}
It "Should have IPAddress as a non-mandatory parameter of type IPAddress[]" {
$CommandUnderTest | Should -HaveParameter IPAddress -Type IPAddress[] -Not -Mandatory
}
It "Should have SubnetIP as a non-mandatory parameter of type IPAddress[]" {
$CommandUnderTest | Should -HaveParameter SubnetIP -Type IPAddress[] -Not -Mandatory
}
It "Should have SubnetMask as a non-mandatory parameter of type IPAddress[]" {
$CommandUnderTest | Should -HaveParameter SubnetMask -Type IPAddress[] -Not -Mandatory
}
It "Should have Port as a non-mandatory parameter of type Int32" {
$CommandUnderTest | Should -HaveParameter Port -Type Int32 -Not -Mandatory
}
It "Should have Dhcp as a non-mandatory switch parameter" {
$CommandUnderTest | Should -HaveParameter Dhcp -Type Switch -Not -Mandatory
}
It "Should have Passthru as a non-mandatory switch parameter" {
$CommandUnderTest | Should -HaveParameter Passthru -Type Switch -Not -Mandatory
}
It "Should have InputObject as a non-mandatory parameter of type AvailabilityGroup[]" {
$CommandUnderTest | Should -HaveParameter InputObject -Type AvailabilityGroup[] -Not -Mandatory
}
It "Should have EnableException as a non-mandatory switch parameter" {
$CommandUnderTest | Should -HaveParameter EnableException -Type Switch -Not -Mandatory
}
}
}

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$agname = "dbatoolsci_ag_newlistener"
$listenerName = 'dbatoolsci_listener'
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Confirm:$false -Certificate dbatoolsci_AGCert
}
AfterEach {
$null = Remove-DbaAgListener -SqlInstance $script:instance3 -Listener $listenerName -AvailabilityGroup $agname -Confirm:$false
}
AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $script:instance3 -AvailabilityGroup $agname -Confirm:$false
}
Context "creates a listener" {
It "returns results with proper data" {
Context "Command usage" {
BeforeAll {
$agname = "dbatoolsci_ag_newlistener"
$listenerName = 'dbatoolsci_listener'
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Confirm:$false -Certificate dbatoolsci_AGCert
}
AfterEach {
$null = Remove-DbaAgListener -SqlInstance $script:instance3 -Listener $listenerName -AvailabilityGroup $agname -Confirm:$false
}
AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $script:instance3 -AvailabilityGroup $agname -Confirm:$false
}
It "creates a listener and 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
}
}
} #$script:instance2 for appveyor
} #$script:instance2 for appveyor
112 changes: 86 additions & 26 deletions tests/Add-DbaAgReplica.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,33 +1,91 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
param($ModuleName = 'dbatools')

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 "$commandname Integration Tests" -Tag "IntegrationTests" {
Describe "Add-DbaAgReplica" {
BeforeAll {
$agname = "dbatoolsci_agroup"
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false
$replicaName = $ag.PrimaryReplica
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
. "$PSScriptRoot\constants.ps1"
}
AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $script:instance3 -AvailabilityGroup $agname -Confirm:$false

Context "Validate parameters" {
BeforeAll {
$CommandUnderTest = Get-Command Add-DbaAgReplica
}
It "Should have SqlInstance parameter" {
$CommandUnderTest | Should -HaveParameter SqlInstance -Type DbaInstanceParameter[]
}
It "Should have SqlCredential parameter" {
$CommandUnderTest | Should -HaveParameter SqlCredential -Type PSCredential
}
It "Should have Name parameter" {
$CommandUnderTest | Should -HaveParameter Name -Type String
}
It "Should have ClusterType parameter" {
$CommandUnderTest | Should -HaveParameter ClusterType -Type String
}
It "Should have AvailabilityMode parameter" {
$CommandUnderTest | Should -HaveParameter AvailabilityMode -Type String
}
It "Should have FailoverMode parameter" {
$CommandUnderTest | Should -HaveParameter FailoverMode -Type String
}
It "Should have BackupPriority parameter" {
$CommandUnderTest | Should -HaveParameter BackupPriority -Type Int32
}
It "Should have ConnectionModeInPrimaryRole parameter" {
$CommandUnderTest | Should -HaveParameter ConnectionModeInPrimaryRole -Type String
}
It "Should have ConnectionModeInSecondaryRole parameter" {
$CommandUnderTest | Should -HaveParameter ConnectionModeInSecondaryRole -Type String
}
It "Should have SeedingMode parameter" {
$CommandUnderTest | Should -HaveParameter SeedingMode -Type String
}
It "Should have Endpoint parameter" {
$CommandUnderTest | Should -HaveParameter Endpoint -Type String
}
It "Should have EndpointUrl parameter" {
$CommandUnderTest | Should -HaveParameter EndpointUrl -Type String[]
}
It "Should have Passthru parameter" {
$CommandUnderTest | Should -HaveParameter Passthru -Type SwitchParameter
}
It "Should have ReadOnlyRoutingList parameter" {
$CommandUnderTest | Should -HaveParameter ReadOnlyRoutingList -Type String[]
}
It "Should have ReadonlyRoutingConnectionUrl parameter" {
$CommandUnderTest | Should -HaveParameter ReadonlyRoutingConnectionUrl -Type String
}
It "Should have Certificate parameter" {
$CommandUnderTest | Should -HaveParameter Certificate -Type String
}
It "Should have ConfigureXESession parameter" {
$CommandUnderTest | Should -HaveParameter ConfigureXESession -Type SwitchParameter
}
It "Should have SessionTimeout parameter" {
$CommandUnderTest | Should -HaveParameter SessionTimeout -Type Int32
}
It "Should have InputObject parameter" {
$CommandUnderTest | Should -HaveParameter InputObject -Type AvailabilityGroup
}
It "Should have EnableException parameter" {
$CommandUnderTest | Should -HaveParameter EnableException -Type SwitchParameter
}
}
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 $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false
$replicaName = $ag.PrimaryReplica

It "returns results with proper data" {
Context "Command usage" {
BeforeAll {
$agname = "dbatoolsci_agroup"
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false
$replicaName = $ag.PrimaryReplica
}
AfterAll {
Remove-DbaAvailabilityGroup -SqlInstance $script:instance3 -AvailabilityGroup $agname -Confirm:$false
}
It "gets ag replicas" {
$agname = "dbatoolsci_add_replicagroup"
$ag = New-DbaAvailabilityGroup -Primary $script:instance3 -Name $agname -ClusterType None -FailoverMode Manual -Certificate dbatoolsci_AGCert -Confirm:$false
$replicaName = $ag.PrimaryReplica

$results = Get-DbaAgReplica -SqlInstance $script:instance3
$results.AvailabilityGroup | Should -Contain $agname
$results.Role | Should -Contain 'Primary'
Expand All @@ -42,4 +100,6 @@ Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
$results.FailoverMode | Should -Be 'Manual'
}
}
} #$script:instance2 for appveyor
}

#$script:instance2 for appveyor
Loading

0 comments on commit 3c80231

Please sign in to comment.