Skip to content

Commit 7b09b14

Browse files
committed
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 <[email protected]>
1 parent 646dbde commit 7b09b14

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

cmd/ipfs/daemon.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
iconn "gx/ipfs/QmT6jBTqNKhhb8dbzCEMUNkGhm3RuRActcMhpShAHLpQtp/go-libp2p-interface-conn"
2727
"gx/ipfs/QmVCNGTyD4EkvNYaAp253uMQ9Rjsjy2oGMvcdJJUoVRfja/go-multiaddr-net"
2828
"gx/ipfs/QmX3QZ5jHEPidwUrymXV1iSCSUhdGxj15sm2gP4jKMef7B/client_golang/prometheus"
29-
util "gx/ipfs/QmZuY8aV7zbNXVy6DyN9SmnuH3o9nG852F4aTiSBpts8d1/go-ipfs-util"
3029
pstore "gx/ipfs/Qme1g4e3m2SmdiSGGU3vSWmUStwUjc5oECnEriaK9Xa1HU/go-libp2p-peerstore"
3130
)
3231

@@ -227,11 +226,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
227226

228227
if initialize {
229228

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) {
229+
if !fsrepo.IsInitialized(req.InvocContext().ConfigRoot) {
235230
err := initWithDefaults(os.Stdout, req.InvocContext().ConfigRoot)
236231
if err != nil {
237232
res.SetError(err, cmds.ErrNormal)

test/sharness/t0063-daemon-init.sh

+52
Original file line numberDiff line numberDiff line change
@@ -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; exit 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

0 commit comments

Comments
 (0)