|
|
|
|
@ -159,7 +159,6 @@ func TestRepository_ListSession(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(10, len(testSessions))
|
|
|
|
|
withIds := []string{testSessions[0].PublicId, testSessions[1].PublicId}
|
|
|
|
|
conn.LogMode(true)
|
|
|
|
|
got, err := repo.ListSessions(context.Background(), WithSessionIds(withIds...), WithOrder("create_time asc"))
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.Equal(2, len(got))
|
|
|
|
|
@ -715,6 +714,222 @@ func TestRepository_TerminateSession(t *testing.T) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRepository_TerminateCompletedSessions(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
rw := db.New(conn)
|
|
|
|
|
wrapper := db.TestWrapper(t)
|
|
|
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
|
|
|
kms := kms.TestKms(t, conn, wrapper)
|
|
|
|
|
repo, err := NewRepository(rw, rw, kms)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
setupFn := func(limit int32, expireIn time.Duration, leaveOpen bool) *Session {
|
|
|
|
|
require.NotEqualf(t, int32(0), limit, "setupFn: limit cannot be zero")
|
|
|
|
|
exp, err := ptypes.TimestampProto(time.Now().Add(expireIn))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
composedOf := TestSessionParams(t, conn, wrapper, iamRepo)
|
|
|
|
|
composedOf.ConnectionLimit = limit
|
|
|
|
|
composedOf.ExpirationTime = ×tamp.Timestamp{Timestamp: exp}
|
|
|
|
|
s := TestSession(t, conn, wrapper, composedOf)
|
|
|
|
|
|
|
|
|
|
srv := TestWorker(t, conn, wrapper)
|
|
|
|
|
tofu := TestTofu(t)
|
|
|
|
|
s, _, err = repo.ActivateSession(context.Background(), s.PublicId, s.Version, srv.PrivateId, srv.Type, tofu)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
c := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 222)
|
|
|
|
|
if !leaveOpen {
|
|
|
|
|
cw := CloseWith{
|
|
|
|
|
ConnectionId: c.PublicId,
|
|
|
|
|
BytesUp: 1,
|
|
|
|
|
BytesDown: 1,
|
|
|
|
|
ClosedReason: ConnectionClosedByUser,
|
|
|
|
|
}
|
|
|
|
|
_, err = repo.CloseConnections(context.Background(), []CloseWith{cw})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type testArgs struct {
|
|
|
|
|
sessions []*Session
|
|
|
|
|
wantTermed map[string]TerminationReason
|
|
|
|
|
}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
setup func() testArgs
|
|
|
|
|
wantErr bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "sessions-with-closed-connections",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 1
|
|
|
|
|
wantTermed := map[string]TerminationReason{}
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
// make one with closed connections
|
|
|
|
|
s := setupFn(1, time.Hour+1, false)
|
|
|
|
|
wantTermed[s.PublicId] = ConnectionLimit
|
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
|
|
|
|
|
|
// make one with connection left open
|
|
|
|
|
s2 := setupFn(1, time.Hour+1, true)
|
|
|
|
|
sessions = append(sessions, s2)
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: wantTermed,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "sessions-with-open-and-closed-connections",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 5
|
|
|
|
|
wantTermed := map[string]TerminationReason{}
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
// make one with closed connections
|
|
|
|
|
s := setupFn(2, time.Hour+1, false)
|
|
|
|
|
_ = TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 222)
|
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
|
wantTermed[s.PublicId] = ConnectionLimit
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: nil,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "sessions-with-no-connections",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 5
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
s := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: nil,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "sessions-with-open-connections",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 5
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
s := setupFn(1, time.Hour+1, true)
|
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: nil,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "expired-sessions",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 5
|
|
|
|
|
wantTermed := map[string]TerminationReason{}
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
// make one with closed connections
|
|
|
|
|
s := setupFn(1, time.Millisecond+1, false)
|
|
|
|
|
// make one with connection left open
|
|
|
|
|
s2 := setupFn(1, time.Millisecond+1, true)
|
|
|
|
|
sessions = append(sessions, s, s2)
|
|
|
|
|
wantTermed[s.PublicId] = TimedOut
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: wantTermed,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "canceled-sessions-with-closed-connections",
|
|
|
|
|
setup: func() testArgs {
|
|
|
|
|
cnt := 1
|
|
|
|
|
wantTermed := map[string]TerminationReason{}
|
|
|
|
|
sessions := make([]*Session, 0, 5)
|
|
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
|
// make one with limit of 3 and closed connections
|
|
|
|
|
s := setupFn(3, time.Hour+1, false)
|
|
|
|
|
wantTermed[s.PublicId] = SessionCanceled
|
|
|
|
|
sessions = append(sessions, s)
|
|
|
|
|
|
|
|
|
|
// make one with connection left open
|
|
|
|
|
s2 := setupFn(1, time.Hour+1, true)
|
|
|
|
|
sessions = append(sessions, s2)
|
|
|
|
|
|
|
|
|
|
// now cancel the sessions
|
|
|
|
|
for _, ses := range sessions {
|
|
|
|
|
_, err := repo.CancelSession(context.Background(), ses.PublicId, ses.Version)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return testArgs{
|
|
|
|
|
sessions: sessions,
|
|
|
|
|
wantTermed: wantTermed,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
args := tt.setup()
|
|
|
|
|
|
|
|
|
|
got, err := repo.TerminateCompletedSessions(context.Background())
|
|
|
|
|
if tt.wantErr {
|
|
|
|
|
require.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
assert.NoError(err)
|
|
|
|
|
assert.Equal(len(args.wantTermed), got)
|
|
|
|
|
|
|
|
|
|
for _, ses := range args.sessions {
|
|
|
|
|
found, _, err := repo.LookupSession(context.Background(), ses.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
_, shouldBeTerminated := args.wantTermed[found.PublicId]
|
|
|
|
|
if shouldBeTerminated {
|
|
|
|
|
assert.Equal(args.wantTermed[found.PublicId].String(), found.TerminationReason)
|
|
|
|
|
t.Logf("terminated %s has a connection limit of %d", found.PublicId, found.ConnectionLimit)
|
|
|
|
|
conn, err := repo.ListConnections(context.Background(), found.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
for _, sc := range conn {
|
|
|
|
|
c, cs, err := repo.LookupConnection(context.Background(), sc.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
assert.NotEmpty(c.ClosedReason)
|
|
|
|
|
for _, s := range cs {
|
|
|
|
|
t.Logf("%s session %s connection state %s at %s", found.PublicId, s.ConnectionId, s.Status, s.EndTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
t.Logf("not terminated %s has a connection limit of %d", found.PublicId, found.ConnectionLimit)
|
|
|
|
|
assert.Equal("", found.TerminationReason)
|
|
|
|
|
conn, err := repo.ListConnections(context.Background(), found.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
for _, sc := range conn {
|
|
|
|
|
cs, err := fetchConnectionStates(context.Background(), rw, sc.PublicId)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
for _, s := range cs {
|
|
|
|
|
t.Logf("%s session %s connection state %s at %s", found.PublicId, s.ConnectionId, s.Status, s.EndTime)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestRepository_CloseConnections(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
|
|
|
|