tests/Import-DbaRegisteredServer.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
    Context "Setup" {
        BeforeAll {
            $srvName = "dbatoolsci-server1"
            $group = "dbatoolsci-group1"
            $regSrvName = "dbatoolsci-server12"
            $regSrvDesc = "dbatoolsci-server123"
            
            $newGroup = Add-DbaRegisteredServerGroup -SqlInstance $script:instance1 -Name $group
            $newServer = Add-DbaRegisteredServer -SqlInstance $script:instance1 -ServerName $srvName -Name $regSrvName -Description $regSrvDesc -Group $newGroup.Name
            
            $srvName2 = "dbatoolsci-server2"
            $group2 = "dbatoolsci-group1a"
            $regSrvName2 = "dbatoolsci-server21"
            $regSrvDesc2 = "dbatoolsci-server321"
            
            $newGroup2 = Add-DbaRegisteredServerGroup -SqlInstance $script:instance1 -Name $group2
            $newServer2 = Add-DbaRegisteredServer -SqlInstance $script:instance1 -ServerName $srvName2 -Name $regSrvName2 -Description $regSrvDesc2
            
            $regSrvName3 = "dbatoolsci-server3"
            $srvName3 = "dbatoolsci-server3"
            $regSrvDesc3 = "dbatoolsci-server3desc"
            
            $newServer3 = Add-DbaRegisteredServer -SqlInstance $script:instance1 -ServerName $srvName3 -Name $regSrvName3 -Description $regSrvDesc3
        }
        AfterAll {
            Get-DbaRegisteredServer -SqlInstance $script:instance1, $script:instance2 | Where-Object Name -match dbatoolsci | Remove-DbaRegisteredServer -Confirm:$false
            Get-DbaRegisteredServerGroup -SqlInstance $script:instance1, $script:instance2 | Where-Object Name -match dbatoolsci | Remove-DbaRegisteredServerGroup -Confirm:$false
        }
        
        It "imports group objects" {
            $results = $newServer.Parent | Import-DbaRegisteredServer -SqlInstance $script:instance2
            $results.Description | Should -Be $regSrvDesc
            $results.ServerName | Should -Be $srvName
            $results.Parent.Name | Should -Be $group
        }
        
        It "imports registered server objects" {
            $results2 = $newServer2 | Import-DbaRegisteredServer -SqlInstance $script:instance2
            $results2.ServerName | Should -Be $newServer2.ServerName
            $results2.Parent.Name | Should -Be $newServer2.Parent.Name
        }
        
        It "imports a file from Export-DbaRegisteredServer" {
            $results3 = $newServer3 | Export-DbaRegisteredServer -Path C:\temp\dbatoolsci_regserverexport.xml
            $results4 = Import-DbaRegisteredServer -SqlInstance $script:instance2 -Path $results3
            $results4.ServerName | Should -Be @('dbatoolsci-server3', 'dbatoolsci-server1')
            $results4.Description | Should -Be @('dbatoolsci-server3desc', 'dbatoolsci-server123')
        }
        It "imports from a random object so long as it has ServerName" {
            $object = [pscustomobject]@{
                ServerName = 'dbatoolsci-randobject'
            }
            $results = $object | Import-DbaRegisteredServer -SqlInstance $script:instance2
            $results.ServerName | Should -Be 'dbatoolsci-randobject'
            $results.Name | Should -Be 'dbatoolsci-randobject'
        }
        It "does not import object if ServerName does not exist" {
            $object = [pscustomobject]@{
                Name = 'dbatoolsci-randobject'
            }
            $results = $object | Import-DbaRegisteredServer -SqlInstance $script:instance2 -WarningAction SilentlyContinue -WarningVariable warn
            $results | Should -Be $null
            $warn | Should -Match 'No servers added'
        }
    }
}