summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-04-02 14:32:32 +0300
committerLars Wirzenius <liw@liw.fi>2017-04-02 14:32:32 +0300
commit5294479b5596cc649ab847ae0a5f490b9c3ff7b8 (patch)
tree9bc4481de2951257567dd29acacc7262251e322c
parentdc01099e0346c831f27009b7cd9d2c34b75593fe (diff)
downloaddistixapi-5294479b5596cc649ab847ae0a5f490b9c3ff7b8.tar.gz
Add smoke test yarns and ./check to run them
-rwxr-xr-xcheck10
-rw-r--r--yarns/100-hello.yarn16
-rw-r--r--yarns/900.yarn36
-rw-r--r--yarns/lib.py29
4 files changed, 91 insertions, 0 deletions
diff --git a/check b/check
new file mode 100755
index 0000000..f4c6167
--- /dev/null
+++ b/check
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+set -eu
+
+yarn yarns/*.yarn \
+ --shell python2 \
+ --shell-arg '' \
+ --shell-library yarns/lib.py \
+ --cd-datadir \
+ "$@"
diff --git a/yarns/100-hello.yarn b/yarns/100-hello.yarn
new file mode 100644
index 0000000..9d06262
--- /dev/null
+++ b/yarns/100-hello.yarn
@@ -0,0 +1,16 @@
+# Backend smoke test
+
+This scenario is just for making sure we can, in our tests, start and
+stop the backend, and make requests to it.
+
+ SCENARIO backend smoke test
+ GIVEN a running backend instance
+
+ WHEN client makes request GET /version
+ THEN HTTP status code is 200
+ AND result matches { "version": "1.0" }
+
+ WHEN client makes request GET /blatherskite
+ THEN HTTP status code is 404
+
+ FINALLY stop backend instance
diff --git a/yarns/900.yarn b/yarns/900.yarn
new file mode 100644
index 0000000..c7e81a0
--- /dev/null
+++ b/yarns/900.yarn
@@ -0,0 +1,36 @@
+# Scenario step implementations
+
+ IMPLEMENTS GIVEN a running backend instance
+ backend = os.path.join(srcdir, 'distix-backend')
+ cliapp.runcmd(['/usr/sbin/daemonize', '-c.', backend, 'pid', 'port'])
+ vars['pid'] = cat('pid').strip()
+ vars['port'] = cat('port').strip()
+
+ IMPLEMENTS FINALLY stop backend instance
+ import signal
+ print 'killing process', repr(vars['pid'])
+ os.kill(int(vars['pid']), signal.SIGTERM)
+
+ IMPLEMENTS WHEN client makes request GET (\S+)
+ path = os.environ['MATCH_1']
+ url = 'http://localhost:{}{}'.format(vars['port'], path)
+ print 'url:', repr(url)
+ import requests
+ r = requests.get(url)
+ vars['http-status'] = r.status_code
+ vars['http-body'] = r.text
+
+ IMPLEMENTS THEN HTTP status code is (\d+)
+ wanted = int(os.environ['MATCH_1'])
+ print 'wanted:', repr(wanted)
+ print 'actual:', repr(vars['http-status'])
+ assert vars['http-status'] == wanted
+
+ IMPLEMENTS THEN result matches (.*)
+ import json
+ body = json.loads(vars['http-body'])
+ pattern = json.loads(os.environ['MATCH_1'])
+ assert type(body) == type(pattern)
+ for key in pattern.keys():
+ assert key in body
+ assert body[key] == pattern[key]
diff --git a/yarns/lib.py b/yarns/lib.py
new file mode 100644
index 0000000..b7df3a7
--- /dev/null
+++ b/yarns/lib.py
@@ -0,0 +1,29 @@
+import errno
+import os
+import time
+
+import cliapp
+
+import yarnutils
+
+datadir = os.environ['DATADIR']
+srcdir = os.environ['SRCDIR']
+
+vars = yarnutils.Variables(datadir)
+
+
+MAX_CAT_TIME = 5 # seconds
+def cat(filename):
+ start = time.time()
+ while time.time() - start < MAX_CAT_TIME:
+ try:
+ with open(filename) as f:
+ data = f.read()
+ if len(data) == 0:
+ continue
+ return data
+ except (IOError, OSError) as e:
+ if e.errno == errno.ENOENT:
+ continue
+ raise
+ raise Exception("cat took more then %s seconds" % MAX_CAT_TIME)