1+ [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute (' PSUseDeclaredVarsMoreThanAssignments' , ' ' , Justification = ' Suppressing this rule because Script Analyzer does not understand Pester syntax.' )]
2+ param ()
3+
4+ BeforeDiscovery {
5+ try
6+ {
7+ if (-not (Get-Module - Name ' DscResource.Test' ))
8+ {
9+ # Assumes dependencies have been resolved, so if this module is not available, run 'noop' task.
10+ if (-not (Get-Module - Name ' DscResource.Test' - ListAvailable))
11+ {
12+ # Redirect all streams to $null, except the error stream (stream 2)
13+ & " $PSScriptRoot /../../../build.ps1" - Tasks ' noop' 3>&1 4>&1 5>&1 6>&1 > $null
14+ }
15+
16+ # If the dependencies have not been resolved, this will throw an error.
17+ Import-Module - Name ' DscResource.Test' - Force - ErrorAction ' Stop'
18+ }
19+ }
20+ catch [System.IO.FileNotFoundException ]
21+ {
22+ throw ' DscResource.Test module dependency not found. Please run ".\build.ps1 -ResolveDependency -Tasks build" first.'
23+ }
24+ }
25+
26+ BeforeAll {
27+ $script :moduleName = ' SqlServerDsc'
28+
29+ Import-Module - Name $script :moduleName - Force - ErrorAction ' Stop'
30+ }
31+
32+ Describe ' Get-SqlDscAudit' - Tag @ (' Integration_SQL2017' , ' Integration_SQL2019' , ' Integration_SQL2022' ) {
33+ BeforeAll {
34+ # Starting the named instance SQL Server service prior to running tests.
35+ Start-Service - Name ' MSSQL$DSCSQLTEST' - Verbose - ErrorAction ' Stop'
36+
37+ $script :mockInstanceName = ' DSCSQLTEST'
38+ $script :mockComputerName = Get-ComputerName
39+
40+ $mockSqlAdministratorUserName = ' SqlAdmin' # Using computer name as NetBIOS name throw exception.
41+ $mockSqlAdministratorPassword = ConvertTo-SecureString - String ' P@ssw0rd1' - AsPlainText - Force
42+
43+ $script :mockSqlAdminCredential = [System.Management.Automation.PSCredential ]::new($mockSqlAdministratorUserName , $mockSqlAdministratorPassword )
44+
45+ $script :serverObject = Connect-SqlDscDatabaseEngine - InstanceName $script :mockInstanceName - Credential $script :mockSqlAdminCredential - ErrorAction Stop
46+
47+ # Create test audits for the tests
48+ $script :testAuditName1 = ' SqlDscTestGetAudit1_' + (Get-Random )
49+ $script :testAuditName2 = ' SqlDscTestGetAudit2_' + (Get-Random )
50+
51+ $null = New-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName1 - LogType ' ApplicationLog' - Force - ErrorAction Stop
52+ $null = New-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName2 - LogType ' ApplicationLog' - Force - ErrorAction Stop
53+ }
54+
55+ AfterAll {
56+ # Clean up test audits
57+ $testAuditsToRemove = @ ($script :testAuditName1 , $script :testAuditName2 )
58+
59+ foreach ($auditName in $testAuditsToRemove )
60+ {
61+ try
62+ {
63+ $existingAudit = Get-SqlDscAudit - ServerObject $script :serverObject - Name $auditName - ErrorAction ' SilentlyContinue'
64+ if ($existingAudit )
65+ {
66+ $null = Remove-SqlDscAudit - ServerObject $script :serverObject - Name $auditName - Force - ErrorAction ' SilentlyContinue'
67+ }
68+ }
69+ catch
70+ {
71+ # Ignore cleanup errors
72+ }
73+ }
74+
75+ Disconnect-SqlDscDatabaseEngine - ServerObject $script :serverObject
76+
77+ # Stop the named instance SQL Server service to save memory on the build worker.
78+ Stop-Service - Name ' MSSQL$DSCSQLTEST' - Verbose - ErrorAction ' Stop'
79+ }
80+
81+ Context ' When getting all SQL Server audits' {
82+ It ' Should return an array of Audit objects' {
83+ $result = Get-SqlDscAudit - ServerObject $script :serverObject
84+
85+ <#
86+ Casting to array to ensure we get the count on Windows PowerShell
87+ when there is only one audit.
88+ #>
89+ @ ($result ).Count | Should - BeGreaterOrEqual 2
90+ @ ($result )[0 ] | Should - BeOfType ' Microsoft.SqlServer.Management.Smo.Audit'
91+ }
92+
93+ It ' Should return test audits that were created' {
94+ $result = Get-SqlDscAudit - ServerObject $script :serverObject
95+
96+ $result.Name | Should - Contain $script :testAuditName1
97+ $result.Name | Should - Contain $script :testAuditName2
98+ }
99+ }
100+
101+ Context ' When getting a specific SQL Server audit' {
102+ It ' Should return the specified audit when it exists' {
103+ $result = Get-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName1
104+
105+ $result | Should - BeOfType ' Microsoft.SqlServer.Management.Smo.Audit'
106+ $result.Name | Should - Be $script :testAuditName1
107+ }
108+
109+ It ' Should throw an error when the audit does not exist' {
110+ { Get-SqlDscAudit - ServerObject $script :serverObject - Name ' NonExistentAudit' - ErrorAction ' Stop' } |
111+ Should - Throw
112+ }
113+
114+ It ' Should return null when the audit does not exist and error action is SilentlyContinue' {
115+ $result = Get-SqlDscAudit - ServerObject $script :serverObject - Name ' NonExistentAudit' - ErrorAction ' SilentlyContinue'
116+
117+ $result | Should - BeNullOrEmpty
118+ }
119+ }
120+
121+ Context ' When using the Refresh parameter' {
122+ It ' Should return the same results with and without Refresh for all audits' {
123+ $resultWithoutRefresh = Get-SqlDscAudit - ServerObject $script :serverObject
124+ $resultWithRefresh = Get-SqlDscAudit - ServerObject $script :serverObject - Refresh
125+
126+ @ ($resultWithoutRefresh ).Count | Should - Be @ ($resultWithRefresh ).Count
127+ }
128+
129+ It ' Should return the same result with and without Refresh for specific audit' {
130+ $resultWithoutRefresh = Get-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName1
131+ $resultWithRefresh = Get-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName1 - Refresh
132+
133+ $resultWithoutRefresh.Name | Should - Be $resultWithRefresh.Name
134+ $resultWithoutRefresh.Name | Should - Be $script :testAuditName1
135+ }
136+ }
137+
138+ Context ' When using pipeline input' {
139+ It ' Should accept ServerObject from pipeline for all audits' {
140+ $result = $script :serverObject | Get-SqlDscAudit
141+
142+ @ ($result ).Count | Should - BeGreaterOrEqual 2
143+ $result.Name | Should - Contain $script :testAuditName1
144+ $result.Name | Should - Contain $script :testAuditName2
145+ }
146+
147+ It ' Should accept ServerObject from pipeline for specific audit' {
148+ $result = $script :serverObject | Get-SqlDscAudit - Name $script :testAuditName1
149+
150+ $result | Should - BeOfType ' Microsoft.SqlServer.Management.Smo.Audit'
151+ $result.Name | Should - Be $script :testAuditName1
152+ }
153+ }
154+
155+ Context ' When testing audit properties' {
156+ It ' Should return audits with expected properties' {
157+ $result = Get-SqlDscAudit - ServerObject $script :serverObject - Name $script :testAuditName1
158+
159+ $result.Name | Should - Be $script :testAuditName1
160+ $result.Parent | Should - Be $script :serverObject
161+ $result | Should - HaveProperty ' Enabled'
162+ $result | Should - HaveProperty ' DestinationType'
163+ }
164+ }
165+ }
0 commit comments