Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ applyTo: "**"
## Tests Requirements
- Unit tests: Add `$env:SqlServerDscCI = $true` in `BeforeAll`, remove in `AfterAll`
- Integration tests:
- If requiring SQL Server DB, start the Windows service in `BeforeAll`, stop it in `AfterAll`.
- Use `Connect-SqlDscDatabaseEngine` for SQL Server DB session, and always with correct CI credentials
- Use `Disconnect-SqlDscDatabaseEngine` after `Connect-SqlDscDatabaseEngine`
- Test config: tests/Integration/Commands/README.md and tests/Integration/Resources/README.md
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Optimized integration test performance by leaving the DSCSQLTEST service running
for subsequent tests, significantly improving CI build times.
- Improved code quality by ensuring all function invocations in the private
and public functions use named parameters instead of positional parameters.
- SqlServerDsc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ AfterAll {

Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

Expand All @@ -63,9 +60,6 @@ Describe 'Assert-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_S

# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When operator exists' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,10 @@ BeforeAll {

Describe 'Assert-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:instanceName = 'DSCSQLTEST'
$script:computerName = Get-ComputerName
}

AfterAll {
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When connecting to SQL Server instance' {
BeforeAll {
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2017', 'Integrati
}

Context 'When connecting to a named instance' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

AfterAll {
# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

It 'Should have the named instance SQL Server service started' {
$getServiceResult = Get-Service -Name 'MSSQL$DSCSQLTEST' -ErrorAction 'Stop'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'

$mockSqlAdministratorUserName = 'SqlAdmin'
Expand All @@ -46,23 +43,19 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When converting empty collection of DatabasePermissionInfo' {
It 'Should return empty array for empty DatabasePermissionInfo collection' {
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()

$result = ConvertTo-SqlDscDatabasePermission -DatabasePermissionInfo $emptyCollection

$result | Should -BeNullOrEmpty
}

It 'Should accept empty collection through pipeline' {
$emptyCollection = [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo[]] @()

$result = $emptyCollection | ConvertTo-SqlDscDatabasePermission

$result | Should -BeNullOrEmpty
Expand All @@ -80,12 +73,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int

# Validate the result structure
$result | Should -Not -BeNullOrEmpty

# Each result should have State and Permission properties
foreach ($permission in $result) {
$permission.State | Should -Not -BeNullOrEmpty
$permission.Permission | Should -Not -BeNullOrEmpty

# Validate that permission state is one of the expected values
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
}
Expand All @@ -102,12 +95,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int

# Validate the result structure
$result | Should -Not -BeNullOrEmpty

# Each result should have State and Permission properties
foreach ($permission in $result) {
$permission.State | Should -Not -BeNullOrEmpty
$permission.Permission | Should -Not -BeNullOrEmpty

# Validate that permission state is one of the expected values
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
}
Expand All @@ -126,12 +119,12 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int

# Validate the result structure
$result | Should -Not -BeNullOrEmpty

# Each result should have State and Permission properties
foreach ($permission in $result) {
$permission.State | Should -Not -BeNullOrEmpty
$permission.Permission | Should -Not -BeNullOrEmpty

# Validate that permission state is one of the expected values
$permission.State | Should -BeIn @('Grant', 'Deny', 'GrantWithGrant')
}
Expand All @@ -151,7 +144,7 @@ Describe 'ConvertTo-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Int
if ($result) {
# Validate that permissions are properly grouped by state
$uniqueStates = $result.State | Sort-Object -Unique

foreach ($state in $uniqueStates) {
$permissionsForState = $result | Where-Object { $_.State -eq $state }
$permissionsForState | Should -HaveCount 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'Deny-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
Expand All @@ -50,9 +47,6 @@ Describe 'Deny-SqlDscServerPermission' -Tag @('Integration_SQL2017', 'Integratio

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When denying server permissions to login' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ AfterAll {

Describe 'Disable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockSqlAdministratorCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
Expand All @@ -61,9 +58,6 @@ Describe 'Disable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_
Set-SqlDscConfigurationOption -ServerObject $script:serverObject -Name 'Agent XPs' -Value 0 -Force -Verbose -ErrorAction 'Stop'

Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stopping the named instance SQL Server service after running tests.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When disabling an existing agent operator' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'
$script:mockComputerName = Get-ComputerName

Expand All @@ -47,9 +44,6 @@ Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL201

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When disabling an audit using ServerObject parameter set' {
Expand Down Expand Up @@ -206,7 +200,7 @@ Describe 'Disable-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL201
'SqlDscTestMultiDisable1_' + (Get-Random),
'SqlDscTestMultiDisable2_' + (Get-Random)
)

foreach ($auditName in $script:testAuditNames)
{
$null = New-SqlDscAudit -ServerObject $script:serverObject -Name $auditName -LogType 'ApplicationLog' -Force -ErrorAction Stop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'Disable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
Expand All @@ -49,9 +46,6 @@ Describe 'Disable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL201

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When disabling a login using ServerObject parameter set' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ AfterAll {

Describe 'Enable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockSqlAdministratorCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
Expand All @@ -61,9 +58,6 @@ Describe 'Enable-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_S
Set-SqlDscConfigurationOption -ServerObject $script:serverObject -Name 'Agent XPs' -Value 0 -Force -Verbose -ErrorAction 'Stop'

Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stopping the named instance SQL Server service after running tests.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When enabling an existing agent operator' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'Enable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
Expand All @@ -49,9 +46,6 @@ Describe 'Enable-SqlDscLogin' -Tag @('Integration_SQL2017', 'Integration_SQL2019

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When enabling a login using ServerObject parameter set' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ Describe 'Get-SqlDscAgentAlert' -Tag @('Integration_SQL2017', 'Integration_SQL20
# Integration tests are run on the DSCSQLTEST instance
$script:sqlServerInstance = 'DSCSQLTEST'

# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

Expand Down Expand Up @@ -75,9 +72,6 @@ END

# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

It 'Should get all alerts' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ AfterAll {

Describe 'Get-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022' {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

Expand All @@ -65,9 +62,6 @@ Describe 'Get-SqlDscAgentOperator' -Tag 'Integration_SQL2017', 'Integration_SQL2

# Disconnect from the SQL Server
Disconnect-SqlDscDatabaseEngine -ServerObject $script:sqlServerObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

It 'Should get all operators' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ BeforeAll {

Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
# Starting the named instance SQL Server service prior to running tests.
Start-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'

$script:mockInstanceName = 'DSCSQLTEST'

$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
Expand All @@ -46,9 +43,6 @@ Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integrat

AfterAll {
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject

# Stop the named instance SQL Server service to save memory on the build worker.
Stop-Service -Name 'MSSQL$DSCSQLTEST' -Verbose -ErrorAction 'Stop'
}

Context 'When getting all configuration options' {
Expand Down Expand Up @@ -85,8 +79,8 @@ Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integrat
$result | Should -BeOfType 'PSCustomObject'
$result.PSTypeNames[0] | Should -Be 'SqlDsc.ConfigurationOption'
$result.Name | Should -Be 'Agent XPs'
$result.RunValue | Should -Be 0
$result.ConfigValue | Should -Be 0
$result.RunValue | Should -Be 1
$result.ConfigValue | Should -Be 1
$result.Minimum | Should -Be 0
$result.Maximum | Should -Be 1
$result.IsDynamic | Should -BeTrue
Expand All @@ -109,7 +103,7 @@ Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integrat
$result = Get-SqlDscConfigurationOption -ServerObject $script:serverObject -Name '*XP*'

@($result).Count | Should -BeGreaterOrEqual 1
$result | Where-Object { $_.Name -eq 'Agent XPs' } | Should -Not -BeNullOrEmpty
$result | Where-Object -FilterScript { $_.Name -eq 'Agent XPs' } | Should -Not -BeNullOrEmpty
}
}

Expand All @@ -120,8 +114,8 @@ Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integrat
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.ConfigProperty'
$result.DisplayName | Should -Be 'Agent XPs'
$result.RunValue | Should -Be 0
$result.ConfigValue | Should -Be 0
$result.RunValue | Should -Be 1
$result.ConfigValue | Should -Be 1
}
}

Expand All @@ -143,7 +137,7 @@ Describe 'Get-SqlDscConfigurationOption' -Tag @('Integration_SQL2017', 'Integrat
$result | Should -Not -BeNullOrEmpty
$result | Should -BeOfType 'PSCustomObject'
$result.Name | Should -Be 'Agent XPs'
$result.RunValue | Should -Be 0
$result.RunValue | Should -Be 1
}
}
}
Loading
Loading