* feat(server): Refactor LookupWorkerByName to be a test method * feat(server): Split worker tags table * refact(worker): Export api and config tags * refact(server): Return worker id from UpsertWorkerStatus * refact(server): Create ListWorkerStorageBucketUpdates domain function * refact(server): Use dedicated HCPb-worker list func * refact(server): Create ConnectionRoute domain function * refact(handlers): Move worker selection logic to internal/server package This commit moves all the logic to select a worker to handle a session to the internal/server package. Most functions were moved to this package with no changes, however, there are a couple of notable exceptions: - Given that we don't use GRPC errors in repos, all errors were adjusted to use the internal/errors package instead. - StorageBucketFilterCredIdFn is now used to prevent the internal/server package from importing internal/storage/plugin, which would cause a dep cycle. Callers of SelectSessionWorkers must implement this func which is responsible for returning a storage bucket's worker filter as well as its associated credential id. - authorizeSessionWithWorkerFilter now receives an internal/server.(*Repository) object when called. This is transparent to SelectSessionWorkers callers as it is handled internally. * feat(server): Add FilteredWithFeatures fn and new server options * refact(server): create new list and lookup worker queries * test(server): Add WorkerList unit tests * feat(server): Implement WorkerList randomness using crypto/rand pkg * chore(db): add index to server_worker table * refact(server): prefix test method with Test * refact(server): rename Downstreamers interface to Graph * chore(server): unexport and rename method * chore: add doc string to WorkerAddress (#5420) --------- Co-authored-by: Irena Rindos <irenarindos@users.noreply.github.com> Co-authored-by: irenarindos <irena.rindos@gmail.com>
127 lines
3.8 KiB
Go
127 lines
3.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package oss_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/db/common"
|
|
"github.com/hashicorp/boundary/internal/db/schema"
|
|
"github.com/hashicorp/boundary/testing/dbtest"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
makeWorkerQuery = "insert into server_worker (public_id, scope_id, type) values ($1, 'global', 'pki')"
|
|
insertWorkerTagsQuery = "insert into server_worker_tag (worker_id, key, value, source) values ($1, $2, $3, $4)"
|
|
selectWorkerApiTagsQuery = "select key, value from server_worker_api_tag where worker_id = $1"
|
|
selectWorkerConfigTagsQuery = "select key, value from server_worker_config_tag where worker_id = $1"
|
|
)
|
|
|
|
type testWorkerTags struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
func Test_WorkerTagTableSplit(t *testing.T) {
|
|
t.Parallel()
|
|
require := require.New(t)
|
|
const priorMigration = 92001
|
|
const serverEnumMigration = 94001
|
|
dialect := dbtest.Postgres
|
|
ctx := context.Background()
|
|
|
|
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
|
|
require.NoError(err)
|
|
t.Cleanup(func() {
|
|
require.NoError(c())
|
|
})
|
|
d, err := common.SqlOpen(dialect, u)
|
|
require.NoError(err)
|
|
|
|
// migration to the prior migration (before the one we want to test)
|
|
m, err := schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
|
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": priorMigration}),
|
|
))
|
|
require.NoError(err)
|
|
|
|
_, err = m.ApplyMigrations(ctx)
|
|
require.NoError(err)
|
|
state, err := m.CurrentState(ctx)
|
|
require.NoError(err)
|
|
want := &schema.State{
|
|
Initialized: true,
|
|
Editions: []schema.EditionState{
|
|
{
|
|
Name: "oss",
|
|
BinarySchemaVersion: priorMigration,
|
|
DatabaseSchemaVersion: priorMigration,
|
|
DatabaseSchemaState: schema.Equal,
|
|
},
|
|
},
|
|
}
|
|
require.Equal(want, state)
|
|
|
|
// Seed the data
|
|
execResult, err := d.ExecContext(ctx, makeWorkerQuery, "test-worker")
|
|
require.NoError(err)
|
|
rowsAffected, err := execResult.RowsAffected()
|
|
require.NoError(err)
|
|
require.Equal(int64(1), rowsAffected)
|
|
execResult, err = d.ExecContext(ctx, insertWorkerTagsQuery, "test-worker", "key1", "value1", "api")
|
|
require.NoError(err)
|
|
rowsAffected, err = execResult.RowsAffected()
|
|
require.NoError(err)
|
|
require.Equal(int64(1), rowsAffected)
|
|
execResult, err = d.ExecContext(ctx, insertWorkerTagsQuery, "test-worker", "key2", "value2", "configuration")
|
|
require.NoError(err)
|
|
rowsAffected, err = execResult.RowsAffected()
|
|
require.NoError(err)
|
|
require.Equal(int64(1), rowsAffected)
|
|
|
|
// now we're ready for the migration we want to test.
|
|
m, err = schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
|
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": serverEnumMigration}),
|
|
))
|
|
require.NoError(err)
|
|
|
|
_, err = m.ApplyMigrations(ctx)
|
|
require.NoError(err)
|
|
state, err = m.CurrentState(ctx)
|
|
require.NoError(err)
|
|
|
|
want = &schema.State{
|
|
Initialized: true,
|
|
Editions: []schema.EditionState{
|
|
{
|
|
Name: "oss",
|
|
BinarySchemaVersion: serverEnumMigration,
|
|
DatabaseSchemaVersion: serverEnumMigration,
|
|
DatabaseSchemaState: schema.Equal,
|
|
},
|
|
},
|
|
}
|
|
require.Equal(want, state)
|
|
|
|
// Check that the tags have been moved to the new tables
|
|
apiTags := new(testWorkerTags)
|
|
row := d.QueryRowContext(ctx, selectWorkerApiTagsQuery, "test-worker")
|
|
require.NoError(row.Scan(
|
|
&apiTags.Key,
|
|
&apiTags.Value,
|
|
))
|
|
require.Equal("key1", apiTags.Key)
|
|
require.Equal("value1", apiTags.Value)
|
|
|
|
configTags := new(testWorkerTags)
|
|
row = d.QueryRowContext(ctx, selectWorkerConfigTagsQuery, "test-worker")
|
|
require.NoError(row.Scan(
|
|
&configTags.Key,
|
|
&configTags.Value,
|
|
))
|
|
require.Equal("key2", configTags.Key)
|
|
require.Equal("value2", configTags.Value)
|
|
}
|