tests/Set-DbaDatabaseState.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
$commandname = $MyInvocation.MyCommand.Name.Replace(".ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
<#
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
 Context "Parameters validation" {
  BeforeAll {
   $server = Connect-DbaSqlServer -SqlInstance $script:instance2
   $db1 = "dbatoolsci_dbsetstate_online"
   $server.Query("CREATE DATABASE $db1")
  }
  AfterAll {
   Remove-DbaDatabase -Confirm:$false -SqlInstance $script:instance2 -Database $db1
  }
  It "Stops if no Database or AllDatabases" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Silent } | Should Throw "You must specify"
  }
  It "Is nice by default" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 *> $null } | Should Not Throw "You must specify"
  }
  It "Errors out when multiple 'access' params are passed with Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -SingleUser -RestrictedUser -Silent } | Should Throw "You can only specify one of: -SingleUser,-RestrictedUser,-MultiUser"
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -MultiUser -RestrictedUser -Silent } | Should Throw "You can only specify one of: -SingleUser,-RestrictedUser,-MultiUser"
  }
  It "Errors out when multiple 'access' params are passed without Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -SingleUser -RestrictedUser *> $null } | Should Not Throw
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -MultiUser -RestrictedUser *> $null } | Should Not Throw
   $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -SingleUser -RestrictedUser *> $null
   $result | Should Be $null
  }
  It "Errors out when multiple 'status' params are passed with Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Offline -Online -Silent } | Should Throw "You can only specify one of: -Online,-Offline,-Emergency,-Detached"
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Emergency -Online -Silent } | Should Throw "You can only specify one of: -Online,-Offline,-Emergency,-Detached"
  }
  It "Errors out when multiple 'status' params are passed without Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Offline -Online *> $null } | Should Not Throw
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Emergency -Online *> $null } | Should Not Throw
   $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Offline -Online *> $null
   $result | Should Be $null
  }
  It "Errors out when multiple 'rw' params are passed with Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -ReadOnly -ReadWrite -Silent } | Should Throw "You can only specify one of: -ReadOnly,-ReadWrite"
  }
  It "Errors out when multiple 'rw' params are passed without Silent" {
   { Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -ReadOnly -ReadWrite *> $null } | Should Not Throw
   $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -ReadOnly -ReadWrite *> $null
   $result | Should Be $null
  }
 }
 Context "Operations on databases" {
  BeforeAll {
   $server = Connect-DbaSqlServer -SqlInstance $script:instance2
   $db1 = "dbatoolsci_dbsetstate_online"
   $db2 = "dbatoolsci_dbsetstate_offline"
   $db3 = "dbatoolsci_dbsetstate_emergency"
   $db4 = "dbatoolsci_dbsetstate_single"
   $db5 = "dbatoolsci_dbsetstate_restricted"
   $db6 = "dbatoolsci_dbsetstate_multi"
   $db7 = "dbatoolsci_dbsetstate_rw"
   $db8 = "dbatoolsci_dbsetstate_ro"
   Get-DbaDatabase -SqlInstance $script:instance2 -Database $db1, $db2, $db3, $db4, $db5, $db6, $db7, $db8 | Remove-DbaDatabase -Confirm:$false
   $server.Query("CREATE DATABASE $db1")
   $server.Query("CREATE DATABASE $db2")
   $server.Query("CREATE DATABASE $db3")
   $server.Query("CREATE DATABASE $db4")
   $server.Query("CREATE DATABASE $db5")
   $server.Query("CREATE DATABASE $db6")
   $server.Query("CREATE DATABASE $db7")
   $server.Query("CREATE DATABASE $db8")
   $setupright = $true
   $needed = Get-DbaDatabase -SqlInstance $script:instance2 -Database $db1, $db2, $db3, $db4, $db5, $db6, $db7, $db8
   if ($needed.Count -ne 8) {
    $setupright = $false
    it "has failed setup" {
     Set-TestInconclusive -message "Setup failed"
    }
   }
  }
  AfterAll {
   $null = Set-DbaDatabaseState -Sqlinstance $script:instance2 -Database $db2, $db3, $db4, $db5, $db7 -Online -ReadWrite -MultiUser -Force
   $null = Remove-DbaDatabase -Confirm:$false -SqlInstance $script:instance2 -Database $db1, $db2, $db3, $db4, $db5, $db6, $db7, $db8
  }
  if ($setupright) {
   # just to have a correct report on how much time BeforeAll takes
   It "Waits for BeforeAll to finish" {
    $true | Should Be $true
   }
   It "Honors the Database parameter" {
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db2 -Emergency -Force
    $result.DatabaseName | Should be $db2
    $result.Status | Should Be 'EMERGENCY'
    $results = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1, $db2 -Emergency -Force
    $results.Count | Should be 2
   }
   It "Honors the ExcludeDatabase parameter" {
    $alldbs = (Get-DbaDatabase -SqlInstance $script:instance2 | Where-Object Name -notin @($db1, $db2, $db3, $db4, $db5, $db6, $db7, $db8)).Name
    $results = Set-DbaDatabaseState -SqlInstance $script:instance2 -ExcludeDatabase $alldbs -Online -Force
    $comparison = Compare-Object -ReferenceObject ($results.DatabaseName) -DifferenceObject (@($db1, $db2, $db3, $db4, $db5, $db6, $db7, $db8))
    $comparison.Count | Should Be 0
   }
    
   It "Sets a database as online" {
    $null = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Emergency -Force
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Online -Force
    $result.DatabaseName | Should Be $db1
    $result.Status | Should Be "ONLINE"
   }
    
   if (-not $env:appveyor) {
    It "Sets a database as offline" {
     $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db2 -Offline -Force
     $result.DatabaseName | Should Be $db2
     $result.Status | Should Be "OFFLINE"
    }
   }
    
   It "Sets a database as emergency" {
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db3 -Emergency -Force
    $result.DatabaseName | Should Be $db3
    $result.Status | Should Be "EMERGENCY"
   }
   if (-not $env:appveyor) {
    It "Sets a database as single_user" {
     $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db4 -SingleUser -Force
     $result.DatabaseName | Should Be $db4
     $result.Access | Should Be "SINGLE_USER"
    }
    It "Sets a database as multi_user" {
     $null = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db6 -RestrictedUser -Force
     $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db6 -MultiUser -Force
     $result.DatabaseName | Should Be $db6
     $result.Access | Should Be "MULTI_USER"
    }
   }
   It "Sets a database as restricted_user" {
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db5 -RestrictedUser -Force
    $result.DatabaseName | Should Be $db5
    $result.Access | Should Be "RESTRICTED_USER"
   }
   It "Sets a database as read_write" {
    $null = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db7 -ReadOnly -Force
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db7 -ReadWrite -Force
    $result.DatabaseName | Should Be $db7
    $result.RW | Should Be "READ_WRITE"
   }
   It "Sets a database as read_only" {
    $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db8 -ReadOnly -Force
    $result.DatabaseName | Should Be $db8
    $result.RW | Should Be "READ_ONLY"
   }
   It "Works when piped from Get-DbaDatabaseState" {
    $results = Get-DbaDatabaseState -SqlInstance $script:instance2 -Database $db7, $db8 | Set-DbaDatabaseState -Online -MultiUser -Force
    $results.Count | Should Be 2
    $comparison = Compare-Object -ReferenceObject ($results.DatabaseName) -DifferenceObject (@($db7, $db8))
    $comparison.Count | Should Be 0
   }
   It "Works when piped from Get-DbaDatabase" {
    $results = Get-DbaDatabase -SqlInstance $script:instance2 -Database $db7, $db8 | Set-DbaDatabaseState -Online -MultiUser -Force
    $results.Count | Should Be 2
    $comparison = Compare-Object -ReferenceObject ($results.DatabaseName) -DifferenceObject (@($db7, $db8))
    $comparison.Count | Should Be 0
   }
   $result = Set-DbaDatabaseState -SqlInstance $script:instance2 -Database $db1 -Emergency -Force
   It "Has the correct properties" {
    $ExpectedProps = 'ComputerName,InstanceName,SqlInstance,DatabaseName,RW,Status,Access,Notes,Database'.Split(',')
    ($result.PsObject.Properties.Name | Sort-Object) | Should Be ($ExpectedProps | Sort-Object)
   }
    
   It "Has the correct default properties" {
    $ExpectedPropsDefault = 'ComputerName,InstanceName,SqlInstance,DatabaseName,RW,Status,Access,Notes'.Split(',')
    ($result.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | Sort-Object) | Should Be ($ExpectedPropsDefault | Sort-Object)
   }
  }
 }
}
#>