Skip to content

Commit

Permalink
Replace server_test.go with an integration test based on pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
gvalkov committed Jul 17, 2018
1 parent 1495f2c commit a7ef07c
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
node_modules/
/tailon
/gin-bin
.pytest_cache

### Editors ###
*~
Expand All @@ -26,4 +27,3 @@ node_modules/

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
BUILD ?= prod

test:
go test -v
cd tests && pytest -v

frontend:
cd frontend && $(MAKE) clean ; $(MAKE) BUILD=$(BUILD)

Expand All @@ -14,4 +18,4 @@ README.md:
sed -i 's/[ \t]*$$//' $@


.PHONY: frontend frontend-watch docker-build README.md
.PHONY: test frontend frontend-watch docker-build README.md
153 changes: 0 additions & 153 deletions server_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.py[cod]
*$py.class
51 changes: 51 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import pytest
import psutil
import aiohttp

import time
import subprocess


@pytest.fixture(scope='session')
def unix_socket(tmpdir_factory):
return str(tmpdir_factory.mktemp('tailon') / 'httpd.sock')


@pytest.fixture(scope='session')
def server(request, unix_socket):
def inner(*args):
cmd = ['./tailon', '-b', unix_socket, *args]

proc = subprocess.Popen(cmd, cwd='../')

# Convenience methods.
proc.sock = unix_socket
proc.get_children = lambda: get_child_procs(proc.pid)

# TODO: Read stdout to determine when server is ready.
time.sleep(0.20)

request.addfinalizer(proc.terminate)
return proc

return inner


@pytest.fixture()
async def client(request, unix_socket, event_loop):
conn = aiohttp.UnixConnector(path=unix_socket)
session = aiohttp.ClientSession(connector=conn)

def close():
async def aclose():
await session.close()
event_loop.run_until_complete(aclose())
request.addfinalizer(close)

return session


def get_child_procs(pid):
procs = psutil.process_iter(attrs=['pid', 'ppid'])
procs = [i for i in procs if i.ppid() == pid]
return procs
3 changes: 3 additions & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest ~= 3.6.0
psutil ~= 5.4.0
pytest-asyncio ~= 0.6.0
62 changes: 62 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import json
import asyncio
from urllib.parse import urljoin

import pytest
import aiohttp


@pytest.mark.asyncio
@pytest.mark.parametrize("root", ['/', 'tailon/', 'tailon/tailon/'])
async def test_relativeroot(server, client, root):
proc = server('--relative-root', root, 'testdata/ex1/var/log/1.log')
for path in '', 'ws', 'vfs/dist/main.js':
url = urljoin('http://localhost', root + path)
res = await client.get(url)
assert res.status == 200

url = urljoin('http://localhost', root + 'vfs/dist/non-existant')
res = await client.get(url)
out = await res.text()
assert res.status == 404, out


@pytest.mark.asyncio
async def test_sockjs_list(server, client):
proc = server('testdata/ex1/var/log/1.log')
async with client.ws_connect('http://localhost/ws/0/0/websocket') as ws:
await ws.send_json(['list'])
res = await get_sockjs_response(ws)

assert '__default__' in res
assert res['__default__'][0]['path'] == 'testdata/ex1/var/log/1.log'


@pytest.mark.asyncio
async def test_sockjs_frontendmessage(server, client):
proc = server('testdata/ex1/var/log/1.log', 'testdata/ex1/var/log/2.log')

msg_tail = '''{"command":"tail","script":null,"entry":{"path":"testdata/ex1/var/log/1.log","alias":"/tmp/t1","size":14342,"mtime":"2018-07-14T15:07:33.524768369+02:00","exists":true},"nlines":10}'''

msg_grep = '''{"command":"grep","script":".*","entry":{"path":"testdata/ex1/var/log/1.log","alias":"/tmp/t1","size":14342,"mtime":"2018-07-14T15:07:33.524768369+02:00","exists":true},"nlines":10}'''

async with client.ws_connect('http://localhost/ws/0/0/websocket') as ws:
await ws.send_json([msg_tail])
await asyncio.sleep(0.1)

assert len(proc.get_children()) == 1

await ws.send_json([msg_grep])
await asyncio.sleep(0.1)
assert len(proc.get_children()) == 2

await ws.send_json([msg_tail])
await asyncio.sleep(0.1)
assert len(proc.get_children()) == 1


async def get_sockjs_response(ws):
assert (await ws.receive()).data == 'o'
res = (await ws.receive()).data[1:]
res = json.loads(res)
return json.loads(res[0])

0 comments on commit a7ef07c

Please sign in to comment.