Skip to content

Commit 4a23f30

Browse files
robertmutuhaihe
authored andcommitted
feat(testing): Improve multi-DB compatibility in unit tests
Refactor the unit test framework to properly support running tests against different database types (GPDB and CBDB) and versions. - Introduce `TEST_DB_TYPE` and rename `TEST_GPDB_VERSION` to `TEST_DB_VERSION` in the Makefile for clearer test configuration. - Update test utilities to use these new environment variables to set up the correct database context for each test run. - Dynamically generate test data in `restore/wrappers_test.go` to prevent version compatibility panics when running the full test suite. This makes our testing more robust and easier to maintain. Fixes #57
1 parent 536e385 commit 4a23f30

File tree

3 files changed

+96
-67
lines changed

3 files changed

+96
-67
lines changed

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ lint : $(GOLANG_LINTER)
6363
golangci-lint run --tests=false
6464

6565
unit : $(GINKGO)
66-
ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
66+
TEST_DB_TYPE=CBDB TEST_DB_VERSION=2.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
6767

6868
unit_all_gpdb_versions : $(GINKGO)
69-
TEST_GPDB_VERSION=5.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
70-
TEST_GPDB_VERSION=6.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
71-
TEST_GPDB_VERSION=7.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1 # GPDB main
69+
TEST_DB_TYPE=CBDB TEST_DB_VERSION=2.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
70+
TEST_DB_TYPE=GPDB TEST_DB_VERSION=5.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
71+
TEST_DB_TYPE=GPDB TEST_DB_VERSION=6.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1
72+
TEST_DB_TYPE=GPDB TEST_DB_VERSION=7.999.0 ginkgo $(GINKGO_FLAGS) $(SUBDIRS_HAS_UNIT) 2>&1 # GPDB main
7273

7374
integration : $(GINKGO)
7475
ginkgo $(GINKGO_FLAGS) integration 2>&1

restore/wrappers_test.go

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package restore_test
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"path/filepath"
@@ -135,64 +136,82 @@ options:
135136
pgport: 1234
136137
`
137138

138-
sampleBackupHistConfig1 := history.BackupConfig{
139-
BackupDir: "",
140-
BackupVersion: "1.11.0+dev.28.g10571fdxs",
141-
Compressed: false,
142-
DatabaseName: "plugin_test_db",
143-
DatabaseVersion: "4.3.99.0+dev.18.gb29642fb22 build dev",
144-
DataOnly: false,
145-
DateDeleted: "",
146-
ExcludeRelations: make([]string, 0),
147-
ExcludeSchemaFiltered: false,
148-
ExcludeSchemas: make([]string, 0),
149-
ExcludeTableFiltered: false,
150-
IncludeRelations: make([]string, 0),
151-
IncludeSchemaFiltered: false,
152-
IncludeSchemas: make([]string, 0),
153-
IncludeTableFiltered: false,
154-
Incremental: false,
155-
LeafPartitionData: false,
156-
MetadataOnly: false,
157-
Plugin: "/Users/pivotal/workspace/gp-backup-ddboost-plugin/gpbackup_ddboost_plugin",
158-
RestorePlan: []history.RestorePlanEntry{{Timestamp: "20170415154408", TableFQNs: []string{"public.test_table"}}},
159-
SingleDataFile: false,
160-
Timestamp: "20170415154408",
161-
WithStatistics: false,
162-
}
163-
sampleBackupHistConfig2 := history.BackupConfig{
164-
BackupDir: "",
165-
BackupVersion: "1.11.0+dev.28.g10571fd",
166-
Compressed: false,
167-
DatabaseName: "plugin_test_db",
168-
DatabaseVersion: "4.3.99.0+dev.18.gb29642fb22 build dev",
169-
DataOnly: false,
170-
DateDeleted: "",
171-
ExcludeRelations: make([]string, 0),
172-
ExcludeSchemaFiltered: false,
173-
ExcludeSchemas: make([]string, 0),
174-
ExcludeTableFiltered: false,
175-
IncludeRelations: make([]string, 0),
176-
IncludeSchemaFiltered: false,
177-
IncludeSchemas: make([]string, 0),
178-
IncludeTableFiltered: false,
179-
Incremental: false,
180-
LeafPartitionData: false,
181-
MetadataOnly: false,
182-
Plugin: "/Users/pivotal/workspace/gp-backup-ddboost-plugin/gpbackup_ddboost_plugin",
183-
PluginVersion: "99.99.9999",
184-
RestorePlan: []history.RestorePlanEntry{{Timestamp: "20180415154238", TableFQNs: []string{"public.test_table"}}},
185-
SingleDataFile: true,
186-
Timestamp: "20180415154238",
187-
WithStatistics: false,
188-
}
139+
var sampleBackupHistConfig1 history.BackupConfig
140+
var sampleBackupHistConfig2 history.BackupConfig
141+
var sampleBackupConfig string
142+
var executor testhelper.TestExecutor
143+
var testConfigPath = "/tmp/unit_test_plugin_config.yml"
144+
var oldWd string
145+
var mdd string
146+
var tempDir string
189147

190-
sampleBackupConfig := `
148+
BeforeEach(func() {
149+
// Set up the backup history configurations.
150+
// The DatabaseVersion is set dynamically based on the current test database type.
151+
var dbVersion string
152+
if connectionPool.Version.IsCBDB() {
153+
dbVersion = "2.0.0.0+dev.18.gb29642fb22 build dev"
154+
} else {
155+
dbVersion = "4.3.99.0+dev.18.gb29642fb22 build dev"
156+
}
157+
sampleBackupHistConfig1 = history.BackupConfig{
158+
BackupDir: "",
159+
BackupVersion: "1.11.0+dev.28.g10571fdxs",
160+
Compressed: false,
161+
DatabaseName: "plugin_test_db",
162+
DatabaseVersion: dbVersion,
163+
DataOnly: false,
164+
DateDeleted: "",
165+
ExcludeRelations: make([]string, 0),
166+
ExcludeSchemaFiltered: false,
167+
ExcludeSchemas: make([]string, 0),
168+
ExcludeTableFiltered: false,
169+
IncludeRelations: make([]string, 0),
170+
IncludeSchemaFiltered: false,
171+
IncludeSchemas: make([]string, 0),
172+
IncludeTableFiltered: false,
173+
Incremental: false,
174+
LeafPartitionData: false,
175+
MetadataOnly: false,
176+
Plugin: "/Users/pivotal/workspace/gp-backup-ddboost-plugin/gpbackup_ddboost_plugin",
177+
RestorePlan: []history.RestorePlanEntry{{Timestamp: "20170415154408", TableFQNs: []string{"public.test_table"}}},
178+
SingleDataFile: false,
179+
Timestamp: "20170415154408",
180+
WithStatistics: false,
181+
}
182+
sampleBackupHistConfig2 = history.BackupConfig{
183+
BackupDir: "",
184+
BackupVersion: "1.11.0+dev.28.g10571fd",
185+
Compressed: false,
186+
DatabaseName: "plugin_test_db",
187+
DatabaseVersion: dbVersion,
188+
DataOnly: false,
189+
DateDeleted: "",
190+
ExcludeRelations: make([]string, 0),
191+
ExcludeSchemaFiltered: false,
192+
ExcludeSchemas: make([]string, 0),
193+
ExcludeTableFiltered: false,
194+
IncludeRelations: make([]string, 0),
195+
IncludeSchemaFiltered: false,
196+
IncludeSchemas: make([]string, 0),
197+
IncludeTableFiltered: false,
198+
Incremental: false,
199+
LeafPartitionData: false,
200+
MetadataOnly: false,
201+
Plugin: "/Users/pivotal/workspace/gp-backup-ddboost-plugin/gpbackup_ddboost_plugin",
202+
PluginVersion: "99.99.9999",
203+
RestorePlan: []history.RestorePlanEntry{{Timestamp: "20180415154238", TableFQNs: []string{"public.test_table"}}},
204+
SingleDataFile: true,
205+
Timestamp: "20180415154238",
206+
WithStatistics: false,
207+
}
208+
209+
sampleBackupConfig = fmt.Sprintf(`
191210
backupdir: ""
192211
backupversion: 1.11.0+dev.28.g10571fd
193212
compressed: false
194213
databasename: plugin_test_db
195-
databaseversion: 4.3.99.0+dev.18.gb29642fb22 build dev
214+
databaseversion: %s
196215
dataonly: false
197216
deleted: false
198217
excluderelations: []
@@ -215,14 +234,8 @@ tablefqns:
215234
singledatafile: true
216235
timestamp: "20180415154238"
217236
withstatistics: false
218-
`
219-
var executor testhelper.TestExecutor
220-
var testConfigPath = "/tmp/unit_test_plugin_config.yml"
221-
var oldWd string
222-
var mdd string
223-
var tempDir string
237+
`, dbVersion)
224238

225-
BeforeEach(func() {
226239
tempDir, _ = ioutil.TempDir("", "temp")
227240

228241
err := ioutil.WriteFile(testConfigPath, []byte(sampleConfigContents), 0777)

testutils/functions.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,24 @@ func SetupTestEnvironment() (*dbconn.DBConn, sqlmock.Sqlmock, *Buffer, *Buffer,
3636
connectionPool, mock, testStdout, testStderr, testLogfile := testhelper.SetupTestEnvironment()
3737

3838
// Default if not set is GPDB version `5.1.0`
39-
envTestGpdbVersion := os.Getenv("TEST_GPDB_VERSION")
40-
if envTestGpdbVersion != "" {
41-
testhelper.SetDBVersion(connectionPool, envTestGpdbVersion)
39+
envTestDbVersion := os.Getenv("TEST_DB_VERSION")
40+
if envTestDbVersion != "" {
41+
testhelper.SetDBVersion(connectionPool, envTestDbVersion)
42+
}
43+
44+
// The testhelper package from the cloudberry-go-libs library does not
45+
// currently have a function to set the DB type. To avoid modifying the
46+
// library, we set the type directly here.
47+
envTestDbType := os.Getenv("TEST_DB_TYPE")
48+
if envTestDbType != "" {
49+
switch envTestDbType {
50+
case "GPDB":
51+
connectionPool.Version.Type = dbconn.GPDB
52+
case "CBDB":
53+
connectionPool.Version.Type = dbconn.CBDB
54+
default:
55+
Fail(fmt.Sprintf("Unsupported TEST_DB_TYPE: %s. Must be GPDB or CBDB.", envTestDbType))
56+
}
4257
}
4358

4459
SetupTestCluster()

0 commit comments

Comments
 (0)