Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlServerDsc: More unit tests work cross-platform #1957

Merged
merged 6 commits into from
Aug 22, 2023
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix a path in VS Code workspace settings to correctly use Script Analyzer on
Linux and macOS.
- Highlighted note comments throughout markdown and generated markdown.
- SqlServerDsc.Common
- Fix unit tests so they work cross-platform.
- ScriptAnalyzer.Tests was fixed so they work cross-platform.
- SqlSetup
- Highlighted a note in the README.md.
- SqlMemory
- Highlighted an important note in the README.md.
- SqlMaxDop
- Highlighted an important note in the README.md.
- `Get-SqlDscPreferredModule`
- Fix unit tests so they work cross-platform.

### Fixed

Expand Down
17 changes: 16 additions & 1 deletion tests/QA/ScriptAnalyzer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ BeforeDiscovery {

foreach ($moduleFile in $moduleFiles)
{
# Skipping Examples on Linux and macOS as they cannot be parsed.
if (($IsLinux -or $IsMacOs) -and $moduleFile.FullName -match 'Examples')
{
continue
}

<#
Skipping prefix.psl because it contains `using module` using a relative
path which is not available when testing the source (only when built).
#>
if ($moduleFile.FullName -match 'prefix\.ps1')
{
continue
}

$moduleFilePathNormalized = $moduleFile.FullName -replace '\\', '/'
$repositoryPathNormalized = $repositoryPath -replace '\\', '/'
$escapedRepositoryPath = [System.Text.RegularExpressions.RegEx]::Escape($repositoryPathNormalized)
Expand All @@ -66,7 +81,7 @@ Describe 'Script Analyzer Rules' {
Context 'When there are source files' {
BeforeAll {
$repositoryPath = Resolve-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath '../..')
$scriptAnalyzerSettingsPath = Join-Path -Path $repositoryPath -ChildPath '.vscode\analyzersettings.psd1'
$scriptAnalyzerSettingsPath = Join-Path -Path $repositoryPath -ChildPath '.vscode/analyzersettings.psd1'
}

It 'Should pass all PS Script Analyzer rules for file ''<RelativePath>''' -ForEach $testCases {
Expand Down
173 changes: 0 additions & 173 deletions tests/Unit/Private/Invoke-SetupAction.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1382,11 +1382,6 @@ Describe 'Invoke-SetupAction' -Tag 'Private' {
MockParameterValue = 'TESTCLU01A' # cspell: disable-line
MockExpectedRegEx = '\/FAILOVERCLUSTERGROUP="TESTCLU01A"' # cspell: disable-line
}
@{
MockParameterName = 'FailoverClusterGroup'
MockParameterValue = 'TESTCLU01A' # cspell: disable-line
MockExpectedRegEx = '\/FAILOVERCLUSTERGROUP="TESTCLU01A"' # cspell: disable-line
}
@{
MockParameterName = 'FailoverClusterDisks'
# This is the failover cluster resource name.
Expand Down Expand Up @@ -1963,11 +1958,6 @@ Describe 'Invoke-SetupAction' -Tag 'Private' {
MockParameterValue = 'TESTCLU01A' # cspell: disable-line
MockExpectedRegEx = '\/FAILOVERCLUSTERGROUP="TESTCLU01A"' # cspell: disable-line
}
@{
MockParameterName = 'FailoverClusterGroup'
MockParameterValue = 'TESTCLU01A' # cspell: disable-line
MockExpectedRegEx = '\/FAILOVERCLUSTERGROUP="TESTCLU01A"' # cspell: disable-line
}
@{
MockParameterName = 'FailoverClusterDisks'
# This is the failover cluster resource name.
Expand Down Expand Up @@ -3003,169 +2993,6 @@ Describe 'Invoke-SetupAction' -Tag 'Private' {
}
}

Context 'When setup action is ''Upgrade''' {
BeforeAll {
Mock -CommandName Assert-SetupActionProperties
Mock -CommandName Assert-ElevatedUser
Mock -CommandName Test-Path -ParameterFilter {
$Path -match 'setup\.exe'
} -MockWith {
return $true
}
}

Context 'When specifying only mandatory parameters' {
BeforeAll {
Mock -CommandName Start-SqlSetupProcess -MockWith {
return 0
} -RemoveParameterValidation 'FilePath'

InModuleScope -ScriptBlock {
$script:mockDefaultParameters = @{
Upgrade = $true
AcceptLicensingTerms = $true
MediaPath = '\SqlMedia'
InstanceName = 'INSTANCE'
}
}
}

Context 'When using parameter Confirm with value $false' {
It 'Should call the mock with the correct argument string' {
InModuleScope -ScriptBlock {
Invoke-SetupAction -Confirm:$false @mockDefaultParameters

Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
$ArgumentList | Should -MatchExactly '\/ACTION=Upgrade'
$ArgumentList | Should -MatchExactly '\/IACCEPTSQLSERVERLICENSETERMS' # cspell: disable-line
$ArgumentList | Should -MatchExactly '\/INSTANCENAME="INSTANCE"' # cspell: disable-line

# Return $true if none of the above throw.
$true
} -Exactly -Times 1 -Scope It
}
}
}

Context 'When using parameter Force' {
It 'Should call the mock with the correct argument string' {
InModuleScope -ScriptBlock {
Invoke-SetupAction -Force @mockDefaultParameters

Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
$ArgumentList | Should -MatchExactly '\/ACTION=Upgrade'
$ArgumentList | Should -MatchExactly '\/IACCEPTSQLSERVERLICENSETERMS' # cspell: disable-line
$ArgumentList | Should -MatchExactly '\/INSTANCENAME="INSTANCE"' # cspell: disable-line

# Return $true if none of the above throw.
$true
} -Exactly -Times 1 -Scope It
}
}
}

Context 'When using parameter WhatIf' {
It 'Should call the mock with the correct argument string' {
InModuleScope -ScriptBlock {
Invoke-SetupAction -WhatIf @mockDefaultParameters

Should -Invoke -CommandName Start-SqlSetupProcess -Exactly -Times 0 -Scope It
}
}
}
}

Context 'When specifying optional parameter <MockParameterName>' -ForEach @(
@{
MockParameterName = 'Enu'
MockParameterValue = $true
MockExpectedRegEx = '\/ENU\s*'
}
@{
MockParameterName = 'UpdateEnabled'
MockParameterValue = $true
MockExpectedRegEx = '\/UPDATEENABLED=True' # cspell: disable-line
}
@{
MockParameterName = 'UpdateSource'
MockParameterValue = '\SqlMedia\Updates'
MockExpectedRegEx = '\/UPDATESOURCE="\\SqlMedia\\Updates"' # cspell: disable-line
}
@{
MockParameterName = 'InstanceDir'
MockParameterValue = 'C:\Program Files\Microsoft SQL Server'
MockExpectedRegEx = '\/INSTANCEDIR="C:\\Program Files\\Microsoft SQL Server"' # cspell: disable-line
}
@{
MockParameterName = 'InstanceId'
MockParameterValue = 'Instance'
MockExpectedRegEx = '\/INSTANCEID="Instance"' # cspell: disable-line
}
@{
MockParameterName = 'ProductKey'
MockParameterValue = '22222-00000-00000-00000-00000'
MockExpectedRegEx = '\/PID="22222-00000-00000-00000-00000"'
}
@{
MockParameterName = 'BrowserSvcStartupType'
MockParameterValue = 'Manual'
MockExpectedRegEx = '\/BROWSERSVCSTARTUPTYPE="Manual"' # cspell: disable-line
}
@{
MockParameterName = 'ISSvcAccount'
MockParameterValue = 'DOMAIN\ServiceAccount$'
MockExpectedRegEx = '\/ISSVCACCOUNT="DOMAIN\\ServiceAccount\$"' # cspell: disable-line
}
@{
MockParameterName = 'ISSvcPassword'
MockParameterValue = 'jT7ELPbD2GGuvLmjABDL' | ConvertTo-SecureString -AsPlainText -Force # cspell: disable-line
MockExpectedRegEx = '\/ISSVCPASSWORD="jT7ELPbD2GGuvLmjABDL"' # cspell: disable-line
}
@{
MockParameterName = 'ISSvcStartupType'
MockParameterValue = 'Automatic'
MockExpectedRegEx = '\/ISSVCSTARTUPTYPE="Automatic"' # cspell: disable-line
}
) {
BeforeAll {
Mock -CommandName Start-SqlSetupProcess -MockWith {
return 0
} -RemoveParameterValidation 'FilePath'

InModuleScope -ScriptBlock {
$script:mockDefaultParameters = @{
Upgrade = $true
AcceptLicensingTerms = $true
MediaPath = '\SqlMedia'
InstanceName = 'INSTANCE'
Force = $true
}
}
}

BeforeEach {
InModuleScope -ScriptBlock {
$script:installSqlDscServerParameters = $mockDefaultParameters.Clone()
}
}

It 'Should call the mock with the correct argument string' {
InModuleScope -Parameters $_ -ScriptBlock {
$installSqlDscServerParameters.$MockParameterName = $MockParameterValue

Invoke-SetupAction @installSqlDscServerParameters

Should -Invoke -CommandName Start-SqlSetupProcess -ParameterFilter {
$ArgumentList | Should -MatchExactly $MockExpectedRegEx

# Return $true if none of the above throw.
$true
} -Exactly -Times 1 -Scope It
}
}
}
}

Context 'When setup action is ''PrepareImage''' {
BeforeAll {
Mock -CommandName Assert-SetupActionProperties
Expand Down
15 changes: 10 additions & 5 deletions tests/Unit/Public/Get-SqlDscPreferredModule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Describe 'Get-SqlDscPreferredModule' -Tag 'Public' {
}

It 'Should return the correct module name' {
Get-SqlDscPreferredModule | Should -Be 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS'
Get-SqlDscPreferredModule | Should -Be ('C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS' -replace '\\', [IO.Path]::DirectorySeparatorChar)
}
}

Expand Down Expand Up @@ -231,7 +231,7 @@ Describe 'Get-SqlDscPreferredModule' -Tag 'Public' {
}

It 'Should return the correct module name' {
Get-SqlDscPreferredModule | Should -Be 'C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS'
Get-SqlDscPreferredModule | Should -Be ('C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS' -replace '\\', [IO.Path]::DirectorySeparatorChar)
}
}
}
Expand Down Expand Up @@ -302,7 +302,7 @@ Describe 'Get-SqlDscPreferredModule' -Tag 'Public' {
}

It 'Should return the correct module name' {
Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be 'C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS'
Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be ('C:\Program Files (x86)\Microsoft SQL Server\130\Tools\PowerShell\Modules\SQLPS' -replace '\\', [IO.Path]::DirectorySeparatorChar)
}
}

Expand Down Expand Up @@ -398,12 +398,17 @@ Describe 'Get-SqlDscPreferredModule' -Tag 'Public' {
}

It 'Should return the correct module name' {
Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be 'C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS'
Get-SqlDscPreferredModule -Name @('SqlServer', 'SQLPS') | Should -Be ('C:\Program Files (x86)\Microsoft SQL Server\160\Tools\PowerShell\Modules\SQLPS' -replace '\\', [IO.Path]::DirectorySeparatorChar)
}
}
}

Context 'When specifying the parameter Refresh' {
<#
This test cannot run on Linux or macOS as Refresh is refreshing the PSModulePath
from the environment variable target scope Machine which does not exist
on Linux and macOS.
#>
Context 'When specifying the parameter Refresh' -Skip:($IsLinux -or $IsMacOS) {
BeforeAll {
Mock -CommandName Set-PSModulePath
Mock -CommandName Get-PSModulePath -MockWith {
Expand Down
Loading