-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3820a4
commit ee62765
Showing
12 changed files
with
716 additions
and
1,367 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,176 +1,153 @@ | ||
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" } | ||
param( | ||
$ModuleName = "dbatools", | ||
$PSDefaultParameterValues = ($global:TestConfig = Get-TestConfig).Defaults | ||
) | ||
|
||
Describe "Export-DbaRegServer" -Tag "UnitTests" { | ||
Context "Parameter validation" { | ||
BeforeAll { | ||
$command = Get-Command Export-DbaRegServer | ||
$expected = $TestConfig.CommonParameters | ||
$expected += @( | ||
"SqlInstance", | ||
"SqlCredential", | ||
"InputObject", | ||
"Path", | ||
"FilePath", | ||
"CredentialPersistenceType", | ||
"Group", | ||
"ExcludeGroup", | ||
"Overwrite", | ||
"EnableException" | ||
) | ||
} | ||
|
||
It "Has parameter: <_>" -ForEach $expected { | ||
$command | Should -HaveParameter $PSItem | ||
} | ||
|
||
It "Should have exactly the number of expected parameters ($($expected.Count))" { | ||
$hasParams = $command.Parameters.Values.Name | ||
Compare-Object -ReferenceObject $expected -DifferenceObject $hasParams | Should -BeNullOrEmpty | ||
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") | ||
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan | ||
$global:TestConfig = Get-TestConfig | ||
|
||
Describe "$CommandName Unit Tests" -Tags "UnitTests" { | ||
Context "Validate parameters" { | ||
It "Should only contain our specific parameters" { | ||
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') } | ||
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'Path', 'FilePath', 'CredentialPersistenceType', 'EnableException', 'Group', 'ExcludeGroup', 'Overwrite' | ||
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters | ||
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should -Be 0 | ||
} | ||
} | ||
} | ||
|
||
Describe "Export-DbaRegServer" -Tag "IntegrationTests" { | ||
BeforeAll { | ||
$random = Get-Random | ||
$newDirectory = "C:\temp-$random" | ||
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { | ||
BeforeEach { | ||
$srvName = "dbatoolsci-server1" | ||
$group = "dbatoolsci-group1" | ||
$regSrvName = "dbatoolsci-server12" | ||
$regSrvDesc = "dbatoolsci-server123" | ||
|
||
$splatServer1 = @{ | ||
SqlInstance = $TestConfig.instance2 | ||
ServerName = "dbatoolsci-server1" | ||
Name = "dbatoolsci-server12" | ||
Description = "dbatoolsci-server123" | ||
} | ||
$newGroup = Add-DbaRegServerGroup -SqlInstance $TestConfig.instance2 -Name $group | ||
$newServer = Add-DbaRegServer -SqlInstance $TestConfig.instance2 -ServerName $srvName -Name $regSrvName -Description $regSrvDesc | ||
|
||
$splatServer2 = @{ | ||
SqlInstance = $TestConfig.instance2 | ||
ServerName = "dbatoolsci-server2" | ||
Name = "dbatoolsci-server21" | ||
Description = "dbatoolsci-server321" | ||
} | ||
$srvName2 = "dbatoolsci-server2" | ||
$group2 = "dbatoolsci-group2" | ||
$regSrvName2 = "dbatoolsci-server21" | ||
$regSrvDesc2 = "dbatoolsci-server321" | ||
|
||
$splatServer3 = @{ | ||
SqlInstance = $TestConfig.instance2 | ||
ServerName = "dbatoolsci-server3" | ||
Name = "dbatoolsci-server3" | ||
Description = "dbatoolsci-server3desc" | ||
} | ||
$newGroup2 = Add-DbaRegServerGroup -SqlInstance $TestConfig.instance2 -Name $group2 | ||
$newServer2 = Add-DbaRegServer -SqlInstance $TestConfig.instance2 -ServerName $srvName2 -Name $regSrvName2 -Description $regSrvDesc2 | ||
|
||
$regSrvName3 = "dbatoolsci-server3" | ||
$srvName3 = "dbatoolsci-server3" | ||
$regSrvDesc3 = "dbatoolsci-server3desc" | ||
|
||
$group1 = Add-DbaRegServerGroup -SqlInstance $TestConfig.instance2 -Name "dbatoolsci-group1" | ||
$group2 = Add-DbaRegServerGroup -SqlInstance $TestConfig.instance2 -Name "dbatoolsci-group2" | ||
$newServer3 = Add-DbaRegServer -SqlInstance $TestConfig.instance2 -ServerName $srvName3 -Name $regSrvName3 -Description $regSrvDesc3 | ||
|
||
$random = Get-Random | ||
$newDirectory = "C:\temp-$random" | ||
} | ||
AfterEach { | ||
Get-DbaRegServer -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServer -Confirm:$false | ||
Get-DbaRegServerGroup -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServerGroup -Confirm:$false | ||
$results, $results2, $results3 | Remove-Item -ErrorAction Ignore | ||
|
||
Remove-Item $newDirectory -ErrorAction Ignore -Recurse -Force | ||
} | ||
|
||
It "should create an xml file" { | ||
$results = $newServer | Export-DbaRegServer | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.Extension -eq '.xml' | Should -Be $true | ||
} | ||
|
||
$server1 = Add-DbaRegServer @splatServer1 | ||
$server2 = Add-DbaRegServer @splatServer2 | ||
$server3 = Add-DbaRegServer @splatServer3 | ||
It "should create a specific xml file when using Path" { | ||
$results2 = $newGroup2 | Export-DbaRegServer -Path C:\temp | ||
$results2 -is [System.IO.FileInfo] | Should -Be $true | ||
$results2.FullName | Should -match 'C\:\\temp' | ||
Get-Content -Path $results2 -Raw | Should -Match $group2 | ||
} | ||
|
||
AfterAll { | ||
It "creates an importable xml file" { | ||
$results3 = $newServer3 | Export-DbaRegServer -Path C:\temp | ||
Get-DbaRegServer -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServer -Confirm:$false | ||
Get-DbaRegServerGroup -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServerGroup -Confirm:$false | ||
$results4 = Import-DbaRegServer -SqlInstance $TestConfig.instance2 -Path $results3 | ||
$newServer3.ServerName | Should -BeIn $results4.ServerName | ||
$newServer3.Description | Should -BeIn $results4.Description | ||
} | ||
|
||
if (Test-Path $newDirectory) { | ||
Remove-Item $newDirectory -Recurse -Force | ||
} | ||
It "Create an xml file using FilePath" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.FullName | Should -Be $outputFileName | ||
} | ||
|
||
Context "When exporting registered servers" { | ||
It "Creates an xml file" { | ||
$results = $server1 | Export-DbaRegServer | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.Extension | Should -Be '.xml' | ||
} | ||
It "Create a regsrvr file using the FilePath alias OutFile" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.regsrvr" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -OutFile $outputFileName | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.FullName | Should -Be $outputFileName | ||
} | ||
|
||
It "Creates a specific xml file when using Path" { | ||
$results = $group2 | Export-DbaRegServer -Path C:\temp | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.FullName | Should -Match 'C\:\\temp' | ||
Get-Content -Path $results -Raw | Should -Match 'dbatoolsci-group2' | ||
} | ||
It "Try to create an invalid file using FilePath" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.txt" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName | ||
$results.length | Should -Be 0 | ||
} | ||
|
||
It "Creates an importable xml file" { | ||
$exportPath = $server3 | Export-DbaRegServer -Path C:\temp | ||
Get-DbaRegServer -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServer -Confirm:$false | ||
Get-DbaRegServerGroup -SqlInstance $TestConfig.instance2 | Where-Object Name -Match dbatoolsci | Remove-DbaRegServerGroup -Confirm:$false | ||
It "Create an xml file using the FilePath alias FileName in a directory that does not yet exist" { | ||
$outputFileName = "$newDirectory\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FileName $outputFileName | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.FullName | Should -Be $outputFileName | ||
} | ||
|
||
$importedServer = Import-DbaRegServer -SqlInstance $TestConfig.instance2 -Path $exportPath | ||
$importedServer.ServerName | Should -Be $splatServer3.ServerName | ||
$importedServer.Description | Should -Be $splatServer3.Description | ||
} | ||
It "Ensure the Overwrite param is working" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.FullName | Should -Be $outputFileName | ||
|
||
It "Creates an xml file using FilePath" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.FullName | Should -Be $outputFile | ||
} | ||
# test without -Overwrite | ||
$invalidResults = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName | ||
$invalidResults.length | Should -Be 0 | ||
|
||
It "Creates a regsrvr file using the FilePath alias OutFile" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.regsrvr" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -OutFile $outputFile | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.FullName | Should -Be $outputFile | ||
} | ||
# test with -Overwrite | ||
$resultsOverwrite = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName -Overwrite | ||
$resultsOverwrite -is [System.IO.FileInfo] | Should -Be $true | ||
$resultsOverwrite.FullName | Should -Be $outputFileName | ||
} | ||
|
||
It "Fails to create an invalid file using FilePath" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.txt" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile | ||
$results.Length | Should -Be 0 | ||
} | ||
It "Test with the Group param" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName -Group $group | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
$results.FullName | Should -Be $outputFileName | ||
|
||
It "Creates an xml file in a new directory using FileName" { | ||
$outputFile = "$newDirectory\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FileName $outputFile | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.FullName | Should -Be $outputFile | ||
} | ||
$fileText = Get-Content -Path $results -Raw | ||
|
||
It "Respects the Overwrite parameter" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$results.FullName | Should -Be $outputFile | ||
$fileText | Should -Match $group | ||
$fileText | Should -Not -Match $group2 | ||
} | ||
|
||
$invalidResults = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile | ||
$invalidResults.Length | Should -Be 0 | ||
It "Test with the Group param and multiple group names" { | ||
$outputFileName = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFileName -Group @($group, $group2) | ||
$results.length | Should -Be 2 | ||
|
||
$resultsOverwrite = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile -Overwrite | ||
$resultsOverwrite | Should -BeOfType [System.IO.FileInfo] | ||
$resultsOverwrite.FullName | Should -Be $outputFile | ||
} | ||
$fileText = Get-Content -Path $results[0] -Raw | ||
|
||
It "Filters by Group parameter" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile -Group "dbatoolsci-group1" | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$fileContent = Get-Content -Path $results -Raw | ||
$fileContent | Should -Match "dbatoolsci-group1" | ||
$fileContent | Should -Not -Match "dbatoolsci-group2" | ||
} | ||
$fileText | Should -Match $group | ||
$fileText | Should -Not -Match $group2 | ||
|
||
It "Handles multiple groups" { | ||
$outputFile = "C:\temp\dbatoolsci-regsrvr-export-$random.xml" | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -FilePath $outputFile -Group @("dbatoolsci-group1", "dbatoolsci-group2") | ||
$results.Length | Should -Be 2 | ||
$fileText = Get-Content -Path $results[1] -Raw | ||
|
||
$content1 = Get-Content -Path $results[0] -Raw | ||
$content1 | Should -Match "dbatoolsci-group1" | ||
$content1 | Should -Not -Match "dbatoolsci-group2" | ||
$fileText | Should -Not -Match $group | ||
$fileText | Should -Match $group2 | ||
} | ||
|
||
$content2 = Get-Content -Path $results[1] -Raw | ||
$content2 | Should -Not -Match "dbatoolsci-group1" | ||
$content2 | Should -Match "dbatoolsci-group2" | ||
} | ||
It "Test with the ExcludeGroup param" { | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -ExcludeGroup $group2 | ||
$results -is [System.IO.FileInfo] | Should -Be $true | ||
|
||
It "Respects ExcludeGroup parameter" { | ||
$results = Export-DbaRegServer -SqlInstance $TestConfig.instance2 -ExcludeGroup "dbatoolsci-group2" | ||
$results | Should -BeOfType [System.IO.FileInfo] | ||
$fileContent = Get-Content -Path $results -Raw | ||
$fileContent | Should -Match "dbatoolsci-group1" | ||
$fileContent | Should -Not -Match "dbatoolsci-group2" | ||
} | ||
$fileText = Get-Content -Path $results -Raw | ||
|
||
$fileText | Should -Match $group | ||
$fileText | Should -Not -Match $group2 | ||
} | ||
} |
Oops, something went wrong.