forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDismount-DbaDatabase.Tests.ps1
102 lines (89 loc) · 5.43 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
$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', 'InputObject', 'UpdateStatistics', 'Force', 'EnableException'
$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
}
}
}
Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
# Setting up the environment we need to test the cmdlet
BeforeAll {
# Everything in here gets executed before anything else in this context
Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
# 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:instance3 -Database $dbname | Remove-DbaDatabase -Confirm:$false
$server = Connect-DbaInstance -SqlInstance $script:instance3
$db1 = "dbatoolsci_dbsetstate_online"
$server.Query("CREATE DATABASE $dbname")
# memorizing $fileStructure for a later test
$fileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDbFile -SqlInstance $script:instance3 -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 = Mount-DbaDatabase -SqlInstance $script:instance3 -Database $dbname -FileStructure $script:fileStructure
$null = Get-DbaDatabase -SqlInstance $script:instance3 -Database $dbname | Remove-DbaDatabase -Confirm:$false
}
# Actual tests
Context "Detaches a single database and tests to ensure the alias still exists" {
$results = Dismount-DbaDatabase -SqlInstance $script:instance3 -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 {
Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $script:instance3
$db1 = "dbatoolsci_dbsetstate_detached"
$server.Query("CREATE DATABASE $db1")
Get-DbaProcess -SqlInstance $script:instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $script:instance3
$db2 = "dbatoolsci_dbsetstate_detached_withSnap"
$server.Query("CREATE DATABASE $db2")
$null = New-DbaDbSnapshot -SqlInstance $script:instance3 -Database $db2
$fileStructure = New-Object System.Collections.Specialized.StringCollection
foreach ($file in (Get-DbaDbFile -SqlInstance $script:instance3 -Database $db1).PhysicalName) {
$null = $fileStructure.Add($file)
}
Stop-DbaProcess -SqlInstance $script:instance3 -Database $db1
}
AfterAll {
$null = Remove-DbaDbSnapshot -SqlInstance $script:instance3 -Database $db2 -Force
$null = Mount-DbaDatabase -SqlInstance $script:instance3 -Database $db1 -FileStructure $fileStructure
$null = Get-DbaDatabase -SqlInstance $script:instance3 -Database $db1, $db2 | Remove-DbaDatabase -Confirm:$false
}
It "Skips detachment if database is snapshotted" {
$result = Dismount-DbaDatabase -SqlInstance $script:instance3 -Database $db2 -Force -WarningAction SilentlyContinue -WarningVariable warn
$result | Should Be $null
$warn -match "snapshot" | Should Be $true
$result = Get-DbaDatabase -SqlInstance $script:instance3 -Database $db2
$result | Should Not Be $null
}
$null = Stop-DbaProcess -SqlInstance $script:instance3 -Database $db1
$result = Dismount-DbaDatabase -SqlInstance $script:instance3 -Database $db1
It "Detaches the database correctly" {
$result = Get-DbaDatabase -SqlInstance $script:instance3 -Database $db1
$result | Should Be $null
}
}
}
#$script:instance2 - to make it show up in appveyor, long story