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: Clean up integration tests using latest template #1278

Merged
merged 18 commits into from
Feb 17, 2019
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
separate build job.
- Updated the appveyor.yml to have the correct build step, and also
correct run the build step only in one of the jobs.
- Update integration tests to use the new integration test template.
- Added SqlAgentOperator resource.
- Changes to SqlServiceAccount
- Fixed Get-ServiceObject when searching for Integration Services service.
Expand Down Expand Up @@ -56,6 +57,13 @@
- Add integration tests
([issue #744](https://github.com/PowerShell/SqlServerDsc/issues/744)).
[Maxime Daniou (@mdaniou)](https://github.com/mdaniou)
- Changes to SqlServiceAccount
- Now the correct service type string value is returned by the function
`Get-TargetResource`. Previously one value was passed in as a parameter
(e.g. `DatabaseEngine`), but a different string value as returned
(e.g. `SqlServer`). Now `Get-TargetResource` return the same values
that can be passed as values in the parameter `ServiceType`
([issue #981](https://github.com/PowerShell/SqlServerDsc/issues/981)).

## 12.2.0.0

Expand Down
92 changes: 88 additions & 4 deletions DSCResources/MSFT_SqlServiceAccount/MSFT_SqlServiceAccount.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ function Get-TargetResource
# Replace a domain of '.' with the value for $ServerName
$serviceAccountName = $serviceObject.ServiceAccount -ireplace '^([\.])\\(.*)$', "$ServerName\`$2"

$serviceType = ConvertTo-ResourceServiceType -ServiceType $serviceObject.Type

# Return a hash table with the service information
return @{
ServerName = $ServerName
InstanceName = $InstanceName
ServiceType = $serviceObject.Type
ServiceType = $serviceType
ServiceAccountName = $serviceAccountName
}
}
Expand Down Expand Up @@ -112,7 +114,7 @@ function Get-TargetResource

.EXAMPLE
Test-TargetResource -ServerName $env:COMPUTERNAME -InstanceName MSSQLSERVER -ServiceType DatabaseEngine -ServiceAccount $account
Test-TargetResource -ServerName $env:COMPUTERNAME -InstanceName MSSQLSERVER -SerticeType IntegrationServices -ServiceAccount $account -VersionNumber 130
Test-TargetResource -ServerName $env:COMPUTERNAME -InstanceName MSSQLSERVER -ServiceType IntegrationServices -ServiceAccount $account -VersionNumber 130

#>
function Test-TargetResource
Expand Down Expand Up @@ -271,7 +273,7 @@ function Set-TargetResource
.PARAMETER ServiceType
Type of service to be managed. Must be one of the following:
DatabaseEngine, SQLServerAgent, Search, IntegrationServices, AnalysisServices, ReportingServices, SQLServerBrowser, NotificationServices.

.PARAMETER VersionNumber
Version number of IntegrationServices.

Expand Down Expand Up @@ -306,7 +308,7 @@ function Get-ServiceObject
if (($ServiceType -eq 'IntegrationServices') -and ([String]::IsNullOrEmpty($VersionNumber)))
{
$errorMessage = $script:localizedData.MissingParameter -f $ServiceType
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'VersionNumber'
New-InvalidArgumentException -Message $errorMessage -ArgumentName 'VersionNumber'
}

# Load the SMO libraries
Expand Down Expand Up @@ -405,6 +407,88 @@ function ConvertTo-ManagedServiceType
return $serviceTypeValue -as [Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType]
}

<#
.SYNOPSIS
Converts from the string value, that was returned from the type
Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType, to the
appropriate project's standard SQL Service type string values.

.PARAMETER ServiceType
The string value of the Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType.
Must be one of the following:
SqlServer, SqlAgent, Search, SqlServerIntegrationService, AnalysisServer,
ReportServer, SqlBrowser, NotificationServer.

.NOTES
If an unknown type is passed in parameter ServiceType, the same type will
be returned.

.EXAMPLE
ConvertTo-ResourceServiceType -ServiceType 'SqlServer'
#>
function ConvertTo-ResourceServiceType
{
[CmdletBinding()]
[OutputType([System.String])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$ServiceType
)

# Map the project-specific ServiceType to a valid value from the ManagedServiceType enumeration
switch ($ServiceType)
{
'SqlServer'
{
$serviceTypeValue = 'DatabaseEngine'
}

'SqlAgent'
{
$serviceTypeValue = 'SQLServerAgent'
}

'Search'
{
$serviceTypeValue = 'Search'
}

'SqlServerIntegrationService'
{
$serviceTypeValue = 'IntegrationServices'
}

'AnalysisServer'
{
$serviceTypeValue = 'AnalysisServices'
}

'ReportServer'
{
$serviceTypeValue = 'ReportingServices'
}

'SqlBrowser'
{
$serviceTypeValue = 'SQLServerBrowser'
}

'NotificationServer'
{
$serviceTypeValue = 'NotificationServices'
}

default
{
$serviceTypeValue = $ServiceType
}
}

return $serviceTypeValue
}

<#
.SYNOPSIS
Gets the name of a service based on the instance name and type.
Expand Down
61 changes: 29 additions & 32 deletions Tests/Integration/MSFT_SqlAgentOperator.Integration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,43 @@ if (Test-SkipContinuousIntegrationTask -Type 'Integration')
return
}

$script:DSCModuleName = 'SqlServerDsc'
$script:DSCResourceFriendlyName = 'SqlAgentOperator'
$script:DSCResourceName = "MSFT_$($script:DSCResourceFriendlyName)"
$script:dscModuleName = 'SqlServerDsc'
$script:dscResourceFriendlyName = 'SqlAgentOperator'
$script:dscResourceName = "MSFT_$($script:dscResourceFriendlyName)"

#region HEADER
# Integration Test Template Version: 1.1.2
[System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
##region HEADER
# Integration Test Template Version: 1.3.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'))
& 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 `
-DSCModuleName $script:dscModuleName `
-DSCResourceName $script:dscResourceName `
-TestType Integration

#endregion

$mockSqlInstallAccountPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
$mockSqlInstallAccountUserName = "$env:COMPUTERNAME\SqlInstall"
$mockSqlInstallCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $mockSqlInstallAccountUserName, $mockSqlInstallAccountPassword

# Using try/finally to always cleanup.
try
{
$configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1"
$configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dscResourceName).config.ps1"
. $configFile

$mockName = $ConfigurationData.AllNodes.Name
$mockEmailAddress = $ConfigurationData.AllNodes.EmailAddress

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

$configurationName = "$($script:DSCResourceName)_Add_Config"
$configurationName = "$($script:dscResourceName)_Add_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlInstallCredential = $mockSqlInstallCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
Expand Down Expand Up @@ -82,24 +74,26 @@ try

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Present'
$resourceCurrentState.Name | Should -Be $mockName
$resourceCurrentState.EmailAddress | Should -Be $mockEmailAddress
$resourceCurrentState.Name | Should -Be $ConfigurationData.AllNodes.Name
$resourceCurrentState.EmailAddress | Should -Be $ConfigurationData.AllNodes.EmailAddress
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be $true
}
}

$configurationName = "$($script:DSCResourceName)_Remove_Config"
$configurationName = "$($script:dscResourceName)_Remove_Config"

Context ('When using configuration {0}' -f $configurationName) {
It 'Should compile and apply the MOF without throwing' {
{
$configurationParameters = @{
SqlInstallCredential = $mockSqlInstallCredential
OutputPath = $TestDrive
# The variable $ConfigurationData was dot-sourced above.
ConfigurationData = $ConfigurationData
Expand Down Expand Up @@ -128,15 +122,18 @@ try

It 'Should have set the resource and all the parameters should match' {
$resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript {
$_.ConfigurationName -eq $configurationName
} | Where-Object -FilterScript {
$_.ResourceId -eq $resourceId
$_.ConfigurationName -eq $configurationName `
-and $_.ResourceId -eq $resourceId
}

$resourceCurrentState.Ensure | Should -Be 'Absent'
$resourceCurrentState.Name | Should -BeNullOrEmpty
$resourceCurrentState.EmailAddress | Should -BeNullOrEmpty
}

It 'Should return $true when Test-DscConfiguration is run' {
Test-DscConfiguration -Verbose | Should -Be $true
}
}
}
}
Expand Down
80 changes: 49 additions & 31 deletions Tests/Integration/MSFT_SqlAgentOperator.config.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
$ConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'
ServerName = $env:COMPUTERNAME
InstanceName = 'DSCSQL2016'
#region HEADER
# Integration Test Config Template Version: 1.2.0
#endregion

Name = 'MyOperator'
EmailAddress = '[email protected]'
$configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json')
if (Test-Path -Path $configFile)
{
<#
Allows reading the configuration data from a JSON file,
for real testing scenarios outside of the CI.
#>
$ConfigurationData = Get-Content -Path $configFile | ConvertFrom-Json
}
else
{
$ConfigurationData = @{
AllNodes = @(
@{
NodeName = 'localhost'

CertificateFile = $env:DscPublicCertificatePath
}
)
UserName = "$env:COMPUTERNAME\SqlInstall"
Password = 'P@ssw0rd1'

ServerName = $env:COMPUTERNAME
InstanceName = 'DSCSQL2016'

Name = 'MyOperator'
EmailAddress = '[email protected]'

CertificateFile = $env:DscPublicCertificatePath
}
)
}
}

<#
.SYNOPSIS
Adds a SQL Agent operator.
#>
Configuration MSFT_SqlAgentOperator_Add_Config
{
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$SqlInstallCredential
)

Import-DscResource -ModuleName 'SqlServerDsc'

node localhost {
node $AllNodes.NodeName
{
SqlAgentOperator 'Integration_Test'
{
Ensure = 'Present'
Expand All @@ -34,24 +51,23 @@ Configuration MSFT_SqlAgentOperator_Add_Config
Name = $Node.Name
EmailAddress = $Node.EmailAddress

PsDscRunAsCredential = $SqlInstallCredential
PsDscRunAsCredential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList @($Node.Username, (ConvertTo-SecureString -String $Node.Password -AsPlainText -Force))
}
}
}

<#
.SYNOPSIS
Removes a SQL Agent operator.
#>
Configuration MSFT_SqlAgentOperator_Remove_Config
{
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.PSCredential]
$SqlInstallCredential
)

Import-DscResource -ModuleName 'SqlServerDsc'

node localhost {
node $AllNodes.NodeName
{
SqlAgentOperator 'Integration_Test'
{
Ensure = 'Absent'
Expand All @@ -60,7 +76,9 @@ Configuration MSFT_SqlAgentOperator_Remove_Config
Name = $Node.Name
EmailAddress = $Node.EmailAddress

PsDscRunAsCredential = $SqlInstallCredential
PsDscRunAsCredential = New-Object `
-TypeName System.Management.Automation.PSCredential `
-ArgumentList @($Node.Username, (ConvertTo-SecureString -String $Node.Password -AsPlainText -Force))
}
}
}
Expand Down
Loading