| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | test_description="Test pubsub command" |
| 4 | |
| 5 | . lib/test-lib.sh |
| 6 | |
| 7 | # start iptb + wait for peering |
| 8 | NUM_NODES=5 |
| 9 | test_expect_success 'init iptb' ' |
| 10 | iptb testbed create -type localipfs -count $NUM_NODES -init |
| 11 | ' |
| 12 | |
| 13 | test_expect_success 'disable the DHT' ' |
| 14 | iptb run -- ipfs config Routing.Type none |
| 15 | ' |
| 16 | |
| 17 | run_pubsub_tests() { |
| 18 | test_expect_success 'peer ids' ' |
| 19 | PEERID_0=$(iptb attr get 0 id) && |
| 20 | PEERID_2=$(iptb attr get 2 id) |
| 21 | ' |
| 22 | |
| 23 | # ipfs pubsub sub |
| 24 | test_expect_success 'pubsub' ' |
| 25 | echo -n -e "test\nOK" | ipfs multibase encode -b base64url > expected && |
| 26 | touch empty && |
| 27 | mkfifo wait || |
| 28 | test_fsh echo init fail |
| 29 | |
| 30 | # ipfs pubsub sub is long-running so we need to start it in the background and |
| 31 | # wait put its output somewhere where we can access it |
| 32 | ( |
| 33 | ipfsi 0 pubsub sub --enc=json testTopic | if read line; then |
| 34 | echo $line | jq -j .data > actual && |
| 35 | echo > wait |
| 36 | fi |
| 37 | ) & |
| 38 | ' |
| 39 | |
| 40 | test_expect_success "wait until ipfs pubsub sub is ready to do work" ' |
| 41 | go-sleep 500ms |
| 42 | ' |
| 43 | |
| 44 | test_expect_success "can see peer subscribed to testTopic" ' |
| 45 | ipfsi 1 pubsub peers testTopic > peers_out |
| 46 | ' |
| 47 | |
| 48 | test_expect_success "output looks good" ' |
| 49 | echo $PEERID_0 > peers_exp && |
| 50 | test_cmp peers_exp peers_out |
| 51 | ' |
| 52 | |
| 53 | test_expect_success "publish something from file" ' |
| 54 | echo -n -e "test\nOK" > payload-file && |
| 55 | ipfsi 1 pubsub pub testTopic payload-file &> pubErr |
| 56 | ' |
| 57 | |
| 58 | test_expect_success "wait until echo > wait executed" ' |
| 59 | cat wait && |
| 60 | test_cmp pubErr empty && |
| 61 | test_cmp expected actual |
| 62 | ' |
| 63 | |
| 64 | test_expect_success "wait for another pubsub message" ' |
| 65 | echo -n -e "test\nOK\r\n2" | ipfs multibase encode -b base64url > expected && |
| 66 | mkfifo wait2 || |
| 67 | test_fsh echo init fail |
| 68 | |
| 69 | # ipfs pubsub sub is long-running so we need to start it in the background and |
| 70 | # wait put its output somewhere where we can access it |
| 71 | ( |
| 72 | ipfsi 2 pubsub sub --enc=json testTopic | if read line; then |
| 73 | echo $line | jq -j .data > actual && |
| 74 | echo > wait2 |
| 75 | fi |
| 76 | ) & |
| 77 | ' |
| 78 | |
| 79 | test_expect_success "wait until ipfs pubsub sub is ready to do work" ' |
| 80 | go-sleep 500ms |
| 81 | ' |
| 82 | |
| 83 | test_expect_success "publish something from stdin" ' |
| 84 | echo -n -e "test\nOK\r\n2" | ipfsi 3 pubsub pub testTopic &> pubErr |
| 85 | ' |
| 86 | |
| 87 | test_expect_success "wait until echo > wait executed" ' |
| 88 | cat wait2 && |
| 89 | test_cmp pubErr empty && |
| 90 | test_cmp expected actual |
| 91 | ' |
| 92 | |
| 93 | test_expect_success 'cleanup fifos' ' |
| 94 | rm -f wait wait2 |
| 95 | ' |
| 96 | |
| 97 | } |
| 98 | |
| 99 | # Normal tests - enabled via config |
| 100 | |
| 101 | test_expect_success 'enable the pubsub' ' |
| 102 | iptb run -- ipfs config --json Pubsub.Enabled true |
| 103 | ' |
| 104 | |
| 105 | startup_cluster $NUM_NODES |
| 106 | run_pubsub_tests |
| 107 | test_expect_success 'stop iptb' ' |
| 108 | iptb stop |
| 109 | ' |
| 110 | |
| 111 | test_expect_success 'disable the pubsub' ' |
| 112 | iptb run -- ipfs config --json Pubsub.Enabled false |
| 113 | ' |
| 114 | |
| 115 | # Normal tests - enabled via daemon option flag |
| 116 | |
| 117 | startup_cluster $NUM_NODES --enable-pubsub-experiment |
| 118 | run_pubsub_tests |
| 119 | test_expect_success 'stop iptb' ' |
| 120 | iptb stop |
| 121 | ' |
| 122 | |
| 123 | # Test with some nodes not signing messages. |
| 124 | |
| 125 | test_expect_success 'disable signing on nodes 1-3' ' |
| 126 | iptb run [0-3] -- ipfs config --json Pubsub.DisableSigning true |
| 127 | ' |
| 128 | |
| 129 | startup_cluster $NUM_NODES --enable-pubsub-experiment |
| 130 | |
| 131 | test_expect_success 'set node 4 to listen on testTopic' ' |
| 132 | rm -f node4_actual && |
| 133 | ipfsi 4 pubsub sub --enc=json testTopic > node4_actual & |
| 134 | ' |
| 135 | |
| 136 | run_pubsub_tests |
| 137 | |
| 138 | test_expect_success 'stop iptb' ' |
| 139 | iptb stop |
| 140 | ' |
| 141 | |
| 142 | test_expect_success 'node 4 got no unsigned messages' ' |
| 143 | test_must_be_empty node4_actual |
| 144 | ' |
| 145 | |
| 146 | |
| 147 | # Confirm negative CLI flag takes precedence over positive config |
| 148 | |
| 149 | # --enable-pubsub-experiment=false + Pubsub.Enabled:true |
| 150 | |
| 151 | test_expect_success 'enable the pubsub via config' ' |
| 152 | iptb run -- ipfs config --json Pubsub.Enabled true |
| 153 | ' |
| 154 | startup_cluster $NUM_NODES --enable-pubsub-experiment=false |
| 155 | |
| 156 | test_expect_success 'pubsub cmd fails because it was disabled via cli flag' ' |
| 157 | test_expect_code 1 ipfsi 4 pubsub ls 2> pubsub_cmd_out |
| 158 | ' |
| 159 | |
| 160 | test_expect_success "pubsub cmd produces error" ' |
| 161 | echo "Error: experimental pubsub feature not enabled, run daemon with --enable-pubsub-experiment to use" > expected && |
| 162 | test_cmp expected pubsub_cmd_out |
| 163 | ' |
| 164 | |
| 165 | test_expect_success 'stop iptb' ' |
| 166 | iptb stop |
| 167 | ' |
| 168 | |
| 169 | test_done |