Skip to content

Commit

Permalink
SqlScript/SqlScriptQuery: Added localization (dsccommunity#1324)
Browse files Browse the repository at this point in the history
- Changes to SqlScript
  - Added en-US localization (issue dsccommunity#624).
  - Added additional unit tests for code coverage.
- Changes to SqlScriptQuery
  - Added en-US localization.
  - Added additional unit tests for code coverage.
  • Loading branch information
johlju authored Apr 24, 2019
1 parent a252cca commit cce75a3
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
`Invoke-SqlScript` so that `PRINT` statements is outputted correctly
when verbose output is requested, e.g
`Start-DscConfiguration -Verbose`.
- Added en-US localization ([issue #624](https://github.com/PowerShell/SqlServerDsc/issues/624)).
- Added additional unit tests for code coverage.
- Changes to SqlScriptQuery
- Correctly passes the `$VerbosePreference` to the helper function
`Invoke-SqlScript` so that `PRINT` statements is outputted correctly
when verbose output is requested, e.g
`Start-DscConfiguration -Verbose`.
- Added en-US localization.
- Added additional unit tests for code coverage.
- Changes to SqlSetup
- Concatenated Robocopy localization strings ([issue #694](https://github.com/PowerShell/SqlServerDsc/issues/694)).
- Changes to SqlWaitForAG
Expand Down
26 changes: 26 additions & 0 deletions DSCResources/MSFT_SqlScript/MSFT_SqlScript.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath '
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlScript'

<#
.SYNOPSIS
Returns the current state of the SQL Server features.
Expand Down Expand Up @@ -90,6 +92,10 @@ function Get-TargetResource
ErrorAction = 'Stop'
}

Write-Verbose -Message (
$script:localizedData.ExecutingGetScript -f $GetFilePath, $ServerInstance
)

$result = Invoke-SqlScript @invokeParameters

$getResult = Out-String -InputObject $result
Expand Down Expand Up @@ -177,6 +183,10 @@ function Set-TargetResource
$Variable
)

Write-Verbose -Message (
$script:localizedData.ExecutingSetScript -f $SetFilePath, $ServerInstance
)

$invokeParameters = @{
ServerInstance = $ServerInstance
InputFile = $SetFilePath
Expand Down Expand Up @@ -261,8 +271,16 @@ function Test-TargetResource
$Variable
)

Write-Verbose -Message (
$script:localizedData.TestingConfiguration
)

try
{
Write-Verbose -Message (
$script:localizedData.ExecutingTestScript -f $TestFilePath, $ServerInstance
)

$invokeParameters = @{
ServerInstance = $ServerInstance
InputFile = $TestFilePath
Expand All @@ -277,10 +295,18 @@ function Test-TargetResource

if ($null -eq $result)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState
)

return $true
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState
)

return $false
}
}
Expand Down
8 changes: 8 additions & 0 deletions DSCResources/MSFT_SqlScript/en-US/MSFT_SqlScript.strings.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ConvertFrom-StringData @'
ExecutingGetScript = Executing the Get script from the file path '{0}' on the instance '{1}'.
ExecutingSetScript = Executing the Set script from the file path '{0}' on the instance '{1}'.
ExecutingTestScript = Executing the Test script from the file path '{0}' on the instance '{1}'.
TestingConfiguration = Determines if the configuration in the Set script is in desired state.
InDesiredState = The configuration is in desired state.
NotInDesiredState = The configuration is not in desired state.
'@
26 changes: 26 additions & 0 deletions DSCResources/MSFT_SqlScriptQuery/MSFT_SqlScriptQuery.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Import-Module -Name (Join-Path -Path $script:localizationModulePath -ChildPath '
$script:resourceHelperModulePath = Join-Path -Path $script:modulesFolderPath -ChildPath 'DscResource.Common'
Import-Module -Name (Join-Path -Path $script:resourceHelperModulePath -ChildPath 'DscResource.Common.psm1')

$script:localizedData = Get-LocalizedData -ResourceName 'MSFT_SqlScriptQuery'

<#
.SYNOPSIS
Returns the current state of the SQL Server features.
Expand Down Expand Up @@ -80,6 +82,10 @@ function Get-TargetResource
$Variable
)

Write-Verbose -Message (
$script:localizedData.ExecutingGetQuery -f $ServerInstance
)

$invokeParameters = @{
Query = $GetQuery
ServerInstance = $ServerInstance
Expand Down Expand Up @@ -177,6 +183,10 @@ function Set-TargetResource
$Variable
)

Write-Verbose -Message (
$script:localizedData.ExecutingSetQuery -f $ServerInstance
)

$invokeParameters = @{
Query = $SetQuery
ServerInstance = $ServerInstance
Expand Down Expand Up @@ -261,8 +271,16 @@ function Test-TargetResource
$Variable
)

Write-Verbose -Message (
$script:localizedData.TestingConfiguration
)

try
{
Write-Verbose -Message (
$script:localizedData.ExecutingTestQuery -f $ServerInstance
)

$invokeParameters = @{
Query = $TestQuery
ServerInstance = $ServerInstance
Expand All @@ -277,10 +295,18 @@ function Test-TargetResource

if ($null -eq $result)
{
Write-Verbose -Message (
$script:localizedData.InDesiredState
)

return $true
}
else
{
Write-Verbose -Message (
$script:localizedData.NotInDesiredState
)

return $false
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ConvertFrom-StringData @'
ExecutingGetQuery = Executing the Get query on the instance '{0}'.
ExecutingSetQuery = Executing the Set query on the instance '{0}'.
ExecutingTestQuery = Executing the Test query on the instance '{0}'.
TestingConfiguration = Determines if the configuration in the Set query is in desired state.
InDesiredState = The configuration is in desired state.
NotInDesiredState = The configuration is not in desired state.
'@
71 changes: 44 additions & 27 deletions Tests/Unit/MSFT_SqlScript.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ $TestEnvironment = Initialize-TestEnvironment `

#endregion HEADER

function Invoke-TestSetup {
function Invoke-TestSetup
{
# Loading mocked classes
Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SqlPowerShellSqlExecutionException.cs')
}

function Invoke-TestCleanup {
function Invoke-TestCleanup
{
Restore-TestEnvironment -TestEnvironment $TestEnvironment
}

Expand Down Expand Up @@ -156,44 +158,59 @@ try
}

Describe 'MSFT_SqlScript\Test-TargetResource' {
Context 'When Test-TargetResource runs script without issue' {
Mock -CommandName Invoke-SqlScript
Context 'When the system is in the desired state' {
Context 'When Test-TargetResource runs script without issue' {
Mock -CommandName Invoke-SqlScript

It 'Should return true' {
$result = Test-TargetResource @testParameters
$result | Should -Be $true
It 'Should return true' {
$result = Test-TargetResource @testParameters
$result | Should -Be $true
}
}
}

Context 'When Test-TargetResource runs script without issue with timeout' {
Mock -CommandName Invoke-SqlScript
Context 'When Test-TargetResource runs script without issue with timeout' {
Mock -CommandName Invoke-SqlScript

It 'Should return true' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $true
It 'Should return true' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $true
}
}
}

Context 'When Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' {
Mock -CommandName Invoke-SqlScript -MockWith {
throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException
}
Context 'When the system is not in the desired state' {
Context 'When Invoke-SqlScript returns an SQL error code from the script that was ran' {
Mock -CommandName Invoke-SqlScript -MockWith {
return 1
}

It 'Should return false' {
$result = Test-TargetResource @testParameters
$result | Should -Be $false
It 'Should return false' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $false
}
}
}

Context 'When Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' {
$errorMessage = "Failed to run SQL Script"
Context 'When Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' {
Mock -CommandName Invoke-SqlScript -MockWith {
throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException
}

Mock -CommandName Invoke-SqlScript -MockWith {
throw $errorMessage
It 'Should return false' {
$result = Test-TargetResource @testParameters
$result | Should -Be $false
}
}

It 'Should throw the correct error from Invoke-Sqlcmd' {
{ Test-TargetResource @testParameters } | Should -Throw $errorMessage
Context 'When Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' {
$errorMessage = "Failed to run SQL Script"

Mock -CommandName Invoke-SqlScript -MockWith {
throw $errorMessage
}

It 'Should throw the correct error from Invoke-Sqlcmd' {
{ Test-TargetResource @testParameters } | Should -Throw $errorMessage
}
}
}
}
Expand Down
70 changes: 43 additions & 27 deletions Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ $TestEnvironment = Initialize-TestEnvironment `

#endregion HEADER

function Invoke-TestSetup {
function Invoke-TestSetup
{
# Loading mocked classes
Add-Type -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SqlPowerShellSqlExecutionException.cs')

# Importing SQLPS stubs
Import-Module -Name (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Stubs') -ChildPath 'SQLPSStub.psm1') -Force -Global
}

function Invoke-TestCleanup {
function Invoke-TestCleanup
{
Restore-TestEnvironment -TestEnvironment $TestEnvironment
}

Expand Down Expand Up @@ -159,44 +161,58 @@ try
}

Describe 'MSFT_SqlScriptQuery\Test-TargetResource' {
Context 'Test-TargetResource runs script without issue' {
Mock -CommandName Invoke-SqlScript
Context 'When the system is in the desired state' {
Context 'Test-TargetResource runs script without issue' {
Mock -CommandName Invoke-SqlScript

It 'Should return true' {
$result = Test-TargetResource @testParameters
$result | Should -Be $true
It 'Should return true' {
$result = Test-TargetResource @testParameters
$result | Should -Be $true
}
}
}

Context 'Test-TargetResource runs script without issue with timeout' {
Mock -CommandName Invoke-SqlScript
Context 'Test-TargetResource runs script without issue with timeout' {
Mock -CommandName Invoke-SqlScript

It 'Should return true' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $true
It 'Should return true' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $true
}
}
}

Context 'Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' {
Mock -CommandName Invoke-SqlScript -MockWith {
throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException
Context 'When the system is not in the desired state' {
Context 'When Invoke-SqlScript returns an SQL error code from the query that was ran' {
Mock -CommandName Invoke-SqlScript -MockWith {
return 1
}
It 'Should return false' {
$result = Test-TargetResource @testParametersTimeout
$result | Should -Be $false
}
}

It 'Should return false' {
$result = Test-TargetResource @testParameters
$result | Should -Be $false
Context 'Test-TargetResource throws the exception SqlPowerShellSqlExecutionException when running the script in the TestFilePath parameter' {
Mock -CommandName Invoke-SqlScript -MockWith {
throw New-Object -TypeName Microsoft.SqlServer.Management.PowerShell.SqlPowerShellSqlExecutionException
}

It 'Should return false' {
$result = Test-TargetResource @testParameters
$result | Should -Be $false
}
}
}

Context 'Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' {
$errorMessage = "Failed to run SQL Script"
Context 'Test-TargetResource throws an unexpected error when running the script in the TestFilePath parameter' {
$errorMessage = "Failed to run SQL Script"

Mock -CommandName Invoke-SqlScript -MockWith {
throw $errorMessage
}
Mock -CommandName Invoke-SqlScript -MockWith {
throw $errorMessage
}

It 'Should throw the correct error from Invoke-Sqlcmd' {
{ Test-TargetResource @testParameters } | Should -Throw $errorMessage
It 'Should throw the correct error from Invoke-Sqlcmd' {
{ Test-TargetResource @testParameters } | Should -Throw $errorMessage
}
}
}
}
Expand Down

0 comments on commit cce75a3

Please sign in to comment.