diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..4cc34ba --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +sudo: required +language: python +addons: + hosts: + - docker +services: + - docker +python: + - "2.7" +install: + - docker build -f tests/docker/$ANKI_VERSION/Dockerfile -t txgio/anki-connect:$ANKI_VERSION . +script: + - docker run -ti -d --rm -p 8888:8765 -e ANKICONNECT_BIND_ADDRESS=0.0.0.0 txgio/anki-connect:$ANKI_VERSION + - ./tests/scripts/wait-up.sh http://docker:8888 + - python -m unittest discover -s tests -v + +env: + - ANKI_VERSION=2.0.x + - ANKI_VERSION=2.1.x \ No newline at end of file diff --git a/tests/docker/2.0.x/Dockerfile b/tests/docker/2.0.x/Dockerfile new file mode 100644 index 0000000..650c050 --- /dev/null +++ b/tests/docker/2.0.x/Dockerfile @@ -0,0 +1,14 @@ +FROM txgio/anki:2.0.45 + +RUN apt-get update && \ + apt-get install -y xvfb + +COPY AnkiConnect.py /data/addons/AnkiConnect.py + +COPY tests/docker/2.0.x/prefs.db /data/prefs.db + +ADD tests/docker/2.0.x/entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] + +CMD ["anki", "-b", "/data"] \ No newline at end of file diff --git a/tests/docker/2.0.x/entrypoint.sh b/tests/docker/2.0.x/entrypoint.sh new file mode 100755 index 0000000..8285fb9 --- /dev/null +++ b/tests/docker/2.0.x/entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e + +# Start Xvfb +Xvfb -ac -screen scrn 1280x2000x24 :99.0 & +export DISPLAY=:99.0 + +exec "$@" \ No newline at end of file diff --git a/tests/docker/2.0.x/prefs.db b/tests/docker/2.0.x/prefs.db new file mode 100644 index 0000000..eee5d70 Binary files /dev/null and b/tests/docker/2.0.x/prefs.db differ diff --git a/tests/docker/2.1.x/Dockerfile b/tests/docker/2.1.x/Dockerfile new file mode 100644 index 0000000..3438c74 --- /dev/null +++ b/tests/docker/2.1.x/Dockerfile @@ -0,0 +1,14 @@ +FROM txgio/anki:2.1.0beta14 + +RUN apt-get update && \ + apt-get install -y xvfb + +COPY AnkiConnect.py /data/addons21/AnkiConnect/__init__.py + +COPY tests/docker/2.1.x/prefs21.db /data/prefs21.db + +ADD tests/docker/2.1.x/entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] + +CMD ["anki", "-b", "/data"] \ No newline at end of file diff --git a/tests/docker/2.1.x/entrypoint.sh b/tests/docker/2.1.x/entrypoint.sh new file mode 100755 index 0000000..8285fb9 --- /dev/null +++ b/tests/docker/2.1.x/entrypoint.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e + +# Start Xvfb +Xvfb -ac -screen scrn 1280x2000x24 :99.0 & +export DISPLAY=:99.0 + +exec "$@" \ No newline at end of file diff --git a/tests/docker/2.1.x/prefs21.db b/tests/docker/2.1.x/prefs21.db new file mode 100644 index 0000000..dc4d505 Binary files /dev/null and b/tests/docker/2.1.x/prefs21.db differ diff --git a/tests/scripts/wait-up.sh b/tests/scripts/wait-up.sh new file mode 100755 index 0000000..6491b19 --- /dev/null +++ b/tests/scripts/wait-up.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -e + +if [ $# -lt 1 ]; then + printf "First parameter URL required.\n" + exit 1 +fi + +COUNTER=0 +STEP_SIZE=1 +MAX_SECONDS=${2:-10} # Wait 10 seconds if parameter not provided +MAX_RETRIES=$(( $MAX_SECONDS / $STEP_SIZE)) + +URL=$1 + +printf "Waiting URL: "$URL"\n" + +until $(curl --insecure --output /dev/null --silent --fail $URL) || [ $COUNTER -eq $MAX_RETRIES ]; do + printf '.' + sleep $STEP_SIZE + COUNTER=$(($COUNTER + 1)) +done +if [ $COUNTER -eq $MAX_RETRIES ]; then + printf "\nTimeout after "$(( $COUNTER * $STEP_SIZE))" second(s).\n" + exit 2 +else + printf "\nUp successfully after "$(( $COUNTER * $STEP_SIZE))" second(s).\n" +fi \ No newline at end of file diff --git a/tests/test_decks.py b/tests/test_decks.py new file mode 100644 index 0000000..f4a79e8 --- /dev/null +++ b/tests/test_decks.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +import unittest +from unittest import TestCase +from util import callAnkiConnectEndpoint + +class TestDeckNames(TestCase): + + def test_deckNames(self): + response = callAnkiConnectEndpoint({'action': 'deckNames'}) + self.assertEqual(['Default'], response) + +class TestGetDeckConfig(TestCase): + + def test_getDeckConfig(self): + response = callAnkiConnectEndpoint({'action': 'getDeckConfig', 'params': {'deck': 'Default'}}) + self.assertDictContainsSubset({'name': 'Default', 'replayq': True}, response) \ No newline at end of file diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 0000000..99d9448 --- /dev/null +++ b/tests/test_misc.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +import unittest +from unittest import TestCase +from util import callAnkiConnectEndpoint + +class TestVersion(TestCase): + + def test_version(self): + response = callAnkiConnectEndpoint({'action': 'version'}) + self.assertEqual(4, response) \ No newline at end of file diff --git a/tests/util.py b/tests/util.py new file mode 100644 index 0000000..bf121a0 --- /dev/null +++ b/tests/util.py @@ -0,0 +1,11 @@ +import json +import urllib +import urllib2 + +def callAnkiConnectEndpoint(data): + url = 'http://docker:8888' + dumpedData = json.dumps(data) + req = urllib2.Request(url, dumpedData) + response = urllib2.urlopen(req).read() + responseData = json.loads(response) + return responseData \ No newline at end of file