forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDismount-DbaDatabase.Tests.ps1
115 lines (96 loc) · 4.96 KB
/
Dismount-DbaDatabase.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
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
param(
$ModuleName = "dbatools",
$PSDefaultParameterValues = ($TestConfig = Get-TestConfig).Defaults
)
Describe "Dismount-DbaDatabase" -Tag "UnitTests" {
BeforeAll {
$command = Get-Command Dismount-DbaDatabase
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Database",
"InputObject",
"UpdateStatistics",
"Force",
"EnableException",
"Confirm",
"WhatIf"
)
}
Context "Parameter validation" {
It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
It "Should have exactly the number of expected parameters ($($expected.Count))" {
$hasparms = $command.Parameters.Values.Name
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
}
}
}
Describe "Dismount-DbaDatabase" -Tag "IntegrationTests" {
BeforeAll {
Get-DbaProcess -SqlInstance $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$dbName = "dbatoolsci_detachattach"
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbName | Remove-DbaDatabase -Confirm:$false
$database = New-DbaDatabase -SqlInstance $TestConfig.instance3 -Name $dbName
$global:fileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDbFile -SqlInstance $TestConfig.instance3 -Database $dbName).PhysicalName) {
$null = $fileStructure.Add($file)
}
}
AfterAll {
$null = Mount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbName -FileStructure $fileStructure
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbName | Remove-DbaDatabase -Confirm:$false
}
Context "When detaching a single database" {
BeforeAll {
$results = Dismount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbName -Force
}
It "Should complete successfully" {
$results.DetachResult | Should -Be "Success"
$results.DatabaseID | Should -Be $database.ID
}
It "Should remove just one database" {
$results.Database | Should -Be $dbName
}
}
Context "When detaching databases with snapshots" {
BeforeAll {
Get-DbaProcess -SqlInstance $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance3
$dbDetached = "dbatoolsci_dbsetstate_detached"
$dbWithSnapshot = "dbatoolsci_dbsetstate_detached_withSnap"
$server.Query("CREATE DATABASE $dbDetached")
$server.Query("CREATE DATABASE $dbWithSnapshot")
$null = New-DbaDbSnapshot -SqlInstance $TestConfig.instance3 -Database $dbWithSnapshot
$splatFileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDbFile -SqlInstance $TestConfig.instance3 -Database $dbDetached).PhysicalName) {
$null = $splatFileStructure.Add($file)
}
Stop-DbaProcess -SqlInstance $TestConfig.instance3 -Database $dbDetached
}
AfterAll {
$null = Remove-DbaDbSnapshot -SqlInstance $TestConfig.instance3 -Database $dbWithSnapshot -Force
$null = Mount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached -FileStructure $splatFileStructure
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached, $dbWithSnapshot | Remove-DbaDatabase -Confirm:$false
}
It "Should skip detachment if database has snapshots" {
$result = Dismount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbWithSnapshot -Force -WarningAction SilentlyContinue -WarningVariable warn 3> $null
$result | Should -BeNullOrEmpty
$warn | Should -Match "snapshot"
$database = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbWithSnapshot
$database | Should -Not -BeNullOrEmpty
}
It "Should detach database without snapshots" {
# skip for now in appveyor, but when we do troubleshoot, maybe it just needs a sleep
Start-Sleep 3
$null = Stop-DbaProcess -SqlInstance $TestConfig.instance3 -Database $dbDetached
$null = Dismount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached
$result = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached
$result | Should -BeNullOrEmpty
}
}
}
#$TestConfig.instance2 - to make it show up in appveyor, long story