forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connect-DbaInstance.Tests.ps1
194 lines (165 loc) · 9.6 KB
/
Connect-DbaInstance.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'ApplicationIntent', 'AzureUnsupported', 'BatchSeparator', 'ClientName', 'ConnectTimeout', 'EncryptConnection', 'FailoverPartner', 'LockTimeout', 'MaxPoolSize', 'MinPoolSize', 'MinimumVersion', 'MultipleActiveResultSets', 'MultiSubnetFailover', 'NetworkProtocol', 'NonPooledConnection', 'PacketSize', 'PooledConnectionLifetime', 'SqlExecutionModes', 'StatementTimeout', 'TrustServerCertificate', 'WorkstationId', 'AlwaysEncrypted', 'AppendConnectionString', 'SqlConnectionOnly', 'AzureDomain', 'Tenant', 'AccessToken', 'DedicatedAdminConnection', 'DisableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
}
}
Context "Validate alias" {
It "Should contain the alias: cdi" {
(Get-Alias cdi) | Should -Not -BeNullOrEmpty
}
}
}
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
if ($env:azuredbpasswd -eq "failstoooften") {
Context "Connect to Azure" {
$securePassword = ConvertTo-SecureString $env:azuredbpasswd -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($script:azuresqldblogin, $securePassword)
It "Should login to Azure" {
$s = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -SqlCredential $cred -Database test
$s.Name | Should -match 'psdbatools.database.windows.net'
$s.DatabaseEngineType | Should -Be 'SqlAzureDatabase'
}
It "Should keep the same database context" {
$s = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -SqlCredential $cred -Database test
$results = Invoke-DbaQuery -SqlInstance $s -Query "select db_name() as dbname"
$results.dbname | Should -Be 'test'
}
It "Should keep the same database context again" {
$s = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -SqlCredential $cred -Database test
$results = Invoke-DbaQuery -SqlInstance $s -Query "select db_name() as dbname"
$results.dbname | Should -Be 'test'
$results = Invoke-DbaQuery -SqlInstance $s -Query "select db_name() as dbname"
$results.dbname | Should -Be 'test'
}
It "Should keep the same database context" {
$s = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -SqlCredential $cred -Database test
$server = Connect-DbaInstance -SqlInstance $s
$server.Query("select db_name() as dbname").dbname | Should -Be 'test'
}
}
}
Context "connection is properly made" {
$server = Connect-DbaInstance -SqlInstance $script:instance1 -ApplicationIntent ReadOnly
It "returns the proper name" {
$server.Name -eq $script:instance1 | Should Be $true
}
It "returns more than one database" {
$server.Databases.Name.Count -gt 0 | Should Be $true
}
It "returns the connection with ApplicationIntent of ReadOnly" {
$server.ConnectionContext.ConnectionString -match "Intent=ReadOnly" | Should Be $true
}
It "sets StatementTimeout to 0" {
$server = Connect-DbaInstance -SqlInstance $script:instance1 -StatementTimeout 0
$server.ConnectionContext.StatementTimeout | Should Be 0
}
It "connects using a connection string" {
$server = Connect-DbaInstance -SqlInstance "Data Source=$script:instance1;Initial Catalog=tempdb;Integrated Security=True;Encrypt=False;Trust Server Certificate=True;"
$server.Databases.Name.Count -gt 0 | Should Be $true
}
It "connects using a dot" {
$newinstance = $script:instance1.Replace("localhost", ".")
Write-Warning "Connecting to $newinstance"
$server = Connect-DbaInstance -SqlInstance $newinstance
$server.Databases.Name.Count -gt 0 | Should Be $true
}
It "connects using a connection object" {
Set-DbatoolsConfig -FullName commands.connect-dbainstance.smo.computername.source -Value 'instance.ComputerName'
[Microsoft.Data.SqlClient.SqlConnection]$sqlconnection = "Data Source=$script:instance1;Initial Catalog=tempdb;Integrated Security=True;Encrypt=False;Trust Server Certificate=True"
$server = Connect-DbaInstance -SqlInstance $sqlconnection
$server.ComputerName | Should Be ([DbaInstance]$script:instance1).ComputerName
$server.Databases.Name.Count -gt 0 | Should Be $true
Set-DbatoolsConfig -FullName commands.connect-dbainstance.smo.computername.source -Value $null
}
It "connects - instance2" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$server.Databases.Name.Count -gt 0 | Should Be $true
}
It "connects using a connection string - instance2" {
$server = Connect-DbaInstance -SqlInstance "Data Source=$script:instance2;Initial Catalog=tempdb;Integrated Security=True;Encrypt=False;Trust Server Certificate=True"
$server.Databases.Name.Count -gt 0 | Should Be $true
}
It "connects using a connection object - instance2" {
Set-DbatoolsConfig -FullName commands.connect-dbainstance.smo.computername.source -Value 'instance.ComputerName'
[Microsoft.Data.SqlClient.SqlConnection]$sqlconnection = "Data Source=$script:instance2;Initial Catalog=tempdb;Integrated Security=True;Encrypt=False;Trust Server Certificate=True"
$server = Connect-DbaInstance -SqlInstance $sqlconnection
$server.ComputerName | Should Be ([DbaInstance]$script:instance2).ComputerName
$server.Databases.Name.Count -gt 0 | Should Be $true
Set-DbatoolsConfig -FullName commands.connect-dbainstance.smo.computername.source -Value $null
}
It "sets connectioncontext parameters that are provided" {
$params = @{
'BatchSeparator' = 'GO'
'ConnectTimeout' = 1
'Database' = 'master'
'LockTimeout' = 1
'MaxPoolSize' = 20
'MinPoolSize' = 1
'NetworkProtocol' = 'TcpIp'
'PacketSize' = 4096
'PooledConnectionLifetime' = 600
'WorkstationId' = 'MadeUpServer'
'SqlExecutionModes' = 'ExecuteSql'
'StatementTimeout' = 0
}
$server = Connect-DbaInstance -SqlInstance $script:instance1 @params
foreach ($param in $params.GetEnumerator()) {
if ($param.Key -eq 'Database') {
$propName = 'DatabaseName'
} else {
$propName = $param.Key
}
$server.ConnectionContext.$propName | Should Be $param.Value
}
}
}
}
Describe "$commandname Integration Tests (moved here from Connect-SqlInstance)" -Tags "IntegrationTests" {
$password = 'MyV3ry$ecur3P@ssw0rd'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$server = Connect-DbaInstance -SqlInstance $script:instance1
$login = "csitester"
#Cleanup
$results = Invoke-DbaQuery -SqlInstance $script:instance1 -Query "IF EXISTS (SELECT * FROM sys.server_principals WHERE name = '$login') EXEC sp_who '$login'"
foreach ($spid in $results.spid) {
Invoke-DbaQuery -SqlInstance $script:instance1 -Query "kill $spid"
}
if ($l = $server.logins[$login]) {
if ($c = $l.EnumCredentials()) {
$l.DropCredential($c)
}
$l.Drop()
}
#Create login
$newLogin = New-Object Microsoft.SqlServer.Management.Smo.Login($server, $login)
$newLogin.LoginType = "SqlLogin"
$newLogin.Create($password)
Context "Connect with a new login" {
It "Should login with newly created Sql Login (also tests credential login) and get instance name" {
$cred = New-Object System.Management.Automation.PSCredential ($login, $securePassword)
$s = Connect-DbaInstance -SqlInstance $script:instance1 -SqlCredential $cred
$s.Name | Should Be $script:instance1
}
It "Should return existing process running under the new login and kill it" {
$results = Invoke-DbaQuery -SqlInstance $script:instance1 -Query "IF EXISTS (SELECT * FROM sys.server_principals WHERE name = '$login') EXEC sp_who '$login'"
$results | Should Not BeNullOrEmpty
foreach ($spid in $results.spid) {
{ Invoke-DbaQuery -SqlInstance $script:instance1 -Query "kill $spid" -ErrorAction Stop} | Should Not Throw
}
}
}
#Cleanup
if ($l = $server.logins[$login]) {
if ($c = $l.EnumCredentials()) {
$l.DropCredential($c)
}
$l.Drop()
}
}