Skip to content

Commit

Permalink
Merge pull request #546 from ViktorTigerstrom/2023-05-export-itest-st…
Browse files Browse the repository at this point in the history
…ructs

itest: export `ServerHarness` and `HarnessNode`
  • Loading branch information
ellemouton authored May 11, 2023
2 parents 8eb5372 + b398205 commit 9131690
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
16 changes: 8 additions & 8 deletions itest/litd_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ var _ lnrpc.LightningClient = (*HarnessNode)(nil)
var _ lnrpc.WalletUnlockerClient = (*HarnessNode)(nil)
var _ invoicesrpc.InvoicesClient = (*HarnessNode)(nil)

// newNode creates a new test lightning node instance from the passed config.
func newNode(t *testing.T, cfg *LitNodeConfig,
harness *NetworkHarness) (*HarnessNode, error) {
// NewNode creates a new test lightning node instance from the passed config.
func NewNode(t *testing.T, cfg *LitNodeConfig,
harness *lntest.HarnessTest) (*HarnessNode, error) {

if cfg.BaseDir == "" {
var err error
Expand Down Expand Up @@ -393,7 +393,7 @@ func newNode(t *testing.T, cfg *LitNodeConfig,

var remoteNode *node.HarnessNode
if cfg.RemoteMode {
lndHarness := harness.LNDHarness
lndHarness := harness

remoteNode = lndHarness.NewNode("bob-custom", cfg.ExtraArgs)
tenBTC := btcutil.Amount(10 * btcutil.SatoshiPerBitcoin)
Expand Down Expand Up @@ -538,7 +538,7 @@ func renameFile(fromFileName, toFileName string) {
//
// This may not clean up properly if an error is returned, so the caller should
// call shutdown() regardless of the return value.
func (hn *HarnessNode) start(litdBinary string, litdError chan<- error,
func (hn *HarnessNode) Start(litdBinary string, litdError chan<- error,
wait bool, litArgOpts ...LitArgOption) error {

hn.quit = make(chan struct{})
Expand Down Expand Up @@ -1165,8 +1165,8 @@ func (hn *HarnessNode) cleanup() error {
return os.RemoveAll(hn.Cfg.BaseDir)
}

// Stop attempts to stop the active litd process.
func (hn *HarnessNode) stop() error {
// Stop attempts to Stop the active litd process.
func (hn *HarnessNode) Stop() error {
// Do nothing if the process is not running.
if hn.processExit == nil {
return nil
Expand Down Expand Up @@ -1246,7 +1246,7 @@ func (hn *HarnessNode) stop() error {
// shutdown stops the active lnd process and cleans up any temporary directories
// created along the way.
func (hn *HarnessNode) shutdown() error {
if err := hn.stop(); err != nil {
if err := hn.Stop(); err != nil {
return err
}
if err := hn.cleanup(); err != nil {
Expand Down
28 changes: 14 additions & 14 deletions itest/network_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type NetworkHarness struct {
LNDHarness *lntest.HarnessTest

// server is an instance of the local Loop/Pool mock server.
server *serverHarness
server *ServerHarness

// BackendCfg houses the information necessary to use a node as LND
// chain backend, such as rpc configuration, P2P information etc.
Expand Down Expand Up @@ -130,8 +130,8 @@ func (n *NetworkHarness) SetUp(t *testing.T,
mockServerAddr := fmt.Sprintf(
node.ListenerFormat, node.NextAvailablePort(),
)
n.server = newServerHarness(mockServerAddr)
err := n.server.start()
n.server = NewServerHarness(mockServerAddr)
err := n.server.Start()
require.NoError(t, err)

// Start a mock autopilot server.
Expand Down Expand Up @@ -273,10 +273,10 @@ func (n *NetworkHarness) NewNode(t *testing.T, name string, extraArgs []string,
remoteMode bool, wait bool) (*HarnessNode, error) {

litArgs := []string{
fmt.Sprintf("--loop.server.host=%s", n.server.serverHost),
fmt.Sprintf("--loop.server.tlspath=%s", n.server.certFile),
fmt.Sprintf("--pool.auctionserver=%s", n.server.serverHost),
fmt.Sprintf("--pool.tlspathauctserver=%s", n.server.certFile),
fmt.Sprintf("--loop.server.host=%s", n.server.ServerHost),
fmt.Sprintf("--loop.server.tlspath=%s", n.server.CertFile),
fmt.Sprintf("--pool.auctionserver=%s", n.server.ServerHost),
fmt.Sprintf("--pool.tlspathauctserver=%s", n.server.CertFile),
"--autopilot.insecure",
fmt.Sprintf(
"--autopilot.address=localhost:%d",
Expand Down Expand Up @@ -315,7 +315,7 @@ func (n *NetworkHarness) newNode(t *testing.T, name string, extraArgs,
RemoteMode: remoteMode,
}

node, err := newNode(t, cfg, n)
node, err := NewNode(t, cfg, n.LNDHarness)
if err != nil {
return nil, err
}
Expand All @@ -326,7 +326,7 @@ func (n *NetworkHarness) newNode(t *testing.T, name string, extraArgs,
n.activeNodes[node.NodeID] = node
n.mtx.Unlock()

err = node.start(n.litdBinary, n.lndErrorChan, wait)
err = node.Start(n.litdBinary, n.lndErrorChan, wait)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -649,7 +649,7 @@ func (n *NetworkHarness) RestartNode(node *HarnessNode, callback func() error,
func (n *NetworkHarness) RestartNodeNoUnlock(node *HarnessNode,
callback func() error, wait bool, litArgOpts ...LitArgOption) error {

if err := node.stop(); err != nil {
if err := node.Stop(); err != nil {
return err
}

Expand All @@ -659,18 +659,18 @@ func (n *NetworkHarness) RestartNodeNoUnlock(node *HarnessNode,
}
}

return node.start(n.litdBinary, n.lndErrorChan, wait, litArgOpts...)
return node.Start(n.litdBinary, n.lndErrorChan, wait, litArgOpts...)
}

// SuspendNode stops the given node and returns a callback that can be used to
// start it again.
func (n *NetworkHarness) SuspendNode(node *HarnessNode) (func() error, error) {
if err := node.stop(); err != nil {
if err := node.Stop(); err != nil {
return nil, err
}

restart := func() error {
return node.start(n.litdBinary, n.lndErrorChan, true)
return node.Start(n.litdBinary, n.lndErrorChan, true)
}

return restart, nil
Expand Down Expand Up @@ -701,7 +701,7 @@ func (n *NetworkHarness) KillNode(node *HarnessNode) error {
// This can be used to temporarily bring a node down during a test, to be later
// started up again.
func (n *NetworkHarness) StopNode(node *HarnessNode) error {
return node.stop()
return node.Stop()
}

// OpenChannel attempts to open a channel between srcNode and destNode with the
Expand Down
25 changes: 14 additions & 11 deletions itest/server_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,47 @@ type loopPoolServer struct {
swapserverrpc.UnimplementedSwapServerServer
}

type serverHarness struct {
serverHost string
type ServerHarness struct {
ServerHost string
mockServer *grpc.Server

certFile string
CertFile string
server *loopPoolServer

errChan chan error

wg sync.WaitGroup
}

func newServerHarness(serverHost string) *serverHarness {
return &serverHarness{
serverHost: serverHost,
// NewServerHarness creates a new ServerHarness instance.
func NewServerHarness(serverHost string) *ServerHarness {
return &ServerHarness{
ServerHost: serverHost,
errChan: make(chan error, 1),
}
}

func (s *serverHarness) stop() {
// Stop stops the mock Loop/Pool server.
func (s *ServerHarness) Stop() {
s.mockServer.Stop()
s.wg.Wait()
}

func (s *serverHarness) start() error {
// Start starts the mock Loop/Pool server.
func (s *ServerHarness) Start() error {
tempDirName, err := ioutil.TempDir("", "litditest")
if err != nil {
return err
}

s.certFile = filepath.Join(tempDirName, "proxy.cert")
s.CertFile = filepath.Join(tempDirName, "proxy.cert")
keyFile := filepath.Join(tempDirName, "proxy.key")
creds, err := genCertPair(s.certFile, keyFile)
creds, err := genCertPair(s.CertFile, keyFile)
if err != nil {
return err
}

httpListener, err := net.Listen("tcp", s.serverHost)
httpListener, err := net.Listen("tcp", s.ServerHost)
if err != nil {
return err
}
Expand Down

0 comments on commit 9131690

Please sign in to comment.