Update against changes from merge

This commit is contained in:
Jeff Mitchell committed 2022-05-09 14:39:15 -04:00
1 parent ab4d542fad
commit 2a8e179692
4 files changed
+21 -171

No files matched your search

@@ -1,151 +0,0 @@
package oss_test
import (
"context"
"testing"
"github.com/hashicorp/boundary/internal/authtoken"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/db/common"
"github.com/hashicorp/boundary/internal/db/schema"
"github.com/hashicorp/boundary/internal/host/static"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/session"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/internal/target/tcp"
"github.com/hashicorp/boundary/internal/types/resource"
"github.com/hashicorp/boundary/testing/dbtest"
"github.com/stretchr/testify/require"
)
func TestMigrations_SessionStateTrigger(t *testing.T) {
t.Parallel()
require := require.New(t)
const (
priorMigration = 27002
currentMigration = 28002
)
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)
require.NoError(m.ApplyMigrations(ctx))
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 database with test data
dbType, err := db.StringToDbType(dialect)
require.NoError(err)
conn, err := db.Open(dbType, u)
require.NoError(err)
rw := db.New(conn)
wrapper := db.TestWrapper(t)
org, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
require.NotNil(prj)
hc := static.TestCatalogs(t, conn, prj.GetPublicId(), 1)[0]
hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0]
h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0]
static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h})
tar := tcp.TestTarget(ctx, t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()}))
kmsCache := kms.TestKms(t, conn, wrapper)
serverId := "worker"
tofu := session.TestTofu(t)
session.TestWorker(t, conn, wrapper, session.WithServerId(serverId))
at := authtoken.TestAuthToken(t, conn, kmsCache, org.GetPublicId())
uId := at.GetIamUserId()
sess := session.TestSession(t, conn, wrapper, session.ComposedOf{
UserId: uId,
HostId: h.GetPublicId(),
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 1,
})
sessionRepo, err := session.NewRepository(rw, rw, kmsCache)
require.NoError(err)
// Make and transition a valid session through pending, active, canceling, and terminated
// Unfortunately, could not recreate an invalid session P-A-T-C using the session repo
_, _, err = sessionRepo.ActivateSession(ctx, sess.PublicId, sess.Version, serverId, resource.Worker.String(), tofu)
require.NoError(err)
connection := session.TestConnection(t, conn, sess.PublicId, "127.0.0.1", 22,
"127.0.0.2", 23, "127.0.0.1")
session.TestConnectionState(t, conn, connection.PublicId, session.StatusConnected)
session.TestConnectionState(t, conn, connection.PublicId, session.StatusClosed)
_, err = sessionRepo.CancelSession(ctx, sess.PublicId, sess.Version+1)
require.NoError(err)
sessionRepo.TerminateCompletedSessions(ctx)
repoSessions, err := sessionRepo.ListSessions(ctx)
require.NoError(err)
var numStates int
for _, s := range repoSessions {
numStates = len(s.States)
}
require.Equal(4, numStates)
// 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": currentMigration}),
))
require.NoError(err)
require.NoError(m.ApplyMigrations(ctx))
state, err = m.CurrentState(ctx)
require.NoError(err)
want = &schema.State{
Initialized: true,
Editions: []schema.EditionState{
{
Name: "oss",
BinarySchemaVersion: currentMigration,
DatabaseSchemaVersion: currentMigration,
DatabaseSchemaState: schema.Equal,
},
},
}
require.Equal(want, state)
// Check that we haven't removed a state
repoSessions, err = sessionRepo.ListSessions(ctx)
require.NoError(err)
for _, s := range repoSessions {
numStates = len(s.States)
}
require.Equal(4, numStates)
}
@@ -452,8 +452,8 @@ func TestRepository_CloseConnectionsForDeadWorkers(t *testing.T) {
kms := kms.TestKms(t, conn, wrapper)
repo, err := NewRepository(rw, rw, kms)
require.NoError(err)
deadWorkerConnCloseMinGrace := 1 * time.Second
connRepo, err := NewConnectionRepository(ctx, rw, rw, kms, WithDeadWorkerConnCloseMinGrace(deadWorkerConnCloseMinGrace))
deadWorkerConnCloseMinGrace := 15 * time.Second
connRepo, err := NewConnectionRepository(ctx, rw, rw, kms)
require.NoError(err)
serversRepo, err := servers.NewRepository(rw, rw, kms)
require.NoError(err)
+1 -1
View File
@@ -642,7 +642,7 @@ func TestRepository_transitionState(t *testing.T) {
var ss []*State
var err error
if status == StatusActive {
s, ss, err = repo.ActivateSession(context.Background(), id, version, srv.PrivateId, srv.Type, tofu)
s, ss, err = repo.ActivateSession(context.Background(), id, version, srv.PrivateId, tofu)
} else {
s, ss, err = repo.updateState(context.Background(), id, version, status)
}
+18 -17
View File
@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/scheduler"
"github.com/hashicorp/boundary/internal/servers"
"github.com/hashicorp/boundary/internal/servers/store"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -44,8 +45,8 @@ func TestSessionConnectionCleanupJob(t *testing.T) {
// Create two "workers". One will remain untouched while the other "goes
// away and comes back" (worker 2).
worker1 := TestWorker(t, conn, wrapper, WithServerId("worker1"))
worker2 := TestWorker(t, conn, wrapper, WithServerId("worker2"))
worker1 := TestWorker(t, conn, wrapper, WithWorkerId("worker1"))
worker2 := TestWorker(t, conn, wrapper, WithWorkerId("worker2"))
// Create a few sessions on each, activate, and authorize a connection
var connIds []string
@@ -55,8 +56,8 @@ func TestSessionConnectionCleanupJob(t *testing.T) {
if i%2 == 0 {
serverId = worker2.PrivateId
}
sess := TestDefaultSession(t, conn, wrapper, iamRepo, WithServerId(serverId), WithDbOpts(db.WithSkipVetForWrite(true)))
sess, _, err = sessionRepo.ActivateSession(ctx, sess.GetPublicId(), sess.Version, serverId, "worker", []byte("foo"))
sess := TestDefaultSession(t, conn, wrapper, iamRepo, WithWorkerId(serverId), WithDbOpts(db.WithSkipVetForWrite(true)))
sess, _, err = sessionRepo.ActivateSession(ctx, sess.GetPublicId(), sess.Version, serverId, []byte("foo"))
require.NoError(err)
c, cs, _, err := AuthorizeConnection(ctx, sessionRepo, connectionRepo, sess.GetPublicId(), serverId)
require.NoError(err)
@@ -108,7 +109,7 @@ func TestSessionConnectionCleanupJob(t *testing.T) {
// Push an upsert to the first worker so that its status has been
// updated.
_, rowsUpdated, err := serversRepo.UpsertServer(ctx, worker1, []servers.Option{}...)
_, rowsUpdated, err := serversRepo.UpsertWorker(ctx, worker1, []servers.Option{}...)
require.NoError(err)
require.Equal(1, rowsUpdated)
@@ -196,10 +197,10 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
// firstly, the last worker will have no connections at all, and we will be
// closing the others in stages to test multiple servers being closed at
// once.
worker1 := TestWorker(t, conn, wrapper, WithServerId("worker1"))
worker2 := TestWorker(t, conn, wrapper, WithServerId("worker2"))
worker3 := TestWorker(t, conn, wrapper, WithServerId("worker3"))
worker4 := TestWorker(t, conn, wrapper, WithServerId("worker4"))
worker1 := TestWorker(t, conn, wrapper, WithWorkerId("worker1"))
worker2 := TestWorker(t, conn, wrapper, WithWorkerId("worker2"))
worker3 := TestWorker(t, conn, wrapper, WithWorkerId("worker3"))
worker4 := TestWorker(t, conn, wrapper, WithWorkerId("worker4"))
// Create sessions on the first three, activate, and authorize connections
var worker1ConnIds, worker2ConnIds, worker3ConnIds []string
@@ -212,8 +213,8 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
} else {
serverId = worker3.PrivateId
}
sess := TestDefaultSession(t, conn, wrapper, iamRepo, WithServerId(serverId), WithDbOpts(db.WithSkipVetForWrite(true)))
sess, _, err = repo.ActivateSession(ctx, sess.GetPublicId(), sess.Version, serverId, "worker", []byte("foo"))
sess := TestDefaultSession(t, conn, wrapper, iamRepo, WithWorkerId(serverId), WithDbOpts(db.WithSkipVetForWrite(true)))
sess, _, err = repo.ActivateSession(ctx, sess.GetPublicId(), sess.Version, serverId, []byte("foo"))
require.NoError(err)
c, cs, err := connRepo.AuthorizeConnection(ctx, sess.GetPublicId(), serverId)
require.NoError(err)
@@ -288,12 +289,12 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
// updateServer is a helper for updating the update time for our
// servers. The controller is read back so that we can reference
// the most up-to-date fields.
updateServer := func(t *testing.T, w *servers.Server) *servers.Server {
updateServer := func(t *testing.T, w *store.Worker) *store.Worker {
t.Helper()
_, rowsUpdated, err := serversRepo.UpsertServer(ctx, w)
_, rowsUpdated, err := serversRepo.UpsertWorker(ctx, w)
require.NoError(err)
require.Equal(1, rowsUpdated)
servers, err := serversRepo.ListServers(ctx, servers.ServerTypeWorker)
servers, err := serversRepo.ListWorkers(ctx)
require.NoError(err)
for _, server := range servers {
if server.PrivateId == w.PrivateId {
@@ -398,7 +399,7 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
// number of connections closed. Due to how things are
require.Equal([]closeConnectionsForDeadWorkersResult{
{
ServerId: worker1.PrivateId,
WorkerId: worker1.PrivateId,
LastUpdateTime: timestampPbAsUTC(t, worker1.UpdateTime.AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},
@@ -422,12 +423,12 @@ func TestCloseConnectionsForDeadWorkers(t *testing.T) {
// Assert that we have one result with the appropriate ID and number of connections closed.
require.Equal([]closeConnectionsForDeadWorkersResult{
{
ServerId: worker2.PrivateId,
WorkerId: worker2.PrivateId,
LastUpdateTime: timestampPbAsUTC(t, worker2.UpdateTime.AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},
{
ServerId: worker3.PrivateId,
WorkerId: worker3.PrivateId,
LastUpdateTime: timestampPbAsUTC(t, worker3.UpdateTime.AsTime()),
NumberConnectionsClosed: 12, // 18 per server, with 6 closed already
},