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

Migrate expect based rpc test suite to bats #4209

Merged
merged 3 commits into from
May 1, 2020
Merged
Changes from 1 commit
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
202 changes: 64 additions & 138 deletions cli/test.sh
Original file line number Diff line number Diff line change
@@ -1,156 +1,82 @@
#!/bin/bash
#!/usr/bin/env bats
#
# References & examples for expect:
# bats v0.4.0 project
#
# - https://pantz.org/software/expect/expect_examples_and_tips.html
# - https://stackoverflow.com/questions/13982310/else-string-matching-in-expect
# - https://gist.github.com/Fluidbyte/6294378
# - https://www.oreilly.com/library/view/exploring-expect/9781565920903/ch04.html
# https://github.com/sstephenson/bats/tree/v0.4.0
#
# Prior to running this script, run:
#
# ./bisq-daemon --apiPassword=xyz
#
# To run this script:
#
# cd <project-root-dir>
# bats cli/test.sh
#
# The data directory used must contain an unencrypted wallet with a 0 BTC balance

# Ensure project root is the current working directory
cd $(dirname $0)/..
@test "test unsupported method error" {
run ./bisq-cli --password=xyz bogus
[ "$status" -eq 1 ]
echo "actual output: $output" >&2 # printed only on test failure
[ "$output" = "Error: 'bogus' is not a supported method" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST unsupported cmd error"
set expected "Error: '\''bogus'\'' is not a supported method"
spawn ./bisq-cli --password=xyz bogus
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test unrecognized option error" {
run ./bisq-cli --bogus getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: bogus is not a recognized option" ]
}

OUTPUT=$(expect -c '
puts "TEST unrecognized option error"
set expected "Error: bogus is not a recognized option"
spawn ./bisq-cli --bogus getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test missing required password option error" {
run ./bisq-cli getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: missing required 'password' option" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST missing required password option error"
set expected "Error: missing required '\''password'\'' option"
spawn ./bisq-cli getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test incorrect password error" {
run ./bisq-cli --password=bogus getversion
[ "$status" -eq 1 ]
echo "actual output: $output" >&2
[ "$output" = "Error: incorrect 'password' rpc header value" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST getversion (incorrect password error)"
set expected "Error: incorrect '\''password'\'' rpc header value"
spawn ./bisq-cli --password=bogus getversion
expect {
$expected { puts "PASS\n" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getversion call with quoted password" {
run ./bisq-cli --password="xyz" getversion
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "1.3.2" ]
}

OUTPUT=$(expect -c '
# exp_internal 1
puts "TEST getversion (password value in quotes)"
set expected "1.3.2"
# Note: have to define quoted argument in a variable as "''value''"
set pwd_in_quotes "''xyz''"
spawn ./bisq-cli --password=$pwd_in_quotes getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getversion" {
run ./bisq-cli --password=xyz getversion
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "1.3.2" ]
}

OUTPUT=$(expect -c '
puts "TEST getversion"
set expected "1.3.2"
spawn ./bisq-cli --password=xyz getversion
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test getbalance (available & unlocked wallet with 0 btc balance)" {
run ./bisq-cli --password=xyz getbalance
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "0.00000000" ]
}

OUTPUT=$(expect -c '
puts "TEST getbalance"
# exp_internal 1
set expected "0.00000000"
spawn ./bisq-cli --password=xyz getbalance
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test help displayed on stderr if no options or arguments" {
run ./bisq-cli
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Bisq RPC Client" ]
[ "${lines[1]}" = "Usage: bisq-cli [options] <method>" ]
# TODO add asserts after help text is modified for new endpoints
}

OUTPUT=$(expect -c '
puts "TEST running with no options or arguments prints help text"
# exp_internal 1
set expected "Bisq RPC Client"
spawn ./bisq-cli
expect {
$expected { puts "PASS" }
default {
set results $expect_out(buffer)
puts "FAIL expected = $expected"
puts " actual = $results"
}
}
')
echo "$OUTPUT"
echo "========================================================================"
@test "test --help option" {
run ./bisq-cli --help
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Bisq RPC Client" ]
[ "${lines[1]}" = "Usage: bisq-cli [options] <method>" ]
# TODO add asserts after help text is modified for new endpoints
}

echo "TEST --help option prints help text"
./bisq-cli --help