@cryptotaxi247 / kubo / commits / 28921471d

daemon: use fsrepo.IsInitialized to test for initialization

Use fsrepo.IsInitialized to test for initialization instead of just testing if the directory exists. Also add test cases for '--init' option, including one the creates an empty directory. License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed Mar 20, 2017 at 20:24 UTC 28921471db5a135e74d2f02e00b3b4924b22fb55
2 files changed +55 -7
cmd/ipfs/daemon.go
+3 -7
@@ -26,7 +26,6 @@ import (
26 iconn "gx/ipfs/QmT6jBTqNKhhb8dbzCEMUNkGhm3RuRActcMhpShAHLpQtp/go-libp2p-interface-conn"
27 "gx/ipfs/QmVCNGTyD4EkvNYaAp253uMQ9Rjsjy2oGMvcdJJUoVRfja/go-multiaddr-net"
28 "gx/ipfs/QmX3QZ5jHEPidwUrymXV1iSCSUhdGxj15sm2gP4jKMef7B/client_golang/prometheus"
29 - util "gx/ipfs/QmZuY8aV7zbNXVy6DyN9SmnuH3o9nG852F4aTiSBpts8d1/go-ipfs-util"
29 pstore "gx/ipfs/Qme1g4e3m2SmdiSGGU3vSWmUStwUjc5oECnEriaK9Xa1HU/go-libp2p-peerstore"
30 )
31
@@ -227,12 +226,9 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
226
227 if initialize {
228
230 - // now, FileExists is our best method of detecting whether ipfs is
231 - // configured. Consider moving this into a config helper method
232 - // `IsInitialized` where the quality of the signal can be improved over
233 - // time, and many call-sites can benefit.
234 - if !util.FileExists(req.InvocContext().ConfigRoot) {
235 - err := initWithDefaults(os.Stdout, req.InvocContext().ConfigRoot)
229 + cfg := req.InvocContext().ConfigRoot
230 + if !fsrepo.IsInitialized(cfg) {
231 + err := initWithDefaults(os.Stdout, cfg)
232 if err != nil {
233 res.SetError(err, cmds.ErrNormal)
234 return
test/sharness/t0063-daemon-init.sh new
+52
@@ -0,0 +1,52 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2014 Juan Batiz-Benet
4 +# MIT Licensed; see the LICENSE file in this repository.
5 +#
6 +
7 +test_description="Test daemon --init command"
8 +
9 +. lib/test-lib.sh
10 +
11 +# We don't want the normal test_init_ipfs but we need to make sure the
12 +# IPFS_PATH is set correctly.
13 +export IPFS_PATH="$(pwd)/.ipfs"
14 +
15 +# safety check since we will be removing the directory
16 +if [ -e "$IPFS_PATH" ]; then
17 + echo "$IPFS_PATH exists"
18 + exit 1
19 +fi
20 +
21 +test_ipfs_daemon_init() {
22 + # Doing it manually since we want to launch the daemon with an
23 + # empty or non-existent repo; the normal
24 + # test_launch_ipfs_daemon does not work since it assumes the
25 + # repo was created a particular way with regard to the API
26 + # server.
27 +
28 + test_expect_success "'ipfs daemon --init' succeeds" '
29 + ipfs daemon --init >actual_daemon 2>daemon_err &
30 + IPFS_PID=$!
31 + sleep 2 &&
32 + if ! kill -0 $IPFS_PID; then cat daemon_err; return 1; fi
33 + '
34 +
35 + test_expect_success "'ipfs daemon' can be killed" '
36 + test_kill_repeat_10_sec $IPFS_PID
37 + '
38 +}
39 +
40 +test_expect_success "remove \$IPFS_PATH dir" '
41 + rm -rf "$IPFS_PATH"
42 +'
43 +test_ipfs_daemon_init
44 +
45 +test_expect_success "create empty \$IPFS_PATH dir" '
46 + rm -rf "$IPFS_PATH" &&
47 + mkdir "$IPFS_PATH"
48 +'
49 +
50 +test_ipfs_daemon_init
51 +
52 +test_done