Files
threshold/internal/scheduler/job/testing.go
T
Jim 8d6dee09a9 refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535)
* refact: Changes required for gorm v2

* refact: Add db.DB wrapper and refact all test fixtures to use it

Stop leaking the gorm abstraction within our unit tests.

* refact: Migrate direct calls to pq to pgx driver

The refactor will remove the remaining direct dependencies on
the pq driver which has been deprecated in favor of the pgx
driver.

It includes the addition of common.SqlOpen which is a wrapper around
sql.Open, which is needed to deal with the driver name changing from
"postgres" to "pgx" but in our code base the dialect and driver are
synonyms.
2021-09-30 09:17:27 -04: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()
rows, err := rw.Query(context.Background(), query, []interface{}{pluginId, name, cId})
if err != nil {
return nil, err
}
if !rows.Next() {
return nil, fmt.Errorf("expected to rows")
}
err = rw.ScanRows(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()
rows, err := rw.Query(context.Background(), 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(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
}