-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace server_test.go with an integration test based on pytest
- Loading branch information
Showing
7 changed files
with
125 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |