Skip to content

Commit 4648d84

Browse files
robertmumy-ship-it
authored andcommitted
fix(test): Prevent panic in AfterSuite when BeforeSuite fails
If the BeforeSuite hook fails before the database connection is initialized, the `connectionPool` variable remains nil. A common cause is a misconfigured test environment, such as when `greenplum_path.sh` is not sourced, leading to a "command not found" error for `createdb`. The previous AfterSuite logic would then attempt to dereference this nil pointer, causing a panic. ``` Running Suite: database query tests - /home/cbdb/Projects/gpbackup/integration ============================================================================== Random Seed: 1760521199 - will randomize all specs Will run 549 of 552 specs ------------------------------ [BeforeSuite] [FAILED] [0.003 seconds] [BeforeSuite] /home/cbdb/Projects/gpbackup/integration/integration_suite_test.go:54 [FAILED] Cannot create database testdb; is GPDB running? In [BeforeSuite] at: /home/cbdb/Projects/gpbackup/integration/integration_suite_test.go:58 @ 10/15/25 17:40:06.839 ------------------------------ [AfterSuite] [PANICKED] [0.000 seconds] [AfterSuite] /home/cbdb/Projects/gpbackup/integration/integration_suite_test.go:127 [PANICKED] Test Panicked In [AfterSuite] at: /usr/lib/go-1.23/src/runtime/panic.go:262 @ 10/15/25 17:40:06.839 runtime error: invalid memory address or nil pointer dereference Full Stack Trace github.com/apache/cloudberry-backup/integration.init.func10() /home/cbdb/Projects/gpbackup/integration/integration_suite_test.go:129 +0x26 ------------------------------ Summarizing 2 Failures: [FAIL] [BeforeSuite] /home/cbdb/Projects/gpbackup/integration/integration_suite_test.go:58 [PANICKED!] [AfterSuite] /usr/lib/go-1.23/src/runtime/panic.go:262 Ran 0 of 552 Specs in 0.023 seconds FAIL! -- A BeforeSuite node failed so all tests were skipped. --- FAIL: TestQueries (0.03s) FAIL Ginkgo ran 1 suite in 7.163251311s ```
1 parent aec11cc commit 4648d84

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

integration/integration_suite_test.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,28 +126,39 @@ var _ = BeforeEach(func() {
126126

127127
var _ = AfterSuite(func() {
128128
CleanupBuildArtifacts()
129-
if connectionPool.Version.IsGPDB() && connectionPool.Version.Before("6") {
130-
testutils.DestroyTestFilespace(connectionPool)
131-
} else {
132-
remoteOutput := testCluster.GenerateAndExecuteCommand(
133-
"Removing /tmp/test_dir* directories on all hosts",
134-
cluster.ON_HOSTS|cluster.INCLUDE_COORDINATOR,
135-
func(contentID int) string {
136-
return fmt.Sprintf("rm -rf /tmp/test_dir*")
137-
})
138-
if remoteOutput.NumErrors != 0 {
139-
Fail("Could not remove /tmp/testdir* directories on 1 or more hosts")
140-
}
141-
}
129+
130+
// connectionPool can be nil if BeforeSuite fails before the connection is established.
131+
// This can happen if the test environment is not set up correctly, for example if
132+
// greenplum_path.sh was not sourced and the `createdb` command cannot be found.
133+
// A nil check is necessary to prevent a panic during cleanup.
142134
if connectionPool != nil {
135+
if connectionPool.Version.IsGPDB() && connectionPool.Version.Before("6") {
136+
testutils.DestroyTestFilespace(connectionPool)
137+
} else {
138+
remoteOutput := testCluster.GenerateAndExecuteCommand(
139+
"Removing /tmp/test_dir* directories on all hosts",
140+
cluster.ON_HOSTS|cluster.INCLUDE_COORDINATOR,
141+
func(contentID int) string {
142+
return fmt.Sprintf("rm -rf /tmp/test_dir*")
143+
})
144+
if remoteOutput.NumErrors != 0 {
145+
Fail("Could not remove /tmp/testdir* directories on 1 or more hosts")
146+
}
147+
}
148+
143149
connectionPool.Close()
144150
err := exec.Command("dropdb", "testdb").Run()
145151
Expect(err).To(BeNil())
152+
153+
// The following cleanup also depends on a successful BeforeSuite.
154+
// It creates a new connection which requires the logger to be initialized,
155+
// and it drops roles that would only have been created if setup succeeded.
156+
template1Conn := testutils.SetupTestDbConn("template1")
157+
testhelper.AssertQueryRuns(template1Conn, "DROP ROLE testrole")
158+
testhelper.AssertQueryRuns(template1Conn, "DROP ROLE anothertestrole")
159+
template1Conn.Close()
146160
}
147-
connection1 := testutils.SetupTestDbConn("template1")
148-
testhelper.AssertQueryRuns(connection1, "DROP ROLE testrole")
149-
testhelper.AssertQueryRuns(connection1, "DROP ROLE anothertestrole")
150-
connection1.Close()
161+
151162
_ = os.RemoveAll("/tmp/helper_test")
152163
_ = os.RemoveAll(examplePluginTestDir)
153164
})

0 commit comments

Comments
 (0)