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
This commit is contained in:
160 files changed
+4369
-3164
No files matched your search
@@ -126,7 +126,7 @@ func TestConnectionState_Delete(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
var initialState ConnectionState
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "connection_id = ? and state = ?", tt.state.ConnectionId, tt.state.Status)
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "connection_id = ? and state = ?", []interface{}{tt.state.ConnectionId, tt.state.Status})
|
||||
require.NoError(err)
|
||||
|
||||
deleteState := allocConnectionState()
|
||||
@@ -148,7 +148,7 @@ func TestConnectionState_Delete(t *testing.T) {
|
||||
}
|
||||
assert.Equal(tt.wantRowsDeleted, deletedRows)
|
||||
foundState := allocConnectionState()
|
||||
err = rw.LookupWhere(context.Background(), &foundState, "connection_id = ? and start_time = ?", tt.state.ConnectionId, initialState.StartTime)
|
||||
err = rw.LookupWhere(context.Background(), &foundState, "connection_id = ? and start_time = ?", []interface{}{tt.state.ConnectionId, initialState.StartTime})
|
||||
require.Error(err)
|
||||
assert.True(errors.IsNotFoundError(err))
|
||||
})
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
func TestSession_ImmutableFields(t *testing.T) {
|
||||
@@ -103,7 +102,7 @@ func TestState_ImmutableFields(t *testing.T) {
|
||||
state := TestState(t, conn, session.PublicId, StatusPending)
|
||||
|
||||
var new State
|
||||
err := rw.LookupWhere(context.Background(), &new, "session_id = ? and state = ?", state.SessionId, state.Status)
|
||||
err := rw.LookupWhere(context.Background(), &new, "session_id = ? and state = ?", []interface{}{state.SessionId, state.Status})
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
@@ -153,7 +152,7 @@ func TestState_ImmutableFields(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
orig := new.Clone()
|
||||
err := rw.LookupWhere(context.Background(), orig, "session_id = ? and start_time = ?", new.SessionId, new.StartTime)
|
||||
err := rw.LookupWhere(context.Background(), orig, "session_id = ? and start_time = ?", []interface{}{new.SessionId, new.StartTime})
|
||||
require.NoError(err)
|
||||
|
||||
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
||||
@@ -161,7 +160,7 @@ func TestState_ImmutableFields(t *testing.T) {
|
||||
assert.Equal(0, rowsUpdated)
|
||||
|
||||
after := new.Clone()
|
||||
err = rw.LookupWhere(context.Background(), after, "session_id = ? and start_time = ?", new.SessionId, new.StartTime)
|
||||
err = rw.LookupWhere(context.Background(), after, "session_id = ? and start_time = ?", []interface{}{new.SessionId, new.StartTime})
|
||||
require.NoError(err)
|
||||
assert.Equal(orig.(*State), after)
|
||||
})
|
||||
@@ -250,11 +249,9 @@ func TestConnectionState_ImmutableFields(t *testing.T) {
|
||||
state := TestConnectionState(t, conn, connection.PublicId, StatusConnected)
|
||||
|
||||
var new ConnectionState
|
||||
err := rw.LookupWhere(context.Background(), &new, "connection_id = ? and state = ?", state.ConnectionId, state.Status)
|
||||
err := rw.LookupWhere(context.Background(), &new, "connection_id = ? and state = ?", []interface{}{state.ConnectionId, state.Status})
|
||||
require.NoError(t, err)
|
||||
|
||||
conn.Logger = logger.Default.LogMode(logger.Info)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
update *ConnectionState
|
||||
@@ -310,7 +307,7 @@ func TestConnectionState_ImmutableFields(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
orig := new.Clone()
|
||||
err := rw.LookupWhere(context.Background(), orig, "connection_id = ? and start_time = ?", new.ConnectionId, new.StartTime)
|
||||
err := rw.LookupWhere(context.Background(), orig, "connection_id = ? and start_time = ?", []interface{}{new.ConnectionId, new.StartTime})
|
||||
require.NoError(err)
|
||||
|
||||
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
||||
@@ -323,7 +320,7 @@ func TestConnectionState_ImmutableFields(t *testing.T) {
|
||||
assert.Contains(err.Error(), tt.wantErrContains)
|
||||
}
|
||||
after := new.Clone()
|
||||
err = rw.LookupWhere(context.Background(), after, "connection_id = ? and start_time = ?", new.ConnectionId, new.StartTime)
|
||||
err = rw.LookupWhere(context.Background(), after, "connection_id = ? and start_time = ?", []interface{}{new.ConnectionId, new.StartTime})
|
||||
require.NoError(err)
|
||||
assert.Equal(orig.(*ConnectionState), after)
|
||||
})
|
||||
|
||||
@@ -187,7 +187,7 @@ func (r *Repository) CloseConnectionsForDeadWorkers(ctx context.Context, gracePe
|
||||
|
||||
for rows.Next() {
|
||||
var result CloseConnectionsForDeadWorkersResult
|
||||
if err := w.ScanRows(rows, &result); err != nil {
|
||||
if err := w.ScanRows(ctx, rows, &result); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +82,7 @@ func TestRepository_ListConnection(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocConnection()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocConnection(); return &i }(), "1=1")
|
||||
testConnections := []*Connection{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
c := TestConnection(t, conn,
|
||||
@@ -107,7 +107,7 @@ func TestRepository_ListConnection(t *testing.T) {
|
||||
}
|
||||
t.Run("withOrder", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocConnection()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocConnection(); return &i }(), "1=1")
|
||||
wantCnt := 5
|
||||
for i := 0; i < wantCnt; i++ {
|
||||
_ = TestConnection(t, conn,
|
||||
|
||||
@@ -105,7 +105,7 @@ func (r *Repository) CreateSession(ctx context.Context, sessionWrapper wrapping.
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var returnedCred DynamicCredential
|
||||
w.ScanRows(rows, &returnedCred)
|
||||
w.ScanRows(ctx, rows, &returnedCred)
|
||||
returnedSession.DynamicCredentials = append(returnedSession.DynamicCredentials, &returnedCred)
|
||||
}
|
||||
}
|
||||
@@ -276,7 +276,7 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
|
||||
var sessionsList []*sessionListView
|
||||
for rows.Next() {
|
||||
var s sessionListView
|
||||
if err := r.reader.ScanRows(rows, &s); err != nil {
|
||||
if err := r.reader.ScanRows(ctx, rows, &s); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
sessionsList = append(sessionsList, &s)
|
||||
@@ -504,7 +504,7 @@ func (r *Repository) sessionAuthzSummary(ctx context.Context, sessionId string)
|
||||
return nil, errors.New(ctx, errors.MultipleRecords, op, "query returned more than one row")
|
||||
}
|
||||
info = &ConnectionAuthzSummary{}
|
||||
if err := r.reader.ScanRows(rows, info); err != nil {
|
||||
if err := r.reader.ScanRows(ctx, rows, info); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +107,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
testSessions := []*Session{}
|
||||
for i := 0; i < tt.createCnt; i++ {
|
||||
s := TestSession(t, conn, wrapper, composedOf)
|
||||
@@ -142,7 +142,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
}
|
||||
t.Run("withOrder", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
wantCnt := 5
|
||||
for i := 0; i < wantCnt; i++ {
|
||||
_ = TestSession(t, conn, wrapper, composedOf)
|
||||
@@ -159,7 +159,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
})
|
||||
t.Run("withUserId", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
wantCnt := 5
|
||||
for i := 0; i < wantCnt; i++ {
|
||||
_ = TestSession(t, conn, wrapper, composedOf)
|
||||
@@ -172,7 +172,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
})
|
||||
t.Run("withUserIdAndwithScopeId", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
wantCnt := 5
|
||||
for i := 0; i < wantCnt; i++ {
|
||||
// Scope 1 User 1
|
||||
@@ -195,7 +195,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
})
|
||||
t.Run("WithSessionIds", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
testSessions := []*Session{}
|
||||
for i := 0; i < 10; i++ {
|
||||
s := TestSession(t, conn, wrapper, composedOf)
|
||||
@@ -212,7 +212,7 @@ func TestRepository_ListSession(t *testing.T) {
|
||||
})
|
||||
t.Run("withServerId", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
for i := 0; i < 6; i++ {
|
||||
if i%2 == 0 {
|
||||
TestWorker(t, conn, wrapper, WithServerId(fmt.Sprintf("server-%d", i/2)))
|
||||
@@ -248,7 +248,7 @@ func TestRepository_ListSessions_Multiple_Scopes(t *testing.T) {
|
||||
repo, err := NewRepository(rw, rw, kms)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
|
||||
const numPerScope = 10
|
||||
var projs []string
|
||||
@@ -1030,7 +1030,7 @@ func TestRepository_TerminateCompletedSessions(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
require.NoError(conn.Where("1=1").Delete(AllocSession()).Error)
|
||||
db.TestDeleteWhere(t, conn, func() interface{} { i := AllocSession(); return &i }(), "1=1")
|
||||
args := tt.setup()
|
||||
|
||||
got, err := repo.TerminateCompletedSessions(context.Background())
|
||||
|
||||
@@ -124,7 +124,7 @@ func TestState_Delete(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
|
||||
var initialState State
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "session_id = ? and state = ?", tt.state.SessionId, tt.state.Status)
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "session_id = ? and state = ?", []interface{}{tt.state.SessionId, tt.state.Status})
|
||||
require.NoError(err)
|
||||
|
||||
deleteState := allocState()
|
||||
@@ -146,7 +146,7 @@ func TestState_Delete(t *testing.T) {
|
||||
}
|
||||
assert.Equal(tt.wantRowsDeleted, deletedRows)
|
||||
foundState := allocState()
|
||||
err = rw.LookupWhere(context.Background(), &foundState, "session_id = ? and start_time = ?", tt.state.SessionId, initialState.StartTime)
|
||||
err = rw.LookupWhere(context.Background(), &foundState, "session_id = ? and start_time = ?", []interface{}{tt.state.SessionId, initialState.StartTime})
|
||||
require.Error(err)
|
||||
assert.True(errors.IsNotFoundError(err))
|
||||
})
|
||||
|
||||
@@ -64,7 +64,7 @@ func Test_TestConnectionState(t *testing.T) {
|
||||
|
||||
rw := db.New(conn)
|
||||
var initialState ConnectionState
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "connection_id = ? and state = ?", cs.ConnectionId, cs.Status)
|
||||
err := rw.LookupWhere(context.Background(), &initialState, "connection_id = ? and state = ?", []interface{}{cs.ConnectionId, cs.Status})
|
||||
require.NoError(err)
|
||||
assert.NotEmpty(initialState.StartTime)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user