Skip to content

Commit

Permalink
itest: add RunCli to run commands against a specific tapd harness
Browse files Browse the repository at this point in the history
Introduce the `RunCli` function to execute arbitrary CLI commands
against a specific tapd integration test instance. This enables
running CLI commands on itest harnesses and retrieving their
responses.
  • Loading branch information
ffranr committed Jan 20, 2025
1 parent 11f762d commit 33af6b1
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions itest/tapd_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/lightninglabs/taproot-assets/proof"
"github.com/lightninglabs/taproot-assets/rfq"
"github.com/lightninglabs/taproot-assets/tapcfg"
"github.com/lightninglabs/taproot-assets/tapcliutil"
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
Expand All @@ -31,6 +32,7 @@ import (
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -288,6 +290,57 @@ func newTapdHarness(t *testing.T, ht *harnessTest, cfg tapdConfig,
}, nil
}

// cliApp creates a new CLI app which calls into the tapd harness.
func cliApp(ctx context.Context, tapClient *tapdHarness,
respChan chan<- interface{}, errChan chan<- error) *cli.App {

app := cli.NewApp()
app.Name = "tapcli-itest"

clientSpecifier := tapcliutil.NewRpcClientHarness(
ctx, tapClient, respChan, errChan,
)
app.Commands = tapcliutil.Commands(clientSpecifier)

return app
}

// RunCli calls into the tapcli command line parser with the given arguments and
// returns the response or an error.
func RunCli(ctx context.Context, tapClient *tapdHarness,
args ...string) (interface{}, error) {

// Prepend a dummy path to the args to make the CLI argument parser
// happy. The first argument is the path to the binary, which is not
// used in the tests.
args = append([]string{"dummy-path"}, args...)

responseChan := make(chan interface{}, 1)
errCh := make(chan error, 1)

go func() {
app := cliApp(ctx, tapClient, responseChan, errCh)
err := app.Run(args)
if err != nil {
errCh <- err
}
}()

// Wait for either a response or an error.
select {
case resp := <-responseChan:
close(responseChan)
return resp, nil
case err := <-errCh:
close(responseChan)
return nil, err
case <-ctx.Done():
// Handle context cancellation
close(responseChan)
return nil, ctx.Err()
}
}

// updateConfigWithNode updates the tapd configuration with the connection
// information of the given lnd node.
func updateConfigWithNode(cfg *tapcfg.Config, lnd *node.HarnessNode) error {
Expand Down

0 comments on commit 33af6b1

Please sign in to comment.