Files
threshold/internal/scheduler/job/testing.go
T
Jim 591ec476e7 refactor: Update internal/db and oplog to use go-dbw package for database operations. (#1785)
* 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
2022-01-05 14:38:38 -05:00

108 lines
2.4 KiB
Go

package job
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/servers"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
)
func testJob(t *testing.T, conn *db.DB, name, description string, wrapper wrapping.Wrapper, opt ...Option) *Job {
t.Helper()
require := require.New(t)
rw := db.New(conn)
kms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(rw, rw, kms)
require.NoError(err)
job, err := repo.CreateJob(context.Background(), name, description, opt...)
require.NoError(err)
require.NotNil(job)
return job
}
func testRun(conn *db.DB, pluginId, name, cId string) (*Run, error) {
query := `
insert into job_run (
job_plugin_id, job_name, server_id
)
values (?,?,?)
returning *;
`
rw := db.New(conn)
run := allocRun()
ctx := context.Background()
rows, err := rw.Query(ctx, query, []interface{}{pluginId, name, cId})
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, fmt.Errorf("expected to rows")
}
err = rw.ScanRows(ctx, rows, run)
if err != nil {
return nil, err
}
_ = rows.Close()
return run, nil
}
func testRunWithUpdateTime(conn *db.DB, pluginId, name, cId string, updateTime time.Time) (*Run, error) {
query := `
insert into job_run (
job_plugin_id, job_name, server_id, update_time
)
values (?,?,?,?)
returning *;
`
rw := db.New(conn)
run := allocRun()
ctx := context.Background()
rows, err := rw.Query(ctx, query, []interface{}{pluginId, name, cId, updateTime})
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, fmt.Errorf("expected to rows")
}
err = rw.ScanRows(ctx, rows, run)
if err != nil {
return nil, err
}
_ = rows.Close()
return run, nil
}
func testController(t *testing.T, conn *db.DB, wrapper wrapping.Wrapper) *servers.Server {
t.Helper()
rw := db.New(conn)
kms := kms.TestKms(t, conn, wrapper)
serversRepo, err := servers.NewRepository(rw, rw, kms)
require.NoError(t, err)
id, err := uuid.GenerateUUID()
require.NoError(t, err)
controller := &servers.Server{
PrivateId: "test-job-server-" + id,
Type: servers.ServerTypeController.String(),
Description: "Test Job Controller",
Address: "127.0.0.1",
}
_, _, err = serversRepo.UpsertServer(context.Background(), controller)
require.NoError(t, err)
return controller
}