From 4f59191aaa40285e47a73d3af14da159d80d4904 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Mon, 21 Jan 2019 07:00:16 +0100 Subject: [PATCH] SqlServerDsc: Attempt to mitigate issue #1260 (#1268) - Changes to SqlServerDsc - Reverting the change that was made as part of the issue #1260 in the previous release, as it only mitigated the issue, it did not solve the issue. - Removed the container testing since that broke the integration tests, possible due to using excessive amount of memory on the AppVeyor build worker. This will make the unit tests to take a bit longer to run (issue #1260). - The unit tests and the integration tests are now run in two separate build workers in AppVeyor. One build worker runs the integration tests, while a second build worker runs the unit tests. The build workers runs in parallel on paid accounts, but sequentially on free accounts (issue #1260). - Clean up error handling in some of the integration tests that was part of a workaround for a bug in Pester. The bug is resolved, and the error handling is not again built into Pester. - Changes to SqlSetup - Updated the integration test to stop the named instance while installing the other instances to mitigate issue #1260. --- CHANGELOG.md | 21 + ...T_SqlAlwaysOnService.Integration.Tests.ps1 | 48 +- ...abaseDefaultLocation.Integration.Tests.ps1 | 6 + .../MSFT_SqlRS.Integration.Tests.ps1 | 57 +- .../MSFT_SqlScript.Integration.Tests.ps1 | 6 + .../MSFT_SqlScriptQuery.Integration.Tests.ps1 | 6 + ...qlServerDatabaseMail.Integration.Tests.ps1 | 6 + .../MSFT_SqlServerLogin.Integration.Tests.ps1 | 6 + ...SFT_SqlServerNetwork.Integration.Tests.ps1 | 6 + .../MSFT_SqlServerRole.Integration.Tests.ps1 | 6 + ...rverSecureConnection.Integration.Tests.ps1 | 6 + ...FT_SqlServiceAccount.Integration.Tests.ps1 | 107 +--- .../MSFT_SqlSetup.Integration.Tests.ps1 | 104 ++-- Tests/Integration/MSFT_SqlSetup.config.ps1 | 48 +- Tests/Unit/CommonResourceHelper.Tests.ps1 | 17 +- Tests/Unit/MSFT_SqlAG.Tests.ps1 | 23 +- Tests/Unit/MSFT_SqlAGDatabase.Tests.ps1 | 500 +++++++++--------- Tests/Unit/MSFT_SqlAGListener.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlAGReplica.Tests.ps1 | 18 +- Tests/Unit/MSFT_SqlAlias.Tests.ps1 | 17 +- Tests/Unit/MSFT_SqlAlwaysOnService.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlDatabase.Tests.ps1 | 10 +- .../MSFT_SqlDatabaseDefaultLocation.Tests.ps1 | 17 +- Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 | 10 +- .../Unit/MSFT_SqlDatabasePermission.Tests.ps1 | 10 +- .../MSFT_SqlDatabaseRecoveryModel.Tests.ps1 | 11 +- Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlRS.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlScript.Tests.ps1 | 17 +- Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 | 17 +- .../MSFT_SqlServerConfiguration.Tests.ps1 | 9 +- .../Unit/MSFT_SqlServerDatabaseMail.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1 | 10 +- ...MSFT_SqlServerEndpointPermission.Tests.ps1 | 10 +- .../MSFT_SqlServerEndpointState.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServerLogin.Tests.ps1 | 30 +- Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServerMemory.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServerNetwork.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServerPermission.Tests.ps1 | 10 +- .../Unit/MSFT_SqlServerReplication.Tests.ps1 | 8 +- Tests/Unit/MSFT_SqlServerRole.Tests.ps1 | 10 +- .../MSFT_SqlServerSecureConnection.Tests.ps1 | 10 +- Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 | 16 +- Tests/Unit/MSFT_SqlSetup.Tests.ps1 | 8 +- Tests/Unit/MSFT_SqlWaitForAG.Tests.ps1 | 16 +- Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 | 17 +- Tests/Unit/SqlServerDSCHelper.Tests.ps1 | 20 +- appveyor.yml | 39 +- 49 files changed, 749 insertions(+), 654 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 002d49421..f9b9210dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ ## Unreleased +- Changes to SqlServerDsc + - Reverting the change that was made as part of the + [issue #1260](https://github.com/PowerShell/SqlServerDsc/issues/1260) + in the previous release, as it only mitigated the issue, it did not + solve the issue. + - Removed the container testing since that broke the integration tests, + possible due to using excessive amount of memory on the AppVeyor build + worker. This will make the unit tests to take a bit longer to run. + ([issue #1260](https://github.com/PowerShell/SqlServerDsc/issues/1260)). + - The unit tests and the integration tests are now run in two separate + build workers in AppVeyor. One build worker runs the integration tests, + while a second build worker runs the unit tests. The build workers runs + in parallel on paid accounts, but sequentially on free accounts. + ([issue #1260](https://github.com/PowerShell/SqlServerDsc/issues/1260)). + - Clean up error handling in some of the integration tests that was + part of a workaround for a bug in Pester. The bug is resolved, and + the error handling is not again built into Pester. - Changes to SqlServiceAccount - Fixed Get-ServiceObject when searching for Integration Services service. Unlike the rest of SQL Server services, the Integration Services service @@ -14,6 +31,10 @@ - Changes to SqlServerLogin - Fixed issue in Test-TargetResource to valid password on disabled accounts. ([issue #915](https://github.com/PowerShell/SqlServerDsc/issues/915)). +- Changes to SqlSetup + - Updated the integration test to stop the named instance while installing + the other instances to mitigate + [issue #1260](https://github.com/PowerShell/SqlServerDsc/issues/1260). ## 12.2.0.0 diff --git a/Tests/Integration/MSFT_SqlAlwaysOnService.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlAlwaysOnService.Integration.Tests.ps1 index 6d49d9cb3..90cd6fae5 100644 --- a/Tests/Integration/MSFT_SqlAlwaysOnService.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlAlwaysOnService.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) @@ -29,8 +35,6 @@ $TestEnvironment = Initialize-TestEnvironment ` #endregion -$script:integrationErrorMessagePrefix = 'INTEGRATION ERROR MESSAGE:' - $testRootFolderPath = Split-Path -Path $PSScriptRoot -Parent Import-Module -Name (Join-Path -Path $testRootFolderPath -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'CommonTestHelper.psm1')) -Force @@ -82,15 +86,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $script:integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -118,15 +114,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $script:integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -170,15 +158,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $script:integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -221,15 +201,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $script:integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } diff --git a/Tests/Integration/MSFT_SqlDatabaseDefaultLocation.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlDatabaseDefaultLocation.Integration.Tests.ps1 index ccfac6198..5f4e1711d 100644 --- a/Tests/Integration/MSFT_SqlDatabaseDefaultLocation.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlDatabaseDefaultLocation.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlRS.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlRS.Integration.Tests.ps1 index 88f3b33cd..3c2ed2da7 100644 --- a/Tests/Integration/MSFT_SqlRS.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlRS.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) @@ -27,7 +33,6 @@ $TestEnvironment = Initialize-TestEnvironment ` -DSCResourceName $script:DSCResourceName ` -TestType Integration -$script:integrationErrorMessagePrefix = 'INTEGRATION ERROR MESSAGE:' #endregion $mockSqlInstallAccountPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force @@ -76,15 +81,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -112,15 +109,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -212,15 +201,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -276,15 +257,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -349,15 +322,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } diff --git a/Tests/Integration/MSFT_SqlScript.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlScript.Integration.Tests.ps1 index bbba95568..96f2ffa6d 100644 --- a/Tests/Integration/MSFT_SqlScript.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlScript.Integration.Tests.ps1 @@ -17,6 +17,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlScriptQuery.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlScriptQuery.Integration.Tests.ps1 index 22c7d4f59..e3ce77b68 100644 --- a/Tests/Integration/MSFT_SqlScriptQuery.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlScriptQuery.Integration.Tests.ps1 @@ -17,6 +17,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServerDatabaseMail.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServerDatabaseMail.Integration.Tests.ps1 index b232831aa..da5a2bf9a 100644 --- a/Tests/Integration/MSFT_SqlServerDatabaseMail.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServerDatabaseMail.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 index 5c0273773..f4506483e 100644 --- a/Tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServerLogin.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServerNetwork.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServerNetwork.Integration.Tests.ps1 index 26bab827b..ced9220a4 100644 --- a/Tests/Integration/MSFT_SqlServerNetwork.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServerNetwork.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServerRole.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServerRole.Integration.Tests.ps1 index e10cc609e..d7278852d 100644 --- a/Tests/Integration/MSFT_SqlServerRole.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServerRole.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServerSecureConnection.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServerSecureConnection.Integration.Tests.ps1 index 7d39536ce..42f4fa10c 100644 --- a/Tests/Integration/MSFT_SqlServerSecureConnection.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServerSecureConnection.Integration.Tests.ps1 @@ -17,6 +17,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Integration/MSFT_SqlServiceAccount.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlServiceAccount.Integration.Tests.ps1 index 4a2bf7d32..3daacb144 100644 --- a/Tests/Integration/MSFT_SqlServiceAccount.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlServiceAccount.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) @@ -27,7 +33,6 @@ $TestEnvironment = Initialize-TestEnvironment ` -DSCResourceName $script:DSCResourceName ` -TestType Integration -$script:integrationErrorMessagePrefix = 'INTEGRATION ERROR MESSAGE:' #endregion $mockSqlInstallAccountPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force @@ -92,15 +97,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -128,15 +125,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -182,15 +171,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -235,15 +216,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -289,15 +262,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -341,15 +306,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -377,15 +334,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -431,15 +380,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -484,15 +425,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } @@ -538,15 +471,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } diff --git a/Tests/Integration/MSFT_SqlSetup.Integration.Tests.ps1 b/Tests/Integration/MSFT_SqlSetup.Integration.Tests.ps1 index f542dc88d..942872135 100644 --- a/Tests/Integration/MSFT_SqlSetup.Integration.Tests.ps1 +++ b/Tests/Integration/MSFT_SqlSetup.Integration.Tests.ps1 @@ -12,6 +12,12 @@ if (-not $env:APPVEYOR -eq $true) return } +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Integration') +{ + Write-Verbose -Message ('Integration test for {0} will be skipped unless $env:CONFIGURATION is set to ''Integration''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Integration Test Template Version: 1.1.2 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) @@ -26,8 +32,6 @@ $TestEnvironment = Initialize-TestEnvironment ` -DSCModuleName $script:DSCModuleName ` -DSCResourceName $script:DSCResourceName ` -TestType Integration - -$script:integrationErrorMessagePrefix = 'INTEGRATION ERROR MESSAGE:' #endregion <# @@ -109,10 +113,19 @@ try # Download SQL Server media if (-not (Test-Path -Path $mockIsoMediaFilePath)) { - Write-Verbose -Message "Start downloading the SQL Server media iso at $(Get-Date -Format 'yyyy-MM-dd hh:mm:ss')" -Verbose + + # By switching to 'SilentlyContinue' should theoretically increase the download speed. + $previousProgressPreference = $ProgressPreference + $ProgressPreference = 'SilentlyContinue' + + Write-Verbose -Message "Start downloading the SQL Server media at $(Get-Date -Format 'yyyy-MM-dd hh:mm:ss')" -Verbose Invoke-WebRequest -Uri $mockSourceMediaUrl -OutFile $mockIsoMediaFilePath + Write-Verbose -Message ('SQL Server media file has SHA1 hash ''{0}''' -f (Get-FileHash -Path $mockIsoMediaFilePath -Algorithm 'SHA1').Hash) -Verbose + + $ProgressPreference = $previousProgressPreference + # Double check that the SQL media was downloaded. if (-not (Test-Path -Path $mockIsoMediaFilePath)) { @@ -186,15 +199,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -317,7 +322,7 @@ try } } - $configurationName = "$($script:DSCResourceName)_StopMultiAnalysisServicesInstance_Config" + $configurationName = "$($script:DSCResourceName)_StopServicesInstance_Config" Context ('When using configuration {0}' -f $configurationName) { It 'Should compile and apply the MOF without throwing' { @@ -339,15 +344,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -378,15 +375,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } -ErrorVariable itBlockError @@ -494,15 +483,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } @@ -532,15 +513,7 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters - } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ - } + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } -ErrorVariable itBlockError @@ -642,15 +615,34 @@ try ErrorAction = 'Stop' } - try - { - Start-DscConfiguration @startDscConfigurationParameters + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + } + + $configurationName = "$($script:DSCResourceName)_StartServicesInstance_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + # The variable $ConfigurationData was dot-sourced above. + ConfigurationData = $ConfigurationData } - catch - { - Write-Verbose -Message ('{0} {1}' -f $integrationErrorMessagePrefix, $_) -Verbose - throw $_ + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' } + + Start-DscConfiguration @startDscConfigurationParameters } | Should -Not -Throw } } diff --git a/Tests/Integration/MSFT_SqlSetup.config.ps1 b/Tests/Integration/MSFT_SqlSetup.config.ps1 index aef829505..9556c2d51 100644 --- a/Tests/Integration/MSFT_SqlSetup.config.ps1 +++ b/Tests/Integration/MSFT_SqlSetup.config.ps1 @@ -231,17 +231,31 @@ Configuration MSFT_SqlSetup_InstallDatabaseEngineNamedInstanceAsSystem_Config } } -Configuration MSFT_SqlSetup_StopMultiAnalysisServicesInstance_Config +Configuration MSFT_SqlSetup_StopServicesInstance_Config { Import-DscResource -ModuleName 'PSDscResources' node localhost { - # Service ('StopSqlServerInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) - # { - # Name = ('MSSQL${0}' -f $Node.DatabaseEngineNamedInstanceName) - # State = 'Stopped' - # } + <# + Stopping the SQL Server Agent service for the named instance. + It will be restarted at the end of the tests. + #> + Service ('StopSqlServerAgentForInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) + { + Name = ('SQLAGENT${0}' -f $Node.DatabaseEngineNamedInstanceName) + State = 'Stopped' + } + + <# + Stopping the Database Engine named instance. It will be restarted + at the end of the tests. + #> + Service ('StopSqlServerInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) + { + Name = ('MSSQL${0}' -f $Node.DatabaseEngineNamedInstanceName) + State = 'Stopped' + } Service ('StopMultiAnalysisServicesInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) { @@ -383,3 +397,25 @@ Configuration MSFT_SqlSetup_StopTabularAnalysisServices_Config } } } + +Configuration MSFT_SqlSetup_StartServicesInstance_Config +{ + Import-DscResource -ModuleName 'PSDscResources' + + node localhost + { + # Start the Database Engine named instance. + Service ('StartSqlServerInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) + { + Name = ('MSSQL${0}' -f $Node.DatabaseEngineNamedInstanceName) + State = 'Running' + } + + # Starting the SQL Server Agent service for the named instance. + Service ('StartSqlServerAgentForInstance{0}' -f $Node.DatabaseEngineNamedInstanceName) + { + Name = ('SQLAGENT${0}' -f $Node.DatabaseEngineNamedInstanceName) + State = 'Running' + } + } +} diff --git a/Tests/Unit/CommonResourceHelper.Tests.ps1 b/Tests/Unit/CommonResourceHelper.Tests.ps1 index 6a69d0ffe..85250a0e1 100644 --- a/Tests/Unit/CommonResourceHelper.Tests.ps1 +++ b/Tests/Unit/CommonResourceHelper.Tests.ps1 @@ -8,21 +8,26 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() -Describe 'CommonResourceHelper Unit Tests' { +$script:helperModuleName = 'CommonResourceHelper' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:helperModuleName) -Verbose + return +} + +Describe "$script:helperModuleName Unit Tests" { BeforeAll { # Import the CommonResourceHelper module to test $dscResourcesFolderFilePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) ` -ChildPath 'DscResources' Import-Module -Name (Join-Path -Path $dscResourcesFolderFilePath ` - -ChildPath 'CommonResourceHelper.psm1') -Force + -ChildPath "$script:helperModuleName.psm1") -Force } - InModuleScope 'CommonResourceHelper' { + InModuleScope $script:helperModuleName { Describe 'Get-LocalizedData' { $mockTestPath = { return $mockTestPathReturnValue diff --git a/Tests/Unit/MSFT_SqlAG.Tests.ps1 b/Tests/Unit/MSFT_SqlAG.Tests.ps1 index 26f787015..80dc09634 100644 --- a/Tests/Unit/MSFT_SqlAG.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAG.Tests.ps1 @@ -8,9 +8,14 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlAG' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} #region HEADER @@ -26,13 +31,14 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -P Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Tests' -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'CommonTestHelper.psm1'))) -Force -Global $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlAG' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER -function Invoke-TestSetup { +function Invoke-TestSetup +{ # Load the SMO stubs Add-Type -Path ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SMO.cs ) @@ -40,7 +46,8 @@ function Invoke-TestSetup { Import-SQLModuleStub } -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment } @@ -49,7 +56,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlAG' { + InModuleScope $script:DSCResourceName { #region parameter mocks diff --git a/Tests/Unit/MSFT_SqlAGDatabase.Tests.ps1 b/Tests/Unit/MSFT_SqlAGDatabase.Tests.ps1 index b6ccbb3a9..cf06947f9 100644 --- a/Tests/Unit/MSFT_SqlAGDatabase.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAGDatabase.Tests.ps1 @@ -8,9 +8,14 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlAGDatabase' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} #region HEADER @@ -30,17 +35,18 @@ Import-Module -Name ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Add-Type -Path ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SMO.cs ) $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlAGDatabase' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER -function Invoke-TestSetup { - +function Invoke-TestSetup +{ } -function Invoke-TestCleanup { +function Invoke-TestCleanup +{ Restore-TestEnvironment -TestEnvironment $TestEnvironment # TODO: Other Optional Cleanup Code Goes Here... @@ -51,320 +57,320 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlAGDatabase' { + InModuleScope $script:DSCResourceName { #region Parameter Mocks - # The databases defined in the resource - $mockDatabaseNameParameter = @( - 'DB*' - 'AnotherDB' - '3rd*OfDatabase' - '4th*OfDatabase' - ) + # The databases defined in the resource + $mockDatabaseNameParameter = @( + 'DB*' + 'AnotherDB' + '3rd*OfDatabase' + '4th*OfDatabase' + ) - $mockDatabaseNameParameterWithNonExistingDatabases = @( - 'NotFound*' - 'Unknown1' - ) + $mockDatabaseNameParameterWithNonExistingDatabases = @( + 'NotFound*' + 'Unknown1' + ) - $mockBackupPath = 'X:\Backup' + $mockBackupPath = 'X:\Backup' - $mockProcessOnlyOnActiveNode = $false + $mockProcessOnlyOnActiveNode = $false #endregion Parameter Mocks #region mock names - $mockServerObjectDomainInstanceName = 'Server1' - $mockPrimaryServerObjectDomainInstanceName = 'Server2' - $mockAvailabilityGroupObjectName = 'AvailabilityGroup1' - $mockAvailabilityGroupWithoutDatabasesObjectName = 'AvailabilityGroupWithoutDatabases' - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServerName = 'AvailabilityGroup2' + $mockServerObjectDomainInstanceName = 'Server1' + $mockPrimaryServerObjectDomainInstanceName = 'Server2' + $mockAvailabilityGroupObjectName = 'AvailabilityGroup1' + $mockAvailabilityGroupWithoutDatabasesObjectName = 'AvailabilityGroupWithoutDatabases' + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServerName = 'AvailabilityGroup2' #endregion mock names #region Availability Replica Mocks - $mockAvailabilityReplicaObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplicaCollection - foreach ( $mockAvailabilityReplicaName in @('Server1','Server2') ) - { - $newAvailabilityReplicaObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica - $newAvailabilityReplicaObject.Name = $mockAvailabilityReplicaName - - if ( $mockServerObjectDomainInstanceName -eq $mockAvailabilityReplicaName ) - { - $newAvailabilityReplicaObject.Role = 'Primary' - } + $mockAvailabilityReplicaObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplicaCollection + foreach ( $mockAvailabilityReplicaName in @('Server1','Server2') ) + { + $newAvailabilityReplicaObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityReplica + $newAvailabilityReplicaObject.Name = $mockAvailabilityReplicaName - $mockAvailabilityReplicaObjects.Add($newAvailabilityReplicaObject) + if ( $mockServerObjectDomainInstanceName -eq $mockAvailabilityReplicaName ) + { + $newAvailabilityReplicaObject.Role = 'Primary' } + $mockAvailabilityReplicaObjects.Add($newAvailabilityReplicaObject) + } + #endregion Availability Replica Mocks #region Availability Group Mocks - $mockAvailabilityDatabaseNames = @( - 'DB2' - '3rdTypeOfDatabase' - 'UndefinedDatabase' - ) - - $mockAvailabilityDatabaseAbsentResults = @( - 'DB2' - '3rdTypeOfDatabase' - ) - - $mockAvailabilityDatabaseExactlyAddResults = @( - 'DB1' - ) - - $mockAvailabilityDatabaseExactlyRemoveResults = @( - 'UndefinedDatabase' - ) - - $mockAvailabilityDatabasePresentResults = @( - 'DB1' - ) - - $mockAvailabilityDatabaseObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityDatabaseCollection - foreach ( $mockAvailabilityDatabaseName in $mockAvailabilityDatabaseNames ) - { - $newAvailabilityDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityDatabase - $newAvailabilityDatabaseObject.Name = $mockAvailabilityDatabaseName - $mockAvailabilityDatabaseObjects.Add($newAvailabilityDatabaseObject) - } + $mockAvailabilityDatabaseNames = @( + 'DB2' + '3rdTypeOfDatabase' + 'UndefinedDatabase' + ) + + $mockAvailabilityDatabaseAbsentResults = @( + 'DB2' + '3rdTypeOfDatabase' + ) + + $mockAvailabilityDatabaseExactlyAddResults = @( + 'DB1' + ) + + $mockAvailabilityDatabaseExactlyRemoveResults = @( + 'UndefinedDatabase' + ) + + $mockAvailabilityDatabasePresentResults = @( + 'DB1' + ) + + $mockAvailabilityDatabaseObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityDatabaseCollection + foreach ( $mockAvailabilityDatabaseName in $mockAvailabilityDatabaseNames ) + { + $newAvailabilityDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityDatabase + $newAvailabilityDatabaseObject.Name = $mockAvailabilityDatabaseName + $mockAvailabilityDatabaseObjects.Add($newAvailabilityDatabaseObject) + } - $mockBadAvailabilityGroupObject = New-Object -TypeName Object + $mockBadAvailabilityGroupObject = New-Object -TypeName Object - $mockAvailabilityGroupObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup - $mockAvailabilityGroupObject.AvailabilityDatabases = $mockAvailabilityDatabaseObjects - $mockAvailabilityGroupObject.Name = $mockAvailabilityGroupObjectName - $mockAvailabilityGroupObject.PrimaryReplicaServerName = $mockServerObjectDomainInstanceName - $mockAvailabilityGroupObject.AvailabilityReplicas = $mockAvailabilityReplicaObjects + $mockAvailabilityGroupObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup + $mockAvailabilityGroupObject.AvailabilityDatabases = $mockAvailabilityDatabaseObjects + $mockAvailabilityGroupObject.Name = $mockAvailabilityGroupObjectName + $mockAvailabilityGroupObject.PrimaryReplicaServerName = $mockServerObjectDomainInstanceName + $mockAvailabilityGroupObject.AvailabilityReplicas = $mockAvailabilityReplicaObjects - $mockAvailabilityGroupWithoutDatabasesObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup - $mockAvailabilityGroupWithoutDatabasesObject.Name = $mockAvailabilityGroupWithoutDatabasesObjectName - $mockAvailabilityGroupWithoutDatabasesObject.PrimaryReplicaServerName = $mockServerObjectDomainInstanceName - $mockAvailabilityGroupWithoutDatabasesObject.AvailabilityReplicas = $mockAvailabilityReplicaObjects + $mockAvailabilityGroupWithoutDatabasesObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup + $mockAvailabilityGroupWithoutDatabasesObject.Name = $mockAvailabilityGroupWithoutDatabasesObjectName + $mockAvailabilityGroupWithoutDatabasesObject.PrimaryReplicaServerName = $mockServerObjectDomainInstanceName + $mockAvailabilityGroupWithoutDatabasesObject.AvailabilityReplicas = $mockAvailabilityReplicaObjects - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.AvailabilityDatabases = $mockAvailabilityDatabaseObjects - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name = $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServerName - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.PrimaryReplicaServerName = $mockPrimaryServerObjectDomainInstanceName - $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.AvailabilityReplicas = $mockAvailabilityReplicaObjects + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroup + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.AvailabilityDatabases = $mockAvailabilityDatabaseObjects + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name = $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServerName + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.PrimaryReplicaServerName = $mockPrimaryServerObjectDomainInstanceName + $mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.AvailabilityReplicas = $mockAvailabilityReplicaObjects #endregion Availability Group Mocks #region Certificate Mocks - [byte[]]$mockThumbprint1 = @( - 83 - 121 - 115 - 116 - 101 - 109 - 46 - 84 - 101 - 120 - 116 - 46 - 85 - 84 - 70 - 56 - 69 - 110 - 99 - 111 - 100 - 105 - 110 - 103 - ) - - [byte[]]$mockThumbprint2 = @( - 83 - 121 - 115 - 23 - 101 - 109 - 46 - 84 - 101 - 120 - 116 - 85 - 85 - 84 - 70 - 56 - 69 - 23 - 99 - 111 - 100 - 105 - 110 - 103 - ) - - $mockCertificateObject1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Certificate - $mockCertificateObject1.Thumbprint = $mockThumbprint1 - - $mockCertificateObject2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Certificate - $mockCertificateObject2.Thumbprint = $mockThumbprint2 - - $mockDatabaseEncryptionKeyObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseEncryptionKey - $mockDatabaseEncryptionKeyObject.EncryptorName = 'TDE Cert' - $mockDatabaseEncryptionKeyObject.Thumbprint = $mockThumbprint1 + [byte[]]$mockThumbprint1 = @( + 83 + 121 + 115 + 116 + 101 + 109 + 46 + 84 + 101 + 120 + 116 + 46 + 85 + 84 + 70 + 56 + 69 + 110 + 99 + 111 + 100 + 105 + 110 + 103 + ) + + [byte[]]$mockThumbprint2 = @( + 83 + 121 + 115 + 23 + 101 + 109 + 46 + 84 + 101 + 120 + 116 + 85 + 85 + 84 + 70 + 56 + 69 + 23 + 99 + 111 + 100 + 105 + 110 + 103 + ) + + $mockCertificateObject1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Certificate + $mockCertificateObject1.Thumbprint = $mockThumbprint1 + + $mockCertificateObject2 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Certificate + $mockCertificateObject2.Thumbprint = $mockThumbprint2 + + $mockDatabaseEncryptionKeyObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseEncryptionKey + $mockDatabaseEncryptionKeyObject.EncryptorName = 'TDE Cert' + $mockDatabaseEncryptionKeyObject.Thumbprint = $mockThumbprint1 #endregion Certificate Mocks #region Database File Mocks - $mockDataFilePath = 'E:\SqlData' - $mockLogFilePath = 'F:\SqlLog' - $mockDataFilePathIncorrect = 'G:\SqlData' - $mockLogFilePathIncorrect = 'H:\SqlData' + $mockDataFilePath = 'E:\SqlData' + $mockLogFilePath = 'F:\SqlLog' + $mockDataFilePathIncorrect = 'G:\SqlData' + $mockLogFilePathIncorrect = 'H:\SqlData' #endregion Database File Mocks #region Database Mocks - # The databases found on the instance - $mockPresentDatabaseNames = @( - 'DB1' - 'DB2' - '3rdTypeOfDatabase' - 'UndefinedDatabase' - ) - - $mockMasterDatabaseName = 'master' - $mockMasterDatabaseObject1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database - $mockMasterDatabaseObject1.Name = $mockMasterDatabaseName - $mockMasterDatabaseObject1.ID = 1 - $mockMasterDatabaseObject1.Certificates = @($mockCertificateObject1) - $mockMasterDatabaseObject1.FileGroups = @{ + # The databases found on the instance + $mockPresentDatabaseNames = @( + 'DB1' + 'DB2' + '3rdTypeOfDatabase' + 'UndefinedDatabase' + ) + + $mockMasterDatabaseName = 'master' + $mockMasterDatabaseObject1 = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database + $mockMasterDatabaseObject1.Name = $mockMasterDatabaseName + $mockMasterDatabaseObject1.ID = 1 + $mockMasterDatabaseObject1.Certificates = @($mockCertificateObject1) + $mockMasterDatabaseObject1.FileGroups = @{ + Name = 'PRIMARY' + Files = @{ + FileName = ( [IO.Path]::Combine( $mockDataFilePath, "$($mockMasterDatabaseName).mdf" ) ) + } + } + $mockMasterDatabaseObject1.LogFiles = @{ + FileName = ( [IO.Path]::Combine( $mockLogFilePath, "$($mockMasterDatabaseName).ldf" ) ) + } + + $mockDatabaseObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseCollection + foreach ( $mockPresentDatabaseName in $mockPresentDatabaseNames ) + { + $newDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database + $newDatabaseObject.Name = $mockPresentDatabaseName + $newDatabaseObject.FileGroups = @{ Name = 'PRIMARY' Files = @{ - FileName = ( [IO.Path]::Combine( $mockDataFilePath, "$($mockMasterDatabaseName).mdf" ) ) + FileName = ( [IO.Path]::Combine( $mockDataFilePath, "$($mockPresentDatabaseName).mdf" ) ) } } - $mockMasterDatabaseObject1.LogFiles = @{ - FileName = ( [IO.Path]::Combine( $mockLogFilePath, "$($mockMasterDatabaseName).ldf" ) ) + $newDatabaseObject.LogFiles = @{ + FileName = ( [IO.Path]::Combine( $mockLogFilePath, "$($mockPresentDatabaseName).ldf" ) ) } - $mockDatabaseObjects = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseCollection - foreach ( $mockPresentDatabaseName in $mockPresentDatabaseNames ) - { - $newDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database - $newDatabaseObject.Name = $mockPresentDatabaseName - $newDatabaseObject.FileGroups = @{ - Name = 'PRIMARY' - Files = @{ - FileName = ( [IO.Path]::Combine( $mockDataFilePath, "$($mockPresentDatabaseName).mdf" ) ) - } - } - $newDatabaseObject.LogFiles = @{ - FileName = ( [IO.Path]::Combine( $mockLogFilePath, "$($mockPresentDatabaseName).ldf" ) ) + # Add the database object to the database collection + $mockDatabaseObjects.Add($newDatabaseObject) + } + $mockDatabaseObjects.Add($mockMasterDatabaseObject1) + + $mockDatabaseObjectsWithIncorrectFileNames = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseCollection + foreach ( $mockPresentDatabaseName in $mockPresentDatabaseNames ) + { + $newDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database + $newDatabaseObject.Name = $mockPresentDatabaseName + $newDatabaseObject.FileGroups = @{ + Name = 'PRIMARY' + Files = @{ + FileName = ( [IO.Path]::Combine( $mockDataFilePathIncorrect, "$($mockPresentDatabaseName).mdf" ) ) } - - # Add the database object to the database collection - $mockDatabaseObjects.Add($newDatabaseObject) } - $mockDatabaseObjects.Add($mockMasterDatabaseObject1) - - $mockDatabaseObjectsWithIncorrectFileNames = New-Object -TypeName Microsoft.SqlServer.Management.Smo.DatabaseCollection - foreach ( $mockPresentDatabaseName in $mockPresentDatabaseNames ) - { - $newDatabaseObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database - $newDatabaseObject.Name = $mockPresentDatabaseName - $newDatabaseObject.FileGroups = @{ - Name = 'PRIMARY' - Files = @{ - FileName = ( [IO.Path]::Combine( $mockDataFilePathIncorrect, "$($mockPresentDatabaseName).mdf" ) ) - } - } - $newDatabaseObject.LogFiles = @{ - FileName = ( [IO.Path]::Combine( $mockLogFilePathIncorrect, "$($mockPresentDatabaseName).ldf" ) ) - } - - # Add the database object to the database collection - $mockDatabaseObjectsWithIncorrectFileNames.Add($newDatabaseObject) + $newDatabaseObject.LogFiles = @{ + FileName = ( [IO.Path]::Combine( $mockLogFilePathIncorrect, "$($mockPresentDatabaseName).ldf" ) ) } + # Add the database object to the database collection + $mockDatabaseObjectsWithIncorrectFileNames.Add($newDatabaseObject) + } + #endregion Database Mocks #region Server mocks - $mockBadServerObject = New-Object -TypeName Object - - $mockServerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server - $mockServerObject.AvailabilityGroups = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroupCollection - $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupObject.Clone()) - $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupWithoutDatabasesObject.Clone()) - $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Clone()) - $mockServerObject.Databases = $mockDatabaseObjects - $mockServerObject.DomainInstanceName = $mockServerObjectDomainInstanceName - $mockServerObject.NetName = $mockServerObjectDomainInstanceName - $mockServerObject.ServiceName = 'MSSQLSERVER' - $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupObject.Name].LocalReplicaRole = 'Primary' - $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupWithoutDatabasesObject.Name].LocalReplicaRole = 'Primary' - $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name].LocalReplicaRole = 'Secondary' - - $mockServer2Object = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server - $mockServer2Object.AvailabilityGroups = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroupCollection - $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupObject.Clone()) - $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupWithoutDatabasesObject.Clone()) - $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Clone()) - $mockServer2Object.Databases = $mockDatabaseObjects - $mockServer2Object.DomainInstanceName = $mockPrimaryServerObjectDomainInstanceName - $mockServer2Object.NetName = $mockPrimaryServerObjectDomainInstanceName - $mockServer2Object.ServiceName = 'MSSQLSERVER' - $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupObject.Name].LocalReplicaRole = 'Secondary' - $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupWithoutDatabasesObject.Name].LocalReplicaRole = 'Secondary' - $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name].LocalReplicaRole = 'Primary' + $mockBadServerObject = New-Object -TypeName Object + + $mockServerObject = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server + $mockServerObject.AvailabilityGroups = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroupCollection + $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupObject.Clone()) + $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupWithoutDatabasesObject.Clone()) + $mockServerObject.AvailabilityGroups.Add($mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Clone()) + $mockServerObject.Databases = $mockDatabaseObjects + $mockServerObject.DomainInstanceName = $mockServerObjectDomainInstanceName + $mockServerObject.NetName = $mockServerObjectDomainInstanceName + $mockServerObject.ServiceName = 'MSSQLSERVER' + $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupObject.Name].LocalReplicaRole = 'Primary' + $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupWithoutDatabasesObject.Name].LocalReplicaRole = 'Primary' + $mockServerObject.AvailabilityGroups[$mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name].LocalReplicaRole = 'Secondary' + + $mockServer2Object = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server + $mockServer2Object.AvailabilityGroups = New-Object -TypeName Microsoft.SqlServer.Management.Smo.AvailabilityGroupCollection + $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupObject.Clone()) + $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupWithoutDatabasesObject.Clone()) + $mockServer2Object.AvailabilityGroups.Add($mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Clone()) + $mockServer2Object.Databases = $mockDatabaseObjects + $mockServer2Object.DomainInstanceName = $mockPrimaryServerObjectDomainInstanceName + $mockServer2Object.NetName = $mockPrimaryServerObjectDomainInstanceName + $mockServer2Object.ServiceName = 'MSSQLSERVER' + $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupObject.Name].LocalReplicaRole = 'Secondary' + $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupWithoutDatabasesObject.Name].LocalReplicaRole = 'Secondary' + $mockServer2Object.AvailabilityGroups[$mockAvailabilityGroupObjectWithPrimaryReplicaOnAnotherServer.Name].LocalReplicaRole = 'Primary' #endregion Server mocks #region Invoke Query Mock - $mockResultInvokeQueryFileExist = { - return @{ - Tables = @{ - Rows = @{ - 'File is a Directory' = 1 - } + $mockResultInvokeQueryFileExist = { + return @{ + Tables = @{ + Rows = @{ + 'File is a Directory' = 1 } } } + } - $mockResultInvokeQueryFileNotExist = { - return @{ - Tables = @{ - Rows = @{ - 'File is a Directory' = 0 - } + $mockResultInvokeQueryFileNotExist = { + return @{ + Tables = @{ + Rows = @{ + 'File is a Directory' = 0 } } } + } - $mockInvokeQueryParameterRestoreDatabase = { - $Query -like 'RESTORE DATABASE * + $mockInvokeQueryParameterRestoreDatabase = { + $Query -like 'RESTORE DATABASE * FROM DISK = * WITH NORECOVERY' - } + } - $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs = { - $Query -like 'EXECUTE AS LOGIN = * + $mockInvokeQueryParameterRestoreDatabaseWithExecuteAs = { + $Query -like 'EXECUTE AS LOGIN = * RESTORE DATABASE * FROM DISK = * WITH NORECOVERY' - } + } #endregion Invoke Query Mock diff --git a/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 b/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 index 5150c71c0..2b798b436 100644 --- a/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAGListener.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlAGListener' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlAGReplica.Tests.ps1 b/Tests/Unit/MSFT_SqlAGReplica.Tests.ps1 index ba16315cd..1c50b53ed 100644 --- a/Tests/Unit/MSFT_SqlAGReplica.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAGReplica.Tests.ps1 @@ -8,9 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlAGReplica' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER @@ -27,8 +33,8 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -P Add-Type -Path ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SMO.cs ) $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlAGReplica' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -47,7 +53,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlAGReplica' { + InModuleScope $script:DSCResourceName { #region parameter mocks diff --git a/Tests/Unit/MSFT_SqlAlias.Tests.ps1 b/Tests/Unit/MSFT_SqlAlias.Tests.ps1 index a02551ec0..91631939a 100644 --- a/Tests/Unit/MSFT_SqlAlias.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAlias.Tests.ps1 @@ -8,9 +8,14 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlAlias' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} #region HEADER @@ -25,8 +30,8 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlAlias' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -43,7 +48,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlAlias' { + InModuleScope $script:DSCResourceName { $registryPath = 'HKLM:\SOFTWARE\Microsoft\MSSQLServer\Client\ConnectTo' $registryPathWow6432Node = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\MSSQLServer\Client\ConnectTo' diff --git a/Tests/Unit/MSFT_SqlAlwaysOnService.Tests.ps1 b/Tests/Unit/MSFT_SqlAlwaysOnService.Tests.ps1 index 29dd82fee..d7319bbe9 100644 --- a/Tests/Unit/MSFT_SqlAlwaysOnService.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlAlwaysOnService.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlAlwaysOnService' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + # Unit Test Template Version: 1.1.0 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` diff --git a/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 index b14acc183..fe7d16e63 100644 --- a/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabase.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlDatabase' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlDatabaseDefaultLocation.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseDefaultLocation.Tests.ps1 index 0dc41e661..7c77a0543 100644 --- a/Tests/Unit/MSFT_SqlDatabaseDefaultLocation.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseDefaultLocation.Tests.ps1 @@ -8,9 +8,14 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlDatabaseDefaultLocation' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} #region Header @@ -27,8 +32,8 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -P Add-Type -Path ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SMO.cs ) $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlDatabaseDefaultLocation' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -48,7 +53,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlDatabaseDefaultLocation' { + InModuleScope $script:DSCResourceName { $mockServerName = 'localhost' $mockInstanceName = 'MSSQLSERVER' $mockSQLDataPath = 'C:\Program Files\Data\' diff --git a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 index 961c259ef..894a1886a 100644 --- a/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseOwner.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlDatabaseOwner' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 index 01dd6672d..90e49b118 100644 --- a/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabasePermission.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlDatabasePermission' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlDatabaseRecoveryModel.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseRecoveryModel.Tests.ps1 index d1a8ee4fc..b69e33734 100644 --- a/Tests/Unit/MSFT_SqlDatabaseRecoveryModel.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseRecoveryModel.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlDatabaseRecoveryModel' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 @@ -47,6 +49,7 @@ function Invoke-TestCleanup try { Invoke-TestSetup + InModuleScope $script:DSCResourceName { $mockServerName = 'localhost' $mockInstanceName = 'MSSQLSERVER' diff --git a/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 b/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 index 4eb18da38..636fa97f9 100644 --- a/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlDatabaseRole.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlDatabaseRole' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlRS.Tests.ps1 b/Tests/Unit/MSFT_SqlRS.Tests.ps1 index abc35f536..25c7b8627 100644 --- a/Tests/Unit/MSFT_SqlRS.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlRS.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlRS' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlScript.Tests.ps1 b/Tests/Unit/MSFT_SqlScript.Tests.ps1 index 9e746e75f..f6bb2193f 100644 --- a/Tests/Unit/MSFT_SqlScript.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlScript.Tests.ps1 @@ -8,12 +8,19 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] # Suppression of this PSSA rule allowed in tests. [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlScript' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 @@ -29,8 +36,8 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'SqlServerDsc $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlScript' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -50,7 +57,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlScript' { + InModuleScope $script:DSCResourceName { InModuleScope 'SqlServerDscHelper' { $script:DSCModuleName = 'SqlServerDsc' $resourceName = 'MSFT_SqlScript' diff --git a/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 b/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 index 75ec443c0..fdc3e21aa 100644 --- a/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlScriptQuery.Tests.ps1 @@ -8,12 +8,19 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] # Suppression of this PSSA rule allowed in tests. [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] Param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlScriptQuery' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 @@ -28,8 +35,8 @@ Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource. Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath 'SqlServerDscHelper.psm1') $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlScriptQuery' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -49,7 +56,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlScriptQuery' { + InModuleScope $script:DSCResourceName { InModuleScope 'SqlServerDscHelper' { $script:DSCModuleName = 'SqlServerDsc' $resourceName = 'MSFT_SqlScriptQuery' diff --git a/Tests/Unit/MSFT_SqlServerConfiguration.Tests.ps1 b/Tests/Unit/MSFT_SqlServerConfiguration.Tests.ps1 index 67fcb2b5d..36d163246 100644 --- a/Tests/Unit/MSFT_SqlServerConfiguration.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerConfiguration.Tests.ps1 @@ -8,13 +8,16 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerConfiguration' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + # Unit Test Template Version: 1.1.0 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` diff --git a/Tests/Unit/MSFT_SqlServerDatabaseMail.Tests.ps1 b/Tests/Unit/MSFT_SqlServerDatabaseMail.Tests.ps1 index 03adf6c3b..c024b71b1 100644 --- a/Tests/Unit/MSFT_SqlServerDatabaseMail.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerDatabaseMail.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerDatabaseMail' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1 b/Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1 index 7c1350292..1800e7be8 100644 --- a/Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerEndpoint.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerEndpoint' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerEndpointPermission.Tests.ps1 b/Tests/Unit/MSFT_SqlServerEndpointPermission.Tests.ps1 index 9c866b203..c22755497 100644 --- a/Tests/Unit/MSFT_SqlServerEndpointPermission.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerEndpointPermission.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerEndpointPermission' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 b/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 index b5bba8c29..3acca8f75 100644 --- a/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerEndpointState.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerEndpointState' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerLogin.Tests.ps1 b/Tests/Unit/MSFT_SqlServerLogin.Tests.ps1 index 99c711671..ba18141b5 100644 --- a/Tests/Unit/MSFT_SqlServerLogin.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerLogin.Tests.ps1 @@ -8,8 +8,6 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] # Suppressing this rule because PlainText is required for one of the functions used in this test [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param() @@ -17,6 +15,12 @@ param() $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerLogin' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 @@ -386,9 +390,9 @@ try $mockTestTargetResourceParameters.Add('Disabled', $true) $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) - + # Override mock declaration - Mock -CommandName Connect-SQL -MockWith {throw $mockAccountDisabledException} + Mock -CommandName Connect-SQL -MockWith {throw $mockAccountDisabledException} # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ @@ -403,7 +407,7 @@ try LoginPasswordExpirationEnabled = $true } } - + # Call the test target $result = Test-TargetResource @mockTestTargetResourceParameters @@ -420,9 +424,9 @@ try $mockTestTargetResourceParameters.Add('Disabled', $true) $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) - + # Override mock declaration - Mock -CommandName Connect-SQL -MockWith {throw $mockLoginFailedException} + Mock -CommandName Connect-SQL -MockWith {throw $mockLoginFailedException} # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ @@ -437,7 +441,7 @@ try LoginPasswordExpirationEnabled = $true } } - + # Call the test target $result = Test-TargetResource @mockTestTargetResourceParameters @@ -447,16 +451,16 @@ try # Should be true $result | Should -Be $false } - + It 'Should throw exception when unkown error occurred and account is disabled' { $mockTestTargetResourceParameters = $getTargetResource_KnownSqlLogin.Clone() $mockTestTargetResourceParameters.Add('Ensure', 'Present') $mockTestTargetResourceParameters.Add('Disabled', $true) $mockTestTargetResourceParameters.Add('LoginType', 'SqlLogin') $mockTestTargetResourceParameters.Add('LoginCredential', (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @($mockTestTargetResourceParameters.Name, $mockSqlLoginPassword))) - + # Override mock declaration - Mock -CommandName Connect-SQL -MockWith {throw $mockException} + Mock -CommandName Connect-SQL -MockWith {throw $mockException} # Override Get-TargetResource Mock -CommandName Get-TargetResource {return New-Object PSObject -Property @{ @@ -471,13 +475,13 @@ try LoginPasswordExpirationEnabled = $true } } - + # Call the test target { Test-TargetResource @mockTestTargetResourceParameters } | Should -Throw Assert-MockCalled -CommandName Get-TargetResource -Scope It -Times 1 -Exactly Assert-MockCAlled -CommandName Connect-SQL -Scope It -Times 1 -Exactly - } + } } Context 'When the desired state is Present' { diff --git a/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 b/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 index eeaac7bda..7792db505 100644 --- a/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerMaxDop.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerMaxDop' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerMemory.Tests.ps1 b/Tests/Unit/MSFT_SqlServerMemory.Tests.ps1 index cebaee00b..9d0df2655 100644 --- a/Tests/Unit/MSFT_SqlServerMemory.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerMemory.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerMemory' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerNetwork.Tests.ps1 b/Tests/Unit/MSFT_SqlServerNetwork.Tests.ps1 index 1f39d5bf2..673477f6b 100644 --- a/Tests/Unit/MSFT_SqlServerNetwork.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerNetwork.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerNetwork' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerPermission.Tests.ps1 b/Tests/Unit/MSFT_SqlServerPermission.Tests.ps1 index e17ef628d..48ffbabe0 100644 --- a/Tests/Unit/MSFT_SqlServerPermission.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerPermission.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerPermission' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 b/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 index 742259445..dc092994b 100644 --- a/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerReplication.Tests.ps1 @@ -9,14 +9,18 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] param() $script:DSCModuleName = 'SqlServerDSC' $script:DSCResourceName = 'MSFT_SqlServerReplication' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.1.0 [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) diff --git a/Tests/Unit/MSFT_SqlServerRole.Tests.ps1 b/Tests/Unit/MSFT_SqlServerRole.Tests.ps1 index 7fece5089..16d3bbd2a 100644 --- a/Tests/Unit/MSFT_SqlServerRole.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerRole.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerRole' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServerSecureConnection.Tests.ps1 b/Tests/Unit/MSFT_SqlServerSecureConnection.Tests.ps1 index 6e190a498..89ec8b503 100644 --- a/Tests/Unit/MSFT_SqlServerSecureConnection.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServerSecureConnection.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServerSecureConnection' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 b/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 index 5fc8b2dd0..13adb1da7 100644 --- a/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlServiceAccount.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlServiceAccount' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.1 @@ -177,7 +179,7 @@ try $managedComputerObject = New-Object -TypeName PSObject -Property @{ Name = $mockDefaultInstanceName ServiceAccount = $mockManagedServiceAccountName - Type = 'SqlServer' + Type = 'SqlServer' } $managedComputerObject | Add-Member @mockAddMemberParameters_SetServiceAccount @@ -498,7 +500,7 @@ try ) # Get the service name - Get-SqlServiceName -InstanceName $mockDefaultInstanceName -ServiceType $ServiceType | Should -Be $ExpectedServiceName + Get-SqlServiceName -InstanceName $mockDefaultInstanceName -ServiceType $ServiceType | Should -Be $ExpectedServiceName # Ensure the mock is utilized Assert-MockCalled -CommandName Get-ChildItem -ParameterFilter $mockGetChildItem_ParameterFilter -Scope It -Exactly -Times 1 @@ -806,7 +808,7 @@ try ServerName = $mockSqlServer InstanceName = $mockDefaultInstanceName ServiceType = $mockServiceType - ServiceAccount = $mockManagedServiceAccountCredential + ServiceAccount = $mockManagedServiceAccountCredential } It 'Should have the Managed Service Account' { diff --git a/Tests/Unit/MSFT_SqlSetup.Tests.ps1 b/Tests/Unit/MSFT_SqlSetup.Tests.ps1 index 8383d2051..caeda7514 100644 --- a/Tests/Unit/MSFT_SqlSetup.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlSetup.Tests.ps1 @@ -8,8 +8,6 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] # Suppressing this rule because PlainText is required for one of the functions used in this test [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param() @@ -17,6 +15,12 @@ param() $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlSetup' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.0 diff --git a/Tests/Unit/MSFT_SqlWaitForAG.Tests.ps1 b/Tests/Unit/MSFT_SqlWaitForAG.Tests.ps1 index 46f2f0e6a..8de046755 100644 --- a/Tests/Unit/MSFT_SqlWaitForAG.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlWaitForAG.Tests.ps1 @@ -8,13 +8,15 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() - $script:DSCModuleName = 'SqlServerDsc' $script:DSCResourceName = 'MSFT_SqlWaitForAG' +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} + #region HEADER # Unit Test Template Version: 1.2.1 @@ -28,8 +30,8 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlWaitForAG' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -46,7 +48,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlWaitForAG' { + InModuleScope $script:DSCResourceName { $mockClusterGroupName = 'AGTest' $mockRetryInterval = 1 $mockRetryCount = 2 diff --git a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 index dcf7c023d..3aa7c3505 100644 --- a/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 +++ b/Tests/Unit/MSFT_SqlWindowsFirewall.Tests.ps1 @@ -8,9 +8,14 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container2', ContainerImage = 'microsoft/windowsservercore')] -param() +$script:DSCModuleName = 'SqlServerDsc' +$script:DSCResourceName = 'MSFT_SqlWindowsFirewall' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:DSCResourceName) -Verbose + return +} #region HEADER @@ -25,8 +30,8 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force $TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName 'SqlServerDsc' ` - -DSCResourceName 'MSFT_SqlWindowsFirewall' ` + -DSCModuleName $script:DSCModuleName ` + -DSCResourceName $script:DSCResourceName ` -TestType Unit #endregion HEADER @@ -43,7 +48,7 @@ try { Invoke-TestSetup - InModuleScope 'MSFT_SqlWindowsFirewall' { + InModuleScope $script:DSCResourceName { <# Testing two major versions to verify Integration Services differences (i.e service name). No point in testing each supported SQL Server version, since there are no difference diff --git a/Tests/Unit/SqlServerDSCHelper.Tests.ps1 b/Tests/Unit/SqlServerDSCHelper.Tests.ps1 index 2ab8817f1..30decc1d3 100644 --- a/Tests/Unit/SqlServerDSCHelper.Tests.ps1 +++ b/Tests/Unit/SqlServerDSCHelper.Tests.ps1 @@ -8,15 +8,19 @@ https://github.com/PowerShell/SqlServerDsc/blob/dev/CONTRIBUTING.md#bootstrap-script-assert-testenvironment #> -# This is used to make sure the unit test run in a container. -[Microsoft.DscResourceKit.UnitTest(ContainerName = 'Container1', ContainerImage = 'microsoft/windowsservercore')] # To run these tests, we have to fake login credentials [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param () # Unit Test Template Version: 1.1.0 -$script:moduleName = 'SqlServerDscHelper' +$script:helperModuleName = 'SqlServerDscHelper' + +if ($env:APPVEYOR -eq $true -and $env:CONFIGURATION -ne 'Unit') +{ + Write-Verbose -Message ('Unit test for {0} will be skipped unless $env:CONFIGURATION is set to ''Unit''.' -f $script:helperModuleName) -Verbose + return +} [System.String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` @@ -26,7 +30,7 @@ if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCR } Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1') -Force -Import-Module (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent | Split-Path -Parent) -ChildPath 'SqlServerDscHelper.psm1') -Scope Global -Force +Import-Module (Join-Path -Path (Split-Path -Path $PSScriptRoot -Parent | Split-Path -Parent) -ChildPath "$script:helperModuleName.psm1") -Scope Global -Force # Loading mocked classes Add-Type -Path ( Join-Path -Path ( Join-Path -Path $PSScriptRoot -ChildPath Stubs ) -ChildPath SMO.cs ) @@ -35,7 +39,7 @@ Add-Type -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Pa Import-Module -Name (Join-Path -Path (Join-Path -Path (Join-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'Tests') -ChildPath 'Unit') -ChildPath 'Stubs') -ChildPath 'SQLPSStub.psm1') -Global -Force # Begin Testing -InModuleScope $script:moduleName { +InModuleScope $script:helperModuleName { $mockNewObject_MicrosoftAnalysisServicesServer = { return New-Object -TypeName Object | Add-Member -MemberType ScriptMethod -Name Connect -Value { @@ -154,7 +158,7 @@ InModuleScope $script:moduleName { $mockInnerException | Add-Member -Name 'Number' -Value 2 -MemberType NoteProperty $mockException = New-Object System.Exception "This is a mock exception object", $mockInnerException $mockException | Add-Member -Name 'Number' -Value 1 -MemberType NoteProperty - + Describe 'Testing Restart-SqlService' { Context 'Restart-SqlService standalone instance' { @@ -2047,7 +2051,7 @@ InModuleScope $script:moduleName { Context 'When getting service account' { It 'Should return NT AUTHORITY\SYSTEM' { $returnValue = Get-ServiceAccount -ServiceAccount $mockLocalSystemAccountCredential - + $returnValue.UserName | Should -Be $mockLocalSystemAccountUserName $returnValue.Password | Should -BeNullOrEmpty } @@ -2076,7 +2080,7 @@ InModuleScope $script:moduleName { Describe 'Testing Find-ExceptionByNumber'{ Context 'When searching Exception objects'{ - It 'Should return true for main exception' { + It 'Should return true for main exception' { Find-ExceptionByNumber -ExceptionToSearch $mockException -ErrorNumber 1 | Should -Be $true } diff --git a/appveyor.yml b/appveyor.yml index d6acc116a..52a777af9 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,11 +3,11 @@ #---------------------------------# version: 9.0.{build}.0 image: Visual Studio 2017 +configuration: + - Unit + - Integration install: - git clone https://github.com/PowerShell/DscResource.Tests - - ps: Start-Sleep -Seconds 5 - - ps: Restart-Computer -Force - - ps: Start-Sleep -Seconds 5 - ps: Write-Verbose -Message "PowerShell version $($PSVersionTable.PSVersion)" -Verbose - ps: Import-Module "$env:APPVEYOR_BUILD_FOLDER\DscResource.Tests\AppVeyor.psm1" - ps: Invoke-AppveyorInstallTask @@ -22,9 +22,14 @@ build: false # test configuration # #---------------------------------# -test_script: +for: +- + matrix: + only: + - configuration: Integration + + test_script: - ps: | - # Workaround for issue #239 and issue #774. Write-Verbose -Message '--- WORKAROUND FOR ISSUE #239 AND ISSUE #774 ---' -Verbose $sqlModules = Get-Module -ListAvailable -Name 'sql*' $sqlUniqueModulePath = Split-Path -Path (Split-Path $sqlModules.Path -Parent) -Parent | Sort-Object -Unique @@ -33,12 +38,10 @@ test_script: Write-Verbose ('Renaming ''{0}'' to ''..\{1}''' -f $_, $newFolderName) -Verbose Rename-Item $_ -NewName $newFolderName -Force } - Write-Verbose -Message '' -Verbose - # Workaround for issue #798 Write-Verbose -Message '--- WORKAROUND FOR ISSUE #798 ---' -Verbose - $azureModules = Get-Module -ListAvailable -Name 'Azure*' + $azureModules = Get-Module -ListAvailable -Name 'Azure*' | Where-Object -FilterScript { $_.Name -notlike 'AzureRm*' } $azureUniqueModulePath = Split-Path -Path (Split-Path $azureModules.Path -Parent) -Parent | Sort-Object -Unique $azureUniqueModulePath | ForEach-Object -Process { $newFolderName = '{0}.old' -f (Split-Path -Path $_ -Leaf) @@ -47,6 +50,26 @@ test_script: } Write-Verbose -Message '' -Verbose + Invoke-AppveyorTestScriptTask -ExcludeTag @() -RunTestInOrder + +- + matrix: + only: + - configuration: Unit + + test_script: + - ps: | + # Workaround for issue #239 and issue #774. + Write-Verbose -Message '--- WORKAROUND FOR ISSUE #239 AND ISSUE #774 ---' -Verbose + $sqlModules = Get-Module -ListAvailable -Name 'sql*' + $sqlUniqueModulePath = Split-Path -Path (Split-Path $sqlModules.Path -Parent) -Parent | Sort-Object -Unique + $sqlUniqueModulePath | ForEach-Object -Process { + $newFolderName = '{0}.old' -f (Split-Path -Path $_ -Leaf) + Write-Verbose ('Renaming ''{0}'' to ''..\{1}''' -f $_, $newFolderName) -Verbose + Rename-Item $_ -NewName $newFolderName -Force + } + Write-Verbose -Message '' -Verbose + Invoke-AppveyorTestScriptTask -CodeCoverage -CodeCovIo -ExcludeTag @() -RunTestInOrder #---------------------------------#