forked from OctopusDeploy/Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql-backup-database.ScriptBody.Tests.ps1
More file actions
251 lines (205 loc) · 12.6 KB
/
sql-backup-database.ScriptBody.Tests.ps1
File metadata and controls
251 lines (205 loc) · 12.6 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
$ErrorActionPreference = "Stop";
Set-StrictMode -Version "Latest";
. (Join-Path $PSScriptRoot ".." "sql-backup-database.ScriptBody.ps1")
function SetupTestEnvironment {
param(
$BackupDirectory,
$DatabaseName,
$IncrementalFiles,
$FullBackupFiles,
$StartDate,
$Devices,
$timestampFormat,
$challengingFilenames
)
# Clean the backup directory to start fresh
if (Test-Path -Path $BackupDirectory) {
Remove-Item -Path "$BackupDirectory\*" -Recurse -Force
}
else {
New-Item -ItemType Directory -Path $BackupDirectory
}
# Function to generate a random time
function Get-RandomTime {
$hours = Get-Random -Minimum 0 -Maximum 23
$minutes = Get-Random -Minimum 0 -Maximum 59
$seconds = Get-Random -Minimum 0 -Maximum 59
return "{0:D2}{1:D2}{2:D2}" -f $hours, $minutes, $seconds
}
function CreateBackupFiles {
param(
$numFiles,
$fileExtension,
$numDevices
)
for ($i = 0; $i -lt $numFiles; $i++) {
$daysToSubtract = Get-Random -Minimum 1 -Maximum 6
$currentDate = $StartDate.AddDays(-$daysToSubtract)
$randomTime = Get-RandomTime
$dateSuffix = $currentDate.ToString("yyyy-MM-dd") + "-" + $randomTime
for ($d = 1; $d -le $numDevices; $d++) {
$deviceSuffix = if ($numDevices -gt 1) { "_$d" } else { "" }
$fileName = "$DatabaseName" + "_$dateSuffix" + $deviceSuffix + $fileExtension
$filePath = Join-Path -Path $BackupDirectory -ChildPath $fileName
New-Item -Path $filePath -ItemType "file" -Force | Out-Null
# Validate that the file was created
if (-not (Test-Path -Path $filePath)) {
throw "Failed to create backup file: $filePath"
}
}
}
}
# Create full and incremental backup files considering the number of devices
CreateBackupFiles -numFiles $FullBackupFiles -fileExtension ".bak" -numDevices $Devices
CreateBackupFiles -numFiles $IncrementalFiles -fileExtension ".trn" -numDevices $Devices
# Create challenging files in the specified directory
foreach ($filename in $challengingFilenames) {
$filePath = Join-Path -Path $BackupDirectory -ChildPath $filename
New-Item -Path $filePath -ItemType "file" -Force | Out-Null
# Validate that the challenging file was created
if (-not (Test-Path -Path $filePath)) {
throw "Failed to create challenging file: $filePath"
}
}
}
Describe "ApplyRetentionPolicy Tests" {
BeforeAll {
$script:BackupDirectory = Join-Path ([System.IO.Path]::GetTempPath()) "OctopusDeployLibrary-SqlBackupTests"
$script:DatabaseName = "ExampleDB"
$script:StartDate = Get-Date
$script:timestampFormat = "yyyy-MM-dd-HHmmss"
$script:challengingFilenames = @(
# similar DB name noted during PR review
"ExampleDB_final_2024-03-18-1030.bak",
# Similar DB name, valid timestamp. Might be confused with a backup for a different but similarly named database.
"ExampleDB1_2024-03-18-1030.bak",
# Same DB, different valid timestamp. Tests accuracy of timestamp matching.
"ExampleDB_2024-03-19-1030.bak",
# Similar timestamp format, but different. Might test pattern matching robustness.
"ExampleDB_20240318_1030.bak",
# Different DB, valid timestamp. Should not be matched if script correctly identifies DB name.
"TestDB_2024-03-18-1030.bak",
# Non-backup file type with valid naming. Should be ignored by the cleanup script.
"ExampleDB_2024-03-18-1030.log",
# Completely unrelated file. Should always be ignored by the cleanup script.
"RandomFile.txt",
# Similar DB name with underscore. Might be confused with main database name if script uses loose matching.
"Example_DB_2024-03-18-1030.bak",
# Same DB name, lowercase. Tests case sensitivity of the script.
"exampledb_2024-03-18-1030.bak",
# Similar timestamp, underscore separator. Variation in timestamp format might challenge pattern matching.
"ExampleDB_2024-03-18_1030.bak",
# Different DB, valid timestamp for trn. Tests database name matching accuracy with incremental backups.
"AnotherDB_2024-03-18-1030.trn"
)
}
BeforeEach {
$Devices = 1
$IncrementalFiles = 10
$FullBackupFiles = 10
SetupTestEnvironment -BackupDirectory $BackupDirectory -DatabaseName $DatabaseName -IncrementalFiles $IncrementalFiles -FullBackupFiles $FullBackupFiles -StartDate $StartDate -Devices $Devices -timestampFormat $timestampFormat -challengingFilenames $challengingFilenames
}
AfterEach {
Remove-Item -Path $BackupDirectory -Recurse -Force
}
Context "ApplyRetentionPolicy functionality for single device backups" {
It "Retains the specified number of the most recent backups" {
$RetentionPolicyCount = 3
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $false -Devices $Devices -timestampFormat $timestampFormat -Verbose:$VerbosePreference
$extension = '.bak'
$devicePattern = if ($Devices -gt 1) { "(_\d+)" } else { "" }
$dateRegex = $timestampFormat -replace "yyyy", "\d{4}" -replace "MM", "\d{2}" -replace "dd", "\d{2}" -replace "HH", "\d{2}" -replace "mm", "\d{2}" -replace "ss", "\d{2}"
$regexPattern = "^${DatabaseName}_${dateRegex}${devicePattern}${extension}$"
$retainedFiles = Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern }
$retainedFiles.Count | Should Be $RetentionPolicyCount
}
It "Does not delete any files when RetentionPolicyCount is undefined" {
$InitialFileCount = (Get-ChildItem -Path $BackupDirectory).Count
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -Incremental $false -Devices $Devices -timestampFormat $timestampFormat
$FinalFileCount = (Get-ChildItem -Path $BackupDirectory).Count
$FinalFileCount | Should Be $InitialFileCount
}
It "Does not delete any files when RetentionPolicyCount is 0" {
$InitialFileCount = (Get-ChildItem -Path $BackupDirectory).Count
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount 0 -Incremental $false -Devices $Devices -timestampFormat $timestampFormat
$FinalFileCount = (Get-ChildItem -Path $BackupDirectory).Count
$FinalFileCount | Should Be $InitialFileCount
}
It "Retains only the most recent backup when the RetentionPolicyCount is 1" {
$RetentionPolicyCount = 1
$InitialFileCount = (Get-ChildItem -Path $BackupDirectory).Count
$extension = '.bak'
$devicePattern = if ($Devices -gt 1) { "(_\d+)" } else { "" }
$dateRegex = $timestampFormat -replace "yyyy", "\d{4}" -replace "MM", "\d{2}" -replace "dd", "\d{2}" -replace "HH", "\d{2}" -replace "mm", "\d{2}" -replace "ss", "\d{2}"
$regexPattern = "^${DatabaseName}_${dateRegex}${devicePattern}${extension}$"
$affectedFiles = @(Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern })
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $false -Devices $Devices -timestampFormat $timestampFormat
$retainedFiles = @(Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern })
$retainedFiles.Count | Should Be $RetentionPolicyCount
$deletedFiles = $affectedFiles.Count - $RetentionPolicyCount
$deletedFiles | Should Be ($affectedFiles.Count - $retainedFiles.Count)
}
It "Does not delete files that do not match the backup file naming pattern" {
$extension = '.bak'
$regexPattern = "^${DatabaseName}_\d{4}-\d{2}-\d{2}-\d{6}${extension}$"
$initialUnrelatedFileCount = @(Get-ChildItem -Path $BackupDirectory -File | Where-Object { -not ($_.Name -match $regexPattern) }).Count
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount 3 -Incremental $false -Devices $Devices -timestampFormat $timestampFormat
$finalUnrelatedFileCount = @(Get-ChildItem -Path $BackupDirectory -File | Where-Object { -not ($_.Name -match $regexPattern) }).Count
$finalUnrelatedFileCount | Should Be $initialUnrelatedFileCount
}
It "Retains the specified number of the most recent incremental backups" {
$RetentionPolicyCount = 5
$extension = '.trn'
$devicePattern = if ($Devices -gt 1) { "(_\d+)" } else { "" }
$dateRegex = $timestampFormat -replace "yyyy", "\d{4}" -replace "MM", "\d{2}" -replace "dd", "\d{2}" -replace "HH", "\d{2}" -replace "mm", "\d{2}" -replace "ss", "\d{2}"
$regexPattern = "^${DatabaseName}_${dateRegex}${devicePattern}${extension}$"
$affectedFiles = @(Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern })
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $true -Devices $Devices -timestampFormat $timestampFormat
$retainedFiles = @(Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern })
$retainedFiles.Count | Should Be $RetentionPolicyCount
$deletedFiles = $affectedFiles.Count - $RetentionPolicyCount
$deletedFiles | Should Be ($affectedFiles.Count - $retainedFiles.Count)
}
}
Context "ApplyRetentionPolicy functionality for multi-device backups" {
BeforeEach {
$Devices = 4
SetupTestEnvironment -BackupDirectory $BackupDirectory -DatabaseName $DatabaseName -IncrementalFiles $IncrementalFiles -FullBackupFiles $FullBackupFiles -StartDate $StartDate -Devices $Devices -timestampFormat $timestampFormat -challengingFilenames $challengingFilenames
}
It "Retains the specified number of the most recent backups" {
$RetentionPolicyCount = 3
$extension = '.bak'
$timestampFormat = "yyyy-MM-dd-HHmmss"
$devicePattern = if ($Devices -gt 1) { "(_\d+)" } else { "" }
$dateRegex = $timestampFormat -replace "yyyy", "\d{4}" -replace "MM", "\d{2}" -replace "dd", "\d{2}" -replace "HH", "\d{2}" -replace "mm", "\d{2}" -replace "ss", "\d{2}"
$regexPattern = "^${DatabaseName}_${dateRegex}${devicePattern}${extension}$"
$affectedFiles = @(Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern })
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $false -Devices $Devices -timestampFormat $timestampFormat
$retainedFiles = Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern }
$totalExpectedRetainedFiles = $RetentionPolicyCount * $Devices
$retainedFiles.Count | Should Be $totalExpectedRetainedFiles
$deletedFiles = $affectedFiles.Count - $RetentionPolicyCount * $Devices
$deletedFiles | Should Be ($affectedFiles.Count - $retainedFiles.Count)
}
It "Does not delete files that do not match the backup file naming pattern for multiple devices" {
$RetentionPolicyCount = 5 # Assuming some files are eligible for deletion
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $true -Devices $Devices -timestampFormat $timestampFormat
$RemainingChallengingFiles = Get-ChildItem -Path $BackupDirectory | Where-Object { $challengingFilenames -contains $_.Name }
$RemainingChallengingFileCount = $RemainingChallengingFiles.Count
$RemainingChallengingFileCount | Should Be $challengingFilenames.Count
}
It "Correctly retains the specified number of the most recent incremental backups for multiple devices" {
$RetentionPolicyCount = 5
$Incremental = $true
$extension = '.trn'
$timestampFormat = "yyyy-MM-dd-HHmmss"
$devicePattern = if ($Devices -gt 1) { "(_\d+)" } else { "" }
$dateRegex = $timestampFormat -replace "yyyy", "\d{4}" -replace "MM", "\d{2}" -replace "dd", "\d{2}" -replace "HH", "\d{2}" -replace "mm", "\d{2}" -replace "ss", "\d{2}"
$regexPattern = "^${DatabaseName}_${dateRegex}${devicePattern}${extension}$"
ApplyRetentionPolicy -BackupDirectory $BackupDirectory -dbName $DatabaseName -RetentionPolicyCount $RetentionPolicyCount -Incremental $Incremental -Devices $Devices -timestampFormat $timestampFormat
$retainedFiles = Get-ChildItem -Path $BackupDirectory -Filter "*$extension" | Where-Object { $_.Name -match $regexPattern }
$totalExpectedRetainedFiles = $RetentionPolicyCount * $Devices
$retainedFiles.Count | Should Be $totalExpectedRetainedFiles
}
}
}