From c6817c17c687fec9389e3d0ac6873f365a1d190d Mon Sep 17 00:00:00 2001 From: Jorropo Date: Fri, 11 Aug 2023 03:34:06 +0200 Subject: [PATCH 1/3] 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 5788bf3ce45..86f9b0e4207 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -717,8 +717,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 00000000000..2d59c99da03 --- /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 dde7e3495fc..4d747a460e6 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) } } From 71aa8ea0533c2b59fefd5669ae9c5b973d6a7aa2 Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Fri, 11 Aug 2023 10:14:40 +0200 Subject: [PATCH 2/3] experiment: use gotestfmt --- .github/workflows/gotest.yml | 5 ++++- coverage/Rules.mk | 4 ++-- test/unit/Rules.mk | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index 08a741532f0..5a73390472a 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -32,7 +32,10 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.19.x + go-version: 1.19.1 + - name: Set up gotestfmt + # https://github.com/GoTestTools/gotestfmt#github-actions + run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest - name: Check out Kubo uses: actions/checkout@v3 - name: Restore Go cache diff --git a/coverage/Rules.mk b/coverage/Rules.mk index 48fce28568c..b1795f4d2ad 100644 --- a/coverage/Rules.mk +++ b/coverage/Rules.mk @@ -21,8 +21,8 @@ $(UCOVER_$(d)): $(d)/coverage_deps ALWAYS $(eval TMP_PKG := $(subst _,/,$(basename $(@F)))) $(eval TMP_DEPS := $(shell $(GOCC) list -f '{{range .Deps}}{{.}} {{end}}' $(go-flags-with-tags) $(TMP_PKG) | sed 's/ /\n/g' | grep ipfs/go-ipfs) $(TMP_PKG)) $(eval TMP_DEPS_LIST := $(call join-with,$(comma),$(TMP_DEPS))) - $(GOCC) test $(go-flags-with-tags) $(GOTFLAGS) -v -covermode=atomic -json -coverpkg=$(TMP_DEPS_LIST) -coverprofile=$@ $(TMP_PKG) | tee -a test/unit/gotest.json - + $(GOCC) test $(go-flags-with-tags) $(GOTFLAGS) -v -covermode=atomic -json -coverpkg=$(TMP_DEPS_LIST) -coverprofile=$@ $(TMP_PKG) | tee -a test/unit/gotest.json | gotestfmt +# ^^^ This is where we generate the gotest.json file. $(d)/unit_tests.coverprofile: $(UCOVER_$(d)) gocovmerge $^ > $@ diff --git a/test/unit/Rules.mk b/test/unit/Rules.mk index 69404637c11..b5b07109a97 100644 --- a/test/unit/Rules.mk +++ b/test/unit/Rules.mk @@ -2,6 +2,7 @@ include mk/header.mk CLEAN += $(d)/gotest.json $(d)/gotest.junit.xml +# looks like: we pass the `cat D/gotest.json` generated by a previous rule to gotestsum for formatting. $(d)/gotest.junit.xml: test/bin/gotestsum coverage/unit_tests.coverprofile gotestsum --no-color --junitfile $@ --raw-command cat $(@D)/gotest.json From ceb1b3e2b9bb8a6298c3813b21ab67d665e5e987 Mon Sep 17 00:00:00 2001 From: Laurent Senta Date: Fri, 11 Aug 2023 13:45:56 +0200 Subject: [PATCH 3/3] wip: try my branch --- .github/workflows/gotest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gotest.yml b/.github/workflows/gotest.yml index 5a73390472a..47bc5f5a757 100644 --- a/.github/workflows/gotest.yml +++ b/.github/workflows/gotest.yml @@ -35,7 +35,7 @@ jobs: go-version: 1.19.1 - name: Set up gotestfmt # https://github.com/GoTestTools/gotestfmt#github-actions - run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest + run: go install github.com/laurentsenta/gotestfmt/v2/cmd/gotestfmt@fix-coverage2 - name: Check out Kubo uses: actions/checkout@v3 - name: Restore Go cache