Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7515aec

Browse files
chriscooldaviddias
authored andcommitted
Add basic sharness tests (#504)
* test: add sharness support files * test: add ipfs-test-lib.sh * test/sharness: unignore 'lib' directory below * test/sharness: add lib/test-lib-hashes.sh * test/sharness: add lib/iptb-lib.sh * test/sharness: add lib/test-lib.sh * test: ignore bin/ * sharness: improve the Makefile * sharness: add t0010-basic-commands.sh * test-lib: properly check the 'ipfs' tool we use * Add Makefile at the root This Makefile is just to launch Sharness tests for now. * .travis: add 'make test' script
1 parent 7e30667 commit 7515aec

12 files changed

+740
-1
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ script:
1313
- npm run lint
1414
- npm test
1515
- npm run coverage
16-
16+
- make test
1717

1818
before_script:
1919
- export DISPLAY=:99.0

Makefile

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
all: help
2+
3+
test: test_expensive
4+
5+
test_short: test_sharness_short
6+
7+
test_expensive: test_sharness_expensive
8+
9+
test_sharness_short:
10+
$(MAKE) -j1 -C test/sharness/
11+
12+
test_sharness_expensive:
13+
TEST_EXPENSIVE=1 $(MAKE) -j1 -C test/sharness/
14+
15+
help:
16+
@echo 'TESTING TARGETS:'
17+
@echo ''
18+
@echo ' test - Run expensive tests'
19+
@echo ' test_short - Run short tests and sharness tests'
20+
@echo ' test_expensive - Run a few extras'
21+
@echo ' test_sharness_short'
22+
@echo ' test_sharness_expensive'
23+
@echo ''

test/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

test/ipfs-test-lib.sh

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Generic test functions for ipfs cli tests
2+
3+
# Quote arguments for sh eval
4+
shellquote() {
5+
_space=''
6+
for _arg
7+
do
8+
# On Mac OS, sed adds a newline character.
9+
# With a printf wrapper the extra newline is removed.
10+
printf "$_space'%s'" "$(printf "%s" "$_arg" | sed -e "s/'/'\\\\''/g;")"
11+
_space=' '
12+
done
13+
printf '\n'
14+
}
15+
16+
# Echo the args, run the cmd, and then also fail,
17+
# making sure a test case fails.
18+
test_fsh() {
19+
echo "> $@"
20+
eval $(shellquote "$@")
21+
echo ""
22+
false
23+
}
24+
25+
# Same as sharness' test_cmp but using test_fsh (to see the output).
26+
# We have to do it twice, so the first diff output doesn't show unless it's
27+
# broken.
28+
test_cmp() {
29+
diff -q "$@" >/dev/null || test_fsh diff -u "$@"
30+
}
31+
32+
# Same as test_cmp above, but we sort files before comparing them.
33+
test_sort_cmp() {
34+
sort "$1" >"$1_sorted" &&
35+
sort "$2" >"$2_sorted" &&
36+
test_cmp "$1_sorted" "$2_sorted"
37+
}
38+
39+
# Same as test_cmp above, but we standardize directory
40+
# separators before comparing the files.
41+
test_path_cmp() {
42+
sed -e "s/\\\\/\//g" "$1" >"$1_std" &&
43+
sed -e "s/\\\\/\//g" "$2" >"$2_std" &&
44+
test_cmp "$1_std" "$2_std"
45+
}
46+
47+
# Docker
48+
49+
# This takes a Dockerfile, and a build context directory
50+
docker_build() {
51+
docker build --rm -f "$1" "$2"
52+
}
53+
54+
# This takes an image as argument and writes a docker ID on stdout
55+
docker_run() {
56+
docker run -d "$1"
57+
}
58+
59+
# This takes a docker ID and a command as arguments
60+
docker_exec() {
61+
if test "$CIRCLE" = 1
62+
then
63+
sudo lxc-attach -n "$(docker inspect --format '{{.Id}}' $1)" -- /bin/bash -c "$2"
64+
else
65+
docker exec -t "$1" /bin/bash -c "$2"
66+
fi
67+
}
68+
69+
# This takes a docker ID as argument
70+
docker_stop() {
71+
docker stop "$1"
72+
}
73+
74+
# Test whether all the expected lines are included in a file. The file
75+
# can have extra lines.
76+
#
77+
# $1 - Path to file with expected lines.
78+
# $2 - Path to file with actual output.
79+
#
80+
# Examples
81+
#
82+
# test_expect_success 'foo says hello' '
83+
# echo hello >expected &&
84+
# foo >actual &&
85+
# test_cmp expected actual
86+
# '
87+
#
88+
# Returns the exit code of the command set by TEST_CMP.
89+
test_includes_lines() {
90+
sort "$1" >"$1_sorted" &&
91+
sort "$2" >"$2_sorted" &&
92+
comm -2 -3 "$1_sorted" "$2_sorted" >"$2_missing" &&
93+
[ ! -s "$2_missing" ] || test_fsh comm -2 -3 "$1_sorted" "$2_sorted"
94+
}
95+
96+
# Depending on GNU seq availability is not nice.
97+
# Git also has test_seq but it uses Perl.
98+
test_seq() {
99+
test "$1" -le "$2" || return
100+
i="$1"
101+
j="$2"
102+
while test "$i" -le "$j"
103+
do
104+
echo "$i"
105+
i=$(expr "$i" + 1)
106+
done
107+
}

test/sharness/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
!/lib/
2+
lib/sharness/
3+
test-results/
4+
trash directory.*.sh/

test/sharness/Makefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Run sharness tests
2+
#
3+
# Copyright (c) 2016 Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
#
6+
# NOTE: run with TEST_VERBOSE=1 for verbose sharness tests.
7+
8+
T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
9+
LIBDIR = lib
10+
SHARNESSDIR = sharness
11+
AGGREGATE = $(LIBDIR)/$(SHARNESSDIR)/aggregate-results.sh
12+
13+
14+
BINS = ../bin/ipfs
15+
16+
all: aggregate
17+
18+
help:
19+
@echo "- use 'make' or 'make all' to run all the tests"
20+
@echo "- use 'make deps' to create an 'ipfs' executable in ../bin"
21+
@echo "- to run tests manually, make sure to include ../bin in your PATH"
22+
23+
clean: clean-test-results
24+
@echo "*** $@ ***"
25+
-rm -rf ../bin/ipfs
26+
27+
clean-test-results:
28+
@echo "*** $@ ***"
29+
-rm -rf test-results
30+
31+
$(T): clean-test-results deps
32+
@echo "*** $@ ***"
33+
./$@
34+
35+
aggregate: clean-test-results $(T)
36+
@echo "*** $@ ***"
37+
ls test-results/t*-*.sh.*.counts | $(AGGREGATE)
38+
39+
deps: sharness $(BINS) curl
40+
41+
sharness:
42+
@echo "*** checking $@ ***"
43+
lib/install-sharness.sh
44+
45+
../bin/ipfs:
46+
mkdir -p ../bin
47+
cd ../bin && ln -s ../../src/cli/bin.js ipfs
48+
49+
curl:
50+
@which curl >/dev/null || (echo "Please install curl!" && false)
51+
52+
.PHONY: all help clean clean-test-results $(T) aggregate deps sharness
53+

test/sharness/lib/install-sharness.sh

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/sh
2+
# install-sharness.sh
3+
#
4+
# Copyright (c) 2014 Juan Batiz-Benet
5+
# Copyright (c) 2015 Christian Couder
6+
# MIT Licensed; see the LICENSE file in this repository.
7+
#
8+
# This script checks that Sharness is installed in:
9+
#
10+
# $(pwd)/$clonedir/$sharnessdir/
11+
#
12+
# where $clonedir and $sharnessdir are configured below.
13+
#
14+
# If Sharness is not installed, this script will clone it
15+
# from $urlprefix (defined below).
16+
#
17+
# If Sharness is not uptodate with $version (defined below),
18+
# this script will fetch and will update the installed
19+
# version to $version.
20+
#
21+
22+
# settings
23+
version=ecba410b0b58400dd6517cfa6594fdac243d9056
24+
urlprefix=https://github.com/chriscool/sharness.git
25+
clonedir=lib
26+
sharnessdir=sharness
27+
28+
if test -f "$clonedir/$sharnessdir/SHARNESS_VERSION_$version"
29+
then
30+
# There is the right version file. Great, we are done!
31+
exit 0
32+
fi
33+
34+
die() {
35+
echo >&2 "$@"
36+
exit 1
37+
}
38+
39+
checkout_version() {
40+
git checkout "$version" || die "Could not checkout '$version'"
41+
rm -f SHARNESS_VERSION_* || die "Could not remove 'SHARNESS_VERSION_*'"
42+
touch "SHARNESS_VERSION_$version" || die "Could not create 'SHARNESS_VERSION_$version'"
43+
echo "Sharness version $version is checked out!"
44+
}
45+
46+
if test -d "$clonedir/$sharnessdir/.git"
47+
then
48+
# We need to update sharness!
49+
cd "$clonedir/$sharnessdir" || die "Could not cd into '$clonedir/$sharnessdir' directory"
50+
git fetch || die "Could not fetch to update sharness"
51+
else
52+
# We need to clone sharness!
53+
mkdir -p "$clonedir" || die "Could not create '$clonedir' directory"
54+
cd "$clonedir" || die "Could not cd into '$clonedir' directory"
55+
56+
git clone "$urlprefix" || die "Could not clone '$urlprefix'"
57+
cd "$sharnessdir" || die "Could not cd into '$sharnessdir' directory"
58+
fi
59+
60+
checkout_version

test/sharness/lib/iptb-lib.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# iptb test framework
2+
#
3+
# Copyright (c) 2014, 2016 Jeromy Johnson, Christian Couder
4+
# MIT Licensed; see the LICENSE file in this repository.
5+
6+
export IPTB_ROOT="$(pwd)/.iptb"
7+
8+
ipfsi() {
9+
dir="$1"
10+
shift
11+
IPFS_PATH="$IPTB_ROOT/$dir" ipfs "$@"
12+
}
13+
14+
check_has_connection() {
15+
node="$1"
16+
ipfsi "$node" swarm peers >"swarm_peers_$node" &&
17+
grep "ipfs" "swarm_peers_$node" >/dev/null
18+
}
19+
20+
startup_cluster() {
21+
num_nodes="$1"
22+
bound=$(expr "$num_nodes" - 1)
23+
24+
test_expect_success "start up nodes" '
25+
iptb start
26+
'
27+
28+
test_expect_success "connect nodes to eachother" '
29+
iptb connect [1-$bound] 0
30+
'
31+
32+
for i in $(test_seq 0 "$bound")
33+
do
34+
test_expect_success "node $i is connected" '
35+
check_has_connection "$i" ||
36+
test_fsh cat "swarm_peers_$i"
37+
'
38+
done
39+
}

test/sharness/lib/test-lib-hashes.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# this file defines several useful hashes used across the test codebase.
2+
# thus they can be defined + changed in one place
3+
4+
HASH_WELCOME_DOCS="QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG"
5+
HASH_GATEWAY_ASSETS="QmXB7PLRWH6bCiwrGh2MrBBjNkLv3mY3JdYXCikYZSwLED"
6+
HASH_HELP_PAGE="QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7"
7+
HASH_EMPTY_DIR="QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"

0 commit comments

Comments
 (0)