diff --git a/database/integration_test.go b/database/integration_test.go index fd4e77eb0..6a2d03970 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -27,6 +27,7 @@ import ( "github.com/go-vela/types/constants" "github.com/go-vela/types/library" "github.com/go-vela/types/raw" + "github.com/google/go-cmp/cmp" ) // Resources represents the object containing test resources. @@ -253,7 +254,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list builds: %v", err) } - if !reflect.DeepEqual(list, resources.Builds) { + if !cmp.Equal(list, resources.Builds) { t.Errorf("ListBuilds() is %v, want %v", list, resources.Builds) } methods["ListBuilds"] = true @@ -266,7 +267,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Builds) { t.Errorf("ListBuildsForDeployment() is %v, want %v", count, len(resources.Builds)) } - if !reflect.DeepEqual(list, []*library.Build{resources.Builds[1], resources.Builds[0]}) { + if !cmp.Equal(list, []*library.Build{resources.Builds[1], resources.Builds[0]}) { t.Errorf("ListBuildsForDeployment() is %v, want %v", list, []*library.Build{resources.Builds[1], resources.Builds[0]}) } methods["ListBuildsForDeployment"] = true @@ -279,7 +280,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Builds) { t.Errorf("ListBuildsForOrg() is %v, want %v", count, len(resources.Builds)) } - if !reflect.DeepEqual(list, resources.Builds) { + if !cmp.Equal(list, resources.Builds) { t.Errorf("ListBuildsForOrg() is %v, want %v", list, resources.Builds) } methods["ListBuildsForOrg"] = true @@ -292,7 +293,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Builds) { t.Errorf("ListBuildsForRepo() is %v, want %v", count, len(resources.Builds)) } - if !reflect.DeepEqual(list, []*library.Build{resources.Builds[1], resources.Builds[0]}) { + if !cmp.Equal(list, []*library.Build{resources.Builds[1], resources.Builds[0]}) { t.Errorf("ListBuildsForRepo() is %v, want %v", list, []*library.Build{resources.Builds[1], resources.Builds[0]}) } methods["ListBuildsForRepo"] = true @@ -302,7 +303,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list pending and running builds: %v", err) } - if !reflect.DeepEqual(queueList, queueBuilds) { + if !cmp.Equal(queueList, queueBuilds) { t.Errorf("ListPendingAndRunningBuilds() is %v, want %v", queueList, queueBuilds) } methods["ListPendingAndRunningBuilds"] = true @@ -312,7 +313,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get last build for repo %d: %v", resources.Repos[0].GetID(), err) } - if !reflect.DeepEqual(got, resources.Builds[1]) { + if !cmp.Equal(got, resources.Builds[1]) { t.Errorf("LastBuildForRepo() is %v, want %v", got, resources.Builds[1]) } methods["LastBuildForRepo"] = true @@ -324,7 +325,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get build %d for repo %d: %v", build.GetID(), repo.GetID(), err) } - if !reflect.DeepEqual(got, build) { + if !cmp.Equal(got, build) { t.Errorf("GetBuildForRepo() is %v, want %v", got, build) } } @@ -353,7 +354,7 @@ func testBuilds(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get build %d by ID: %v", build.GetID(), err) } - if !reflect.DeepEqual(got, build) { + if !cmp.Equal(got, build) { t.Errorf("GetBuild() is %v, want %v", got, build) } } @@ -418,7 +419,7 @@ func testExecutables(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get executable %d for build %d: %v", executable.GetID(), executable.GetBuildID(), err) } - if !reflect.DeepEqual(got, executable) { + if !cmp.Equal(got, executable) { t.Errorf("PopBuildExecutable() is %v, want %v", got, executable) } } @@ -484,7 +485,7 @@ func testHooks(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list hooks: %v", err) } - if !reflect.DeepEqual(list, resources.Hooks) { + if !cmp.Equal(list, resources.Hooks) { t.Errorf("ListHooks() is %v, want %v", list, resources.Hooks) } methods["ListHooks"] = true @@ -498,7 +499,7 @@ func testHooks(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Hooks)-1 { t.Errorf("ListHooksForRepo() is %v, want %v", count, len(resources.Hooks)) } - if !reflect.DeepEqual(list, []*library.Hook{resources.Hooks[1], resources.Hooks[0]}) { + if !cmp.Equal(list, []*library.Hook{resources.Hooks[1], resources.Hooks[0]}) { t.Errorf("ListHooksForRepo() is %v, want %v", list, []*library.Hook{resources.Hooks[1], resources.Hooks[0]}) } methods["ListHooksForRepo"] = true @@ -508,7 +509,7 @@ func testHooks(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get last hook for repo %d: %v", resources.Repos[0].GetID(), err) } - if !reflect.DeepEqual(got, resources.Hooks[1]) { + if !cmp.Equal(got, resources.Hooks[1]) { t.Errorf("LastHookForRepo() is %v, want %v", got, resources.Hooks[1]) } methods["LastHookForRepo"] = true @@ -530,7 +531,7 @@ func testHooks(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get hook %d for repo %d: %v", hook.GetID(), repo.GetID(), err) } - if !reflect.DeepEqual(got, hook) { + if !cmp.Equal(got, hook) { t.Errorf("GetHookForRepo() is %v, want %v", got, hook) } } @@ -549,7 +550,7 @@ func testHooks(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get hook %d by ID: %v", hook.GetID(), err) } - if !reflect.DeepEqual(got, hook) { + if !cmp.Equal(got, hook) { t.Errorf("GetHook() is %v, want %v", got, hook) } } @@ -625,7 +626,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list logs: %v", err) } - if !reflect.DeepEqual(list, resources.Logs) { + if !cmp.Equal(list, resources.Logs) { t.Errorf("ListLogs() is %v, want %v", list, resources.Logs) } methods["ListLogs"] = true @@ -638,7 +639,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Logs) { t.Errorf("ListLogsForBuild() is %v, want %v", count, len(resources.Logs)) } - if !reflect.DeepEqual(list, resources.Logs) { + if !cmp.Equal(list, resources.Logs) { t.Errorf("ListLogsForBuild() is %v, want %v", list, resources.Logs) } methods["ListLogsForBuild"] = true @@ -650,7 +651,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get log %d for service %d: %v", log.GetID(), service.GetID(), err) } - if !reflect.DeepEqual(got, log) { + if !cmp.Equal(got, log) { t.Errorf("GetLogForService() is %v, want %v", got, log) } } @@ -663,7 +664,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get log %d for step %d: %v", log.GetID(), step.GetID(), err) } - if !reflect.DeepEqual(got, log) { + if !cmp.Equal(got, log) { t.Errorf("GetLogForStep() is %v, want %v", got, log) } } @@ -682,7 +683,7 @@ func testLogs(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get log %d by ID: %v", log.GetID(), err) } - if !reflect.DeepEqual(got, log) { + if !cmp.Equal(got, log) { t.Errorf("GetLog() is %v, want %v", got, log) } } @@ -758,7 +759,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list pipelines: %v", err) } - if !reflect.DeepEqual(list, resources.Pipelines) { + if !cmp.Equal(list, resources.Pipelines) { t.Errorf("ListPipelines() is %v, want %v", list, resources.Pipelines) } methods["ListPipelines"] = true @@ -771,7 +772,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Pipelines) { t.Errorf("ListPipelinesForRepo() is %v, want %v", count, len(resources.Pipelines)) } - if !reflect.DeepEqual(list, resources.Pipelines) { + if !cmp.Equal(list, resources.Pipelines) { t.Errorf("ListPipelines() is %v, want %v", list, resources.Pipelines) } methods["ListPipelinesForRepo"] = true @@ -783,7 +784,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get pipeline %d for repo %d: %v", pipeline.GetID(), repo.GetID(), err) } - if !reflect.DeepEqual(got, pipeline) { + if !cmp.Equal(got, pipeline) { t.Errorf("GetPipelineForRepo() is %v, want %v", got, pipeline) } } @@ -802,7 +803,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get pipeline %d by ID: %v", pipeline.GetID(), err) } - if !reflect.DeepEqual(got, pipeline) { + if !cmp.Equal(got, pipeline) { t.Errorf("GetPipeline() is %v, want %v", got, pipeline) } } @@ -888,7 +889,7 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list repos: %v", err) } - if !reflect.DeepEqual(list, resources.Repos) { + if !cmp.Equal(list, resources.Repos) { t.Errorf("ListRepos() is %v, want %v", list, resources.Repos) } methods["ListRepos"] = true @@ -901,7 +902,7 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Repos) { t.Errorf("ListReposForOrg() is %v, want %v", count, len(resources.Repos)) } - if !reflect.DeepEqual(list, resources.Repos) { + if !cmp.Equal(list, resources.Repos) { t.Errorf("ListReposForOrg() is %v, want %v", list, resources.Repos) } methods["ListReposForOrg"] = true @@ -914,7 +915,7 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Repos) { t.Errorf("ListReposForUser() is %v, want %v", count, len(resources.Repos)) } - if !reflect.DeepEqual(list, resources.Repos) { + if !cmp.Equal(list, resources.Repos) { t.Errorf("ListReposForUser() is %v, want %v", list, resources.Repos) } methods["ListReposForUser"] = true @@ -925,7 +926,7 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get repo %d by org: %v", repo.GetID(), err) } - if !reflect.DeepEqual(got, repo) { + if !cmp.Equal(got, repo) { t.Errorf("GetRepoForOrg() is %v, want %v", got, repo) } } @@ -944,7 +945,7 @@ func testRepos(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get repo %d by ID: %v", repo.GetID(), err) } - if !reflect.DeepEqual(got, repo) { + if !cmp.Equal(got, repo) { t.Errorf("GetRepo() is %v, want %v", got, repo) } } @@ -1022,7 +1023,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list schedules: %v", err) } - if !reflect.DeepEqual(list, resources.Schedules) { + if !cmp.Equal(list, resources.Schedules, CmpOptApproxUpdatedAt()) { t.Errorf("ListSchedules() is %v, want %v", list, resources.Schedules) } methods["ListSchedules"] = true @@ -1032,7 +1033,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list schedules: %v", err) } - if !reflect.DeepEqual(list, resources.Schedules) { + if !cmp.Equal(list, resources.Schedules, CmpOptApproxUpdatedAt()) { t.Errorf("ListActiveSchedules() is %v, want %v", list, resources.Schedules) } methods["ListActiveSchedules"] = true @@ -1045,7 +1046,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { if int(count) != len(resources.Schedules) { t.Errorf("ListSchedulesForRepo() is %v, want %v", count, len(resources.Schedules)) } - if !reflect.DeepEqual(list, []*library.Schedule{resources.Schedules[1], resources.Schedules[0]}) { + if !cmp.Equal(list, []*library.Schedule{resources.Schedules[1], resources.Schedules[0]}, CmpOptApproxUpdatedAt()) { t.Errorf("ListSchedulesForRepo() is %v, want %v", list, []*library.Schedule{resources.Schedules[1], resources.Schedules[0]}) } methods["ListSchedulesForRepo"] = true @@ -1057,7 +1058,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get schedule %d for repo %d: %v", schedule.GetID(), repo.GetID(), err) } - if !reflect.DeepEqual(got, schedule) { + if !cmp.Equal(got, schedule, CmpOptApproxUpdatedAt()) { t.Errorf("GetScheduleForRepo() is %v, want %v", got, schedule) } } @@ -1071,7 +1072,7 @@ func testSchedules(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update schedule %d: %v", schedule.GetID(), err) } - if !reflect.DeepEqual(got, schedule) { + if !cmp.Equal(got, schedule, CmpOptApproxUpdatedAt()) { t.Errorf("GetSchedule() is %v, want %v", got, schedule) } } @@ -1184,7 +1185,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list secrets: %v", err) } - if !reflect.DeepEqual(list, resources.Secrets) { + if !cmp.Equal(list, resources.Secrets, CmpOptApproxUpdatedAt()) { t.Errorf("ListSecrets() is %v, want %v", list, resources.Secrets) } methods["ListSecrets"] = true @@ -1200,7 +1201,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if int(count) != 1 { t.Errorf("ListSecretsForOrg() is %v, want %v", count, 1) } - if !reflect.DeepEqual(list, []*library.Secret{secret}) { + if !cmp.Equal(list, []*library.Secret{secret}) { t.Errorf("ListSecretsForOrg() is %v, want %v", list, []*library.Secret{secret}) } methods["ListSecretsForOrg"] = true @@ -1213,7 +1214,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if int(count) != 1 { t.Errorf("ListSecretsForRepo() is %v, want %v", count, 1) } - if !reflect.DeepEqual(list, []*library.Secret{secret}) { + if !cmp.Equal(list, []*library.Secret{secret}, CmpOptApproxUpdatedAt()) { t.Errorf("ListSecretsForRepo() is %v, want %v", list, []*library.Secret{secret}) } methods["ListSecretsForRepo"] = true @@ -1226,7 +1227,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if int(count) != 1 { t.Errorf("ListSecretsForTeam() is %v, want %v", count, 1) } - if !reflect.DeepEqual(list, []*library.Secret{secret}) { + if !cmp.Equal(list, []*library.Secret{secret}, CmpOptApproxUpdatedAt()) { t.Errorf("ListSecretsForTeam() is %v, want %v", list, []*library.Secret{secret}) } methods["ListSecretsForTeam"] = true @@ -1239,7 +1240,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if int(count) != 1 { t.Errorf("ListSecretsForTeams() is %v, want %v", count, 1) } - if !reflect.DeepEqual(list, []*library.Secret{secret}) { + if !cmp.Equal(list, []*library.Secret{secret}, CmpOptApproxUpdatedAt()) { t.Errorf("ListSecretsForTeams() is %v, want %v", list, []*library.Secret{secret}) } methods["ListSecretsForTeams"] = true @@ -1256,7 +1257,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get secret %d for org %s: %v", secret.GetID(), secret.GetOrg(), err) } - if !reflect.DeepEqual(got, secret) { + if !cmp.Equal(got, secret, CmpOptApproxUpdatedAt()) { t.Errorf("GetSecretForOrg() is %v, want %v", got, secret) } methods["GetSecretForOrg"] = true @@ -1266,7 +1267,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get secret %d for repo %d: %v", secret.GetID(), resources.Repos[0].GetID(), err) } - if !reflect.DeepEqual(got, secret) { + if !cmp.Equal(got, secret, CmpOptApproxUpdatedAt()) { t.Errorf("GetSecretForRepo() is %v, want %v", got, secret) } methods["GetSecretForRepo"] = true @@ -1276,7 +1277,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get secret %d for team %s: %v", secret.GetID(), secret.GetTeam(), err) } - if !reflect.DeepEqual(got, secret) { + if !cmp.Equal(got, secret, CmpOptApproxUpdatedAt()) { t.Errorf("GetSecretForTeam() is %v, want %v", got, secret) } methods["GetSecretForTeam"] = true @@ -1293,7 +1294,7 @@ func testSecrets(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update secret %d: %v", secret.GetID(), err) } - if !reflect.DeepEqual(got, secret) { + if !cmp.Equal(got, secret, CmpOptApproxUpdatedAt()) { t.Errorf("GetSecret() is %v, want %v", got, secret) } } @@ -1369,7 +1370,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list services: %v", err) } - if !reflect.DeepEqual(list, resources.Services) { + if !cmp.Equal(list, resources.Services) { t.Errorf("ListServices() is %v, want %v", list, resources.Services) } methods["ListServices"] = true @@ -1379,7 +1380,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list services for build %d: %v", resources.Builds[0].GetID(), err) } - if !reflect.DeepEqual(list, []*library.Service{resources.Services[1], resources.Services[0]}) { + if !cmp.Equal(list, []*library.Service{resources.Services[1], resources.Services[0]}) { t.Errorf("ListServicesForBuild() is %v, want %v", list, []*library.Service{resources.Services[1], resources.Services[0]}) } if int(count) != len(resources.Services) { @@ -1395,7 +1396,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list service image count: %v", err) } - if !reflect.DeepEqual(images, expected) { + if !cmp.Equal(images, expected) { t.Errorf("ListServiceImageCount() is %v, want %v", images, expected) } methods["ListServiceImageCount"] = true @@ -1411,7 +1412,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list service status count: %v", err) } - if !reflect.DeepEqual(statuses, expected) { + if !cmp.Equal(statuses, expected) { t.Errorf("ListServiceStatusCount() is %v, want %v", statuses, expected) } methods["ListServiceStatusCount"] = true @@ -1423,7 +1424,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get service %d for build %d: %v", service.GetID(), build.GetID(), err) } - if !reflect.DeepEqual(got, service) { + if !cmp.Equal(got, service) { t.Errorf("GetServiceForBuild() is %v, want %v", got, service) } } @@ -1447,7 +1448,7 @@ func testServices(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update service %d: %v", service.GetID(), err) } - if !reflect.DeepEqual(got, service) { + if !cmp.Equal(got, service) { t.Errorf("UpdateService() is %v, want %v", got, service) } } @@ -1523,7 +1524,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list steps: %v", err) } - if !reflect.DeepEqual(list, resources.Steps) { + if !cmp.Equal(list, resources.Steps) { t.Errorf("ListSteps() is %v, want %v", list, resources.Steps) } methods["ListSteps"] = true @@ -1533,7 +1534,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list steps for build %d: %v", resources.Builds[0].GetID(), err) } - if !reflect.DeepEqual(list, []*library.Step{resources.Steps[1], resources.Steps[0]}) { + if !cmp.Equal(list, []*library.Step{resources.Steps[1], resources.Steps[0]}) { t.Errorf("ListStepsForBuild() is %v, want %v", list, []*library.Step{resources.Steps[1], resources.Steps[0]}) } if int(count) != len(resources.Steps) { @@ -1549,7 +1550,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list step image count: %v", err) } - if !reflect.DeepEqual(images, expected) { + if !cmp.Equal(images, expected) { t.Errorf("ListStepImageCount() is %v, want %v", images, expected) } methods["ListStepImageCount"] = true @@ -1565,7 +1566,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list step status count: %v", err) } - if !reflect.DeepEqual(statuses, expected) { + if !cmp.Equal(statuses, expected) { t.Errorf("ListStepStatusCount() is %v, want %v", statuses, expected) } methods["ListStepStatusCount"] = true @@ -1577,7 +1578,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get step %d for build %d: %v", step.GetID(), build.GetID(), err) } - if !reflect.DeepEqual(got, step) { + if !cmp.Equal(got, step) { t.Errorf("GetStepForBuild() is %v, want %v", got, step) } } @@ -1601,7 +1602,7 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update step %d: %v", step.GetID(), err) } - if !reflect.DeepEqual(got, step) { + if !cmp.Equal(got, step) { t.Errorf("GetStep() is %v, want %v", got, step) } } @@ -1689,7 +1690,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list users: %v", err) } - if !reflect.DeepEqual(list, resources.Users) { + if !cmp.Equal(list, resources.Users) { t.Errorf("ListUsers() is %v, want %v", list, resources.Users) } methods["ListUsers"] = true @@ -1699,7 +1700,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list lite users: %v", err) } - if !reflect.DeepEqual(list, liteUsers) { + if !cmp.Equal(list, liteUsers) { t.Errorf("ListLiteUsers() is %v, want %v", list, liteUsers) } if int(count) != len(liteUsers) { @@ -1713,7 +1714,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get user %d by name: %v", user.GetID(), err) } - if !reflect.DeepEqual(got, user) { + if !cmp.Equal(got, user) { t.Errorf("GetUserForName() is %v, want %v", got, user) } } @@ -1727,7 +1728,7 @@ func testUsers(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update user %d: %v", user.GetID(), err) } - if !reflect.DeepEqual(got, user) { + if !cmp.Equal(got, user) { t.Errorf("GetUser() is %v, want %v", got, user) } } @@ -1793,7 +1794,7 @@ func testWorkers(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to list workers: %v", err) } - if !reflect.DeepEqual(list, resources.Workers) { + if !cmp.Equal(list, resources.Workers) { t.Errorf("ListWorkers() is %v, want %v", list, resources.Workers) } methods["ListWorkers"] = true @@ -1804,7 +1805,7 @@ func testWorkers(t *testing.T, db Interface, resources *Resources) { if err != nil { t.Errorf("unable to get worker %d by hostname: %v", worker.GetID(), err) } - if !reflect.DeepEqual(got, worker) { + if !cmp.Equal(got, worker) { t.Errorf("GetWorkerForHostname() is %v, want %v", got, worker) } } @@ -1818,7 +1819,7 @@ func testWorkers(t *testing.T, db Interface, resources *Resources) { t.Errorf("unable to update worker %d: %v", worker.GetID(), err) } - if !reflect.DeepEqual(got, worker) { + if !cmp.Equal(got, worker) { t.Errorf("GetWorker() is %v, want %v", got, worker) } } @@ -2317,3 +2318,38 @@ func newResources() *Resources { Workers: []*library.Worker{workerOne, workerTwo}, } } + +// CmpOptApproxUpdatedAt is a custom comparator for cmp.Equal +// to reduce flakiness in tests when comparing structs with UpdatedAt field. +func CmpOptApproxUpdatedAt() cmp.Option { + // Custom Comparer for *int64 fields typically used to store unix timestamps. + // Will consider time difference of 5s to be equal for sake of tests. + // + // https://pkg.go.dev/github.com/google/go-cmp/cmp#Comparer + cmpApproximateUnixTime := cmp.Comparer(func(x, y *int64) bool { + if x == nil && y == nil { + return true + } + + if x != nil && y == nil || y != nil && x == nil { + return false + } + + // make sure we subtract smaller value from larger one + if *y < *x { + *x, *y = *y, *x + } + + // is it less than 5 seconds? consider the values equal + return *y-*x < 5 + }) + + // only apply to structs with UpdatedAt field + // + // https://pkg.go.dev/github.com/google/go-cmp/cmp#FilterPath + return cmp.FilterPath( + func(p cmp.Path) bool { + return p.Last().String() == "UpdatedAt" || p.Last().String() == ".UpdatedAt" + }, + cmpApproximateUnixTime) +}