@@ -26,4 +26,179 @@ Describe $CommandName -Tag UnitTests {
2626 Compare-Object - ReferenceObject $expectedParameters - DifferenceObject $hasParameters | Should - BeNullOrEmpty
2727 }
2828 }
29+ }
30+
31+ Describe $CommandName - Tag IntegrationTests {
32+ BeforeAll {
33+ $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
34+
35+ $agName = " dbatoolsci_agroup_healthcheck"
36+ $dbName = " dbatoolsci_agdb_$ ( Get-Random ) "
37+
38+ $splatPrimary = @ {
39+ Primary = $TestConfig.instance3
40+ Name = $agName
41+ ClusterType = " None"
42+ FailoverMode = " Manual"
43+ Certificate = " dbatoolsci_AGCert"
44+ }
45+ $null = New-DbaAvailabilityGroup @splatPrimary
46+
47+ $splatDatabase = @ {
48+ SqlInstance = $TestConfig.instance3
49+ Name = $dbName
50+ Owner = " sa"
51+ }
52+ $null = New-DbaDatabase @splatDatabase
53+
54+ $splatAddDatabase = @ {
55+ SqlInstance = $TestConfig.instance3
56+ AvailabilityGroup = $agName
57+ Database = $dbName
58+ SeedingMode = " Automatic"
59+ }
60+ $null = Add-DbaAgDatabase @splatAddDatabase
61+
62+ $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
63+ }
64+
65+ AfterAll {
66+ $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
67+
68+ $null = Remove-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName
69+ $null = Remove-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $dbName
70+ $null = Get-DbaEndpoint - SqlInstance $TestConfig.instance3 - Type DatabaseMirroring | Remove-DbaEndpoint
71+
72+ $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
73+ }
74+
75+ Context " When using -HealthCheck parameter" {
76+ It " Returns health check results with expected properties" {
77+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
78+ $results | Should -Not - BeNullOrEmpty
79+ $results.Count | Should - BeGreaterThan 0
80+
81+ $firstResult = $results | Select-Object - First 1
82+ $firstResult.ComputerName | Should -Not - BeNullOrEmpty
83+ $firstResult.InstanceName | Should -Not - BeNullOrEmpty
84+ $firstResult.SqlInstance | Should -Not - BeNullOrEmpty
85+ $firstResult.AvailabilityGroup | Should - Be $agName
86+ $firstResult.PrimaryReplica | Should -Not - BeNullOrEmpty
87+ }
88+
89+ It " Returns replica-level health information" {
90+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
91+ $firstResult = $results | Select-Object - First 1
92+
93+ $firstResult.ReplicaServerName | Should -Not - BeNullOrEmpty
94+ $firstResult.ReplicaRole | Should - BeIn @ (" Primary" , " Secondary" )
95+ $firstResult.ReplicaAvailabilityMode | Should - BeIn @ (" SynchronousCommit" , " AsynchronousCommit" )
96+ $firstResult.ReplicaFailoverMode | Should - BeIn @ (" Manual" , " Automatic" )
97+ $firstResult.ReplicaConnectionState | Should - BeIn @ (" Connected" , " Disconnected" )
98+ $firstResult.ReplicaJoinState | Should - BeIn @ (" Joined" , " NotJoined" )
99+ $firstResult.ReplicaSynchronizationState | Should - BeIn @ (" Synchronized" , " Synchronizing" , " NotSynchronizing" , " PartiallyHealthy" , " Healthy" )
100+ }
101+
102+ It " Returns database-level synchronization information" {
103+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
104+ $dbResult = $results | Where-Object DatabaseName -eq $dbName
105+
106+ $dbResult | Should -Not - BeNullOrEmpty
107+ $dbResult.DatabaseName | Should - Be $dbName
108+ $dbResult.SynchronizationState | Should - BeIn @ (" Synchronized" , " Synchronizing" , " NotSynchronizing" , " Initializing" , " Reverting" )
109+ $dbResult.PSObject.Properties.Name | Should - Contain " IsFailoverReady"
110+ $dbResult.PSObject.Properties.Name | Should - Contain " IsJoined"
111+ $dbResult.PSObject.Properties.Name | Should - Contain " IsSuspended"
112+ $dbResult.PSObject.Properties.Name | Should - Contain " SuspendReason"
113+ }
114+
115+ It " Returns performance metrics for databases" {
116+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
117+ $firstResult = $results | Select-Object - First 1
118+
119+ $firstResult.PSObject.Properties.Name | Should - Contain " LogSendQueueSize"
120+ $firstResult.PSObject.Properties.Name | Should - Contain " LogSendRate"
121+ $firstResult.PSObject.Properties.Name | Should - Contain " RedoQueueSize"
122+ $firstResult.PSObject.Properties.Name | Should - Contain " RedoRate"
123+ $firstResult.PSObject.Properties.Name | Should - Contain " FileStreamSendRate"
124+ }
125+
126+ It " Returns LSN tracking information" {
127+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
128+ $firstResult = $results | Select-Object - First 1
129+
130+ $firstResult.PSObject.Properties.Name | Should - Contain " LastCommitLSN"
131+ $firstResult.PSObject.Properties.Name | Should - Contain " LastCommitTime"
132+ $firstResult.PSObject.Properties.Name | Should - Contain " LastHardenedLSN"
133+ $firstResult.PSObject.Properties.Name | Should - Contain " LastHardenedTime"
134+ $firstResult.PSObject.Properties.Name | Should - Contain " LastReceivedLSN"
135+ $firstResult.PSObject.Properties.Name | Should - Contain " LastReceivedTime"
136+ $firstResult.PSObject.Properties.Name | Should - Contain " LastRedoneLSN"
137+ $firstResult.PSObject.Properties.Name | Should - Contain " LastRedoneTime"
138+ $firstResult.PSObject.Properties.Name | Should - Contain " LastSentLSN"
139+ $firstResult.PSObject.Properties.Name | Should - Contain " LastSentTime"
140+ }
141+
142+ It " Returns recovery metrics" {
143+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
144+ $firstResult = $results | Select-Object - First 1
145+
146+ $firstResult.PSObject.Properties.Name | Should - Contain " EstimatedRecoveryTime"
147+ $firstResult.PSObject.Properties.Name | Should - Contain " EstimatedDataLoss"
148+ $firstResult.PSObject.Properties.Name | Should - Contain " SynchronizationPerformance"
149+ }
150+
151+ It " Works with Linux AGs (instance3 is Linux)" {
152+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
153+ $results | Should -Not - BeNullOrEmpty
154+ $results.Count | Should - BeGreaterThan 0
155+ $results.AvailabilityGroup | Should - Contain $agName
156+ }
157+
158+ It " Returns multiple results when AG has multiple databases" {
159+ $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
160+ $secondDbName = " dbatoolsci_agdb2_$ ( Get-Random ) "
161+
162+ $splatDatabase2 = @ {
163+ SqlInstance = $TestConfig.instance3
164+ Name = $secondDbName
165+ Owner = " sa"
166+ }
167+ $null = New-DbaDatabase @splatDatabase2
168+
169+ $splatAddDatabase2 = @ {
170+ SqlInstance = $TestConfig.instance3
171+ AvailabilityGroup = $agName
172+ Database = $secondDbName
173+ SeedingMode = " Automatic"
174+ }
175+ $null = Add-DbaAgDatabase @splatAddDatabase2
176+ $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
177+
178+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
179+ $results.Count | Should - BeGreaterOrEqual 2
180+
181+ $dbNames = $results.DatabaseName | Select-Object - Unique
182+ $dbNames | Should - Contain $dbName
183+ $dbNames | Should - Contain $secondDbName
184+
185+ $PSDefaultParameterValues [" *-Dba*:EnableException" ] = $true
186+ $null = Remove-DbaAgDatabase - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - Database $secondDbName
187+ $null = Remove-DbaDatabase - SqlInstance $TestConfig.instance3 - Database $secondDbName
188+ $PSDefaultParameterValues.Remove (" *-Dba*:EnableException" )
189+ }
190+ }
191+
192+ Context " When using -HealthCheck without AddDatabase compatibility" {
193+ It " Returns health check data without requiring database validation parameters" {
194+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
195+ $results | Should -Not - BeNullOrEmpty
196+ $results.AvailabilityGroup | Should - Contain $agName
197+ }
198+
199+ It " Does not require primary replica connection when using -HealthCheck" {
200+ $results = Test-DbaAvailabilityGroup - SqlInstance $TestConfig.instance3 - AvailabilityGroup $agName - HealthCheck
201+ $results | Should -Not - BeNullOrEmpty
202+ }
203+ }
29204}
0 commit comments