github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/sharness/t0062-daemon-api.sh (about) 1 #!/bin/sh 2 # 3 # MIT Licensed; see the LICENSE file in this repository. 4 # 5 6 test_description="Test daemon command" 7 8 . lib/test-lib.sh 9 10 test_init_ipfs 11 12 differentport=$((PORT_API + 1)) 13 api_different="/ip4/127.0.0.1/tcp/$differentport" 14 api_unreachable="/ip4/127.0.0.1/tcp/1" 15 16 test_expect_success "config setup" ' 17 api_fromcfg=$(ipfs config Addresses.API) && 18 peerid=$(ipfs config Identity.PeerID) 19 ' 20 21 test_client() { 22 printf "$peerid" >expected && 23 ipfs "$@" id -f="<id>" >actual && 24 test_cmp expected actual 25 } 26 27 test_client_must_fail() { 28 echo "Error: api not running" >expected_err && 29 test_must_fail ipfs "$@" id -f="<id>" >actual 2>actual_err && 30 test_cmp expected_err actual_err 31 } 32 33 test_client_suite() { 34 state="$1" 35 cfg_success="$2" 36 diff_success="$3" 37 # must always work 38 test_expect_success "client should work $state" ' 39 test_client 40 ' 41 42 # must always err 43 test_expect_success "client --api unreachable should err $state" ' 44 test_client_must_fail --api "$api_unreachable" 45 ' 46 47 if [ "$cfg_success" = true ]; then 48 test_expect_success "client --api fromcfg should work $state" ' 49 test_client --api "$api_fromcfg" 50 ' 51 else 52 test_expect_success "client --api fromcfg should err $state" ' 53 test_client_must_fail --api "$api_fromcfg" 54 ' 55 fi 56 57 if [ "$diff_success" = true ]; then 58 test_expect_success "client --api different should work $state" ' 59 test_client --api "$api_different" 60 ' 61 else 62 test_expect_success "client --api different should err $state" ' 63 test_client_must_fail --api "$api_different" 64 ' 65 fi 66 } 67 68 # first, test things without daemon, without /api file 69 test_client_suite "(daemon off, no --api, no /api file)" false false 70 71 72 # then, test things with daemon, with /api file 73 74 test_launch_ipfs_daemon 75 76 test_expect_success "'ipfs daemon' creates api file" ' 77 test -f ".ipfs/api" 78 ' 79 80 test_expect_success "api file looks good" ' 81 printf "$ADDR_API" >expected && 82 test_cmp expected .ipfs/api 83 ' 84 85 test_client_suite "(daemon on, no --api, /api file from cfg)" true false 86 87 # then, test things without daemon, with /api file 88 89 test_kill_ipfs_daemon 90 91 test_client_suite "(daemon off, no --api, /api file from cfg)" false false 92 93 # then, test things with daemon --api $api_different, with /api file 94 95 PORT_API=$differentport 96 ADDR_API=$api_different 97 98 test_launch_ipfs_daemon --api "$ADDR_API" 99 100 test_expect_success "'ipfs daemon' --api option works" ' 101 printf "$api_different" >expected && 102 test_cmp expected .ipfs/api 103 ' 104 105 test_client_suite "(daemon on, /api file different)" false true 106 107 # then, test things with daemon off, with /api file, for good measure. 108 109 test_kill_ipfs_daemon 110 111 test_client_suite "(daemon off, /api file different)" false false 112 113 test_done