Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit 5c934c5

Browse files
committed
Merge pull request #21 from uber/ringpop-admin-tests
Add test framework
2 parents 450dfae + fc3af1b commit 5c934c5

18 files changed

+1197
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/*
22
npm-debug.log
33
*.swp
44
hosts.json
5+
.bin

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
before_script:
5+
# Install cram without sudo. This allows us to use Travis containers
6+
# which are *much* faster than using sudo: yes.
7+
- "pip install --target=/tmp cram"
8+
- "mv /tmp/cram.py /tmp/cram"
9+
- "chmod +x /tmp/cram"
10+
- export PATH=$PATH:/tmp
11+
script: npm test

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ringpop-admin
22
Command-line tools for Ringpop
33

4-
# Usage
4+
## Usage
55

66
```
77
Usage: ringpop-admin [options] [command]
@@ -29,3 +29,15 @@ Command-line tools for Ringpop
2929
-h, --help output usage information
3030
-V, --version output the version number
3131
```
32+
33+
## Tests
34+
35+
Tests are run by [cram](https://bitheap.org/cram/). To run the tests, first install cram:
36+
37+
pip install cram
38+
39+
Then run the tests:
40+
41+
npm test
42+
43+
For more information about how the tests work, see the file `tests/README.md`.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
},
2323
"scripts": {
2424
"jshint": "jshint --verbose *.js lib/*.js",
25-
"tchannel-v1": "mv node_modules/tchannel node_modules/tchannelv2 && npm install [email protected] && mv node_modules/tchannel node_modules/tchannelv1 && mv node_modules/tchannelv2 node_modules/tchannel"
25+
"tchannel-v1": "mv node_modules/tchannel node_modules/tchannelv2 && npm install [email protected] && mv node_modules/tchannel node_modules/tchannelv1 && mv node_modules/tchannelv2 node_modules/tchannel",
26+
"test": "cd tests && ./run-tests"
2627
},
2728
"dependencies": {
2829
"async": "^0.9.0",
@@ -38,6 +39,7 @@
3839
"pre-commit": "^0.0.9"
3940
},
4041
"pre-commit": [
41-
"jshint"
42+
"jshint",
43+
"test"
4244
]
4345
}

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.t.err
2+
hosts.json

tests/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
ringpop-admin tests
2+
===================
3+
4+
ringpop-admin tests are written using [cram](https://bitheap.org/cram/). Cram runs rinpop-admin commands and compares the output to known good output.
5+
6+
In order to run ringpop-admin commands, the test runner spins up a test cluster using `tick-cluster.js` (from ringpop-node).
7+
8+
9+
Prerequisites
10+
-------------
11+
12+
* cram (tested with v0.6) (`sudo pip install cram`)
13+
14+
Tested on Mac OS X.
15+
16+
Note that `tick-cluster.js` uses TCP ports 2999-3005. Please ensure these ports are free before running the tests.
17+
18+
19+
Running the tests
20+
-----------------
21+
22+
You can run the tests by doing:
23+
24+
./run-tests
25+
26+
Modifying existing tests
27+
------------------------
28+
29+
If you've modified the output of ringpop-admin and you want to update the "known good" data, call `run-tests` with the `--update` flag:
30+
31+
./run-tests --update
32+
33+
This will pass the `-i` flag to cram. When the test fails, a diff will be displayed and you will have the option to accept the new output, e.g.:
34+
35+
$ ./run-tests --update
36+
...
37+
ringpop-admin.t: failed
38+
--- ringpop-admin.t
39+
+++ ringpop-admin.t.err
40+
@@ -20,6 +20,7 @@
41+
leave Makes node leave the cluster
42+
list List member information
43+
lookup Lookup a key in the ring
44+
+ join Makes node (re)join the cluster
45+
status Status of members in ring
46+
partitions Show partition information of a ring
47+
top General membership information
48+
Accept this change? [yN]
49+
50+
51+
When you press 'Y' the test file will automatically be updated. You should commit the updated file to the repository.
52+
53+
Creating new tests
54+
------------------
55+
56+
There is a tool to help you get started creating new tests.
57+
58+
Call this script with an optional description and then the command you want to test:
59+
60+
./create-cram-test "Test help output" -- ringpop-admin --help
61+
62+
The script will output a valid cram test to stdout.

tests/checksum.t

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Test checksum success:
2+
3+
$ ringpop-admin checksums 127.0.0.1:3000
4+
127.0.0.1:3000 * (glob)
5+
127.0.0.1:3001 * (glob)
6+
127.0.0.1:3002 * (glob)
7+
127.0.0.1:3003 * (glob)
8+
127.0.0.1:3004 * (glob)
9+
10+
With bootstrap file option:
11+
12+
$ ringpop-admin checksums $TESTDIR/hosts.json
13+
127.0.0.1:3000 * (glob)
14+
127.0.0.1:3001 * (glob)
15+
127.0.0.1:3002 * (glob)
16+
127.0.0.1:3003 * (glob)
17+
127.0.0.1:3004 * (glob)
18+
19+
Invalid bootstrap file
20+
21+
$ ringpop-admin checksums QWERTY
22+
23+
assert.js:93
24+
throw new assert.AssertionError({
25+
^
26+
AssertionError: invalid destination
27+
at TChannelPeer.makeOutSocket * (glob)
28+
at TChannelPeer.connect * (glob)
29+
at TChannelPeer.waitForIdentified * (glob)
30+
at TChannel.waitForIdentified * (glob)
31+
at AdminClient.requestV2 * (glob)
32+
at AdminClient.request * (glob)
33+
at AdminClient.stats * (glob)
34+
at mapMember * (glob)
35+
at Object.q.process [as _onImmediate] * (glob)
36+
at processImmediate [as _immediateCallback] * (glob)
37+
[8]

tests/count.t

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Test count success:
2+
3+
$ ringpop-admin count -m 127.0.0.1:3000
4+
5
5+
$ ringpop-admin count -h 127.0.0.1:3000
6+
1
7+
$ ringpop-admin count -p 127.0.0.1:3000
8+
1
9+
10+
With bootstrap file:
11+
12+
$ ringpop-admin count -m $TESTDIR/hosts.json
13+
5
14+
$ ringpop-admin count -h $TESTDIR/hosts.json
15+
1
16+
$ ringpop-admin count -p $TESTDIR/hosts.json
17+
1

tests/create-cram-test

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
#
3+
# Helper tool for quickly creating new cram tests.
4+
#
5+
6+
declare desc=
7+
declare indent_num=2
8+
9+
usage() {
10+
{
11+
echo
12+
echo "Create new cram test and output to stdout."
13+
echo "For more info, see https://bitheap.org/cram/"
14+
echo
15+
echo "Usage: $0 [description] -- <command> [args ...]"
16+
echo " E.g.: $0 \"Test bash help output\" -- bash --help >> tests/bash-help.t"
17+
echo
18+
} >&2
19+
}
20+
21+
#
22+
# Adds quotes around the given parameter, if necessary.
23+
#
24+
# $1:
25+
#
26+
_escape() {
27+
local escaped_args=
28+
local n=
29+
30+
for s in "$@"; do
31+
32+
unset n
33+
34+
if [[ "$s" =~ \ |\' ]]; then
35+
n="\"$s\""
36+
else
37+
n="$s"
38+
fi
39+
escaped_args="$escaped_args $n"
40+
done
41+
42+
echo "$escaped_args"
43+
}
44+
45+
if [ "$1" != "--" ]; then
46+
desc=$1
47+
shift
48+
fi
49+
50+
if [ "$1" != "--" ]; then
51+
usage
52+
exit 1
53+
fi
54+
55+
if [ -z "$desc" ]; then
56+
desc="Test"
57+
fi
58+
59+
shift # '--'
60+
61+
declare indent=$(printf "%${indent_num}s" "")
62+
63+
# Output cram test
64+
echo "$desc:"
65+
echo
66+
echo "${indent}\$ $(_escape "$@")"
67+
68+
return_output=$("$@" 2>&1)
69+
return_code=$?
70+
71+
echo "$return_output" | sed "s/^/${indent}/g"
72+
73+
# Add return code check if it's not success
74+
if [ $return_code -gt 0 ]; then
75+
echo "${indent}[$return_code]"
76+
fi

tests/dump.t

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Dump command dumps file successfully:
2+
3+
$ ringpop-admin dump $TESTDIR/hosts.json -f /tmp/ringpop-admin-dump-test.json
4+
5+
Check for non-zero file:
6+
7+
$ du /tmp/ringpop-admin-dump-test.json |awk '{print $1}'
8+
[^0] (re)
9+
$ rm -f /tmp/ringpop-admin-dump-test.json

0 commit comments

Comments
 (0)