Skip to content

Commit

Permalink
Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkocikowski committed Oct 1, 2013
1 parent ea32721 commit de1a52b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 7 deletions.
15 changes: 10 additions & 5 deletions esbench/api.py
Expand Up @@ -57,10 +57,15 @@ def close(self):
self.conn = None

@retry_and_reconnect_on_IOError
def get(self, path):
def get(self, path, data=None):
method = 'GET'
curl = "curl -X%s http://%s:%i/%s" % (method, self.host, self.port, path)
self.conn.request(method, path, body=None)
if data:
curl = "curl -X%s http://%s:%i/%s -d '%s'" % (method, self.host, self.port, path, data)
head = {'Content-type': 'application/json'}
else:
curl = "curl -X%s http://%s:%i/%s" % (method, self.host, self.port, path)
head = {}
self.conn.request(method, path, data, head)
resp = self.conn.getresponse()
data = resp.read()
if resp.status not in [200, 201]:
Expand Down Expand Up @@ -110,8 +115,8 @@ def delete(self, path):


@contextlib.contextmanager
def connect(host='localhost', port=9200, timeout=DEFAULT_TIMEOUT):
conn = Conn(host=host, port=port, timeout=timeout)
def connect(host='localhost', port=9200, timeout=DEFAULT_TIMEOUT, conn_cls=httplib.HTTPConnection):
conn = Conn(host=host, port=port, timeout=timeout, conn_cls=conn_cls)
yield conn
conn.close()

Expand Down
2 changes: 1 addition & 1 deletion esbench/bench.py
Expand Up @@ -49,7 +49,7 @@ def __init__(self, name, query, observation_id, index, doctype):
self.query['stats'] = [self.stats_group_name]

self.query_path = '%s/%s/_search' % (index, doctype)
self.query_string = json.dumps(self.query)
self.query_string = json.dumps(self.query, sort_keys=True)

self.t_client = None

Expand Down
1 change: 1 addition & 0 deletions esbench/client.py
Expand Up @@ -35,6 +35,7 @@ def get_lines_iterator(path=None, count=None):


def args_parser():

parser = argparse.ArgumentParser(description="Elasticsearch benchmark runner")
parser.add_argument('--version', action='version', version=esbench.__version__)
parser.add_argument('-v', '--verbose', action='store_true')
Expand Down
28 changes: 27 additions & 1 deletion esbench/test/test_api.py
Expand Up @@ -36,6 +36,8 @@ def __init__(self, host='localhost', port=9200, timeout=10):
self.timeout = timeout
self.sock = None
self.req = None
self.requests = []
self.responses = []

def connect(self):
self.sock = True
Expand All @@ -45,10 +47,13 @@ def close(self):

def request(self, method, url, body=None, headers=None):
self.req = (method, url, body)
self.requests.append(self.req)
return

def getresponse(self):
return MockHTTPResponse(self.req)
resp = MockHTTPResponse(self.req)
self.responses.append(resp)
return resp


class ApiConnTest(unittest.TestCase):
Expand All @@ -62,11 +67,32 @@ def test_conn(self):
c.close()
self.assertIsNone(c.conn)

def test_connect(self):
with esbench.api.connect(conn_cls=MockHTTPConnection) as c:
resp = c.get("foo/bar")
self.assertEqual(resp.status, 200)
self.assertIs(c.conn.sock, True)
self.assertIsNone(c.conn)

def test_req_resp(self):
# test request and response recording
c = esbench.api.Conn(conn_cls=MockHTTPConnection)
resp = c.get("foo/bar")
self.assertEqual(resp.status, 200)
resp = c.get("foo/bar", json.dumps({'status': 404, 'reason': 'not found'}))
self.assertEqual(resp.status, 404)
self.assertEqual(resp.reason, 'not found')
resp = c.delete("foo/bar")
self.assertEqual(c.conn.requests, [('GET', 'foo/bar', None), ('GET', 'foo/bar', '{"status": 404, "reason": "not found"}'), ('DELETE', 'foo/bar', None)])
self.assertEqual([r.status for r in c.conn.responses], [200, 404, 200])

def test_conn_get(self):
c = esbench.api.Conn(conn_cls=MockHTTPConnection)
resp = c.get("test/_stats")
self.assertIsInstance(resp, esbench.api.ApiResponse)
self.assertEqual(resp.curl, "curl -XGET http://localhost:9200/test/_stats")
resp = c.get("foo/bar", 'baz')
self.assertEqual(resp.curl, "curl -XGET http://localhost:9200/foo/bar -d 'baz'")

def test_conn_put(self):
c = esbench.api.Conn(conn_cls=MockHTTPConnection)
Expand Down
32 changes: 32 additions & 0 deletions esbench/test/test_bench.py
Expand Up @@ -8,6 +8,8 @@
import json

import esbench.bench
import esbench.api
import esbench.test.test_api

class BenchTest(unittest.TestCase):

Expand All @@ -18,6 +20,36 @@ def test_rands(self):
self.assertNotEqual(esbench.bench.rands(), esbench.bench.rands())



class SearchQueryTest(unittest.TestCase):

def test_execute(self):

with self.assertRaises(ValueError):
q = esbench.bench.SearchQuery(
name='match',
query=json.dumps({'match': {'foo': 'bar'}}),
observation_id='ABCDEFGH',
index='test',
doctype='doc'
)

q = esbench.bench.SearchQuery(
name='match',
query={'match': {'foo': 'bar'}},
observation_id='ABCDEFGH',
index='test',
doctype='doc'
)
c = esbench.api.Conn(conn_cls=esbench.test.test_api.MockHTTPConnection)
resp = q.execute(c)
self.assertEqual(resp.curl, """curl -XPOST http://localhost:9200/test/doc/_search -d \'{"match": {"foo": "bar"}, "stats": ["ABCDEFGH_match"]}\'""")


class BenchmarkTest(unittest.TestCase):
pass



if __name__ == "__main__":
unittest.main()
Expand Down
24 changes: 24 additions & 0 deletions esbench/test/test_client.py
@@ -0,0 +1,24 @@
# -*- coding: UTF-8 -*-
# (c)2013 Mik Kocikowski, MIT License (http://opensource.org/licenses/MIT)
# https://github.com/mkocikowski/esbench

import datetime
import os.path
import unittest
import json

import esbench.client

class ClientTest(unittest.TestCase):

def test_args(self):
parser = esbench.client.args_parser()
self.assertRaises(SystemExit, parser.parse_args, "".split())
args = parser.parse_args("run".split())
self.assertEqual(args.__dict__, {'no_optimize_calls': False, 'verbose': False, 'segments': None, 'repetitions': 1000, 'n': 100, 'command': 'run', 'observations': 10, 'data': None, 'append': False, 'config_file_path': './config.json'})



if __name__ == "__main__":
unittest.main()

1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -21,6 +21,7 @@
long_description=ld,
packages = ['esbench', 'esbench.test'],
package_data={'': ['README.md',],},
data_files=[('./', './esbench/config.json')],
entry_points = {
'console_scripts': [
'esbench = esbench.client:main',
Expand Down

0 comments on commit de1a52b

Please sign in to comment.