Skip to content

Commit b6d72c3

Browse files
committed
itest: test both client transport modes
In this commit, various changes are made to the itests so that all tests are run with the various different setups.
1 parent 0562016 commit b6d72c3

File tree

6 files changed

+133
-162
lines changed

6 files changed

+133
-162
lines changed

itest/client_harness.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import (
1212
"github.com/lightninglabs/lightning-node-connect/mailbox"
1313
"github.com/lightningnetwork/lnd/keychain"
1414
"google.golang.org/grpc"
15+
"google.golang.org/grpc/credentials"
1516
)
1617

1718
type clientHarness struct {
1819
serverAddr string
20+
grpcClient bool
21+
tlsConfig *tls.Config
1922

2023
grpcConn *grpc.ClientConn
2124
clientConn mockrpc.MockServiceClient
@@ -28,15 +31,24 @@ type clientHarness struct {
2831
cancel func()
2932
}
3033

31-
func newClientHarness(serverAddress string, entropy []byte) (*clientHarness,
32-
error) {
34+
func newClientHarness(serverAddress string, entropy []byte, insecure,
35+
grpcClient bool) (*clientHarness, error) {
3336

3437
privKey, err := btcec.NewPrivateKey()
3538
if err != nil {
3639
return nil, err
3740
}
3841

42+
tlsConfig := &tls.Config{}
43+
if insecure {
44+
tlsConfig = &tls.Config{
45+
InsecureSkipVerify: true,
46+
}
47+
}
48+
3949
return &clientHarness{
50+
tlsConfig: tlsConfig,
51+
grpcClient: grpcClient,
4052
serverAddr: serverAddress,
4153
passphraseEntropy: entropy,
4254
localStatic: &keychain.PrivKeyECDH{PrivKey: privKey},
@@ -62,18 +74,27 @@ func (c *clientHarness) start() error {
6274
},
6375
)
6476

65-
transportConn, err := mailbox.NewWebsocketsClient(
66-
ctx, c.serverAddr, connData,
67-
)
77+
var err error
78+
if c.grpcClient {
79+
c.client, err = mailbox.NewGrpcClient(
80+
ctx, c.serverAddr, connData,
81+
grpc.WithTransportCredentials(
82+
credentials.NewTLS(c.tlsConfig),
83+
),
84+
)
85+
} else {
86+
c.client, err = mailbox.NewWebsocketsClient(
87+
ctx, c.serverAddr, connData,
88+
)
89+
}
6890
if err != nil {
6991
return err
7092
}
71-
c.client = transportConn
7293

7394
noiseConn := mailbox.NewNoiseGrpcConn(connData)
7495

7596
dialOpts := []grpc.DialOption{
76-
grpc.WithContextDialer(transportConn.Dial),
97+
grpc.WithContextDialer(c.client.Dial),
7798
grpc.WithTransportCredentials(noiseConn),
7899
grpc.WithPerRPCCredentials(noiseConn),
79100
grpc.WithDefaultCallOptions(

itest/connection_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func testHashmailServerReconnect(t *harnessTest) {
4646

4747
// Shut down hashmail server
4848
require.NoError(t.t, t.hmserver.stop())
49-
t.t.Logf("")
5049

5150
// Check that the client and server status are updated appropriately.
5251
assertServerStatus(t, mailbox.ServerStatusNotConnected)

itest/integration_test.go

Lines changed: 36 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,61 @@
11
package itest
22

33
import (
4+
"fmt"
45
"testing"
56
)
67

7-
const testnetMailbox = "mailbox.testnet.lightningcluster.com:443"
8-
98
// TestLightningNodeConnect runs the itests against a local mailbox instance.
109
func TestLightningNodeConnect(t *testing.T) {
1110
// If no tests are registered, then we can exit early.
1211
if len(testCases) == 0 {
1312
t.Skip("integration tests not selected")
1413
}
1514

16-
ht := newHarnessTest(t, nil, nil, nil)
17-
ht.setupLogging()
15+
setupLogging(t)
16+
17+
testConfigs := []*testConfig{
18+
{stagingMailbox: false, grpcClientConn: false},
19+
{stagingMailbox: false, grpcClientConn: true},
20+
{stagingMailbox: true, grpcClientConn: false},
21+
{stagingMailbox: true, grpcClientConn: true},
22+
}
1823

1924
t.Logf("Running %v integration tests", len(testCases))
2025
for _, testCase := range testCases {
2126

2227
testCase := testCase
28+
for _, config := range testConfigs {
29+
config := config
2330

24-
success := t.Run(testCase.name, func(t1 *testing.T) {
25-
clientHarness, serverHarness, hashmailHarness := setupHarnesses(t1)
26-
27-
ht := newHarnessTest(
28-
t1, clientHarness, serverHarness,
29-
hashmailHarness,
30-
)
31-
32-
// Now we have everything to run the test case.
33-
ht.RunTestCase(testCase)
34-
35-
// Shut down both client, server and hashmail server
36-
// to remove all state.
37-
err := ht.shutdown()
38-
if err != nil {
39-
t1.Fatalf("error shutting down harness: %v",
40-
err)
31+
if config.stagingMailbox && testCase.localMailboxOnly {
32+
continue
4133
}
42-
})
43-
44-
// Close at the first failure. Mimic behavior of original test
45-
// framework.
46-
if !success {
47-
break
48-
}
49-
}
50-
}
51-
52-
// TestLightningNodeConnectTestnetMailbox runs the itests using the mailbox
53-
// running in the LL testnet environment.
54-
func TestLightningNodeConnectTestnetMailbox(t *testing.T) {
55-
// If no tests are registered, then we can exit early.
56-
if len(stagingMailboxTests) == 0 {
57-
t.Skip("integration tests not selected")
58-
}
5934

60-
ht := newHarnessTest(t, nil, nil, nil)
61-
ht.setupLogging()
62-
63-
t.Logf("Running %v integration tests", len(stagingMailboxTests))
64-
for _, testCase := range stagingMailboxTests {
65-
66-
testCase := testCase
67-
68-
success := t.Run(testCase.name, func(t1 *testing.T) {
69-
clientHarness, serverHarness :=
70-
setupClientAndServerHarnesses(
71-
t1, testnetMailbox, false,
72-
)
73-
74-
ht := newHarnessTest(
75-
t1, clientHarness, serverHarness, nil,
76-
)
77-
78-
// Now we have everything to run the test case.
79-
ht.RunTestCase(testCase)
80-
81-
// Shut down both client, server and hashmail server
82-
// to remove all state.
83-
err := ht.shutdown()
84-
if err != nil {
85-
t1.Fatalf("error shutting down harness: %v",
86-
err)
35+
name := fmt.Sprintf("%s(stagingMailbox:%t,"+
36+
"grpcClientConn:%t)", testCase.name,
37+
config.stagingMailbox, config.grpcClientConn)
38+
39+
success := t.Run(name, func(t1 *testing.T) {
40+
ht := newHarnessTest(t1, config)
41+
42+
// Now we have everything to run the test case.
43+
ht.RunTestCase(testCase)
44+
45+
// Shut down both client, server and hashmail
46+
// server to remove all state.
47+
err := ht.shutdown()
48+
if err != nil {
49+
t1.Fatalf("error shutting down "+
50+
"harness: %v", err)
51+
}
52+
})
53+
54+
// Close at the first failure. Mimic behavior of
55+
// original test framework.
56+
if !success {
57+
break
8758
}
88-
})
89-
90-
// Close at the first failure. Mimic behavior of original test
91-
// framework.
92-
if !success {
93-
break
9459
}
9560
}
9661
}

itest/test_harness.go

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ import (
1717

1818
var interceptor *signal.Interceptor
1919

20+
const testnetMailbox = "mailbox.testnet.lightningcluster.com:443"
21+
2022
// testCase is a struct that holds a single test case.
2123
type testCase struct {
2224
name string
2325
test func(t *harnessTest)
26+
27+
// localMailboxOnly indicates if the test should only be run against a
28+
// local mailbox instance and not against the staging mailbox.
29+
localMailboxOnly bool
2430
}
2531

2632
// harnessTest wraps a regular testing.T providing enhanced error detection
@@ -41,12 +47,59 @@ type harnessTest struct {
4147
hmserver *hashmailHarness
4248
}
4349

50+
// testConfig determines the way in which the test will be set up.
51+
type testConfig struct {
52+
stagingMailbox bool
53+
grpcClientConn bool
54+
}
55+
4456
// newHarnessTest creates a new instance of a harnessTest from a regular
4557
// testing.T instance.
46-
func newHarnessTest(t *testing.T, client *clientHarness, server *serverHarness,
47-
hashmail *hashmailHarness) *harnessTest {
58+
func newHarnessTest(t *testing.T, cfg *testConfig) *harnessTest {
59+
ht := &harnessTest{t: t}
60+
61+
mailboxAddr := testnetMailbox
62+
var insecure bool
63+
if !cfg.stagingMailbox {
64+
ht.hmserver = newHashmailHarness()
65+
if err := ht.hmserver.start(); err != nil {
66+
t.Fatalf("could not start hashmail server: %v", err)
67+
}
68+
mailboxAddr = ht.hmserver.apertureCfg.ListenAddr
69+
insecure = true
70+
}
71+
72+
var err error
73+
ht.server, err = newServerHarness(mailboxAddr, insecure)
74+
require.NoError(t, err)
75+
require.NoError(t, ht.server.start())
76+
t.Cleanup(func() {
77+
ht.server.stop()
78+
})
79+
80+
select {
81+
case err := <-ht.server.errChan:
82+
if err != nil {
83+
t.Fatalf("could not start server: %v", err)
84+
}
85+
default:
86+
}
4887

49-
return &harnessTest{t, nil, client, server, hashmail}
88+
// Give the server some time to set up the first mailbox
89+
time.Sleep(1000 * time.Millisecond)
90+
91+
ht.client, err = newClientHarness(
92+
mailboxAddr, ht.server.passphraseEntropy, insecure,
93+
cfg.grpcClientConn,
94+
)
95+
require.NoError(t, err)
96+
97+
require.NoError(t, ht.client.start())
98+
t.Cleanup(func() {
99+
_ = ht.client.cleanup()
100+
})
101+
102+
return ht
50103
}
51104

52105
// Skipf calls the underlying testing.T's Skip method, causing the current test
@@ -98,93 +151,46 @@ func (h *harnessTest) Log(args ...interface{}) {
98151

99152
// setupLogging initializes the logging subsystem for the server and client
100153
// packages.
101-
func (h *harnessTest) setupLogging() {
154+
func setupLogging(t *testing.T) {
102155
logWriter := build.NewRotatingLogWriter()
103156

104157
if interceptor != nil {
105158
return
106159
}
107160

108161
ic, err := signal.Intercept()
109-
require.NoError(h.t, err)
162+
require.NoError(t, err)
110163
interceptor = &ic
111164

112165
aperture.SetupLoggers(logWriter, *interceptor)
113-
lnd.AddSubLogger(logWriter, mailbox.Subsystem, *interceptor, mailbox.UseLogger)
166+
lnd.AddSubLogger(
167+
logWriter, mailbox.Subsystem, *interceptor, mailbox.UseLogger,
168+
)
114169
lnd.AddSubLogger(logWriter, gbn.Subsystem, *interceptor, gbn.UseLogger)
115170

116171
err = build.ParseAndSetDebugLevels(
117172
"debug,PRXY=warn,GOBN=trace", logWriter,
118173
)
119-
require.NoError(h.t, err)
174+
require.NoError(t, err)
120175
}
121176

122177
// shutdown stops the client, server and hashmail server
123178
func (h *harnessTest) shutdown() error {
124179
var returnErr error
125180

126-
if h.hmserver != nil {
127-
err := h.hmserver.stop()
128-
if err != nil {
129-
returnErr = err
130-
}
131-
}
132-
133181
err := h.client.cleanup()
134182
if err != nil {
135183
returnErr = err
136184
}
137185

138186
h.server.stop()
139-
return returnErr
140-
}
141-
142-
// setupHarnesses creates new server, client and hashmail harnesses.
143-
func setupHarnesses(t *testing.T) (*clientHarness, *serverHarness,
144-
*hashmailHarness) {
145-
146-
hashmailHarness := newHashmailHarness()
147-
if err := hashmailHarness.start(); err != nil {
148-
t.Fatalf("could not start hashmail server: %v", err)
149-
}
150-
151-
client, server := setupClientAndServerHarnesses(
152-
t, hashmailHarness.apertureCfg.ListenAddr, true,
153-
)
154-
155-
return client, server, hashmailHarness
156-
}
157-
158-
func setupClientAndServerHarnesses(t *testing.T,
159-
mailboxAddr string, insecure bool) (*clientHarness, *serverHarness) {
160-
161-
serverHarness, err := newServerHarness(mailboxAddr, insecure)
162-
require.NoError(t, err)
163-
require.NoError(t, serverHarness.start())
164-
t.Cleanup(func() {
165-
serverHarness.stop()
166-
})
167187

168-
select {
169-
case err := <-serverHarness.errChan:
188+
if h.hmserver != nil {
189+
err := h.hmserver.stop()
170190
if err != nil {
171-
t.Fatalf("could not start server: %v", err)
191+
returnErr = err
172192
}
173-
default:
174193
}
175194

176-
// Give the server some time to set up the first mailbox
177-
time.Sleep(1000 * time.Millisecond)
178-
179-
clientHarness, err := newClientHarness(
180-
mailboxAddr, serverHarness.passphraseEntropy,
181-
)
182-
require.NoError(t, err)
183-
184-
require.NoError(t, clientHarness.start())
185-
t.Cleanup(func() {
186-
_ = clientHarness.cleanup()
187-
})
188-
189-
return clientHarness, serverHarness
195+
return returnErr
190196
}

0 commit comments

Comments
 (0)