~michael.nelson/charms/precise/elasticsearch/add-provides-elasticsearch

« back to all changes in this revision

Viewing changes to tests/helpers/__init__.py

  • Committer: Michael Nelson
  • Date: 2014-02-06 12:54:59 UTC
  • Revision ID: michael.nelson@canonical.com-20140206125459-pnko7nuu75uee9pf
Initial elastic search with ansible, unicast and tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import sys
 
2
 
 
3
import amulet
 
4
import requests
 
5
 
 
6
 
 
7
def check_response(response, expected_code=200):
 
8
    if response.status_code != expected_code:
 
9
        sys.exit("Elastic search did not respond as expected. \n"
 
10
            "Expected status code: %{expected_code} \n"
 
11
            "Status code: %{status_code} \n"
 
12
            "Response text: %{response_text}".format(
 
13
                expected_code=expected_code,
 
14
                status_code=response.status_code,
 
15
                response_text=response.text))
 
16
 
 
17
 
 
18
def setup_deployment(deployment, timeout=900):
 
19
    """Setup the deployment and wait until installed."""
 
20
    try:
 
21
        deployment.setup(timeout=timeout)
 
22
        deployment.sentry.wait()
 
23
    except amulet.helpers.TimeoutError:
 
24
        amulet.raise_status(amulet.SKIP, msg="Environment wasn't setup in time")
 
25
    except:
 
26
        raise
 
27
 
 
28
 
 
29
def get_unit_address(deployment, unit_number=0):
 
30
    unit = deployment.sentry.unit['elasticsearch/%s' % unit_number]
 
31
    elastic_ip = unit.info['public-address']
 
32
    return 'http://' + elastic_ip + ":9200"
 
33
 
 
34
 
 
35
def get_cluster_health(deployment, unit_number=0, wait_for_nodes=0,
 
36
                       timeout=180):
 
37
    addr = get_unit_address(deployment, unit_number=0)
 
38
    addr = addr + "/_cluster/health?timeout={}s".format(timeout)
 
39
    if wait_for_nodes > 0:
 
40
        addr = addr + "&wait_for_nodes={}".format(wait_for_nodes)
 
41
    response = requests.get(addr)
 
42
    check_response(response, expected_code=200)
 
43
    return response.json()
 
44
 
 
45
 
 
46
def get_index_health(deployment, index_name, unit_number=0):
 
47
    addr = get_unit_address(deployment, unit_number=0)
 
48
    response = requests.get(addr + "/_cluster/health/" + index_name)
 
49
    check_response(response, expected_code=200)
 
50
    return response.json()
 
51
 
 
52
 
 
53
def assert_unit_ok(deployment, unit_number=0):
 
54
    addr = get_unit_address(d, unit_number=unit_number)
 
55
    response = requests.get(addr + "/_nodes/process")
 
56
    check_response(response, expected_code=200)
 
57
    response_dict = response.json()
 
58
    if not response_dict['ok']:
 
59
        sys.exit("Elastic search responded with ok=%s" % response_dict['ok'])