From 33af6b1b0583cdc3479f9b4689275f9d1199f030 Mon Sep 17 00:00:00 2001 From: ffranr Date: Mon, 20 Jan 2025 17:10:24 +0000 Subject: [PATCH] itest: add `RunCli` to run commands against a specific tapd harness 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. --- itest/tapd_harness.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/itest/tapd_harness.go b/itest/tapd_harness.go index 297804337..0de610b38 100644 --- a/itest/tapd_harness.go +++ b/itest/tapd_harness.go @@ -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" @@ -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" @@ -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 {