forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dismount-DbaDatabase.Tests.ps1
85 lines (73 loc) · 4.32 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
$commandname = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
# Setting up the environment we need to test the cmdlet
BeforeAll {
# Everything in here gets executed before anything else in this context
# Setting up variables names. If you want them to persist between all of the pester blocks, they can be moved outside
$dbname = "dbatoolsci_detachattach"
# making room in the remote case a db with the same name exists
$null = Get-DbaDatabase -SqlInstance $script:instance2 -Database $dbname | Remove-DbaDatabase -Confirm:$false
# restoring from the "common test data" (see https://github.com/sqlcollaborative/appveyor-lab)
$null = Restore-DbaDatabase -SqlInstance $script:instance2 -Path C:\github\appveyor-lab\detachattach\detachattach.bak -DatabaseName $dbname -WithReplace
# memorizing $fileStructure for a later test
$fileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDatabaseFile -SqlInstance $script:instance2 -Database $dbname).PhysicalName) {
$null = $fileStructure.Add($file)
}
}
# Everything we create/touch/mess with should be reverted to a "clean" state whenever possible
AfterAll {
# this gets executed always (think "finally" in try/catch/finally) and it's the best place for final cleanups
$null = Attach-DbaDatabase -SqlInstance $script:instance2 -Database $dbname -FileStructure $script:fileStructure
$null = Get-DbaDatabase -SqlInstance $script:instance2 -Database $dbname | Remove-DbaDatabase -Confirm:$false
}
# Actual tests
Context "Detaches a single database and tests to ensure the alias still exists" {
$results = Detach-DbaDatabase -SqlInstance $script:instance2 -Database $dbname -Force
It "was successfull" {
$results.DetachResult | Should Be "Success"
}
It "removed just one database" {
$results.Database | Should Be $dbname
}
It "has the correct properties" {
$ExpectedProps = 'ComputerName,InstanceName,SqlInstance,Database,DetachResult'.Split(',')
($results.PsObject.Properties.Name | Sort-Object) | Should Be ($ExpectedProps | Sort-Object)
}
}
Context "Database Detachment" {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$db1 = "dbatoolsci_dbsetstate_detached"
$db2 = "dbatoolsci_dbsetstate_detached_withSnap"
$server.Query("CREATE DATABASE $db1")
$server.Query("CREATE DATABASE $db2")
$null = New-DbaDatabaseSnapshot -SqlInstance $script:instance2 -Database $db2
$fileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDatabaseFile -SqlInstance $script:instance2 -Database $db1).PhysicalName) {
$null = $fileStructure.Add($file)
}
Stop-DbaProcess -SqlInstance $script:instance2 -Database $db1
}
AfterAll {
$null = Remove-DbaDatabaseSnapshot -SqlInstance $script:instance2 -Database $db2 -Force
$null = Attach-DbaDatabase -SqlInstance $script:instance2 -Database $db1 -FileStructure $fileStructure
$null = Get-DbaDatabase -SqlInstance $script:instance2 -Database $db1, $db2 | Remove-DbaDatabase -Confirm:$false
}
It "Skips detachment if database is snapshotted" {
$result = Dismount-DbaDatabase -SqlInstance $script:instance2 -Database $db2 -Force -WarningAction SilentlyContinue -WarningVariable warn
$result | Should Be $null
$warn -match "snapshot" | Should Be $true
$result = Get-DbaDatabase -SqlInstance $script:instance2 -Database $db2
$result | Should Not Be $null
}
$null = Stop-DbaProcess -SqlInstance $script:instance2 -Database $db1
$result = Dismount-DbaDatabase -SqlInstance $script:instance2 -Database $db1
It "Detaches the database correctly" {
$result = Get-DbaDatabase -SqlInstance $script:instance2 -Database $db1
$result | Should Be $null
}
}
}