Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daemon: use fsrepo.IsInitialized to test for initialization #3805

Merged
merged 1 commit into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
iconn "gx/ipfs/QmT6jBTqNKhhb8dbzCEMUNkGhm3RuRActcMhpShAHLpQtp/go-libp2p-interface-conn"
"gx/ipfs/QmVCNGTyD4EkvNYaAp253uMQ9Rjsjy2oGMvcdJJUoVRfja/go-multiaddr-net"
"gx/ipfs/QmX3QZ5jHEPidwUrymXV1iSCSUhdGxj15sm2gP4jKMef7B/client_golang/prometheus"
util "gx/ipfs/QmZuY8aV7zbNXVy6DyN9SmnuH3o9nG852F4aTiSBpts8d1/go-ipfs-util"
pstore "gx/ipfs/Qme1g4e3m2SmdiSGGU3vSWmUStwUjc5oECnEriaK9Xa1HU/go-libp2p-peerstore"
)

Expand Down Expand Up @@ -227,12 +226,9 @@ func daemonFunc(req cmds.Request, res cmds.Response) {

if initialize {

// now, FileExists is our best method of detecting whether ipfs is
// configured. Consider moving this into a config helper method
// `IsInitialized` where the quality of the signal can be improved over
// time, and many call-sites can benefit.
if !util.FileExists(req.InvocContext().ConfigRoot) {
err := initWithDefaults(os.Stdout, req.InvocContext().ConfigRoot)
cfg := req.InvocContext().ConfigRoot
if !fsrepo.IsInitialized(cfg) {
err := initWithDefaults(os.Stdout, cfg)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
52 changes: 52 additions & 0 deletions test/sharness/t0063-daemon-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh
#
# Copyright (c) 2014 Juan Batiz-Benet
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test daemon --init command"

. lib/test-lib.sh

# We don't want the normal test_init_ipfs but we need to make sure the
# IPFS_PATH is set correctly.
export IPFS_PATH="$(pwd)/.ipfs"

# safety check since we will be removing the directory
if [ -e "$IPFS_PATH" ]; then
echo "$IPFS_PATH exists"
exit 1
fi

test_ipfs_daemon_init() {
# Doing it manually since we want to launch the daemon with an
# empty or non-existent repo; the normal
# test_launch_ipfs_daemon does not work since it assumes the
# repo was created a particular way with regard to the API
# server.

test_expect_success "'ipfs daemon --init' succeeds" '
ipfs daemon --init >actual_daemon 2>daemon_err &
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be problematic with other daemons people have running (port numbers). Not really sure how to solve it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... i'm not sure how to fix it either. We should look into adding a way to specify ports from here.

Though not sure if this should block the PR, Its probably a separate concern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will cause the test to fail on users machines who also have an IPFS daemon running. Should we flag it some how? If so I am not sure how.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could check if ports from config are bound (8080, 4001, 5001) using netcat and if any of those are, skip the test. How that sounds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, lets do that. Its better to print a message about why it fails rather than just randomly failing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use nc -z -w1 localhost 5001

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still want to do that? The test won't fail currently.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevina Hrm... i think we can probably do without for now. Lets add a todo to add a flag for different ports on the daemon

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whyrusleeping do you think we I should reduce the timeout to 1 sec to minimize the chance of this test failing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldnt worry about it, lets just make a note to do proper port selection for this later.

IPFS_PID=$!
sleep 2 &&
if ! kill -0 $IPFS_PID; then cat daemon_err; return 1; fi
'

test_expect_success "'ipfs daemon' can be killed" '
test_kill_repeat_10_sec $IPFS_PID
'
}

test_expect_success "remove \$IPFS_PATH dir" '
rm -rf "$IPFS_PATH"
'
test_ipfs_daemon_init

test_expect_success "create empty \$IPFS_PATH dir" '
rm -rf "$IPFS_PATH" &&
mkdir "$IPFS_PATH"
'

test_ipfs_daemon_init

test_done