Skip to content

Commit bc4439f

Browse files
authored
Merge pull request #988 from ellemouton/sql17Sessions9
[sql-17] sessions: test preparation
2 parents ee46094 + 44625c3 commit bc4439f

File tree

5 files changed

+72
-37
lines changed

5 files changed

+72
-37
lines changed

itest/litd_firewall_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,9 @@ func testSessionLinking(net *NetworkHarness, t *harnessTest) {
866866
LinkedGroupId: sessResp.Session.GroupId,
867867
},
868868
)
869-
require.ErrorContains(t.t, err, "is still active")
869+
require.ErrorContains(
870+
t.t, err, session.ErrSessionsInGroupStillActive.Error(),
871+
)
870872

871873
// Revoke the previous one and repeat.
872874
_, err = litAutopilotClient.RevokeAutopilotSession(

session/errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,16 @@ var (
66
// ErrSessionNotFound is an error returned when we attempt to retrieve
77
// information about a session but it is not found.
88
ErrSessionNotFound = errors.New("session not found")
9+
10+
// ErrUnknownGroup is returned when an attempt is made to insert a
11+
// session and link it to an existing group where the group is not
12+
// known.
13+
ErrUnknownGroup = errors.New("unknown group")
14+
15+
// ErrSessionsInGroupStillActive is returned when an attempt is made to
16+
// insert a session and link it to a group that still has other active
17+
// sessions.
18+
ErrSessionsInGroupStillActive = errors.New(
19+
"group has active sessions",
20+
)
921
)

session/kvdb_store.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
224224
if session.ID != session.GroupID {
225225
_, err = getKeyForID(sessionBucket, session.GroupID)
226226
if err != nil {
227-
return fmt.Errorf("unknown linked session "+
228-
"%x: %w", session.GroupID, err)
227+
return fmt.Errorf("%w: unknown linked "+
228+
"session %x: %w", ErrUnknownGroup,
229+
session.GroupID, err)
229230
}
230231

231232
// Fetch all the session IDs for this group. This will
@@ -237,18 +238,22 @@ func (db *BoltStore) NewSession(label string, typ Type, expiry time.Time,
237238
return err
238239
}
239240

241+
// Ensure that the all the linked sessions are no longer
242+
// active.
240243
for _, id := range sessionIDs {
241244
sess, err := getSessionByID(sessionBucket, id)
242245
if err != nil {
243246
return err
244247
}
245248

246-
// Ensure that the session is no longer active.
247-
if !sess.State.Terminal() {
248-
return fmt.Errorf("session (id=%x) "+
249-
"in group %x is still active",
250-
sess.ID, sess.GroupID)
249+
if sess.State.Terminal() {
250+
continue
251251
}
252+
253+
return fmt.Errorf("%w: session (id=%x) in "+
254+
"group %x is still active",
255+
ErrSessionsInGroupStillActive, sess.ID,
256+
sess.GroupID)
252257
}
253258
}
254259

@@ -625,14 +630,14 @@ func (db *BoltStore) GetGroupID(sessionID ID) (ID, error) {
625630

626631
sessionIDBkt := idIndex.Bucket(sessionID[:])
627632
if sessionIDBkt == nil {
628-
return fmt.Errorf("no index entry for session ID: %x",
629-
sessionID)
633+
return fmt.Errorf("%w: no index entry for session "+
634+
"ID: %x", ErrUnknownGroup, sessionID)
630635
}
631636

632637
groupIDBytes := sessionIDBkt.Get(groupIDKey)
633638
if len(groupIDBytes) == 0 {
634-
return fmt.Errorf("group ID not found for session "+
635-
"ID %x", sessionID)
639+
return fmt.Errorf("%w: group ID not found for "+
640+
"session ID %x", ErrUnknownGroup, sessionID)
636641
}
637642

638643
copy(groupID[:], groupIDBytes)
@@ -801,7 +806,7 @@ func addIDToGroupIDPair(sessionBkt *bbolt.Bucket, id, groupID ID) error {
801806
func getSessionByID(bucket *bbolt.Bucket, id ID) (*Session, error) {
802807
keyBytes, err := getKeyForID(bucket, id)
803808
if err != nil {
804-
return nil, err
809+
return nil, fmt.Errorf("%w: %w", ErrSessionNotFound, err)
805810
}
806811

807812
v := bucket.Get(keyBytes)

session/store_test.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ var testTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
1616
func TestBasicSessionStore(t *testing.T) {
1717
// Set up a new DB.
1818
clock := clock.NewTestClock(testTime)
19-
db, err := NewDB(t.TempDir(), "test.db", clock)
20-
require.NoError(t, err)
21-
t.Cleanup(func() {
22-
_ = db.Close()
23-
})
19+
db := NewTestDB(t, clock)
20+
21+
// Try fetch a session that doesn't exist yet.
22+
_, err := db.GetSessionByID(ID{1, 3, 4, 4})
23+
require.ErrorIs(t, err, ErrSessionNotFound)
2424

2525
// Reserve a session. This should succeed.
2626
s1, err := reserveSession(db, "session 1")
@@ -183,7 +183,7 @@ func TestBasicSessionStore(t *testing.T) {
183183
require.Empty(t, sessions)
184184

185185
_, err = db.GetGroupID(s4.ID)
186-
require.ErrorContains(t, err, "no index entry")
186+
require.ErrorIs(t, err, ErrUnknownGroup)
187187

188188
// Only session 1 should remain in this group.
189189
sessIDs, err = db.GetSessionIDs(s4.GroupID)
@@ -197,11 +197,7 @@ func TestLinkingSessions(t *testing.T) {
197197

198198
// Set up a new DB.
199199
clock := clock.NewTestClock(testTime)
200-
db, err := NewDB(t.TempDir(), "test.db", clock)
201-
require.NoError(t, err)
202-
t.Cleanup(func() {
203-
_ = db.Close()
204-
})
200+
db := NewTestDB(t, clock)
205201

206202
groupID, err := IDFromBytes([]byte{1, 2, 3, 4})
207203
require.NoError(t, err)
@@ -211,7 +207,7 @@ func TestLinkingSessions(t *testing.T) {
211207
_, err = reserveSession(
212208
db, "session 2", withLinkedGroupID(&groupID),
213209
)
214-
require.ErrorContains(t, err, "unknown linked session")
210+
require.ErrorIs(t, err, ErrUnknownGroup)
215211

216212
// Create a new session with no previous link.
217213
s1 := createSession(t, db, "session 1")
@@ -220,7 +216,7 @@ func TestLinkingSessions(t *testing.T) {
220216
// session. This should fail due to the first session still being
221217
// active.
222218
_, err = reserveSession(db, "session 2", withLinkedGroupID(&s1.GroupID))
223-
require.ErrorContains(t, err, "is still active")
219+
require.ErrorIs(t, err, ErrSessionsInGroupStillActive)
224220

225221
// Revoke the first session.
226222
require.NoError(t, db.ShiftState(s1.ID, StateRevoked))
@@ -238,11 +234,7 @@ func TestLinkedSessions(t *testing.T) {
238234

239235
// Set up a new DB.
240236
clock := clock.NewTestClock(testTime)
241-
db, err := NewDB(t.TempDir(), "test.db", clock)
242-
require.NoError(t, err)
243-
t.Cleanup(func() {
244-
_ = db.Close()
245-
})
237+
db := NewTestDB(t, clock)
246238

247239
// Create a few sessions. The first one is a new session and the two
248240
// after are all linked to the prior one. All these sessions belong to
@@ -294,18 +286,14 @@ func TestLinkedSessions(t *testing.T) {
294286
func TestStateShift(t *testing.T) {
295287
// Set up a new DB.
296288
clock := clock.NewTestClock(testTime)
297-
db, err := NewDB(t.TempDir(), "test.db", clock)
298-
require.NoError(t, err)
299-
t.Cleanup(func() {
300-
_ = db.Close()
301-
})
289+
db := NewTestDB(t, clock)
302290

303291
// Add a new session to the DB.
304292
s1 := createSession(t, db, "label 1")
305293

306294
// Check that the session is in the StateCreated state. Also check that
307295
// the "RevokedAt" time has not yet been set.
308-
s1, err = db.GetSession(s1.LocalPublicKey)
296+
s1, err := db.GetSession(s1.LocalPublicKey)
309297
require.NoError(t, err)
310298
require.Equal(t, StateCreated, s1.State)
311299
require.Equal(t, time.Time{}, s1.RevokedAt)

session/test_kvdb.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package session
2+
3+
import (
4+
"testing"
5+
6+
"github.com/lightningnetwork/lnd/clock"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
// NewTestDB is a helper function that creates an BBolt database for testing.
11+
func NewTestDB(t *testing.T, clock clock.Clock) *BoltStore {
12+
return NewTestDBFromPath(t, t.TempDir(), clock)
13+
}
14+
15+
// NewTestDBFromPath is a helper function that creates a new BoltStore with a
16+
// connection to an existing BBolt database for testing.
17+
func NewTestDBFromPath(t *testing.T, dbPath string,
18+
clock clock.Clock) *BoltStore {
19+
20+
store, err := NewDB(dbPath, DBFilename, clock)
21+
require.NoError(t, err)
22+
23+
t.Cleanup(func() {
24+
require.NoError(t, store.DB.Close())
25+
})
26+
27+
return store
28+
}

0 commit comments

Comments
 (0)