* 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
213 lines
5.8 KiB
Go
213 lines
5.8 KiB
Go
package session
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestConnectionState_Create(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
session := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
connection := TestConnection(t, conn, session.PublicId, "127.0.0.1", 443, "127.0.0.1", 4443, "127.0.0.1")
|
|
|
|
type args struct {
|
|
connectionId string
|
|
status ConnectionStatus
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want *ConnectionState
|
|
wantErr bool
|
|
wantIsErr errors.Code
|
|
create bool
|
|
wantCreateErr bool
|
|
}{
|
|
{
|
|
name: "valid",
|
|
args: args{
|
|
connectionId: connection.PublicId,
|
|
status: StatusClosed,
|
|
},
|
|
want: &ConnectionState{
|
|
ConnectionId: connection.PublicId,
|
|
Status: StatusClosed,
|
|
},
|
|
create: true,
|
|
},
|
|
{
|
|
name: "empty-connectionId",
|
|
args: args{
|
|
status: StatusClosed,
|
|
},
|
|
wantErr: true,
|
|
wantIsErr: errors.InvalidParameter,
|
|
},
|
|
{
|
|
name: "empty-status",
|
|
args: args{
|
|
connectionId: connection.PublicId,
|
|
},
|
|
wantErr: true,
|
|
wantIsErr: errors.InvalidParameter,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
got, err := NewConnectionState(tt.args.connectionId, tt.args.status)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
assert.True(errors.Match(errors.T(tt.wantIsErr), err))
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
assert.Equal(tt.want, got)
|
|
if tt.create {
|
|
err = db.New(conn).Create(context.Background(), got)
|
|
if tt.wantCreateErr {
|
|
assert.Error(err)
|
|
return
|
|
} else {
|
|
assert.NoError(err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConnectionState_Delete(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
s := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
c := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
c2 := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
|
|
tests := []struct {
|
|
name string
|
|
state *ConnectionState
|
|
deleteConnectionStateId string
|
|
wantRowsDeleted int
|
|
wantErr bool
|
|
wantErrMsg string
|
|
}{
|
|
{
|
|
name: "valid",
|
|
state: TestConnectionState(t, conn, c.PublicId, StatusClosed),
|
|
wantErr: false,
|
|
wantRowsDeleted: 1,
|
|
},
|
|
{
|
|
name: "bad-id",
|
|
state: TestConnectionState(t, conn, c2.PublicId, StatusClosed),
|
|
deleteConnectionStateId: func() string {
|
|
id, err := db.NewPublicId(ConnectionStatePrefix)
|
|
require.NoError(t, err)
|
|
return id
|
|
}(),
|
|
wantErr: false,
|
|
wantRowsDeleted: 0,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
var initialState ConnectionState
|
|
err := rw.LookupWhere(context.Background(), &initialState, "connection_id = ? and state = ?", []interface{}{tt.state.ConnectionId, tt.state.Status})
|
|
require.NoError(err)
|
|
|
|
deleteState := allocConnectionState()
|
|
if tt.deleteConnectionStateId != "" {
|
|
deleteState.ConnectionId = tt.deleteConnectionStateId
|
|
} else {
|
|
deleteState.ConnectionId = tt.state.ConnectionId
|
|
}
|
|
deleteState.StartTime = initialState.StartTime
|
|
deletedRows, err := rw.Delete(context.Background(), &deleteState)
|
|
if tt.wantErr {
|
|
require.Error(err)
|
|
return
|
|
}
|
|
require.NoError(err)
|
|
if tt.wantRowsDeleted == 0 {
|
|
assert.Equal(tt.wantRowsDeleted, deletedRows)
|
|
return
|
|
}
|
|
assert.Equal(tt.wantRowsDeleted, deletedRows)
|
|
foundState := allocConnectionState()
|
|
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))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConnectionState_Clone(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
t.Run("valid", func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
s := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
c := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
state := TestConnectionState(t, conn, c.PublicId, StatusConnected)
|
|
cp := state.Clone()
|
|
assert.Equal(cp.(*ConnectionState), state)
|
|
})
|
|
t.Run("not-equal", func(t *testing.T) {
|
|
assert := assert.New(t)
|
|
s := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
c := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
state := TestConnectionState(t, conn, c.PublicId, StatusConnected)
|
|
state2 := TestConnectionState(t, conn, c.PublicId, StatusConnected)
|
|
|
|
cp := state.Clone()
|
|
assert.NotEqual(cp.(*ConnectionState), state2)
|
|
})
|
|
}
|
|
|
|
func TestConnectionState_SetTableName(t *testing.T) {
|
|
t.Parallel()
|
|
defaultTableName := defaultConnectionStateTableName
|
|
tests := []struct {
|
|
name string
|
|
setNameTo string
|
|
want string
|
|
}{
|
|
{
|
|
name: "new-name",
|
|
setNameTo: "new-name",
|
|
want: "new-name",
|
|
},
|
|
{
|
|
name: "reset to default",
|
|
setNameTo: "",
|
|
want: defaultTableName,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
def := allocConnectionState()
|
|
require.Equal(defaultTableName, def.TableName())
|
|
s := allocConnectionState()
|
|
s.SetTableName(tt.setNameTo)
|
|
assert.Equal(tt.want, s.TableName())
|
|
})
|
|
}
|
|
}
|