From 768f669dfdece77ddbe1e46a6e60b91570867d49 Mon Sep 17 00:00:00 2001 From: jtsmedley <38006759+jtsmedley@users.noreply.github.com> Date: Thu, 7 Sep 2023 16:38:11 -0500 Subject: [PATCH] Add IPNI Endpoint Flag to Daemon Closes #409 by adding the IPNI Endpoint Flag to the Daemon and adds a positive/negative test for the flag on the daemon. Adding the positive/negative test required changing the exit command on 119 of daemon.go to pass the err on up. Please review and confirm this won't introduce any unintentional side effects. --- cmd/lassie/daemon.go | 4 ++-- cmd/lassie/daemon_test.go | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cmd/lassie/daemon.go b/cmd/lassie/daemon.go index 62be5d17..ff5ce7e5 100644 --- a/cmd/lassie/daemon.go +++ b/cmd/lassie/daemon.go @@ -62,6 +62,7 @@ var daemonFlags = []cli.Flag{ DefaultText: "no limit", EnvVars: []string{"LASSIE_CONCURRENT_SP_RETRIEVALS"}, }, + FlagIPNIEndpoint, FlagEventRecorderAuth, FlagEventRecorderInstanceId, FlagEventRecorderUrl, @@ -115,7 +116,7 @@ func daemonAction(cctx *cli.Context) error { lassieCfg, err := buildLassieConfigFromCLIContext(cctx, lassieOpts, libp2pOpts) if err != nil { - return cli.Exit(err, 1) + return err } // http server config @@ -165,7 +166,6 @@ func defaultDaemonRun( httpServerCfg httpserver.HttpServerConfig, eventRecorderCfg *aggregateeventrecorder.EventRecorderConfig, ) error { - lassie, err := lassie.NewLassieWithConfig(ctx, lassieCfg) if err != nil { return nil diff --git a/cmd/lassie/daemon_test.go b/cmd/lassie/daemon_test.go index ae7e874e..55bb34e0 100644 --- a/cmd/lassie/daemon_test.go +++ b/cmd/lassie/daemon_test.go @@ -6,6 +6,7 @@ import ( "time" a "github.com/filecoin-project/lassie/pkg/aggregateeventrecorder" + "github.com/filecoin-project/lassie/pkg/indexerlookup" l "github.com/filecoin-project/lassie/pkg/lassie" h "github.com/filecoin-project/lassie/pkg/server/http" "github.com/libp2p/go-libp2p/core/peer" @@ -17,9 +18,10 @@ import ( func TestDaemonCommandFlags(t *testing.T) { tests := []struct { - name string - args []string - assert daemonRunFunc + name string + args []string + shouldError bool + assert daemonRunFunc }{ { name: "with default args", @@ -149,6 +151,19 @@ func TestDaemonCommandFlags(t *testing.T) { return nil }, }, + { + name: "with ipni endpoint", + args: []string{"daemon", "--ipni-endpoint", "https://cid.contact"}, + assert: func(ctx context.Context, lCfg *l.LassieConfig, hCfg h.HttpServerConfig, erCfg *a.EventRecorderConfig) error { + require.IsType(t, &indexerlookup.IndexerCandidateFinder{}, lCfg.Finder, "finder should be an IndexerCandidateFinder when providing an ipni endpoint") + return nil + }, + }, + { + name: "with bad ipni endpoint", + args: []string{"daemon", "--ipni-endpoint", "not-a-url"}, + shouldError: true, + }, { name: "with event recorder url", args: []string{"daemon", "--event-recorder-url", "https://myeventrecorder.com/v1/retrieval-events"}, @@ -185,6 +200,10 @@ func TestDaemonCommandFlags(t *testing.T) { for _, test := range tests { daemonRun = test.assert + if test.shouldError { + daemonRun = noopDaemonRun + } + app := &cli.App{ Name: "cli-test", Flags: daemonFlags, @@ -193,9 +212,17 @@ func TestDaemonCommandFlags(t *testing.T) { t.Run(test.name, func(t *testing.T) { err := app.Run(append([]string{"cli-test"}, test.args...)) - if err != nil { + if err != nil && !test.shouldError { t.Fatal(err) } + + if err == nil && test.shouldError { + t.Fatal("expected error") + } }) } } + +func noopDaemonRun(ctx context.Context, lCfg *l.LassieConfig, hCfg h.HttpServerConfig, erCfg *a.EventRecorderConfig) error { + return nil +}