-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL Restore.Tests.ps1
More file actions
102 lines (93 loc) · 3.44 KB
/
Copy pathSQL Restore.Tests.ps1
File metadata and controls
102 lines (93 loc) · 3.44 KB
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
#Requires -Modules Pester
#Requires -Version 5.1
BeforeAll {
$testScript = $PSCommandPath.Replace('.Tests.ps1', '.ps1')
$testParams = @{
ComputerName = 'pc1'
Query = 'EXECUTE RESTORE'
BackupFile = 'TestDrive:\backup123.bak'
RestoreFile = 'TestDrive:\c\k\l\backup.bak'
}
New-Item $testParams.BackupFile -ItemType File -Force
Mock Invoke-Sqlcmd
}
Describe 'when tests pass' {
BeforeAll {
$testParams.BackupFile | Should -Exist
$testParams.RestoreFile | Split-Path | Should -Not -Exist
$actual = & $testScript @testParams
}
It 'create the restore folder' {
$testParams.RestoreFile | Split-Path | Should -Exist
}
It 'copy the backup file' {
$testParams.RestoreFile | Should -Exist
}
It 'call Invoke-Sqlcmd to restore the database backup' {
Should -Invoke Invoke-Sqlcmd -Times 1 -Exactly -Scope Describe -ParameterFilter {
($ServerInstance -eq $testParams.ComputerName) -and
($Query -eq $testParams.Query)
}
}
It 'return a result object' {
$actual.CopyOk | Should -BeTrue
$actual.RestoreOk | Should -BeTrue
$actual.Error | Should -BeNullOrEmpty
}
}
Describe 'create an error when' {
It 'the backup file cannot be found' {
$testParams = @{
ComputerName = 'fail'
Query = 'EXECUTE dbo.DatabaseBackup'
BackupFile = 'TestDrive:\notExisting.bak'
RestoreFile = 'TestDrive:\c\k\l\backup.bak'
}
$actual = & $testScript @testParams
$actual.CopyOk | Should -BeFalse
$actual.RestoreOk | Should -BeFalse
$actual.Error | Should -Be "Backup file 'TestDrive:\notExisting.bak' not found"
}
It 'the restore folder cannot be created' {
$testParams = @{
ComputerName = 'pc1'
Query = 'EXECUTE dbo.DatabaseBackup'
BackupFile = 'TestDrive:\backup123.bak'
RestoreFile = 'x:\backup folder\backup.bak'
}
$actual = & $testScript @testParams
$actual.CopyOk | Should -BeFalse
$actual.RestoreOk | Should -BeFalse
$actual.Error | Should -BeLike "Failed creating restore folder 'x:\backup folder'*"
}
It 'the copy of the restore file fails' {
Mock Copy-Item {
throw 'oops'
}
$testParams = @{
ComputerName = 'pc1'
Query = 'EXECUTE dbo.DatabaseBackup'
BackupFile = 'TestDrive:\backup123.bak'
RestoreFile = 'TestDrive:\backup folder\backup.bak'
}
$actual = & $testScript @testParams
$actual.CopyOk | Should -BeFalse
$actual.RestoreOk | Should -BeFalse
$actual.Error | Should -Be "Failed copying file 'TestDrive:\backup123.bak' to 'TestDrive:\backup folder\backup.bak': Oops"
}
It 'the database restore fails' {
Mock Invoke-Sqlcmd {
throw 'oops'
}
$testParams = @{
ComputerName = 'pc1'
Query = 'EXECUTE dbo.DatabaseBackup'
BackupFile = 'TestDrive:\backup123.bak'
RestoreFile = 'TestDrive:\c\k\l\backup.bak'
}
$actual = & $testScript @testParams
$actual.RestoreOk | Should -BeFalse
$actual.CopyOk | Should -BeTrue
$actual.Error | Should -Be "Restore failed on 'pc1': oops"
}
}