[MM-61074] Fix errcheck issues in oauth_test.go and web_test.go (#30707)

Co-authored-by: Claude <noreply@anthropic.com>
Этот коммит содержится в:
Ben Schumacher
2025-05-07 12:41:10 +02:00
коммит произвёл GitHub
родитель b8b3efda48
Коммит bfb15ab179
18 изменённых файлов: 288 добавлений и 303 удалений

Просмотреть файл

@@ -36,15 +36,14 @@ type TestHelper struct {
IncludeCacheLayer bool
ConfigStore *config.Store
t testing.TB
tempWorkspace string
oldWatcherPollingInterval int
}
func setupTestHelper(t testing.TB, dbStore store.Store, enterprise bool, includeCacheLayer bool,
func setupTestHelper(tb testing.TB, dbStore store.Store, enterprise bool, includeCacheLayer bool,
updateCfg func(cfg *model.Config), options []app.Option) *TestHelper {
tempWorkspace, err := os.MkdirTemp("", "jobstest")
require.NoError(t, err)
require.NoError(tb, err)
configStore := config.NewTestMemoryStore()
memoryConfig := configStore.Get()
@@ -62,7 +61,7 @@ func setupTestHelper(t testing.TB, dbStore store.Store, enterprise bool, include
}
_, _, err = configStore.Set(memoryConfig)
require.NoError(t, err)
require.NoError(tb, err)
buffer := &mlog.Buffer{}
@@ -75,19 +74,19 @@ func setupTestHelper(t testing.TB, dbStore store.Store, enterprise bool, include
}
testLogger, err := mlog.NewLogger()
require.NoError(t, err)
require.NoError(tb, err)
logCfg, err := config.MloggerConfigFromLoggerConfig(&memoryConfig.LogSettings, nil, config.GetLogFileLocation)
require.NoError(t, err)
require.NoError(tb, err)
err = testLogger.ConfigureTargets(logCfg, nil)
require.NoError(t, err, "failed to configure test logger")
require.NoError(tb, err, "failed to configure test logger")
err = mlog.AddWriterTarget(testLogger, buffer, true, mlog.StdAll...)
require.NoError(t, err, "failed to add writer target to test logger")
require.NoError(tb, err, "failed to add writer target to test logger")
// lock logger config so server init cannot override it during testing.
testLogger.LockConfiguration()
options = append(options, app.SetLogger(testLogger))
s, err := app.NewServer(options...)
require.NoError(t, err)
require.NoError(tb, err)
th := &TestHelper{
App: app.New(app.ServerConnector(s.Channels())),
@@ -97,19 +96,47 @@ func setupTestHelper(t testing.TB, dbStore store.Store, enterprise bool, include
TestLogger: testLogger,
IncludeCacheLayer: includeCacheLayer,
ConfigStore: configStore,
t: t,
tempWorkspace: tempWorkspace,
}
prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = "localhost:0" })
err = th.Server.Start()
require.NoError(t, err)
require.NoError(tb, err)
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
th.App.Srv().Store().MarkSystemRanUnitTests()
tb.Cleanup(func() {
if th.IncludeCacheLayer {
// Clean all the caches
appErr := th.App.Srv().InvalidateAllCaches()
require.Nil(tb, appErr)
}
done := make(chan bool)
go func() {
th.Server.Shutdown()
close(done)
}()
select {
case <-done:
case <-time.After(30 * time.Second):
// panic instead of fatal to terminate all tests in this package, otherwise the
// still running App could spuriously fail subsequent tests.
panic("failed to shutdown App within 30 seconds")
}
if th.tempWorkspace != "" {
os.RemoveAll(th.tempWorkspace)
}
if th.oldWatcherPollingInterval != 0 {
jobs.DefaultWatcherPollingInterval = th.oldWatcherPollingInterval
}
})
return th
}
@@ -118,6 +145,7 @@ func Setup(tb testing.TB, options ...app.Option) *TestHelper {
}
func SetupWithUpdateCfg(tb testing.TB, updateCfg func(cfg *model.Config), options ...app.Option) *TestHelper {
tb.Helper()
if testing.Short() {
tb.SkipNow()
}
@@ -135,27 +163,31 @@ func SetupWithUpdateCfg(tb testing.TB, updateCfg func(cfg *model.Config), option
return th
}
func (th *TestHelper) InitBasic() *TestHelper {
th.SystemAdminUser = th.CreateUser()
func (th *TestHelper) InitBasic(tb testing.TB) *TestHelper {
tb.Helper()
th.SystemAdminUser = th.CreateUser(tb)
_, appErr := th.App.UpdateUserRoles(th.Context, th.SystemAdminUser.Id, model.SystemUserRoleId+" "+model.SystemAdminRoleId, false)
require.Nil(th.t, appErr)
require.Nil(tb, appErr)
th.SystemAdminUser, appErr = th.App.GetUser(th.SystemAdminUser.Id)
require.Nil(th.t, appErr)
require.Nil(tb, appErr)
th.BasicUser = th.CreateUser()
th.BasicUser = th.CreateUser(tb)
th.BasicUser, appErr = th.App.GetUser(th.BasicUser.Id)
require.Nil(th.t, appErr)
require.Nil(tb, appErr)
th.BasicUser2 = th.CreateUser()
th.BasicUser2 = th.CreateUser(tb)
th.BasicUser2, appErr = th.App.GetUser(th.BasicUser2.Id)
require.Nil(th.t, appErr)
require.Nil(tb, appErr)
th.BasicTeam = th.CreateTeam()
th.BasicTeam = th.CreateTeam(tb)
return th
}
func (th *TestHelper) CreateTeam() *model.Team {
func (th *TestHelper) CreateTeam(tb testing.TB) *model.Team {
tb.Helper()
id := model.NewId()
team := &model.Team{
DisplayName: "dn_" + id,
@@ -165,15 +197,17 @@ func (th *TestHelper) CreateTeam() *model.Team {
}
team, err := th.App.CreateTeam(th.Context, team)
require.Nil(th.t, err)
require.Nil(tb, err)
return team
}
func (th *TestHelper) CreateUser() *model.User {
return th.CreateUserOrGuest(false)
func (th *TestHelper) CreateUser(tb testing.TB) *model.User {
tb.Helper()
return th.CreateUserOrGuest(tb, false)
}
func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
func (th *TestHelper) CreateUserOrGuest(tb testing.TB, guest bool) *model.User {
tb.Helper()
id := model.NewId()
user := &model.User{
@@ -190,45 +224,12 @@ func (th *TestHelper) CreateUserOrGuest(guest bool) *model.User {
} else {
user, err = th.App.CreateUser(th.Context, user)
}
require.Nil(th.t, err)
require.Nil(tb, err)
return user
}
func (th *TestHelper) ShutdownApp() {
done := make(chan bool)
go func() {
th.Server.Shutdown()
close(done)
}()
select {
case <-done:
case <-time.After(30 * time.Second):
// panic instead of fatal to terminate all tests in this package, otherwise the
// still running App could spuriously fail subsequent tests.
panic("failed to shutdown App within 30 seconds")
}
}
func (th *TestHelper) TearDown() {
if th.IncludeCacheLayer {
// Clean all the caches
appErr := th.App.Srv().InvalidateAllCaches()
require.Nil(th.t, appErr)
}
th.ShutdownApp()
if th.tempWorkspace != "" {
os.RemoveAll(th.tempWorkspace)
}
if th.oldWatcherPollingInterval != 0 {
jobs.DefaultWatcherPollingInterval = th.oldWatcherPollingInterval
}
}
func (th *TestHelper) SetupBatchWorker(t *testing.T, worker *jobs.BatchWorker) *model.Job {
t.Helper()
func (th *TestHelper) SetupBatchWorker(tb testing.TB, worker *jobs.BatchWorker) *model.Job {
tb.Helper()
jobId := model.NewId()
th.Server.Jobs.RegisterJobType(jobId, worker, nil)
@@ -236,7 +237,7 @@ func (th *TestHelper) SetupBatchWorker(t *testing.T, worker *jobs.BatchWorker) *
jobData := make(model.StringMap)
jobData["batch_number"] = "1"
job, appErr := th.Server.Jobs.CreateJob(th.Context, jobId, jobData)
require.Nil(t, appErr)
require.Nil(tb, appErr)
done := make(chan bool)
go func() {
@@ -245,8 +246,8 @@ func (th *TestHelper) SetupBatchWorker(t *testing.T, worker *jobs.BatchWorker) *
}()
// When ending the test, ensure we wait for the worker to finish.
t.Cleanup(func() {
waitDone(t, done, "worker did not stop running")
tb.Cleanup(func() {
waitDone(tb, done, "worker did not stop running")
})
// Give the worker time to start running
@@ -255,36 +256,36 @@ func (th *TestHelper) SetupBatchWorker(t *testing.T, worker *jobs.BatchWorker) *
return job
}
func (th *TestHelper) WaitForJobStatus(t *testing.T, job *model.Job, status string) {
t.Helper()
func (th *TestHelper) WaitForJobStatus(tb testing.TB, job *model.Job, status string) {
tb.Helper()
require.Eventuallyf(t, func() bool {
require.Eventuallyf(tb, func() bool {
actualJob, appErr := th.Server.Jobs.GetJob(th.Context, job.Id)
require.Nil(t, appErr)
require.Equal(t, job.Id, actualJob.Id)
require.Nil(tb, appErr)
require.Equal(tb, job.Id, actualJob.Id)
return actualJob.Status == status
}, 5*time.Second, 250*time.Millisecond, "job never transitioned to %s", status)
}
func (th *TestHelper) WaitForBatchNumber(t *testing.T, job *model.Job, batchNumber int) {
t.Helper()
func (th *TestHelper) WaitForBatchNumber(tb testing.TB, job *model.Job, batchNumber int) {
tb.Helper()
require.Eventuallyf(t, func() bool {
require.Eventuallyf(tb, func() bool {
actualJob, appErr := th.Server.Jobs.GetJob(th.Context, job.Id)
require.Nil(t, appErr)
require.Equal(t, job.Id, actualJob.Id)
require.Nil(tb, appErr)
require.Equal(tb, job.Id, actualJob.Id)
finalBatchNumber, err := strconv.Atoi(actualJob.Data["batch_number"])
require.NoError(t, err)
require.NoError(tb, err)
return finalBatchNumber == batchNumber
}, 5*time.Second, 250*time.Millisecond, "job did not stop at batch %d", batchNumber)
}
func waitDone(t *testing.T, done chan bool, msg string) {
t.Helper()
func waitDone(tb testing.TB, done chan bool, msg string) {
tb.Helper()
require.Eventually(t, func() bool {
require.Eventually(tb, func() bool {
select {
case <-done:
return true
@@ -294,32 +295,34 @@ func waitDone(t *testing.T, done chan bool, msg string) {
}, 5*time.Second, 100*time.Millisecond, msg)
}
func (th *TestHelper) SetupWorkers(t *testing.T) {
func (th *TestHelper) SetupWorkers(tb testing.TB) {
tb.Helper()
err := th.App.Srv().Jobs.StartWorkers()
require.NoError(t, err)
require.NoError(tb, err)
}
func (th *TestHelper) RunJob(t *testing.T, jobType string, jobData map[string]string) *model.Job {
t.Helper()
func (th *TestHelper) RunJob(tb testing.TB, jobType string, jobData map[string]string) *model.Job {
tb.Helper()
job, appErr := th.Server.Jobs.CreateJob(th.Context, jobType, jobData)
require.Nil(t, appErr)
require.Nil(tb, appErr)
// poll until completion
th.checkJobStatus(t, job.Id, model.JobStatusSuccess)
th.checkJobStatus(tb, job.Id, model.JobStatusSuccess)
job, appErr = th.Server.Jobs.GetJob(th.Context, job.Id)
require.Nil(t, appErr)
require.Nil(tb, appErr)
return job
}
func (th *TestHelper) checkJobStatus(t *testing.T, jobId string, status string) {
t.Helper()
func (th *TestHelper) checkJobStatus(tb testing.TB, jobId string, status string) {
tb.Helper()
require.Eventuallyf(t, func() bool {
require.Eventuallyf(tb, func() bool {
// it's ok if there's an error, it might take awhile for the job to finish.
job, appErr := th.Server.Jobs.GetJob(th.Context, jobId)
assert.Nil(th.t, appErr)
assert.Nil(tb, appErr)
if jobId == job.Id {
return job.Status == status
}