Skip to content

Commit

Permalink
Connect-SqlDscDatabaseEngine: Improve integration tests (#2031)
Browse files Browse the repository at this point in the history
- `Connect-SqlDscDatabaseEngine`
  - Update comment-based help with more examples.
  • Loading branch information
johlju authored May 21, 2024
1 parent 89706ac commit 47e02f2
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 90 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Connect-SqlDscDatabaseEngine`
- Added integration test for the command.

### Changed

- `Connect-SqlDscDatabaseEngine`
- Update comment-based help with more examples.

### Fixed

- `Connect-SqlDscDatabaseEngine`
Expand Down
10 changes: 10 additions & 0 deletions source/Public/Connect-SqlDscDatabaseEngine.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
Connects to the instance 'MyInstance' on the server 'sql.company.local'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -Credential ([System.Management.Automation.PSCredential]::new('DOMAIN\SqlUser', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force)))
Connects to the default instance on the local server impersonating the Windows user 'DOMAIN\SqlUser'.
.EXAMPLE
Connect-SqlDscDatabaseEngine -LoginType 'SqlLogin' -Credential ([System.Management.Automation.PSCredential]::new('sa', (ConvertTo-SecureString -String 'MyP@ssw0rd1' -AsPlainText -Force)))
Connects to the default instance on the local server using the SQL login 'sa'.
.OUTPUTS
`[Microsoft.SqlServer.Management.Smo.Server]`
#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,49 @@ Describe 'Connect-SqlDscDatabaseEngine' -Tag @('Integration_SQL2016', 'Integrati
}


$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}

Context 'When connecting to the named instance impersonating a Windows user' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}


$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
} | Should -Not -Throw
}
}

Context 'When connecting to the named instance using a SQL login' {
It 'Should return the correct result' {
{
$sqlAdministratorUserName = 'sa'
$sqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force

$connectSqlDscDatabaseEngineParameters = @{
InstanceName = 'DSCSQLTEST' # cSpell: disable-line
LoginType = 'SqlLogin'
Credential = [System.Management.Automation.PSCredential]::new($sqlAdministratorUserName, $sqlAdministratorPassword)
Verbose = $true
ErrorAction = 'Stop'
}


$sqlServerObject = Connect-SqlDscDatabaseEngine @connectSqlDscDatabaseEngineParameters

$sqlServerObject.Status.ToString() | Should -Match '^Online$'
Expand Down
209 changes: 122 additions & 87 deletions tests/Integration/Commands/Install-SqlDscServer.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ BeforeDiscovery {
Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
BeforeAll {
Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose

$computerName = Get-ComputerName
}

Context 'When using Install parameter set' {
Context 'When installing database engine default instance' {
It 'Should run the command without throwing' {
{
$computerName = Get-ComputerName

# Set splatting parameters for Install-SqlDscServer
$installSqlDscServerParameters = @{
Install = $true
Expand Down Expand Up @@ -158,95 +158,130 @@ Describe 'Install-SqlDscServer' -Tag @('Integration_SQL2016', 'Integration_SQL20
# $modulePath = Split-Path -Parent -Path (Get-Module -name SqlServerDsc -ListAvailable).ModuleBase
# }

# It 'Should run the command without throwing' {
# {
# <#
# Fails with the following error message:

# VERBOSE: Exit code (Decimal): -2068774911
# VERBOSE: Exit facility code: 1201
# VERBOSE: Exit error code: 1
# VERBOSE: Exit message: There was an error generating the XML document.

# Searches points to a permission issue, but the user has been
# granted the local administrator permissions. But code be
# Searches also points to user right SeEnableDelegationPrivilege
# which was not evaluated if it was set correctly or even needed.
# #>
# $installScriptBlock = {
# param
# (
# [Parameter(Mandatory = $true)]
# [System.String]
# $IsoDrivePath,

# [Parameter(Mandatory = $true)]
# [System.String]
# $ComputerName,

# [Parameter(Mandatory = $true)]
# [System.String]
# $ModulePath
# )

# Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose

# Import-Module -Name $ModulePath -Force -ErrorAction 'Stop'

# # Set splatting parameters for Install-SqlDscServer
# $installSqlDscServerParameters = @{
# Install = $true
# AcceptLicensingTerms = $true
# InstanceName = 'DSCSQLTEST' # cSpell: disable-line
# Features = 'SQLENGINE'
# SqlSysAdminAccounts = @(
# ('{0}\SqlAdmin' -f $ComputerName)
# )
# SqlSvcAccount = '{0}\svc-SqlPrimary' -f $ComputerName
# SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
# SqlSvcStartupType = 'Automatic'
# AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $ComputerName
# AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
# AgtSvcStartupType = 'Automatic'
# BrowserSvcStartupType = 'Automatic'
# SecurityMode = 'SQL'
# SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
# SqlCollation = 'Finnish_Swedish_CI_AS'
# InstallSharedDir = 'C:\Program Files\Microsoft SQL Server'
# InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server'
# NpEnabled = $true
# TcpEnabled = $true
# MediaPath = $IsoDrivePath
# Verbose = $true
# ErrorAction = 'Stop'
# Force = $true
# }

# Install-SqlDscServer @installSqlDscServerParameters
# }

# $invokeCommandUsername = 'SqlInstall' # Using computer name as NetBIOS name throw exception.
# $invokeCommandPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
# $invokeCommandCredential = New-Object System.Management.Automation.PSCredential ($invokeCommandUsername, $invokeCommandPassword)

# # Runs command as SqlInstall user.
# Invoke-Command -ComputerName 'localhost' -Credential $invokeCommandCredential -ScriptBlock $installScriptBlock -ArgumentList @(
# $env:IsoDrivePath, # Already set by the prerequisites tests
# (Get-ComputerName),
# $modulePath
# )
# } | Should -Not -Throw
# }
It 'Should run the command without throwing' {
{
# Set splatting parameters for Install-SqlDscServer
$installSqlDscServerParameters = @{
Install = $true
AcceptLicensingTerms = $true
InstanceName = 'DSCSQLTEST'
Features = 'SQLENGINE'
SqlSysAdminAccounts = @(
('{0}\SqlAdmin' -f $computerName)
)
SqlSvcAccount = '{0}\svc-SqlPrimary' -f $computerName
SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
SqlSvcStartupType = 'Automatic'
AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $computerName
AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
AgtSvcStartupType = 'Automatic'
BrowserSvcStartupType = 'Automatic'
SecurityMode = 'SQL'
SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
SqlCollation = 'Finnish_Swedish_CI_AS'
InstallSharedDir = 'C:\Program Files\Microsoft SQL Server'
InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server'
NpEnabled = $true
TcpEnabled = $true
MediaPath = $env:IsoDrivePath
Verbose = $true
ErrorAction = 'Stop'
Force = $true
}

# It 'Should have installed the SQL Server database engine' {
# # Validate the SQL Server installation
# $sqlServerService = Get-Service -Name 'SQL Server (DSCSQLTEST)' # cSpell: disable-line
Install-SqlDscServer @installSqlDscServerParameters

# $sqlServerService | Should -Not -BeNullOrEmpty
# $sqlServerService.Status | Should -Be 'Running'
# }
# {
# <#
# Fails with the following error message:

# VERBOSE: Exit code (Decimal): -2068774911
# VERBOSE: Exit facility code: 1201
# VERBOSE: Exit error code: 1
# VERBOSE: Exit message: There was an error generating the XML document.

# Searches points to a permission issue, but the user has been
# granted the local administrator permissions. But code be
# Searches also points to user right SeEnableDelegationPrivilege
# which was not evaluated if it was set correctly or even needed.
# #>
# $installScriptBlock = {
# param
# (
# [Parameter(Mandatory = $true)]
# [System.String]
# $IsoDrivePath,

# [Parameter(Mandatory = $true)]
# [System.String]
# $ComputerName,

# [Parameter(Mandatory = $true)]
# [System.String]
# $ModulePath
# )

# Write-Verbose -Message ('Running integration test as user ''{0}''.' -f $env:UserName) -Verbose

# Import-Module -Name $ModulePath -Force -ErrorAction 'Stop'

# # Set splatting parameters for Install-SqlDscServer
# $installSqlDscServerParameters = @{
# Install = $true
# AcceptLicensingTerms = $true
# InstanceName = 'DSCSQLTEST' # cSpell: disable-line
# Features = 'SQLENGINE'
# SqlSysAdminAccounts = @(
# ('{0}\SqlAdmin' -f $ComputerName)
# )
# SqlSvcAccount = '{0}\svc-SqlPrimary' -f $ComputerName
# SqlSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
# SqlSvcStartupType = 'Automatic'
# AgtSvcAccount = '{0}\svc-SqlAgentPri' -f $ComputerName
# AgtSvcPassword = ConvertTo-SecureString -String 'yig-C^Equ3' -AsPlainText -Force
# AgtSvcStartupType = 'Automatic'
# BrowserSvcStartupType = 'Automatic'
# SecurityMode = 'SQL'
# SAPwd = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
# SqlCollation = 'Finnish_Swedish_CI_AS'
# InstallSharedDir = 'C:\Program Files\Microsoft SQL Server'
# InstallSharedWOWDir = 'C:\Program Files (x86)\Microsoft SQL Server'
# NpEnabled = $true
# TcpEnabled = $true
# MediaPath = $IsoDrivePath
# Verbose = $true
# ErrorAction = 'Stop'
# Force = $true
# }

# Install-SqlDscServer @installSqlDscServerParameters
# }

# $invokeCommandUsername = 'SqlInstall' # Using computer name as NetBIOS name throw exception.
# $invokeCommandPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
# $invokeCommandCredential = New-Object System.Management.Automation.PSCredential ($invokeCommandUsername, $invokeCommandPassword)

# # Runs command as SqlInstall user.
# Invoke-Command -ComputerName 'localhost' -Credential $invokeCommandCredential -ScriptBlock $installScriptBlock -ArgumentList @(
# $env:IsoDrivePath, # Already set by the prerequisites tests
# (Get-ComputerName),
# $modulePath
# )
# } | Should -Not -Throw
} | Should -Not -Throw
}

It 'Should have installed the SQL Server database engine' {
# Validate the SQL Server installation
$sqlServerService = Get-Service -Name 'SQL Server (DSCSQLTEST)' # cSpell: disable-line

$sqlServerService | Should -Not -BeNullOrEmpty
$sqlServerService.Status | Should -Be 'Running'
}
}

# # Enable this to debugging the last installation by output the Summary.txt.
# # Currently there seems impossible to run this only when an It-block fails.
# Context 'Output the Summary.txt log file' {
# BeforeAll {
# <#
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/Commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,16 @@ The following local users are created and can be used by integration tests.
<!-- markdownlint-disable MD013 -->
User | Password | Permission | Description
--- | --- | --- | ---
.\SqlInstall | P@ssw0rd1 | Local Windows administrator and sysadmin | Runs Setup for all instances.
.\SqlAdmin | P@ssw0rd1 | Local Windows user and sysadmin | Administrator of all SQL Server instances.
.\SqlInstall | P@ssw0rd1 | Local Windows administrator and sysadmin | Runs Setup for all the instances.
.\SqlAdmin | P@ssw0rd1 | Local Windows user and sysadmin | Administrator of all the SQL Server instances.
.\svc-SqlPrimary | yig-C^Equ3 | Local Windows user. | Runs the SQL Server service.
.\svc-SqlAgentPri | yig-C^Equ3 | Local Windows user. | Runs the SQL Server Agent service.
.\svc-SqlSecondary | yig-C^Equ3 | Local Windows user. | Runs the SQL Server service in multi node scenarios.
.\svc-SqlAgentSec | yig-C^Equ3 | Local Windows user. | Runs the SQL Server Agent service in multi node scenarios.

Login | Password | Permission | Description
--- | --- | --- | ---
sa | P@ssw0rd1 | sysadmin | Administrator of the Database Engine instances DSCSQLTEST.
sa | P@ssw0rd1 | sysadmin | Administrator of all the Database Engine instances.
<!-- markdownlint-enable MD013 -->

### Image media (ISO)
Expand Down

0 comments on commit 47e02f2

Please sign in to comment.