Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: parallel test must not use the loop var in the closure (#3441)

Yilong Li55a3907d2661
diff --git a/api/project_test.go b/api/project_test.go
index 820c5c6e4c..ebe27599b3 100644
--- a/api/project_test.go
+++ b/api/project_test.go
@@ -102,6 +102,8 @@ func TestValidateRepositoryFilePathTemplate(t *testing.T) {
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
err := ValidateRepositoryFilePathTemplate(test.template, test.tenantMode, test.dbNameTemplate)
if test.errPart == "" {
diff --git a/plugin/db/driver_test.go b/plugin/db/driver_test.go
index fc36c952b9..641e6db27e 100644
--- a/plugin/db/driver_test.go
+++ b/plugin/db/driver_test.go
@@ -309,6 +309,8 @@ func TestParseSchemaFileInfo(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
got, err := ParseSchemaFileInfo(test.baseDirectory, test.schemaPathTemplate, test.file)
require.NoError(t, err)
diff --git a/plugin/db/mysql/binlog_stream_parser_test.go b/plugin/db/mysql/binlog_stream_parser_test.go
index f78a074a6a..8749488e1c 100644
--- a/plugin/db/mysql/binlog_stream_parser_test.go
+++ b/plugin/db/mysql/binlog_stream_parser_test.go
@@ -446,6 +446,8 @@ DELIMITER ;
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
txns, err := ParseBinlogStream(strings.NewReader(test.stream))
@@ -608,6 +610,8 @@ func TestFilterBinlogTransactionsByThreadID(t *testing.T) {
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
_, err := FilterBinlogTransactionsByThreadID(test.txnList, test.threadID)
diff --git a/plugin/db/mysql/rollback_test.go b/plugin/db/mysql/rollback_test.go
index 123dd5e65e..53390cdc68 100644
--- a/plugin/db/mysql/rollback_test.go
+++ b/plugin/db/mysql/rollback_test.go
@@ -218,6 +218,8 @@ SET
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
sql, err := test.txn.GetRollbackSQL(test.tableCatalog)
@@ -258,6 +260,8 @@ CREATE TABLE balance (
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
a := require.New(t)
tableMap, err := GetTableColumns(test.schema)
diff --git a/tests/schema_update_test.go b/tests/schema_update_test.go
index 75e34006e7..87763fd078 100644
--- a/tests/schema_update_test.go
+++ b/tests/schema_update_test.go
@@ -347,6 +347,8 @@ func TestVCS(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
@@ -776,6 +778,8 @@ func TestVCS_SDL(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
@@ -821,7 +825,7 @@ func TestVCS_SDL(t *testing.T) {
a.NoError(err)
// Create a VCS
- vcs, err := ctl.createVCS(
+ apiVCS, err := ctl.createVCS(
api.VCSCreate{
Name: t.Name(),
Type: test.vcsType,
@@ -847,7 +851,7 @@ func TestVCS_SDL(t *testing.T) {
ctl.vcsProvider.CreateRepository(test.externalID)
_, err = ctl.createRepository(
api.RepositoryCreate{
- VCSID: vcs.ID,
+ VCSID: apiVCS.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: test.repositoryFullPath,
@@ -893,7 +897,10 @@ func TestVCS_SDL(t *testing.T) {
schemaFile: schemaFileContent,
})
a.NoError(err)
-
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "1", "2", []vcs.FileDiff{
+ {Path: schemaFile, Type: vcs.FileDiffTypeAdded},
+ })
+ a.NoError(err)
payload, err := json.Marshal(test.newWebhookPushEvent(nil /* added */, []string{schemaFile}, "1", "2"))
a.NoError(err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
@@ -931,7 +938,10 @@ func TestVCS_SDL(t *testing.T) {
dataFile: `INSERT INTO users (id) VALUES (1);`,
})
a.NoError(err)
-
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "2", "3", []vcs.FileDiff{
+ {Path: dataFile, Type: vcs.FileDiffTypeAdded},
+ })
+ a.NoError(err)
payload, err = json.Marshal(test.newWebhookPushEvent([]string{dataFile}, nil /* modified */, "2", "3"))
a.NoError(err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
@@ -1263,6 +1273,8 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
@@ -1313,7 +1325,7 @@ func TestWildcardInVCSFilePathTemplate(t *testing.T) {
FullPath: repoFullPath,
WebURL: fmt.Sprintf("%s/%s", ctl.vcsURL, repoFullPath),
BranchFilter: branchFilter,
- BaseDirectory: baseDirectory,
+ BaseDirectory: test.baseDirectory,
FilePathTemplate: test.filePathTemplate,
SchemaPathTemplate: "{{ENV_NAME}}/.{{DB_NAME}}##LATEST.sql",
ExternalID: externalID,
@@ -1482,7 +1494,11 @@ func TestVCS_SQL_Review(t *testing.T) {
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
+ t.Parallel()
+
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
diff --git a/tests/sheet_vcs_test.go b/tests/sheet_vcs_test.go
index 57b1dfbfa6..581dacbaa9 100644
--- a/tests/sheet_vcs_test.go
+++ b/tests/sheet_vcs_test.go
@@ -36,6 +36,8 @@ func TestSheetVCS(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
diff --git a/tests/tenant_test.go b/tests/tenant_test.go
index 5c50f41e25..8e753690ae 100644
--- a/tests/tenant_test.go
+++ b/tests/tenant_test.go
@@ -213,7 +213,7 @@ func TestTenantVCS(t *testing.T) {
vcsType vcs.Type
externalID string
repositoryFullPath string
- newWebhookPushEvent func(gitFile string) interface{}
+ newWebhookPushEvent func(gitFile, beforeSHA, afterSHA string) interface{}
}{
{
name: "GitLab",
@@ -221,10 +221,12 @@ func TestTenantVCS(t *testing.T) {
vcsType: vcs.GitLabSelfHost,
externalID: "121",
repositoryFullPath: "test/schemaUpdate",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return gitlab.WebhookPushEvent{
ObjectKind: gitlab.WebhookPush,
Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Project: gitlab.WebhookProject{
ID: 121,
},
@@ -243,9 +245,11 @@ func TestTenantVCS(t *testing.T) {
vcsType: vcs.GitHubCom,
externalID: "octocat/Hello-World",
repositoryFullPath: "octocat/Hello-World",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return github.WebhookPushEvent{
- Ref: "refs/heads/feature/foo",
+ Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Repository: github.WebhookRepository{
ID: 211,
FullName: "octocat/Hello-World",
@@ -273,6 +277,8 @@ func TestTenantVCS(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
@@ -291,7 +297,7 @@ func TestTenantVCS(t *testing.T) {
a.NoError(err)
// Create a VCS.
- vcs, err := ctl.createVCS(
+ apiVCS, err := ctl.createVCS(
api.VCSCreate{
Name: t.Name(),
Type: test.vcsType,
@@ -317,7 +323,7 @@ func TestTenantVCS(t *testing.T) {
ctl.vcsProvider.CreateRepository(test.externalID)
_, err = ctl.createRepository(
api.RepositoryCreate{
- VCSID: vcs.ID,
+ VCSID: apiVCS.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: test.repositoryFullPath,
@@ -441,8 +447,11 @@ func TestTenantVCS(t *testing.T) {
gitFile := "bbtest/testTenantVCSSchemaUpdate##ver1##migrate##create_a_test_table.sql"
err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile: migrationStatement})
a.NoError(err)
-
- payload, err := json.Marshal(test.newWebhookPushEvent(gitFile))
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "1", "2", []vcs.FileDiff{
+ {Path: gitFile, Type: vcs.FileDiffTypeAdded},
+ })
+ a.NoError(err)
+ payload, err := json.Marshal(test.newWebhookPushEvent(gitFile, "1", "2"))
a.NoError(err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
a.NoError(err)
@@ -662,7 +671,7 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
vcsType vcs.Type
externalID string
repositoryFullPath string
- newWebhookPushEvent func(gitFile string) interface{}
+ newWebhookPushEvent func(gitFile, beforeSHA, afterSHA string) interface{}
}{
{
name: "GitLab",
@@ -670,10 +679,12 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
vcsType: vcs.GitLabSelfHost,
externalID: "121",
repositoryFullPath: "test/schemaUpdate",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return gitlab.WebhookPushEvent{
ObjectKind: gitlab.WebhookPush,
Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Project: gitlab.WebhookProject{
ID: 121,
},
@@ -692,9 +703,11 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
vcsType: vcs.GitHubCom,
externalID: "octocat/Hello-World",
repositoryFullPath: "octocat/Hello-World",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return github.WebhookPushEvent{
- Ref: "refs/heads/feature/foo",
+ Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Repository: github.WebhookRepository{
ID: 211,
FullName: "octocat/Hello-World",
@@ -722,13 +735,15 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- err := ctl.StartServer(ctx, t.TempDir(), fake.NewGitHub, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, t.TempDir(), test.vcsProviderCreator, getTestPort(t.Name()))
a.NoError(err)
defer func() {
_ = ctl.Close(ctx)
@@ -740,7 +755,7 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
a.NoError(err)
// Create a VCS.
- vcs, err := ctl.createVCS(
+ apiVCS, err := ctl.createVCS(
api.VCSCreate{
Name: t.Name(),
Type: test.vcsType,
@@ -767,7 +782,7 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
ctl.vcsProvider.CreateRepository(test.externalID)
_, err = ctl.createRepository(
api.RepositoryCreate{
- VCSID: vcs.ID,
+ VCSID: apiVCS.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: test.repositoryFullPath,
@@ -896,8 +911,11 @@ func TestTenantVCSDatabaseNameTemplate(t *testing.T) {
gitFile := "bbtest/testTenantVCSSchemaUpdate##ver1##migrate##create_a_test_table.sql"
err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile: migrationStatement})
a.NoError(err)
-
- payload, err := json.Marshal(test.newWebhookPushEvent(gitFile))
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "1", "2", []vcs.FileDiff{
+ {Path: gitFile, Type: vcs.FileDiffTypeAdded},
+ })
+ a.NoError(err)
+ payload, err := json.Marshal(test.newWebhookPushEvent(gitFile, "1", "2"))
a.NoError(err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
a.NoError(err)
@@ -989,7 +1007,7 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
vcsType vcs.Type
externalID string
repositoryFullPath string
- newWebhookPushEvent func(gitFile string) interface{}
+ newWebhookPushEvent func(gitFile, beforeSHA, afterSHA string) interface{}
}{
{
name: "GitLab",
@@ -997,10 +1015,12 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
vcsType: vcs.GitLabSelfHost,
externalID: "121",
repositoryFullPath: "test/schemaUpdate",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return gitlab.WebhookPushEvent{
ObjectKind: gitlab.WebhookPush,
Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Project: gitlab.WebhookProject{
ID: 121,
},
@@ -1019,9 +1039,11 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
vcsType: vcs.GitHubCom,
externalID: "octocat/Hello-World",
repositoryFullPath: "octocat/Hello-World",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return github.WebhookPushEvent{
- Ref: "refs/heads/feature/foo",
+ Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Repository: github.WebhookRepository{
ID: 211,
FullName: "octocat/Hello-World",
@@ -1049,13 +1071,15 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
a := require.New(t)
ctx := context.Background()
ctl := &controller{}
- err := ctl.StartServer(ctx, t.TempDir(), fake.NewGitHub, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, t.TempDir(), test.vcsProviderCreator, getTestPort(t.Name()))
a.NoError(err)
defer func() {
_ = ctl.Close(ctx)
@@ -1067,7 +1091,7 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
a.NoError(err)
// Create a VCS.
- vcs, err := ctl.createVCS(
+ apiVCS, err := ctl.createVCS(
api.VCSCreate{
Name: t.Name(),
Type: test.vcsType,
@@ -1093,7 +1117,7 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
ctl.vcsProvider.CreateRepository(test.externalID)
_, err = ctl.createRepository(
api.RepositoryCreate{
- VCSID: vcs.ID,
+ VCSID: apiVCS.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: test.repositoryFullPath,
@@ -1197,8 +1221,11 @@ func TestTenantVCSDatabaseNameTemplate_Empty(t *testing.T) {
gitFile := baseDirectory + "/ver1##migrate##create_a_test_table.sql"
err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile: migrationStatement})
a.NoError(err)
-
- payload, err := json.Marshal(test.newWebhookPushEvent(gitFile))
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "1", "2", []vcs.FileDiff{
+ {Path: gitFile, Type: vcs.FileDiffTypeAdded},
+ })
+ a.NoError(err)
+ payload, err := json.Marshal(test.newWebhookPushEvent(gitFile, "1", "2"))
a.NoError(err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
a.NoError(err)
@@ -1259,7 +1286,7 @@ func TestTenantVCS_YAML(t *testing.T) {
vcsType vcs.Type
externalID string
repositoryFullPath string
- newWebhookPushEvent func(gitFile string) interface{}
+ newWebhookPushEvent func(gitFile, beforeSHA, afterSHA string) interface{}
}{
{
name: "GitLab",
@@ -1267,10 +1294,12 @@ func TestTenantVCS_YAML(t *testing.T) {
vcsType: vcs.GitLabSelfHost,
externalID: "121",
repositoryFullPath: "test/dataUpdate",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return gitlab.WebhookPushEvent{
ObjectKind: gitlab.WebhookPush,
Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Project: gitlab.WebhookProject{
ID: 121,
},
@@ -1289,9 +1318,11 @@ func TestTenantVCS_YAML(t *testing.T) {
vcsType: vcs.GitHubCom,
externalID: "octocat/Hello-World",
repositoryFullPath: "octocat/Hello-World",
- newWebhookPushEvent: func(gitFile string) interface{} {
+ newWebhookPushEvent: func(gitFile, beforeSHA, afterSHA string) interface{} {
return github.WebhookPushEvent{
- Ref: "refs/heads/feature/foo",
+ Ref: "refs/heads/feature/foo",
+ Before: beforeSHA,
+ After: afterSHA,
Repository: github.WebhookRepository{
ID: 211,
FullName: "octocat/Hello-World",
@@ -1319,12 +1350,14 @@ func TestTenantVCS_YAML(t *testing.T) {
},
}
for _, test := range tests {
+ // Fix the problem that closure in a for loop will always use the last element.
+ // test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
ctl := &controller{}
- err := ctl.StartServer(ctx, t.TempDir(), fake.NewGitHub, getTestPort(t.Name()))
+ err := ctl.StartServer(ctx, t.TempDir(), test.vcsProviderCreator, getTestPort(t.Name()))
require.NoError(t, err)
defer func() {
_ = ctl.Close(ctx)
@@ -1336,7 +1369,7 @@ func TestTenantVCS_YAML(t *testing.T) {
require.NoError(t, err)
// Create a VCS.
- vcs, err := ctl.createVCS(
+ apiVCS, err := ctl.createVCS(
api.VCSCreate{
Name: t.Name(),
Type: test.vcsType,
@@ -1362,7 +1395,7 @@ func TestTenantVCS_YAML(t *testing.T) {
ctl.vcsProvider.CreateRepository(test.externalID)
_, err = ctl.createRepository(
api.RepositoryCreate{
- VCSID: vcs.ID,
+ VCSID: apiVCS.ID,
ProjectID: project.ID,
Name: "Test Repository",
FullPath: test.repositoryFullPath,
@@ -1463,11 +1496,14 @@ func TestTenantVCS_YAML(t *testing.T) {
require.Equal(t, stagingTenantNumber, len(stagingDatabases))
// Simulate Git commits for schema update.
- gitFile := baseDirectory + "/ver1##migrate##create_a_test_table.sql"
- err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile: migrationStatement})
+ gitFile1 := baseDirectory + "/ver1##migrate##create_a_test_table.sql"
+ err = ctl.vcsProvider.AddFiles(test.externalID, map[string]string{gitFile1: migrationStatement})
require.NoError(t, err)
-
- payload, err := json.Marshal(test.newWebhookPushEvent(gitFile))
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "1", "2", []vcs.FileDiff{
+ {Path: gitFile1, Type: vcs.FileDiffTypeAdded},
+ })
+ require.NoError(t, err)
+ payload, err := json.Marshal(test.newWebhookPushEvent(gitFile1, "1", "2"))
require.NoError(t, err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
require.NoError(t, err)
@@ -1487,11 +1523,11 @@ func TestTenantVCS_YAML(t *testing.T) {
require.Equal(t, api.TaskDone, status)
// Simulate Git commits for data update.
- gitFile = baseDirectory + "/ver2##data##insert_a_new_row.yml"
+ gitFile2 := baseDirectory + "/ver2##data##insert_a_new_row.yml"
err = ctl.vcsProvider.AddFiles(
test.externalID,
map[string]string{
- gitFile: fmt.Sprintf(`
+ gitFile2: fmt.Sprintf(`
databases:
- name: %s
statement: |
@@ -1502,8 +1538,11 @@ statement: |
},
)
require.NoError(t, err)
-
- payload, err = json.Marshal(test.newWebhookPushEvent(gitFile))
+ err = ctl.vcsProvider.AddCommitsDiff(test.externalID, "2", "3", []vcs.FileDiff{
+ {Path: gitFile2, Type: vcs.FileDiffTypeAdded},
+ })
+ require.NoError(t, err)
+ payload, err = json.Marshal(test.newWebhookPushEvent(gitFile2, "2", "3"))
require.NoError(t, err)
err = ctl.vcsProvider.SendWebhookPush(test.externalID, payload)
require.NoError(t, err)