@@ -172,9 +172,6 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016',
172172 }
173173
174174 It ' Should be able to access the ReportServer site without any error' {
175- # Wait for 1 minute for the ReportServer to be ready.
176- Start-Sleep - Seconds 30
177-
178175 if ($script :sqlVersion -in @ (' 140' , ' 150' , ' 160' ))
179176 {
180177 # SSRS 2017 and 2019 do not support multiple instances
@@ -185,20 +182,52 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016',
185182 $reportServerUri = ' http://{0}/ReportServer_{1}' -f $env: COMPUTERNAME , $ConfigurationData.AllNodes.InstanceName
186183 }
187184
188- try
189- {
190- $webRequestReportServer = Invoke-WebRequest - Uri $reportServerUri - UseDefaultCredentials
191- # if the request finishes successfully this should return status code 200.
192- $webRequestStatusCode = $webRequestReportServer.StatusCode -as [int ]
193- }
194- catch
185+ # Retry logic to wait for ReportServer to be ready (up to 2 minutes)
186+ $maxRetries = 24
187+ $retryIntervalSeconds = 5
188+ $webRequestStatusCode = 0
189+
190+ for ($attempt = 1 ; $attempt -le $maxRetries ; $attempt ++ )
195191 {
196- <#
197- If the request generated an exception i.e. "HTTP Error 503. The service is unavailable."
198- we can pull the status code from the Exception.Response property.
199- #>
200- $webRequestResponse = $_.Exception.Response
201- $webRequestStatusCode = $webRequestResponse.StatusCode -as [int ]
192+ try
193+ {
194+ $webRequestReportServer = Invoke-WebRequest - Uri $reportServerUri - UseDefaultCredentials - UseBasicParsing - ErrorAction Stop
195+ # if the request finishes successfully this should return status code 200.
196+ $webRequestStatusCode = $webRequestReportServer.StatusCode -as [int ]
197+
198+ if ($webRequestStatusCode -eq 200 )
199+ {
200+ Write-Verbose - Message " ReportServer is accessible (attempt $attempt )." - Verbose
201+ break
202+ }
203+ }
204+ catch
205+ {
206+ <#
207+ If the request generated an exception i.e. "HTTP Error 503. The service is unavailable."
208+ we can pull the status code from the Exception.Response property.
209+ #>
210+ $webRequestResponse = $_.Exception.Response
211+ $webRequestStatusCode = $webRequestResponse.StatusCode -as [int ]
212+
213+ if ($webRequestStatusCode -eq 0 -and $attempt -lt $maxRetries )
214+ {
215+ Write-Verbose - Message " ReportServer not yet accessible (attempt $attempt of $maxRetries ). Waiting $retryIntervalSeconds seconds..." - Verbose
216+ Start-Sleep - Seconds $retryIntervalSeconds
217+ }
218+ elseif ($webRequestStatusCode -eq 0 -and $attempt -eq $maxRetries )
219+ {
220+ # On the last attempt with status code 0, re-throw to get error details
221+ Write-Verbose - Message " ReportServer still not accessible after $maxRetries attempts. Re-throwing exception for diagnostics." - Verbose
222+ throw $_
223+ }
224+ elseif ($webRequestStatusCode -ne 0 )
225+ {
226+ # If we got an actual HTTP error code, break and let the assertion handle it
227+ Write-Verbose - Message " ReportServer returned HTTP status code $webRequestStatusCode (attempt $attempt )." - Verbose
228+ break
229+ }
230+ }
202231 }
203232
204233 $webRequestStatusCode | Should - BeExactly 200
@@ -215,20 +244,52 @@ Describe "$($script:dscResourceName)_Integration" -Tag @('Integration_SQL2016',
215244 $reportsUri = ' http://{0}/Reports_{1}' -f $env: COMPUTERNAME , $ConfigurationData.AllNodes.InstanceName
216245 }
217246
218- try
219- {
220- $webRequestReportServer = Invoke-WebRequest - Uri $reportsUri - UseDefaultCredentials
221- # if the request finishes successfully this should return status code 200.
222- $webRequestStatusCode = $webRequestReportServer.StatusCode -as [int ]
223- }
224- catch
247+ # Retry logic to wait for Reports site to be ready (up to 2 minutes)
248+ $maxRetries = 24
249+ $retryIntervalSeconds = 5
250+ $webRequestStatusCode = 0
251+
252+ for ($attempt = 1 ; $attempt -le $maxRetries ; $attempt ++ )
225253 {
226- <#
227- If the request generated an exception i.e. "HTTP Error 503. The service is unavailable."
228- we can pull the status code from the Exception.Response property.
229- #>
230- $webRequestResponse = $_.Exception.Response
231- $webRequestStatusCode = $webRequestResponse.StatusCode -as [int ]
254+ try
255+ {
256+ $webRequestReportServer = Invoke-WebRequest - Uri $reportsUri - UseDefaultCredentials - UseBasicParsing - ErrorAction Stop
257+ # if the request finishes successfully this should return status code 200.
258+ $webRequestStatusCode = $webRequestReportServer.StatusCode -as [int ]
259+
260+ if ($webRequestStatusCode -eq 200 )
261+ {
262+ Write-Verbose - Message " Reports site is accessible (attempt $attempt )." - Verbose
263+ break
264+ }
265+ }
266+ catch
267+ {
268+ <#
269+ If the request generated an exception i.e. "HTTP Error 503. The service is unavailable."
270+ we can pull the status code from the Exception.Response property.
271+ #>
272+ $webRequestResponse = $_.Exception.Response
273+ $webRequestStatusCode = $webRequestResponse.StatusCode -as [int ]
274+
275+ if ($webRequestStatusCode -eq 0 -and $attempt -lt $maxRetries )
276+ {
277+ Write-Verbose - Message " Reports site not yet accessible (attempt $attempt of $maxRetries ). Waiting $retryIntervalSeconds seconds..." - Verbose
278+ Start-Sleep - Seconds $retryIntervalSeconds
279+ }
280+ elseif ($webRequestStatusCode -eq 0 -and $attempt -eq $maxRetries )
281+ {
282+ # On the last attempt with status code 0, re-throw to get error details
283+ Write-Verbose - Message " Reports site still not accessible after $maxRetries attempts. Re-throwing exception for diagnostics." - Verbose
284+ throw $_
285+ }
286+ elseif ($webRequestStatusCode -ne 0 )
287+ {
288+ # If we got an actual HTTP error code, break and let the assertion handle it
289+ Write-Verbose - Message " Reports site returned HTTP status code $webRequestStatusCode (attempt $attempt )." - Verbose
290+ break
291+ }
292+ }
232293 }
233294
234295 $webRequestStatusCode | Should - BeExactly 200
0 commit comments