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

SqlServerLogin: Added integration tests #998

Merged
merged 1 commit into from
Jan 6, 2018
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,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