From a45d3fec348aee0a03e3c7240962e9aefa56fe53 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 11 Aug 2023 03:34:06 +0200 Subject: [PATCH] fix: allow daemon to start correctly if the API is null Fixes: #10056 --- cmd/ipfs/daemon.go | 7 +++++-- test/cli/daemon_test.go | 18 ++++++++++++++++++ test/cli/harness/ipfs.go | 17 ++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/cli/daemon_test.go diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 1e03a8264a8e..ff6e536534fd 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -714,8 +714,11 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error return nil, fmt.Errorf("serveHTTPApi: ConstructNode() failed: %s", err) } - if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil { - return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err) + if len(listeners) > 0 { + // Only add an api file if the API is running. + if err := node.Repo.SetAPIAddr(rewriteMaddrToUseLocalhostIfItsAny(listeners[0].Multiaddr())); err != nil { + return nil, fmt.Errorf("serveHTTPApi: SetAPIAddr() failed: %w", err) + } } errc := make(chan error) diff --git a/test/cli/daemon_test.go b/test/cli/daemon_test.go new file mode 100644 index 000000000000..2d59c99da037 --- /dev/null +++ b/test/cli/daemon_test.go @@ -0,0 +1,18 @@ +package cli + +import ( + "testing" + + "github.com/ipfs/kubo/test/cli/harness" +) + +func TestDaemon(t *testing.T) { + t.Parallel() + + t.Run("daemon starts if api is set to null", func(t *testing.T) { + t.Parallel() + node := harness.NewT(t).NewNode().Init() + node.SetIPFSConfig("API", nil) + node.IPFS("daemon") // can't use .StartDaemon because it do a .WaitOnAPI + }) +} diff --git a/test/cli/harness/ipfs.go b/test/cli/harness/ipfs.go index dde7e3495fc6..4d747a460e6a 100644 --- a/test/cli/harness/ipfs.go +++ b/test/cli/harness/ipfs.go @@ -38,9 +38,20 @@ func (n *Node) SetIPFSConfig(key string, val interface{}, flags ...string) { n.IPFS(args...) // validate the config was set correctly - var newVal string - n.GetIPFSConfig(key, &newVal) - if val != newVal { + + // Create a new value which is a pointer to the same type as the source. + var newVal any + if val != nil { + // If it is not nil grab the type with reflect. + newVal = reflect.New(reflect.TypeOf(val)).Interface() + } else { + // else just set a pointer to an any. + var anything any + newVal = &anything + } + n.GetIPFSConfig(key, newVal) + // dereference newVal using reflect to load the resulting value + if val != reflect.ValueOf(newVal).Elem().Interface() { log.Panicf("key '%s' did not retain value '%s' after it was set, got '%s'", key, val, newVal) } }