From 6823a511cb7da2495ae3817acc30964fc79a3afc Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Sun, 27 Oct 2024 09:43:48 +0100 Subject: [PATCH] rewrites --- tests/Copy-DbaCustomError.Tests.ps1 | 64 ++++++--- tests/Copy-DbaLinkedServer.Tests.ps1 | 160 +++++++++++------------ tests/Copy-DbaResourceGovernor.Tests.ps1 | 104 +++++---------- 3 files changed, 156 insertions(+), 172 deletions(-) diff --git a/tests/Copy-DbaCustomError.Tests.ps1 b/tests/Copy-DbaCustomError.Tests.ps1 index 65895c3abf..fd3cadf3c7 100644 --- a/tests/Copy-DbaCustomError.Tests.ps1 +++ b/tests/Copy-DbaCustomError.Tests.ps1 @@ -36,33 +36,61 @@ Describe "Copy-DbaCustomError" -Tag "UnitTests" { Describe "Copy-DbaCustomError" -Tag "IntegrationTests" { BeforeAll { - $server = Connect-DbaInstance -SqlInstance $TestConfig.instance2 -Database master - $server.Query("EXEC sp_addmessage @msgnum = 60000, @severity = 16,@msgtext = N'The item named %s already exists in %s.',@lang = 'us_english';") - $server.Query("EXEC sp_addmessage @msgnum = 60000, @severity = 16, @msgtext = N'L''élément nommé %1! existe déjà dans %2!',@lang = 'French';") + $primaryServer = Connect-DbaInstance -SqlInstance $TestConfig.instance2 -Database master + + # Add test messages in English and French + $messageParams = @{ + msgnum = 60000 + severity = 16 + englishText = "The item named %s already exists in %s." + frenchText = "L'élément nommé %1! existe déjà dans %2!" + } + + $primaryServer.Query("EXEC sp_addmessage @msgnum = $($messageParams.msgnum), + @severity = $($messageParams.severity), + @msgtext = N'$($messageParams.englishText)', + @lang = 'us_english'") + + $primaryServer.Query("EXEC sp_addmessage @msgnum = $($messageParams.msgnum), + @severity = $($messageParams.severity), + @msgtext = N'$($messageParams.frenchText)', + @lang = 'French'") } + AfterAll { - $server = Connect-DbaInstance -SqlInstance $TestConfig.instance2 -Database master - $server.Query("EXEC sp_dropmessage @msgnum = 60000, @lang = 'all';") - $server = Connect-DbaInstance -SqlInstance $TestConfig.instance3 -Database master - $server.Query("EXEC sp_dropmessage @msgnum = 60000, @lang = 'all';") + $serversToClean = @($TestConfig.instance2, $TestConfig.instance3) + foreach ($serverInstance in $serversToClean) { + $cleanupServer = Connect-DbaInstance -SqlInstance $serverInstance -Database master + $cleanupServer.Query("EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'") + } } Context "When copying custom errors" { - It "Copies the sample custom error" { - $results = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000 - $results.Name | Should -Be "60000:'us_english'", "60000:'Français'" - $results.Status | Should -Be 'Successful', 'Successful' + It "Should successfully copy custom error messages" { + $copyResults = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000 + + $copyResults.Name.Count | Should -Be 2 + $copyResults.Name[0] | Should -BeExactly "60000:'us_english'" + # the French message broke Pester v5 encoding so we're using -Match instead of -BeExactly + # Expected @('60000:'us_english'', '60000:'Français''), but got @(60000:'us_english', 60000:'Français'). + $copyResults.Name[1] | Should -Match "60000:'Fran" + $copyResults.Status | Should -BeExactly @('Successful', 'Successful') } - It "Doesn't overwrite existing custom errors" { - $results = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000 - $results.Name | Should -Be "60000:'us_english'", "60000:'Français'" - $results.Status | Should -Be 'Skipped', 'Skipped' + It "Should skip existing custom errors" { + $duplicateResults = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000 + + $duplicateResults.Name.Count | Should -Be 2 + $duplicateResults.Name[0] | Should -BeExactly "60000:'us_english'" + # the French message broke Pester v5 so we're using -Match instead of -BeExactly + # Expected @('60000:'us_english'', '60000:'Français''), but got @(60000:'us_english', 60000:'Français'). + $duplicateResults.Name[1] | Should -Match "60000:'Fran" + $duplicateResults.Status | Should -BeExactly @('Skipped', 'Skipped') } - It "Verifies the newly copied custom error exists" { - $results = Get-DbaCustomError -SqlInstance $TestConfig.instance2 - $results.ID | Should -Contain 60000 + It "Should verify custom error exists" { + $customErrors = Get-DbaCustomError -SqlInstance $TestConfig.instance2 + $customErrors.ID | Should -Contain 60000 } } } \ No newline at end of file diff --git a/tests/Copy-DbaLinkedServer.Tests.ps1 b/tests/Copy-DbaLinkedServer.Tests.ps1 index 9e6ef629bb..bfb15187ae 100644 --- a/tests/Copy-DbaLinkedServer.Tests.ps1 +++ b/tests/Copy-DbaLinkedServer.Tests.ps1 @@ -37,86 +37,80 @@ Describe "Copy-DbaLinkedServer" -Tag "UnitTests" { } Describe "Copy-DbaLinkedServer" -Tag "IntegrationTests" { - BeforeAll { - $splatSource = @{ - SqlInstance = $TestConfig.instance2 - } - $splatDestination = @{ - SqlInstance = $TestConfig.instance3 - } - - $createsql = "EXEC master.dbo.sp_addlinkedserver @server = N'dbatoolsci_localhost', @srvproduct=N'SQL Server'; - EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatoolsci_localhost',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool'; - EXEC master.dbo.sp_addlinkedserver @server = N'dbatoolsci_localhost2', @srvproduct=N'', @provider=N'SQLNCLI10'; - EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatoolsci_localhost2',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool';" - - $sourceServer = Connect-DbaInstance @splatSource - $destServer = Connect-DbaInstance @splatDestination - $sourceServer.Query($createsql) - } - - AfterAll { - $dropsql = "EXEC master.dbo.sp_dropserver @server=N'dbatoolsci_localhost', @droplogins='droplogins'; - EXEC master.dbo.sp_dropserver @server=N'dbatoolsci_localhost2', @droplogins='droplogins'" - - try { - $sourceServer.Query($dropsql) - $destServer.Query($dropsql) - } catch { - # Ignore cleanup errors - } - } - - Context "When copying linked server with the same properties" { - BeforeAll { - $splatCopy = @{ - Source = $TestConfig.instance2 - Destination = $TestConfig.instance3 - LinkedServer = "dbatoolsci_localhost" - WarningAction = "SilentlyContinue" - } - } - - It "Copies successfully" { - $result = Copy-DbaLinkedServer @splatCopy - $result.Name | Select-Object -Unique | Should -Be "dbatoolsci_localhost" - $result.Status | Select-Object -Unique | Should -Be "Successful" - } - - It "Retains the same properties" { - $sourceLinkedServer = Get-DbaLinkedServer -SqlInstance $sourceServer -LinkedServer dbatoolsci_localhost -WarningAction SilentlyContinue - $destLinkedServer = Get-DbaLinkedServer -SqlInstance $destServer -LinkedServer dbatoolsci_localhost -WarningAction SilentlyContinue - - $destLinkedServer.Name | Should -Be $sourceLinkedServer.Name - $destLinkedServer.LinkedServer | Should -Be $sourceLinkedServer.LinkedServer - } - - It "Skips existing linked servers" { - $results = Copy-DbaLinkedServer @splatCopy - $results.Status | Should -Be "Skipped" - } - - It "Upgrades SQLNCLI provider based on what is registered" -Skip:($sourceServer.VersionMajor -gt 14 -or $destServer.VersionMajor -gt 14) { - $splatUpgrade = @{ - Source = $TestConfig.instance2 - Destination = $TestConfig.instance3 - LinkedServer = "dbatoolsci_localhost2" - UpgradeSqlClient = $true - } - $null = Copy-DbaLinkedServer @splatUpgrade - - $sourceServer = Connect-DbaInstance @splatSource - $destServer = Connect-DbaInstance @splatDestination - - $sourceScript = $sourceServer.LinkedServers['dbatoolsci_localhost2'].Script() - $destScript = $destServer.LinkedServers['dbatoolsci_localhost2'].Script() - - $sourceScript | Should -Match 'SQLNCLI\d+' - $destScript | Should -Match 'SQLNCLI\d+' - # Verify destination has same or higher version - $sourceVersion = [regex]::Match($sourceScript, 'SQLNCLI(\d+)').Groups[1].Value - $destVersion = [regex]::Match($destScript, 'SQLNCLI(\d+)').Groups[1].Value - [int]$destVersion | Should -BeGreaterOrEqual ([int]$sourceVersion) - } - } -} + BeforeAll { + $server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance2 + $server2 = Connect-DbaInstance -SqlInstance $TestConfig.instance3 + + $createSql = "EXEC master.dbo.sp_addlinkedserver @server = N'dbatoolsci_localhost', @srvproduct=N'SQL Server'; + EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatoolsci_localhost',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool'; + EXEC master.dbo.sp_addlinkedserver @server = N'dbatoolsci_localhost2', @srvproduct=N'', @provider=N'SQLNCLI10'; + EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatoolsci_localhost2',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool';" + + $server1.Query($createSql) + } + + AfterAll { + $dropSql = "EXEC master.dbo.sp_dropserver @server=N'dbatoolsci_localhost', @droplogins='droplogins'; + EXEC master.dbo.sp_dropserver @server=N'dbatoolsci_localhost2', @droplogins='droplogins'" + try { + $server1.Query($dropSql) + $server2.Query($dropSql) + } catch { + # Silently continue + } + } + + Context "When copying linked server with the same properties" { + It "Copies successfully" { + $copySplat = @{ + Source = $TestConfig.instance2 + Destination = $TestConfig.instance3 + LinkedServer = 'dbatoolsci_localhost' + WarningAction = 'SilentlyContinue' + } + $result = Copy-DbaLinkedServer @copySplat + $result | Select-Object -ExpandProperty Name -Unique | Should -BeExactly "dbatoolsci_localhost" + $result | Select-Object -ExpandProperty Status -Unique | Should -BeExactly "Successful" + } + + It "Retains the same properties" { + $getLinkSplat = @{ + LinkedServer = 'dbatoolsci_localhost' + WarningAction = 'SilentlyContinue' + } + $LinkedServer1 = Get-DbaLinkedServer -SqlInstance $server1 @getLinkSplat + $LinkedServer2 = Get-DbaLinkedServer -SqlInstance $server2 @getLinkSplat + + $LinkedServer1.Name | Should -BeExactly $LinkedServer2.Name + $LinkedServer1.LinkedServer | Should -BeExactly $LinkedServer2.LinkedServer + } + + It "Skips existing linked servers" { + $copySplat = @{ + Source = $TestConfig.instance2 + Destination = $TestConfig.instance3 + LinkedServer = 'dbatoolsci_localhost' + WarningAction = 'SilentlyContinue' + } + $results = Copy-DbaLinkedServer @copySplat + $results.Status | Should -BeExactly "Skipped" + } + + # SQLNCLI10 and SQLNCLI11 are not used on newer versions, not sure which versions, but skipping if later than 2017 + It "Upgrades SQLNCLI provider based on what is registered" -Skip:($server1.VersionMajor -gt 14 -or $server2.VersionMajor -gt 14) { + $upgradeSplat = @{ + Source = $TestConfig.instance2 + Destination = $TestConfig.instance3 + LinkedServer = 'dbatoolsci_localhost2' + UpgradeSqlClient = $true + } + $result = Copy-DbaLinkedServer @upgradeSplat + + $server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance2 + $server2 = Connect-DbaInstance -SqlInstance $TestConfig.instance3 + + $server1.LinkedServers.Script() | Should -Match 'SQLNCLI10' + $server2.LinkedServers.Script() | Should -Match 'SQLNCLI11' + } + } +} \ No newline at end of file diff --git a/tests/Copy-DbaResourceGovernor.Tests.ps1 b/tests/Copy-DbaResourceGovernor.Tests.ps1 index ea066b024a..25d5017481 100644 --- a/tests/Copy-DbaResourceGovernor.Tests.ps1 +++ b/tests/Copy-DbaResourceGovernor.Tests.ps1 @@ -38,98 +38,60 @@ Describe "Copy-DbaResourceGovernor" -Tag "UnitTests" { Describe "Copy-DbaResourceGovernor" -Tag "IntegrationTests" { BeforeAll { - $splatSetup = @{ - WarningAction = 'SilentlyContinue' + $querySplat = @{ SqlInstance = $TestConfig.instance2 + WarningAction = 'SilentlyContinue' } - # Ensure clean state before setup - $cleanupQueries = @( - "IF EXISTS (SELECT 1 FROM sys.resource_governor_configuration WHERE classifier_function_id IS NOT NULL) ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL)", - "ALTER RESOURCE GOVERNOR RECONFIGURE", - "IF OBJECT_ID('dbo.dbatoolsci_fnRG') IS NOT NULL DROP FUNCTION dbo.dbatoolsci_fnRG", - "IF EXISTS (SELECT 1 FROM sys.resource_governor_workload_groups WHERE name = 'dbatoolsci_prodprocessing') DROP WORKLOAD GROUP dbatoolsci_prodprocessing", - "IF EXISTS (SELECT 1 FROM sys.resource_governor_workload_groups WHERE name = 'dbatoolsci_goffhoursprocessing') DROP WORKLOAD GROUP dbatoolsci_goffhoursprocessing", - "IF EXISTS (SELECT 1 FROM sys.resource_governor_resource_pools WHERE name = 'dbatoolsci_prod') DROP RESOURCE POOL dbatoolsci_prod", - "IF EXISTS (SELECT 1 FROM sys.resource_governor_resource_pools WHERE name = 'dbatoolsci_offhoursprocessing') DROP RESOURCE POOL dbatoolsci_offhoursprocessing", - "ALTER RESOURCE GOVERNOR RECONFIGURE" - ) + # Create prod pool and workload + Invoke-DbaQuery @querySplat -Query "CREATE RESOURCE POOL dbatoolsci_prod WITH (MAX_CPU_PERCENT = 100, MIN_CPU_PERCENT = 50)" + Invoke-DbaQuery @querySplat -Query "CREATE WORKLOAD GROUP dbatoolsci_prodprocessing WITH (IMPORTANCE = MEDIUM) USING dbatoolsci_prod" - foreach ($query in $cleanupQueries) { - Invoke-DbaQuery @splatSetup -Query $query - } + # Create offhours pool and workload + Invoke-DbaQuery @querySplat -Query "CREATE RESOURCE POOL dbatoolsci_offhoursprocessing WITH (MAX_CPU_PERCENT = 50, MIN_CPU_PERCENT = 0)" + Invoke-DbaQuery @querySplat -Query "CREATE WORKLOAD GROUP dbatoolsci_goffhoursprocessing WITH (IMPORTANCE = LOW) USING dbatoolsci_offhoursprocessing" - # Setup test environment - $setupQueries = @( - "CREATE RESOURCE POOL dbatoolsci_prod WITH (MAX_CPU_PERCENT = 100, MIN_CPU_PERCENT = 50)", - "CREATE WORKLOAD GROUP dbatoolsci_prodprocessing WITH (IMPORTANCE = MEDIUM) USING dbatoolsci_prod", - "CREATE RESOURCE POOL dbatoolsci_offhoursprocessing WITH (MAX_CPU_PERCENT = 50, MIN_CPU_PERCENT = 0)", - "CREATE WORKLOAD GROUP dbatoolsci_goffhoursprocessing WITH (IMPORTANCE = LOW) USING dbatoolsci_offhoursprocessing", - "ALTER RESOURCE GOVERNOR RECONFIGURE", - "CREATE FUNCTION dbo.dbatoolsci_fnRG() RETURNS sysname WITH SCHEMABINDING AS BEGIN RETURN N'dbatoolsci_goffhoursprocessing' END", - "ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = dbo.dbatoolsci_fnRG)", - "ALTER RESOURCE GOVERNOR RECONFIGURE" - ) - - foreach ($query in $setupQueries) { - Invoke-DbaQuery @splatSetup -Query $query - } + Invoke-DbaQuery @querySplat -Query "ALTER RESOURCE GOVERNOR RECONFIGURE" - # Clean destination before tests - $splatDestination = @{ - WarningAction = 'SilentlyContinue' - SqlInstance = $TestConfig.instance3 - } - foreach ($query in $cleanupQueries) { - Invoke-DbaQuery @splatDestination -Query $query - } + # Create and set classifier function + Invoke-DbaQuery @querySplat -Query "CREATE FUNCTION dbatoolsci_fnRG() RETURNS sysname WITH SCHEMABINDING AS BEGIN RETURN N'dbatoolsci_goffhoursprocessing' END" + Invoke-DbaQuery @querySplat -Query "ALTER RESOURCE GOVERNOR with (CLASSIFIER_FUNCTION = dbo.dbatoolsci_fnRG); ALTER RESOURCE GOVERNOR RECONFIGURE;" } AfterAll { - $splatCleanup = @{ + $cleanupSplat = @{ + SqlInstance = $TestConfig.instance2, $TestConfig.instance3 WarningAction = 'SilentlyContinue' - SqlInstance = @($TestConfig.instance2, $TestConfig.instance3) } - foreach ($query in $cleanupQueries) { - Invoke-DbaQuery @splatCleanup -Query $query - } + Get-DbaProcess -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue + + Invoke-DbaQuery @cleanupSplat -Query "ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL); ALTER RESOURCE GOVERNOR RECONFIGURE" + Invoke-DbaQuery @cleanupSplat -Query "DROP FUNCTION [dbo].[dbatoolsci_fnRG];ALTER RESOURCE GOVERNOR RECONFIGURE" + Invoke-DbaQuery @cleanupSplat -Query "DROP WORKLOAD GROUP [dbatoolsci_prodprocessing];ALTER RESOURCE GOVERNOR RECONFIGURE" + Invoke-DbaQuery @cleanupSplat -Query "DROP WORKLOAD GROUP [dbatoolsci_goffhoursprocessing];ALTER RESOURCE GOVERNOR RECONFIGURE" + Invoke-DbaQuery @cleanupSplat -Query "DROP RESOURCE POOL [dbatoolsci_offhoursprocessing];ALTER RESOURCE GOVERNOR RECONFIGURE" + Invoke-DbaQuery @cleanupSplat -Query "DROP RESOURCE POOL [dbatoolsci_prod];ALTER RESOURCE GOVERNOR RECONFIGURE" } - Context "When copying resource governor configuration" { - BeforeAll { - $splatCopy = @{ + Context "When copying resource governor settings" { + It "Copies the resource governor successfully" { + $copyRGSplat = @{ Source = $TestConfig.instance2 Destination = $TestConfig.instance3 Force = $true WarningAction = 'SilentlyContinue' } - $results = Copy-DbaResourceGovernor @splatCopy - } - - It "Copies all components successfully" { - $results | Should -Not -BeNullOrEmpty - $results | Where-Object Type -eq 'Resource Pool' | Select-Object -ExpandProperty Status | Should -Be 'Successful' - $results | Where-Object Type -eq 'Workload Group' | Select-Object -ExpandProperty Status | Should -Be 'Successful' - $results | Where-Object Type -eq 'Classifier Function' | Select-Object -ExpandProperty Status | Should -Be 'Successful' - } - - It "Copies resource pools correctly" { - $pools = Get-DbaRgResourcePool -SqlInstance $TestConfig.instance3 -ResourcePool "dbatoolsci_*" - $pools.Name | Should -Contain 'dbatoolsci_prod' - $pools.Name | Should -Contain 'dbatoolsci_offhoursprocessing' - } - It "Copies workload groups correctly" { - $groups = Get-DbaRgWorkloadGroup -SqlInstance $TestConfig.instance3 -WorkloadGroup "dbatoolsci_*" - $groups.Name | Should -Contain 'dbatoolsci_prodprocessing' - $groups.Name | Should -Contain 'dbatoolsci_goffhoursprocessing' + $results = Copy-DbaResourceGovernor @copyRGSplat + $results.Status | Select-Object -Unique | Should -BeExactly 'Successful' + $results.Status.Count | Should -BeGreaterThan 3 + $results.Name | Should -Contain 'dbatoolsci_prod' } - It "Copies the classifier function" { - $classifier = Get-DbaRgClassifierFunction -SqlInstance $TestConfig.instance3 - $classifier.Name | Should -Be 'dbatoolsci_fnRG' - $classifier.IsEnabled | Should -Be $true + It "Returns the proper classifier function" { + $results = Get-DbaRgClassifierFunction -SqlInstance $TestConfig.instance3 + $results.Name | Should -BeExactly 'dbatoolsci_fnRG' } } -} +} \ No newline at end of file