Files
threshold/internal/scheduler/job/testing.go
T
Todd 2ecd550e27 Add fields to server_worker to be a boundary resource (#2083)
* Remove the server_id from session table, change it to worker_id on session_connection

This is step 1 in updating the worker table.  The worker is becoming a public resource so it must have
a public id.  We drop the server_id column on the session_connection table and add the worker_id column
so we can use the wt_public_key type.  Since db migrations are applied while the controller is offline,
it should be safe to assume all session connections are not in a running state, thus allowing us to null
out all the worker ids in the session_connection table.

For now we don't have very good enforcement of the wt_public_id contraints, since the id is currently
defined in the worker config, but this will soon change and the worker's id will be generated by the
controller.

* Updating server_id references to controller_id.

* Adding worker tag db writing test.

* Move the server table back to the top.

* Update comment for the worker object.

* Update comment for the worker table.

* Update the newWorkerId call to take a context and fix its error.

* Correct comment for the wt_network_address domain.
2022-05-18 17:03:24 -07:00

110 lines
2.5 KiB
Go

package job
import (
"context"
"fmt"
"testing"
"time"
"github.com/hashicorp/boundary/internal/servers/store"
"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/v2"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
)
func testJob(t testing.TB, 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.UpsertJob(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, controller_id
)
values (?,?,?)
on conflict (job_plugin_id, job_name) where status = 'running'
do nothing
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, nil
}
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, controller_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) *store.Controller {
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 := &store.Controller{
PrivateId: "test-job-server-" + id,
Address: "127.0.0.1",
}
_, err = serversRepo.UpsertController(context.Background(), controller)
require.NoError(t, err)
return controller
}