* chore: Add new dbw dependency * refactor: Convert to use dbw.DB * refactor (db): Change GetTicket(...) signature * feature (db tests): Added TestDeleteWhere(...) Adding this allows us to eliminate a leak of the gorm library * refactor: Add ctx to ScanRows(...) signature This is required to refactor all the deprecated errors pkg calls. This refactor required adding a context to quite a few call chains and in particular the job scheduler bits * feature (db/tests): Add support for test opt: WithTestLogLevel(...) This test only option allows callers to specify a log level for the underlying db package (where supported/applicable) when calling TestSetup(...) * refactor (db): Simplify Db type Only store the underlying *DB and create the *RW on demand when needed. * refactor (oplog): Port to using dbw pkg for db operations * refactor (db): Remove gormDB(...) func * tests (db): Add unit tests for TestDeleteWhere(...) * chore: make fmt changes to formatting
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
package job
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_TestJob(t *testing.T) {
|
|
t.Parallel()
|
|
assert, require := assert.New(t), require.New(t)
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
job := testJob(t, conn, "testJob", "testDescription", wrapper)
|
|
require.NotNil(job)
|
|
|
|
rw := db.New(conn)
|
|
var got Job
|
|
err := rw.LookupWhere(context.Background(), &got, "name = ?", []interface{}{job.Name})
|
|
require.NoError(err)
|
|
assert.Equal("testJob", got.Name)
|
|
assert.Equal("testDescription", got.Description)
|
|
assert.Equal(defaultPluginId, got.PluginId)
|
|
assert.NotEmpty(got.NextScheduledRun)
|
|
|
|
job1 := testJob(t, conn, "testJob1", "testDescription1", wrapper, WithNextRunIn(time.Hour))
|
|
require.NotNil(job1)
|
|
|
|
var got1 Job
|
|
err = rw.LookupWhere(context.Background(), &got1, "name = ?", []interface{}{job1.Name})
|
|
require.NoError(err)
|
|
assert.Equal("testJob1", got1.Name)
|
|
assert.Equal("testDescription1", got1.Description)
|
|
assert.Equal(defaultPluginId, got.PluginId)
|
|
assert.NotEmpty(got1.NextScheduledRun)
|
|
|
|
// The previous job next scheduled run should have been created with the current database time,
|
|
// while the current job should be an hour later
|
|
previousJobNextRun := got.NextScheduledRun.AsTime()
|
|
currentJobNextRun := got1.NextScheduledRun.AsTime()
|
|
assert.Equal(currentJobNextRun.Round(time.Minute), previousJobNextRun.Add(time.Hour).Round(time.Minute))
|
|
}
|
|
|
|
func Test_TestRun(t *testing.T) {
|
|
t.Parallel()
|
|
assert, require := assert.New(t), require.New(t)
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
iam.TestRepo(t, conn, wrapper)
|
|
|
|
job := testJob(t, conn, "testJob", "testDescription", wrapper)
|
|
require.NotNil(job)
|
|
|
|
server := testController(t, conn, wrapper)
|
|
|
|
run, err := testRun(conn, job.PluginId, job.Name, server.PrivateId)
|
|
require.NoError(err)
|
|
require.NotNil(run)
|
|
|
|
rw := db.New(conn)
|
|
var got Run
|
|
err = rw.LookupWhere(context.Background(), &got, "private_id = ?", []interface{}{run.PrivateId})
|
|
require.NoError(err)
|
|
assert.Equal(server.PrivateId, got.ServerId)
|
|
assert.Equal(job.Name, got.JobName)
|
|
assert.Equal(job.PluginId, got.JobPluginId)
|
|
assert.NotEmpty(got.CreateTime)
|
|
assert.Equal(Running.string(), got.Status)
|
|
}
|