tests/Copy-DbaLinkedServer.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
72
73
74
75
76
77
78
79
80
81
82
$commandname = $MyInvocation.MyCommand.Name.Replace(".ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

if ($env:appveyor) {
    try {
        $connstring = "Server=ADMIN:$script:instance1;Trusted_Connection=True"
        $server = New-Object Microsoft.SqlServer.Management.Smo.Server $script:instance1
        $server.ConnectionContext.ConnectionString = $connstring
        $server.ConnectionContext.Connect()
        $server.ConnectionContext.Disconnect()
        Clear-DbaSqlConnectionPool
    }
    catch {
        Write-Host "DAC not working this round, likely due to Appveyor resources"
        return
    }
}
<#
$dropsql = "EXEC master.dbo.sp_dropserver @server=N'dbatools-localhost', @droplogins='droplogins';
            EXEC master.dbo.sp_dropserver @server=N'dbatools-localhost2', @droplogins='droplogins'"
 
$createsql = "EXEC master.dbo.sp_addlinkedserver @server = N'dbatools-localhost', @srvproduct=N'SQL Server';
        EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatools-localhost',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool';
        EXEC master.dbo.sp_addlinkedserver @server = N'dbatools-localhost2', @srvproduct=N'SQL Server';
        EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatools-localhost2',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='supfool';"
# One more for the road - clearing the connection pool is important for DAC since only one is allowed
Clear-DbaSqlConnectionPool
 
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
    Context "Create new linked server" {
        $server1 = Connect-DbaSqlServer -SqlInstance $script:instance1
        $server2 = Connect-DbaSqlServer -SqlInstance $script:instance2
        try {
            $server1.Query($dropsql)
            $server2.Query($dropsql)
        }
        catch {
            # dont care
        }
         
        $server1.Query($createsql)
    }
     
    Context "Copy Credential with the same properties." {
        It "Should copy successfully" {
            $results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer dbatools-localhost
            foreach ($result in $results) {
                $result.Name | Should Be "dbatools-localhost"
                $result.Status | Should Be "Successful"
            }
        }
         
        It "Should retain its same properties" {
             
            $LinkedServer1 = Get-DbaLinkedServer -SqlInstance $script:instance1 -LinkedServer dbatools-localhost
            $LinkedServer2 = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer dbatools-localhost
             
            # Compare its value
            $LinkedServer1.Name | Should Be $LinkedServer2.Name
            $LinkedServer1.LinkedServer | Should Be $LinkedServer2.LinkedServer
        }
    }
    Clear-DbaSqlConnectionPool
    Context "No overwrite and cleanup" {
        $results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer dbatools-localhost -WarningVariable warning 3>&1
        It "Should not attempt overwrite" {
            $warning | Should Match "exists"
             
        }
         
        try {
            $server1.Query($dropsql)
            $server2.Query($dropsql)
        }
        catch {
            # dont care
        }
    }
    Clear-DbaSqlConnectionPool
}
#>