Skip to content

Commit

Permalink
[WIP] Add simple integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
lispyclouds committed Aug 16, 2018
1 parent 4410a26 commit 84ede2c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ language: clojure
jdk:
- oraclejdk9
- oraclejdk10

after_success:
- python3 integration-tests/run.py
15 changes: 15 additions & 0 deletions integration-tests/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"host": "localhost",
"port": 7777,
"protocol": "http",
"tests": [
{
"name": "Status Check",
"path": "/api/can-we-build-it",
"method": "GET",
"response": {
"message": "Yes we can! 🔨 🔨"
}
}
]
}
89 changes: 89 additions & 0 deletions integration-tests/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/python3

# This file is part of Bob.
#
# Bob is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bob. If not, see <http://www.gnu.org/licenses/>.

import json
import subprocess
from urllib import request
from urllib.error import URLError
from urllib.parse import urljoin

import time

with open("integration-tests/config.json") as json_data:
CONFIG = json.load(json_data)

BASE_URL = "{}://{}:{}".format(
CONFIG["protocol"], CONFIG["host"], CONFIG["port"]
)


def start_bob():
# TODO: Store PID and kill it post test.

subprocess.Popen(["lein", "run"])
print("Started bob.")


def wait_for_it():
# TODO: HORRIBLE way to wait. Please improve.

while True:
try:
req = request.Request(BASE_URL)

with request.urlopen(req) as _:
pass
except (ConnectionRefusedError, URLError):
print("Waiting for bob at {}".format(BASE_URL))
time.sleep(1)

continue

break


def run_tests():
for test in CONFIG["tests"]:
print("Testing {}.".format(test["name"]))

url = urljoin(BASE_URL, test["path"])

if test["method"] == "GET":
req = request.Request(url)

with request.urlopen(req) as res:
expected = json.loads(res.read().decode("utf-8"))

assert expected == test["response"]
else:
raise Exception(
"Unknown method {} at test {}.".format(
test["method"], test["name"]
)
)

print("{} passed.".format(test["name"]))


if __name__ == "__main__":
start_bob()

wait_for_it()

run_tests()

print("All checks passed!")

1 comment on commit 84ede2c

@lispyclouds
Copy link
Member Author

Choose a reason for hiding this comment

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

Closes #3

Please sign in to comment.