Skip to content

Commit ec5b512

Browse files
committed
itest: optimise the LNC tests
To speed up the existing LNC auth tests, this commit creates a single LNC session & connection and does all the tests over the single connection rather than spinning up one per test. This imporves the time taken to run the tests drastically.
1 parent 61792bf commit ec5b512

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

itest/litd_mode_integrated_test.go

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,21 @@ func testModeIntegrated(net *NetworkHarness, t *harnessTest) {
367367
t.t.Run("lnc auth", func(tt *testing.T) {
368368
cfg := net.Alice.Cfg
369369

370+
ctx := context.Background()
371+
ctxt, cancel := context.WithTimeout(ctx, defaultTimeout)
372+
defer cancel()
373+
374+
rawLNCConn := setUpLNCConn(
375+
ctxt, t.t, cfg.LitAddr(), cfg.TLSCertPath,
376+
cfg.LitMacPath,
377+
)
378+
defer rawLNCConn.Close()
379+
370380
for _, endpoint := range endpoints {
371381
endpoint := endpoint
372382
tt.Run(endpoint.name+" lit port", func(ttt *testing.T) {
373383
runLNCAuthTest(
374-
ttt, cfg.LitAddr(), cfg.TLSCertPath,
375-
cfg.LitMacPath, endpoint.requestFn,
384+
ttt, rawLNCConn, endpoint.requestFn,
376385
endpoint.successPattern,
377386
endpoint.allowedThroughLNC,
378387
)
@@ -381,6 +390,41 @@ func testModeIntegrated(net *NetworkHarness, t *harnessTest) {
381390
})
382391
}
383392

393+
// setUpLNCConn creates a new LNC session and then creates a connection to that
394+
// session via the mailbox that the session was created with.
395+
func setUpLNCConn(ctx context.Context, t *testing.T, hostPort, tlsCertPath,
396+
macPath string) *grpc.ClientConn {
397+
398+
rawConn, err := connectRPC(ctx, hostPort, tlsCertPath)
399+
require.NoError(t, err)
400+
401+
macBytes, err := ioutil.ReadFile(macPath)
402+
require.NoError(t, err)
403+
ctxm := macaroonContext(ctx, macBytes)
404+
405+
// We first need to create an LNC session that we can use to connect.
406+
litClient := litrpc.NewSessionsClient(rawConn)
407+
sessResp, err := litClient.AddSession(ctxm, &litrpc.AddSessionRequest{
408+
Label: "integration-test",
409+
SessionType: litrpc.SessionType_TYPE_MACAROON_READONLY,
410+
ExpiryTimestampSeconds: uint64(
411+
time.Now().Add(5 * time.Minute).Unix(),
412+
),
413+
MailboxServerAddr: mailboxServerAddr,
414+
})
415+
require.NoError(t, err)
416+
417+
// Try the LNC connection now.
418+
connectPhrase := strings.Split(
419+
sessResp.Session.PairingSecretMnemonic, " ",
420+
)
421+
422+
rawLNCConn, err := connectMailbox(ctx, connectPhrase)
423+
require.NoError(t, err)
424+
425+
return rawLNCConn
426+
}
427+
384428
// runCertificateCheck checks that the TLS certificates presented to clients are
385429
// what we expect them to be.
386430
func runCertificateCheck(t *testing.T, node *HarnessNode) {
@@ -624,44 +668,14 @@ func runRESTAuthTest(t *testing.T, hostPort, uiPassword, macaroonPath, restURI,
624668

625669
// runLNCAuthTest tests authentication of the given interface when connecting
626670
// through Lightning Node Connect.
627-
func runLNCAuthTest(t *testing.T, hostPort, tlsCertPath, macPath string,
671+
func runLNCAuthTest(t *testing.T, rawLNCConn grpc.ClientConnInterface,
628672
makeRequest requestFn, successContent string, callAllowed bool) {
629673

630-
ctxb := context.Background()
631-
ctxt, cancel := context.WithTimeout(ctxb, defaultTimeout)
632-
defer cancel()
633-
634-
rawConn, err := connectRPC(ctxt, hostPort, tlsCertPath)
635-
require.NoError(t, err)
636-
637-
macBytes, err := ioutil.ReadFile(macPath)
638-
require.NoError(t, err)
639-
ctxm := macaroonContext(ctxt, macBytes)
640-
641-
// We first need to create an LNC session that we can use to connect.
642-
// We use the UI password to create the session.
643-
litClient := litrpc.NewSessionsClient(rawConn)
644-
sessResp, err := litClient.AddSession(ctxm, &litrpc.AddSessionRequest{
645-
Label: "integration-test",
646-
SessionType: litrpc.SessionType_TYPE_MACAROON_READONLY,
647-
ExpiryTimestampSeconds: uint64(
648-
time.Now().Add(5 * time.Minute).Unix(),
649-
),
650-
MailboxServerAddr: mailboxServerAddr,
651-
})
652-
require.NoError(t, err)
653-
654-
// Try the LNC connection now.
655-
connectPhrase := strings.Split(
656-
sessResp.Session.PairingSecretMnemonic, " ",
674+
ctxt, cancel := context.WithTimeout(
675+
context.Background(), defaultTimeout,
657676
)
658-
659-
ctxt, cancel = context.WithTimeout(ctxb, defaultTimeout)
660677
defer cancel()
661678

662-
rawLNCConn, err := connectMailbox(ctxt, connectPhrase)
663-
require.NoError(t, err)
664-
665679
// We should be able to make a request via LNC to the given RPC
666680
// endpoint, unless it is explicitly disallowed (we currently don't want
667681
// to support creating more sessions through LNC until we have all
@@ -767,7 +781,7 @@ func getServerCertificates(hostPort string) ([]*x509.Certificate, error) {
767781
// connectMailbox tries to establish a connection through LNC using the given
768782
// connect phrase and the test mailbox server.
769783
func connectMailbox(ctx context.Context,
770-
connectPhrase []string) (grpc.ClientConnInterface, error) {
784+
connectPhrase []string) (*grpc.ClientConn, error) {
771785

772786
var mnemonicWords [mailbox.NumPassphraseWords]string
773787
copy(mnemonicWords[:], connectPhrase)

itest/litd_mode_remote_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,21 @@ func testModeRemote(net *NetworkHarness, t *harnessTest) {
134134
t.t.Run("lnc auth", func(tt *testing.T) {
135135
cfg := net.Bob.Cfg
136136

137+
ctx := context.Background()
138+
ctxt, cancel := context.WithTimeout(ctx, defaultTimeout)
139+
defer cancel()
140+
141+
rawLNCConn := setUpLNCConn(
142+
ctxt, tt, cfg.LitAddr(), cfg.LitTLSCertPath,
143+
cfg.LitMacPath,
144+
)
145+
defer rawLNCConn.Close()
146+
137147
for _, endpoint := range endpoints {
138148
endpoint := endpoint
139149
tt.Run(endpoint.name+" lit port", func(ttt *testing.T) {
140150
runLNCAuthTest(
141-
ttt, cfg.LitAddr(), cfg.LitTLSCertPath,
142-
cfg.LitMacPath, endpoint.requestFn,
151+
ttt, rawLNCConn, endpoint.requestFn,
143152
endpoint.successPattern,
144153
endpoint.allowedThroughLNC,
145154
)

0 commit comments

Comments
 (0)