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

Run tests on Travis CI #224

Merged
merged 33 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
ce7b4c8
Run tests on Travis CI
abitrolly Jul 29, 2020
40d18da
Test server is up with `curl` before running tests
abitrolly Jul 29, 2020
06d8fa7
`cd` to tests/ dir when runner is executed with relpath
abitrolly Jul 29, 2020
3905047
Wait 3 seconds to let server to start
abitrolly Jul 29, 2020
c515c56
Use `python3` command if virtualenv doesn't exist
abitrolly Jul 29, 2020
1a1624e
Detect Python version using Python
abitrolly Jul 29, 2020
1a5aa64
Turn off standalone mode to run tests against remote server
abitrolly Jul 29, 2020
60ae85e
Show which test is being run
abitrolly Jul 29, 2020
09dff20
Add color to printed test names
abitrolly Jul 29, 2020
c0bbc24
Run `curl https://cht.sh` to pinpoint Travis problems with ANSI
abitrolly Jul 30, 2020
4610690
Travis doesn't support bright colors, use 8 colors palette
abitrolly Jul 30, 2020
1387f8b
Show test command executed when `$show_details == YES`
abitrolly Jul 30, 2020
5737a16
Use unified diff format
abitrolly Jul 30, 2020
a0d344c
Use http://localhost:8002 for default tests CHTSH_URL
abitrolly Jul 30, 2020
e0f84e8
Pipe colored diffs
abitrolly Jul 30, 2020
1032623
Fix `btrfs` test drift as trailing whitespaces were fixed
abitrolly Jul 31, 2020
be9d3a7
Run cheat.sh service (app) by `docker-compose` in debug mode
abitrolly Jul 31, 2020
6be6a25
`docker-compose run` requires explicit port permission
abitrolly Jul 31, 2020
9a495a7
Try to show container log in case of error on Travis
abitrolly Jul 31, 2020
5dab632
Try --no-color to avoid log output being stripped by Travis
abitrolly Jul 31, 2020
10bfaab
Modern DevOps is a PITA
abitrolly Jul 31, 2020
2e1d804
Try `docker-compose` logs with no color
abitrolly Jul 31, 2020
f62fb51
View container log without `docker-compose`
abitrolly Jul 31, 2020
ca55415
Check `docker logs` with unbuffered Python again
abitrolly Jul 31, 2020
198a2c4
Add --debug option to chtsh container the `docker-compose` way
abitrolly Jul 31, 2020
acd07d2
Override files needs a version too
abitrolly Jul 31, 2020
ee273e2
Now fixing connection to Redis on Travis Docker
abitrolly Jul 31, 2020
91320d9
Set Redis host using cheat.sh config framework
abitrolly Jul 31, 2020
36a7cd3
Set proper config names and update `docker` status in README.md
abitrolly Jul 31, 2020
ca88810
run-tests.sh Quotes are not needed in default param
abitrolly Aug 1, 2020
b74707a
Redis doesn't need to be Strict
abitrolly Aug 1, 2020
18e851c
Merge branch 'master' into run-tests
chubin Aug 6, 2020
7578ab7
minifix
chubin Aug 6, 2020
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
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ language:
- generic

before_install:
- docker-compose up -d
- docker ps
- docker-compose build
- docker images
- docker-compose -f docker-compose.yml -f docker-compose.debug.yml up -d
- docker-compose ps

script:
- sleep 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sleep is probably not a good way to know when the server is up. If you run it locally on a slower machine this can easily fail.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, cap. So any ideas how to fix that - https://travis-ci.com/github/abitrolly/cheat.sh/builds/177678164#L390? I checked - it is non-trivial

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retry after one second up to X if the exit code is 52 maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How exit code 52 is related?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command "curl http://localhost:8002" exited with 52.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe better check until a port is accepting connections?

- curl http://localhost:8002
- docker-compose logs --no-color
- docker logs chtsh
- CHEATSH_TEST_STANDALONE=NO bash tests/run-tests.sh
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ RUN mkdir -p /root/.cheat.sh/log/ \

# installing server dependencies
RUN apk add --update --no-cache py3-jinja2 py3-flask bash gawk
ENTRYPOINT ["python3"]
CMD ["bin/srv.py"]
ENTRYPOINT ["python3", "-u", "bin/srv.py"]
CMD [""]
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,16 @@ scoop install cht

### Docker

Currently, the easiest way to get a self-hosted instance running is by using the docker-compose.yml file provided in the extra/docker folder.
This pulls down the latest image with baked in cheatsheets and starts the app and a Redis instance to back it, making the service available on port 8002 of the local host. This is currently an early implementation and should probably not be used for anything outside of internal/dev/personal use right now.
Currently, the easiest way to get a self-hosted instance running is by using
the `docker-compose.yml` file.

docker-compose up

This builds and runs the image with baked in cheatsheets and starts the app
and a Redis instance to back it, making the service available at
http://localhost:8002 This is currently an early implementation and should
probably not be used for anything outside of internal/dev/personal use right
now.

## Editors integration

Expand Down
2 changes: 1 addition & 1 deletion bin/clean_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import redis
REDIS = redis.StrictRedis(host='localhost', port=6379, db=0)
REDIS = redis.Redis(host='localhost', port=6379, db=0)

for key in sys.argv[1:]:
REDIS.delete(key)
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Compose override to add --debug option to bin/srv.py
# call to print tracebacks on errors to stdout.
#
# See https://docs.docker.com/compose/extends/
version: '2'
services:
app:
command: "--debug"
7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
version: '2'
services:
app:
build:
context: .
build: .
image: cheat.sh
container_name: chtsh
depends_on:
- redis
environment:
- CHEATSH_CACHE_REDIS_HOST=redis
ports:
- "8002:8002"
redis:
Expand Down
2 changes: 1 addition & 1 deletion lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
_REDIS = None
if CONFIG['cache.type'] == 'redis':
import redis
_REDIS = redis.StrictRedis(
_REDIS = redis.Redis(
host=CONFIG['cache.redis.host'],
port=CONFIG['cache.redis.port'],
db=CONFIG['cache.redis.db'])
Expand Down
2 changes: 1 addition & 1 deletion tests/results/4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Create a btrfs file system on /dev/sdb, /dev/sdc, and /dev/sdd
mkfs.btrfs /dev/sdb /dev/sdc /dev/sdd

# btrfs with just one hard drive, metadata not redundant 
# btrfs with just one hard drive, metadata not redundant
# (this is danegerous: if your metadata is lost, your data is lost as well)
mkfs.btrfs -m single /dev/sdb

Expand Down
31 changes: 24 additions & 7 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,36 @@

# 1) start server:
# without caching:
# REDIS_HOST=None CHEATSH_PORT=50000 python bin/srv.py
# CHEATSH_CACHE_TYPE=none CHEATSH_PORT=50000 python bin/srv.py
# (recommended)
# with caching:
# REDIS_PREFIX=TEST1 CHEATSH_PORT=50000 python bin/srv.py
# CHEATSH_REDIS_PREFIX=TEST1 CHEATSH_PORT=50000 python bin/srv.py
# (for complex search queries + to test caching)
# 2) configure CHTSH_URL
# 3) run the script

# work from script's dir
cd "$(dirname "$0")" || exit
abitrolly marked this conversation as resolved.
Show resolved Hide resolved

# detect Python - if not set in env, try default virtualenv
PYTHON="${PYTHON:-../ve/bin/python}"
"$PYTHON" --version 2>&1 | grep -q 'Python 2' && python_version=2 || python_version=3
# if no virtalenv, try current python3 binary
if ! command -v "$PYTHON" &> /dev/null; then
PYTHON=$(command -v python3)
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
fi
python_version="$($PYTHON -c 'import sys; print(sys.version_info[0])')"
echo "Using PYTHON $python_version: $PYTHON"

skip_online="${CHEATSH_TEST_SKIP_ONLINE:-NO}"
test_standalone="${CHEATSH_TEST_STANDALONE:-YES}"
show_details="${CHEATSH_TEST_SHOW_DETAILS:-YES}"
CHTSH_URL="${CHTSH_URL:-http://localhost:8002}"

TMP=$(mktemp /tmp/cht.sh.tests-XXXXXXXXXXXXXX)
TMP2=$(mktemp /tmp/cht.sh.tests-XXXXXXXXXXXXXX)
TMP3=$(mktemp /tmp/cht.sh.tests-XXXXXXXXXXXXXX)
trap 'rm -rf $TMP $TMP2 $TMP3' EXIT

export CHTSH_URL=http://cht.sh:50000
CHTSH_SCRIPT=$(dirname "$(dirname "$(readlink -f "$0")")")/share/cht.sh.txt

export PYTHONIOENCODING=UTF-8
Expand All @@ -39,6 +48,7 @@ failed=0


while read -r number test_line; do
echo -e "\e[34mRunning $number: \e[36m$test_line\e[0m"
if [ "$skip_online" = YES ]; then
if [[ $test_line = *\[online\]* ]]; then
echo "$number is [online]; skipping"
Expand All @@ -47,10 +57,12 @@ while read -r number test_line; do
fi

if [[ "$python_version" = 2 ]] && [[ $test_line = *\[python3\]* ]]; then
echo "$number is for Python 3; skipping"
continue
fi

if [[ "$python_version" = 3 ]] && [[ $test_line = *\[python2\]* ]]; then
echo "$number is for Python 2; skipping"
continue
fi

Expand All @@ -59,19 +71,24 @@ while read -r number test_line; do

if [ "$test_standalone" = YES ]; then
test_line="${test_line//cht.sh /}"
"${PYTHON}" ../lib/standalone.py "$test_line" > "$TMP" 2> /dev/null
[[ $show_details == YES ]] && echo "${PYTHON} ../lib/standalone.py $test_line"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could convert this to a function which takes the string as an argument.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I am that bash proficient. Show an example?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log() {
  [[ $show_details == YES ]] && echo "$1" 
}

log("${PYTHON} ../lib/standalone.py $test_line")

"${PYTHON}" ../lib/standalone.py "$test_line" > "$TMP"
elif [[ $test_line = "cht.sh "* ]]; then
test_line="${test_line//cht.sh /}"
[[ $show_details == YES ]] && echo "bash $CHTSH_SCRIPT $test_line"
eval "bash $CHTSH_SCRIPT $test_line" > "$TMP"
else
[[ $show_details == YES ]] && echo "curl -s $CHTSH_URL/$test_line"
eval "curl -s $CHTSH_URL/$test_line" > "$TMP"
fi

if ! diff results/"$number" "$TMP" > "$TMP2"; then
if ! diff -u3 --color=always results/"$number" "$TMP" > "$TMP2"; then
if [ "$show_details" = YES ]; then
echo "$ CHEATSH_CACHE_TYPE=none python ../lib/standalone.py $test_line"
cat "$TMP2"
fi
if grep -q "Internal Server Error" "$TMP2"; then
[[ $TRAVIS == true ]] && docker logs chtsh
fi
echo "FAILED: [$number] $test_line"
((failed++))
fi
Expand Down