Skip to content

Commit 64155b8

Browse files
committed
Enhance error messaging in test functions for better clarity and debugging.
1 parent 16e54fb commit 64155b8

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

src/image.tests.ps1

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function Test-Port([string]$ContainerName, [int]$Port) {
6262

6363
# Asserts that InputValue contains at least one occurence of Pattern.
6464
# If -ReturnMatchPosition is informed, return the match value for that pattern position.
65-
function Contains([Parameter(ValueFromPipeline)]$InputValue, [string[]]$Pattern, [int]$ReturnMatchPosition) {
65+
function Contains([Parameter(ValueFromPipeline)]$InputValue, [string[]]$Pattern, [int]$ReturnMatchPosition, [string]$ErrorMessage) {
6666
process {
6767
if ($hasMatch) { return; }
6868
$_matches = $InputValue | Select-String -Pattern $Pattern
@@ -74,37 +74,49 @@ function Contains([Parameter(ValueFromPipeline)]$InputValue, [string[]]$Pattern,
7474
}
7575

7676
end {
77-
assert $hasMatch
77+
if ([string]::IsNullOrEmpty($ErrorMessage)) {
78+
$ErrorMessage = "InputValue does not contain the specified Pattern."
79+
}
80+
assert $hasMatch $ErrorMessage
7881
return $result
7982
}
8083
}
8184

8285
# Asserts that InputValue contains exactly ExpectedCount occurences of Pattern.
83-
function ContainsExactly([Parameter(ValueFromPipeline)]$InputValue, [string[]]$Pattern, [int]$ExpectedCount) {
86+
function ContainsExactly([Parameter(ValueFromPipeline)]$InputValue, [string[]]$Pattern, [int]$ExpectedCount, [string]$ErrorMessage) {
8487
process {
8588
$_matches = $InputValue | Select-String -Pattern $Pattern
8689
$totalMatches += $_matches.Count
8790
}
8891

8992
end {
90-
assert ($totalMatches -eq $ExpectedCount)
93+
if ([string]::IsNullOrEmpty($ErrorMessage)) {
94+
$ErrorMessage = "InputValue does not contain exactly $ExpectedCount occurrences of Pattern."
95+
}
96+
assert ($totalMatches -eq $ExpectedCount) $ErrorMessage
9197
}
9298
}
9399

94100
# Asserts that LastExitCode is equal to ExpectedValue.
95-
function ExitCodeIs ([Parameter(ValueFromPipeline)]$InputValue, [int]$ExpectedValue) {
101+
function ExitCodeIs ([Parameter(ValueFromPipeline)]$InputValue, [int]$ExpectedValue, [string]$ErrorMessage) {
96102
process { }
97103
end {
98-
assert ($LastExitCode -eq $ExpectedValue)
104+
if ([string]::IsNullOrEmpty($ErrorMessage)) {
105+
$ErrorMessage = "ExitCode = $InputValue, expected = $ExpectedValue."
106+
}
107+
assert ($LastExitCode -eq $ExpectedValue) $ErrorMessage
99108
}
100109
}
101110

102111
# Asserts that the difference between two DateTime values are under a given tolerance.
103-
function IsAdjacent ([Parameter(ValueFromPipeline)][datetime]$InputValue, [datetime]$ExpectedValue, [timespan]$Tolerance=[timespan]::FromSeconds(2)) {
112+
function IsAdjacent ([Parameter(ValueFromPipeline)][datetime]$InputValue, [datetime]$ExpectedValue, [timespan]$Tolerance=[timespan]::FromSeconds(1), [string]$ErrorMessage) {
104113
process { }
105114
end {
106115
$difference = $InputValue - $ExpectedValue
107-
assert ($difference.Duration() -lt $Tolerance)
116+
if ([string]::IsNullOrEmpty($ErrorMessage)) {
117+
$ErrorMessage = "The difference between $InputValue and $ExpectedValue is larger than the expected tolerance ($Tolerance)."
118+
}
119+
assert ($difference.Duration() -lt $Tolerance) $ErrorMessage
108120
}
109121
}
110122

@@ -122,7 +134,7 @@ function New-TemporaryDirectory {
122134

123135
task With_command_should_not_start_Firebird {
124136
Invoke-Container -ImageParameters 'ps', '-A' |
125-
ContainsExactly -Pattern 'firebird|fbguard' -ExpectedCount 0
137+
ContainsExactly -Pattern 'firebird|fbguard' -ExpectedCount 0 -ErrorMessage "Firebird processes should not be running when a command is specified."
126138
}
127139

128140
task Without_command_should_start_Firebird {
@@ -131,18 +143,18 @@ task Without_command_should_start_Firebird {
131143

132144
# Both firebird and fbguard must be running
133145
docker exec $cId ps -A |
134-
ContainsExactly -Pattern 'firebird|fbguard' -ExpectedCount 2
146+
ContainsExactly -Pattern 'firebird|fbguard' -ExpectedCount 2 -ErrorMessage "Expected 'firebird' and 'fbguard' processes to be running."
135147

136148
# "Starting" but no "Stopping"
137149
docker logs $cId |
138-
ContainsExactly -Pattern 'Starting Firebird|Stopping Firebird' -ExpectedCount 1
150+
ContainsExactly -Pattern 'Starting Firebird|Stopping Firebird' -ExpectedCount 1 -ErrorMessage "Expected 'Starting Firebird' log entry."
139151

140152
# Stop
141153
docker stop $cId > $null
142154

143155
# "Starting" and "Stopping"
144156
docker logs $cId |
145-
ContainsExactly -Pattern 'Starting Firebird|Stopping Firebird' -ExpectedCount 2
157+
ContainsExactly -Pattern 'Starting Firebird|Stopping Firebird' -ExpectedCount 2 -ErrorMessage "Expected both 'Starting Firebird' and 'Stopping Firebird' log entries after container stop."
146158
}
147159
}
148160

@@ -151,10 +163,10 @@ task FIREBIRD_DATABASE_can_create_database {
151163
param($cId)
152164

153165
docker exec $cId test -f /var/lib/firebird/data/test.fdb |
154-
ExitCodeIs -ExpectedValue 0
166+
ExitCodeIs -ExpectedValue 0 -ErrorMessage "Expected database file '/var/lib/firebird/data/test.fdb' to exist."
155167

156168
docker logs $cId |
157-
Contains -Pattern "Creating database '/var/lib/firebird/data/test.fdb'"
169+
Contains -Pattern "Creating database '/var/lib/firebird/data/test.fdb'" -ErrorMessage "Expected log message indicating creation of database '/var/lib/firebird/data/test.fdb'."
158170
}
159171
}
160172

@@ -163,10 +175,10 @@ task FIREBIRD_DATABASE_can_create_database_with_absolute_path {
163175
param($cId)
164176

165177
docker exec $cId test -f /tmp/test.fdb |
166-
ExitCodeIs -ExpectedValue 0
178+
ExitCodeIs -ExpectedValue 0 -ErrorMessage "Expected database file '/tmp/test.fdb' to exist when absolute path is used."
167179

168180
docker logs $cId |
169-
Contains -Pattern "Creating database '/tmp/test.fdb'"
181+
Contains -Pattern "Creating database '/tmp/test.fdb'" -ErrorMessage "Expected log message indicating creation of database '/tmp/test.fdb' with absolute path."
170182
}
171183
}
172184

@@ -176,15 +188,15 @@ task FIREBIRD_DATABASE_PAGE_SIZE_can_set_page_size_on_database_creation {
176188

177189
'SET LIST ON; SELECT mon$page_size FROM mon$database;' |
178190
docker exec -i $cId isql -b -q /var/lib/firebird/data/test.fdb |
179-
Contains -Pattern 'MON\$PAGE_SIZE(\s+)4096'
191+
Contains -Pattern 'MON\$PAGE_SIZE(\s+)4096' -ErrorMessage "Expected database page size to be 4096."
180192
}
181193

182194
Use-Container -Parameters '-e', 'FIREBIRD_DATABASE=test.fdb', '-e', 'FIREBIRD_DATABASE_PAGE_SIZE=16384' {
183195
param($cId)
184196

185197
'SET LIST ON; SELECT mon$page_size FROM mon$database;' |
186198
docker exec -i $cId isql -b -q /var/lib/firebird/data/test.fdb |
187-
Contains -Pattern 'MON\$PAGE_SIZE(\s+)16384'
199+
Contains -Pattern 'MON\$PAGE_SIZE(\s+)16384' -ErrorMessage "Expected database page size to be 16384."
188200
}
189201
}
190202

@@ -194,24 +206,24 @@ task FIREBIRD_DATABASE_DEFAULT_CHARSET_can_set_default_charset_on_database_creat
194206

195207
'SET LIST ON; SELECT rdb$character_set_name FROM rdb$database;' |
196208
docker exec -i $cId isql -b -q /var/lib/firebird/data/test.fdb |
197-
Contains -Pattern 'RDB\$CHARACTER_SET_NAME(\s+)NONE'
209+
Contains -Pattern 'RDB\$CHARACTER_SET_NAME(\s+)NONE' -ErrorMessage "Expected default database charset to be NONE."
198210
}
199211

200212
Use-Container -Parameters '-e', 'FIREBIRD_DATABASE=test.fdb', '-e', 'FIREBIRD_DATABASE_DEFAULT_CHARSET=UTF8' {
201213
param($cId)
202214

203215
'SET LIST ON; SELECT rdb$character_set_name FROM rdb$database;' |
204216
docker exec -i $cId isql -b -q /var/lib/firebird/data/test.fdb |
205-
Contains -Pattern 'RDB\$CHARACTER_SET_NAME(\s+)UTF8'
217+
Contains -Pattern 'RDB\$CHARACTER_SET_NAME(\s+)UTF8' -ErrorMessage "Expected default database charset to be UTF8."
206218
}
207219
}
208220

209221
task FIREBIRD_USER_fails_without_password {
210222
# Captures both stdout and stderr
211223
$($stdout = Invoke-Container -DockerParameters '-e', 'FIREBIRD_USER=alice') 2>&1 |
212-
Contains -Pattern 'FIREBIRD_PASSWORD variable is not set.' # stderr
224+
Contains -Pattern 'FIREBIRD_PASSWORD variable is not set.' -ErrorMessage "Expected error message 'FIREBIRD_PASSWORD variable is not set.' when FIREBIRD_USER is set without FIREBIRD_PASSWORD." # stderr
213225

214-
assert ($stdout -eq $null)
226+
assert ($stdout -eq $null) "Expected stdout to be null when FIREBIRD_USER is set without FIREBIRD_PASSWORD."
215227
}
216228

217229
task FIREBIRD_USER_can_create_user {
@@ -224,19 +236,19 @@ task FIREBIRD_USER_can_create_user {
224236
# Correct password
225237
'SELECT 1 FROM rdb$database;' |
226238
docker exec -i $cId isql -b -q -u alice -p bird inet:///var/lib/firebird/data/test.fdb |
227-
ExitCodeIs -ExpectedValue 0
239+
ExitCodeIs -ExpectedValue 0 -ErrorMessage "Expected successful login with correct password for user 'alice'."
228240

229241
# Incorrect password
230242
'SELECT 1 FROM rdb$database;' |
231243
docker exec -i $cId isql -b -q -u alice -p tiger inet:///var/lib/firebird/data/test.fdb 2>&1 |
232-
ExitCodeIs -ExpectedValue 1
244+
ExitCodeIs -ExpectedValue 1 -ErrorMessage "Expected failed login with incorrect password for user 'alice'."
233245

234246
# File /opt/firebird/SYSDBA.password exists?
235247
docker exec $cId test -f /opt/firebird/SYSDBA.password |
236-
ExitCodeIs -ExpectedValue 0
248+
ExitCodeIs -ExpectedValue 0 -ErrorMessage "Expected SYSDBA.password file to exist when a new user is created."
237249

238250
docker logs $cId |
239-
Contains -Pattern "Creating user 'alice'"
251+
Contains -Pattern "Creating user 'alice'" -ErrorMessage "Expected log message indicating creation of user 'alice'."
240252
}
241253
}
242254

@@ -247,19 +259,19 @@ task FIREBIRD_ROOT_PASSWORD_can_change_sysdba_password {
247259
# Correct password
248260
'SELECT 1 FROM rdb$database;' |
249261
docker exec -i $cId isql -b -q -u SYSDBA -p passw0rd inet:///var/lib/firebird/data/test.fdb |
250-
ExitCodeIs -ExpectedValue 0
262+
ExitCodeIs -ExpectedValue 0 -ErrorMessage "Expected successful login with new SYSDBA password."
251263

252264
# Incorrect password
253265
'SELECT 1 FROM rdb$database;' |
254266
docker exec -i $cId isql -b -q -u SYSDBA -p tiger inet:///var/lib/firebird/data/test.fdb 2>&1 |
255-
ExitCodeIs -ExpectedValue 1
267+
ExitCodeIs -ExpectedValue 1 -ErrorMessage "Expected failed login with incorrect (old) SYSDBA password."
256268

257269
# File /opt/firebird/SYSDBA.password removed?
258270
docker exec $cId test -f /opt/firebird/SYSDBA.password |
259-
ExitCodeIs -ExpectedValue 1
271+
ExitCodeIs -ExpectedValue 1 -ErrorMessage "Expected SYSDBA.password file to be removed after changing SYSDBA password."
260272

261273
docker logs $cId |
262-
Contains -Pattern 'Changing SYSDBA password'
274+
Contains -Pattern 'Changing SYSDBA password' -ErrorMessage "Expected log message indicating SYSDBA password change."
263275
}
264276
}
265277

@@ -268,10 +280,10 @@ task FIREBIRD_USE_LEGACY_AUTH_enables_legacy_auth {
268280
param($cId)
269281

270282
$logs = docker logs $cId
271-
$logs | Contains -Pattern "Using Legacy_Auth"
272-
$logs | Contains -Pattern "AuthServer = Legacy_Auth"
273-
$logs | Contains -Pattern "AuthClient = Legacy_Auth"
274-
$logs | Contains -Pattern "WireCrypt = Enabled"
283+
$logs | Contains -Pattern "Using Legacy_Auth" -ErrorMessage "Expected log message 'Using Legacy_Auth'."
284+
$logs | Contains -Pattern "AuthServer = Legacy_Auth" -ErrorMessage "Expected log message 'AuthServer = Legacy_Auth'."
285+
$logs | Contains -Pattern "AuthClient = Legacy_Auth" -ErrorMessage "Expected log message 'AuthClient = Legacy_Auth'."
286+
$logs | Contains -Pattern "WireCrypt = Enabled" -ErrorMessage "Expected log message 'WireCrypt = Enabled' when Legacy_Auth is used."
275287
}
276288
}
277289

@@ -280,8 +292,8 @@ task FIREBIRD_CONF_can_change_any_setting {
280292
param($cId)
281293

282294
$logs = docker logs $cId
283-
$logs | Contains -Pattern "DefaultDbCachePages = 64K"
284-
$logs | Contains -Pattern "FileSystemCacheThreshold = 100M"
295+
$logs | Contains -Pattern "DefaultDbCachePages = 64K" -ErrorMessage "Expected log message 'DefaultDbCachePages = 64K'."
296+
$logs | Contains -Pattern "FileSystemCacheThreshold = 100M" -ErrorMessage "Expected log message 'FileSystemCacheThreshold = 100M'."
285297
}
286298
}
287299

@@ -290,14 +302,14 @@ task FIREBIRD_CONF_key_is_case_sensitive {
290302
param($cId)
291303

292304
$logs = docker logs $cId
293-
$logs | Contains -Pattern "WireCrypt = Disabled"
305+
$logs | Contains -Pattern "WireCrypt = Disabled" -ErrorMessage "Expected log message 'WireCrypt = Disabled' when using correct case."
294306
}
295307

296308
Use-Container -Parameters '-e', 'FIREBIRD_CONF_WIRECRYPT=Disabled' {
297309
param($cId)
298310

299311
$logs = docker logs $cId
300-
$logs | ContainsExactly -Pattern "WireCrypt = Disabled" -ExpectedCount 0
312+
$logs | ContainsExactly -Pattern "WireCrypt = Disabled" -ExpectedCount 0 -ErrorMessage "Expected no log message 'WireCrypt = Disabled' when using incorrect case (WIRECRYPT)."
301313
}
302314
}
303315

@@ -326,12 +338,12 @@ task Can_init_db_with_scripts {
326338
param($cId)
327339

328340
$logs = docker logs $cId
329-
$logs | Contains -Pattern "10-create-table.sql"
330-
$logs | Contains -Pattern "20-insert-data.sql"
341+
$logs | Contains -Pattern "10-create-table.sql" -ErrorMessage "Expected log message for '10-create-table.sql' execution."
342+
$logs | Contains -Pattern "20-insert-data.sql" -ErrorMessage "Expected log message for '20-insert-data.sql' execution."
331343

332344
'SET LIST ON; SELECT count(*) AS country_count FROM country;' |
333345
docker exec -i $cId isql -b -q /var/lib/firebird/data/test.fdb |
334-
Contains -Pattern 'COUNTRY_COUNT(\s+)5'
346+
Contains -Pattern 'COUNTRY_COUNT(\s+)5' -ErrorMessage "Expected country count to be 5 after init scripts."
335347
}
336348
}
337349
finally {

0 commit comments

Comments
 (0)