Skip to content

Commit

Permalink
SqlServerLogin: Added integration tests (#998)
Browse files Browse the repository at this point in the history
- Changes to xSqlServerLogin
  - Added integration tests (issue #748).
  • Loading branch information
johlju authored Jan 6, 2018
1 parent 7516394 commit 60d19e6
Show file tree
Hide file tree
Showing 4 changed files with 669 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
Reporting Services to use SSL when connecting ([issue #990](https://github.com/PowerShell/SqlServerDsc/issues/990)).
- Added complete example for SqlRS (based on the integration tests)
([issue #634](https://github.com/PowerShell/SqlServerDsc/issues/634)).
- Changes to SqlServerLogin
- Added integration tests ([issue #748](https://github.com/PowerShell/SqlServerDsc/issues/748)).
- Changes to SqlServerNetwork
- Added sysadmin account parameter usage to the examples.
- Changes to SqlServerReplication
Expand Down
379 changes: 379 additions & 0 deletions Tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,379 @@
$script:DSCModuleName = 'SqlServerDsc'
$script:DSCResourceFriendlyName = 'SqlServerLogin'
$script:DSCResourceName = "MSFT_$($script:DSCResourceFriendlyName)"

if (-not $env:APPVEYOR -eq $true)
{
Write-Warning -Message ('Integration test for {0} will be skipped unless $env:APPVEYOR equals $true' -f $script:DSCResourceName)
return
}

#region HEADER
# Integration Test Template Version: 1.1.2
[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or `
(-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) )
{
& git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))
}

Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceName `
-TestType Integration

#endregion

$mockSqlAdminAccountPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockSqlAdminAccountUserName = "$env:COMPUTERNAME\SqlAdmin"
$mockSqlAdminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $mockSqlAdminAccountUserName, $mockSqlAdminAccountPassword

$mockUserAccountPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockUserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'OnlyPasswordIsUsed', $mockUserAccountPassword

try
{
$configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1"
. $configFile

$mockDscUser1Name = $ConfigurationData.AllNodes.DscUser1Name
$mockDscUser1Type = $ConfigurationData.AllNodes.DscUser1Type
$mockDscUser2Name = $ConfigurationData.AllNodes.DscUser2Name
$mockDscUser2Type = $ConfigurationData.AllNodes.DscUser2Type
$mockDscUser3Name = $ConfigurationData.AllNodes.DscUser3Name
$mockDscUser3Type = $ConfigurationData.AllNodes.DscUser3Type
$mockDscUser4Name = $ConfigurationData.AllNodes.DscUser4Name
$mockDscUser4Type = $ConfigurationData.AllNodes.DscUser4Type
$mockDscSqlUsers1Name = $ConfigurationData.AllNodes.DscSqlUsers1Name
$mockDscSqlUsers1Type = $ConfigurationData.AllNodes.DscSqlUsers1Type

Describe "$($script:DSCResourceName)_Integration" {
BeforeAll {
$resourceId = "[$($script:DSCResourceFriendlyName)]Integration_Test"
}

$configurationName = "$($script:DSCResourceName)_CreateDependencies_Config"

<#
This will install dependencies using other resource modules, and is
done without running extra tests on the result.
This is done to speed up testing.
#>
Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
UserCredential = $mockUserCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}
}

$configurationName = "$($script:DSCResourceName)_AddLoginDscUser1_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockDscUser1Name
$resourceCurrentState.LoginType | Should -Be $mockDscUser1Type
$resourceCurrentState.Disabled | Should -Be $false
}
}

$configurationName = "$($script:DSCResourceName)_AddLoginDscUser2_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockDscUser2Name
$resourceCurrentState.LoginType | Should -Be $mockDscUser2Type
$resourceCurrentState.Disabled | Should -Be $false
}
}

$configurationName = "$($script:DSCResourceName)_AddLoginDscUser3_Disabled_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockDscUser3Name
$resourceCurrentState.LoginType | Should -Be $mockDscUser3Type
$resourceCurrentState.Disabled | Should -Be $true
}
}

$configurationName = "$($script:DSCResourceName)_AddLoginDscUser4_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
UserCredential = $mockUserCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockDscUser4Name
$resourceCurrentState.LoginType | Should -Be $mockDscUser4Type
$resourceCurrentState.Disabled | Should -Be $false
}
}

$configurationName = "$($script:DSCResourceName)_AddLoginDscSqlUsers1_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockDscSqlUsers1Name
$resourceCurrentState.LoginType | Should -Be $mockDscSqlUsers1Type
$resourceCurrentState.Disabled | Should -Be $false
}
}

$configurationName = "$($script:DSCResourceName)_RemoveLoginDscUser3_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlAdministratorCredential = $mockSqlAdminCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
}

& $configurationName @configurationParameters

$startDscConfigurationParameters = @{
Path = $TestDrive
ComputerName = 'localhost'
Wait = $true
Verbose = $true
Force = $true
ErrorAction = 'Stop'
}

Start-DscConfiguration @startDscConfigurationParameters
} | Should -Not -Throw
}

It 'Should be able to call Get-DscConfiguration without throwing' {
{ Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw
}

It 'Should have set the resource and all the parameters should match' {
$currentConfiguration = Get-DscConfiguration

$resourceCurrentState = $currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Absent'
$resourceCurrentState.Name | Should -Be $mockDscUser3Name
$resourceCurrentState.LoginType | Should -BeNullOrEmpty
}
}
}
}
finally
{
#region FOOTER

Restore-TestEnvironment -TestEnvironment $TestEnvironment

#endregion
}
Loading

0 comments on commit 60d19e6

Please sign in to comment.