Merge lp:~lazypower/charms/trusty/elasticsearch/play-it-again-sam into lp:charms/trusty/elasticsearch

Proposed by Charles Butler
Status: Merged
Approved by: Charles Butler
Approved revision: 44
Merged at revision: 43
Proposed branch: lp:~lazypower/charms/trusty/elasticsearch/play-it-again-sam
Merge into: lp:charms/trusty/elasticsearch
Diff against target: 302 lines (+119/-140)
7 files modified
hooks/hooks.py (+5/-4)
tests/00-create-index (+0/-32)
tests/00-single-to-scale-test.py (+112/-0)
tests/01-config-changes (+0/-22)
tests/02-deploy-three-units (+0/-18)
tests/helpers/__init__.py (+0/-64)
tests/tests.yaml (+2/-0)
To merge this branch: bzr merge lp:~lazypower/charms/trusty/elasticsearch/play-it-again-sam
Reviewer Review Type Date Requested Status
Chris Glass (community) Approve
Review via email: mp+293799@code.launchpad.net

Description of the change

Lint fixes
Refactored the amulet test suite according to current best practices

To post a comment you must log in.
Revision history for this message
Chris Glass (tribaal) wrote :

Looks good, thanks for resubmitting! +1

review: Approve
44. By Charles Butler

Fixed flake8 e901 - Invalid Syntax on os.chmod() call.

Revision history for this message
Charles Butler (lazypower) wrote :

this made it in as of cs:trusty/elasticsearch-15

@tribaal - do we need to get this merged in that lp branch?

Revision history for this message
Michael Nelson (michael.nelson) wrote :

Done :)

Also, let me know if you think it makes sense to move this back to a branch you control. We took ownership at the time because we were the only ones using it in prod and needed to land without blocking on the long reviews. I'm happy to leave it as is, but given that it no longer requires lots of updates from us (it's working well now), it's fine if you want to move it back.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'hooks/hooks.py'
--- hooks/hooks.py 2015-10-27 22:30:30 +0000
+++ hooks/hooks.py 2016-05-04 19:49:57 +0000
@@ -86,11 +86,12 @@
86 raise RuntimeError('Persistent storage contains old data. '86 raise RuntimeError('Persistent storage contains old data. '
87 'Please investigate and migrate data manually '87 'Please investigate and migrate data manually '
88 'to: {}'.format(new_path))88 'to: {}'.format(new_path))
89 os.chmod(new_path, 0700)89 os.chmod(new_path, 0o700)
90 charmhelpers.core.host.service_stop('elasticsearch')90 charmhelpers.core.host.service_stop('elasticsearch')
91 charmhelpers.core.host.rsync(os.path.join(old_path, ''), # Ensure we have trailing slashes91 # Ensure we have trailing slashes
92 os.path.join(new_path, ''),92 charmhelpers.core.host.rsync(os.path.join(old_path, ''),
93 options=['--archive'])93 os.path.join(new_path, ''),
94 options=['--archive'])
94 shutil.rmtree(old_path)95 shutil.rmtree(old_path)
95 os.symlink(new_path, old_path)96 os.symlink(new_path, old_path)
96 charmhelpers.core.host.service_start('elasticsearch')97 charmhelpers.core.host.service_start('elasticsearch')
9798
=== removed file 'tests/00-create-index'
--- tests/00-create-index 2014-10-30 01:02:33 +0000
+++ tests/00-create-index 1970-01-01 00:00:00 +0000
@@ -1,32 +0,0 @@
1#!/usr/bin/python3
2import amulet
3import helpers
4
5
6d = amulet.Deployment(series='trusty')
7
8d.add('elasticsearch')
9
10helpers.setup_deployment(d)
11
12health = helpers.get_cluster_health(d)
13
14if health['status'] not in ('green', 'yellow'):
15 msg = "Elastic Search status is %s" % health['status']
16 amulet.raise_status(amulet.FAIL, msg=msg)
17
18# Create a test index.
19curl_command = """
20curl -XPUT 'http://localhost:9200/test/tweet/1' -d '{
21 "user" : "me",
22 "message" : "testing"
23}'
24"""
25response = helpers.curl_on_unit(curl_command, d)
26
27health = helpers.get_index_health(d, 'test')
28if health['status'] not in ('green', 'yellow'):
29 msg = "Elastic Search test index status is %s." % health['status']
30 amulet.raise_status(amulet.FAIL, msg=msg)
31
32print("Successfully created test index.")
330
=== added file 'tests/00-single-to-scale-test.py'
--- tests/00-single-to-scale-test.py 1970-01-01 00:00:00 +0000
+++ tests/00-single-to-scale-test.py 2016-05-04 19:49:57 +0000
@@ -0,0 +1,112 @@
1#!/usr/bin/python3
2
3import amulet
4import unittest
5import requests
6import json
7
8class TestElasticsearch(unittest.TestCase):
9
10 @classmethod
11 def setUpClass(self):
12 self.deployment = amulet.Deployment(series='trusty')
13 self.deployment.add('elasticsearch')
14 self.deployment.configure('elasticsearch',
15 {'cluster-name': 'unique-name'})
16
17 try:
18 self.deployment.setup(timeout=1200)
19 self.deployment.sentry.wait()
20 except amulet.helpers.TimeoutError:
21 amulet.raise_status(
22 amulet.SKIP, msg="Environment wasn't setup in time")
23
24
25 def test_health(self):
26 ''' Test the health of the node upon first deployment
27 by getting the cluster health, then inserting data and
28 validating cluster health'''
29 health = self.get_cluster_health()
30 assert health['status'] in ('green', 'yellow')
31
32 # Create a test index.
33 curl_command = """
34 curl -XPUT 'http://localhost:9200/test/tweet/1' -d '{
35 "user" : "me",
36 "message" : "testing"
37 }'
38 """
39 response = self.curl_on_unit(curl_command)
40 health = self.get_index_health('test')
41 assert health['status'] in ('green', 'yellow')
42
43 def test_config(self):
44 ''' Validate our configuration of the cluster name made it to the
45 application configuration'''
46 health = self.get_cluster_health()
47 cluster_name = health['cluster_name']
48 assert cluster_name == 'unique-name'
49
50 def test_scale(self):
51 ''' Validate scaling the elasticsearch cluster yields a healthy
52 response from the API, and all units are participating '''
53 self.deployment.add_unit('elasticsearch', units=2)
54 self.deployment.setup(timeout=1200)
55 self.deployment.sentry.wait()
56 health = self.get_cluster_health(wait_for_nodes=3)
57 index_health = self.get_index_health('test')
58 print(health['number_of_nodes'])
59 assert health['number_of_nodes'] == 3
60 assert index_health['status'] in ('green', 'yellow')
61
62 def curl_on_unit(self, curl_command, unit_number=0):
63 unit = "elasticsearch"
64 response = self.deployment.sentry[unit][unit_number].run(curl_command)
65 if response[1] != 0:
66 msg = (
67 "Elastic search didn't respond to the command \n"
68 "'{curl_command}' as expected.\n"
69 "Return code: {return_code}\n"
70 "Result: {result}".format(
71 curl_command=curl_command,
72 return_code=response[1],
73 result=response[0])
74 )
75 amulet.raise_status(amulet.FAIL, msg=msg)
76
77 return json.loads(response[0])
78
79 def get_cluster_health(self, unit_number=0, wait_for_nodes=0,
80 timeout=180):
81 curl_command = "curl http://localhost:9200"
82 curl_command = curl_command + "/_cluster/health?timeout={}s".format(
83 timeout)
84 if wait_for_nodes > 0:
85 curl_command = curl_command + "&wait_for_nodes={}".format(
86 wait_for_nodes)
87
88 return self.curl_on_unit(curl_command, unit_number=unit_number)
89
90 def get_index_health(self, index_name, unit_number=0):
91 curl_command = "curl http://localhost:9200"
92 curl_command = curl_command + "/_cluster/health/" + index_name
93
94 return self.curl_on_unit(curl_command)
95
96
97def check_response(response, expected_code=200):
98 if response.status_code != expected_code:
99 msg = (
100 "Elastic search did not respond as expected. \n"
101 "Expected status code: %{expected_code} \n"
102 "Status code: %{status_code} \n"
103 "Response text: %{response_text}".format(
104 expected_code=expected_code,
105 status_code=response.status_code,
106 response_text=response.text))
107
108 amulet.raise_status(amulet.FAIL, msg=msg)
109
110
111if __name__ == "__main__":
112 unittest.main()
0113
=== removed file 'tests/01-config-changes'
--- tests/01-config-changes 2014-10-30 01:02:33 +0000
+++ tests/01-config-changes 1970-01-01 00:00:00 +0000
@@ -1,22 +0,0 @@
1#!/usr/bin/python3
2import amulet
3import helpers
4
5d = amulet.Deployment(series='trusty')
6
7d.add('elasticsearch')
8config = {'cluster-name': 'unique-name'}
9d.configure('elasticsearch', config)
10
11helpers.setup_deployment(d)
12
13health = helpers.get_cluster_health(d)
14
15cluster_name = health['cluster_name']
16
17if cluster_name != 'unique-name':
18 msg = ("Expected cluster name to be 'unique-name' "
19 "but was '{}'.".format(cluster_name))
20 amulet.raise_status(amulet.FAIL, msg=msg)
21
22print("Successfully created cluster with non-default cluster-name.")
230
=== removed file 'tests/02-deploy-three-units'
--- tests/02-deploy-three-units 2014-10-30 01:02:33 +0000
+++ tests/02-deploy-three-units 1970-01-01 00:00:00 +0000
@@ -1,18 +0,0 @@
1#!/usr/bin/python3
2import amulet
3import helpers
4
5d = amulet.Deployment(series='trusty')
6
7d.add('elasticsearch', units=3)
8
9helpers.setup_deployment(d)
10
11health = helpers.get_cluster_health(d, wait_for_nodes=3)
12
13if health['number_of_nodes'] != 3:
14 amulet.raise_status(amulet.FAIL, msg=msg)
15 msg = ("Expected at least 3 nodes in cluster with 3 units deployed, "
16 "got {}".format(health['number_of_nodes']))
17
18print("Successfully deployed cluster of 3 units.")
190
=== removed file 'tests/__init__.py'
=== removed directory 'tests/helpers'
=== removed file 'tests/helpers/__init__.py'
--- tests/helpers/__init__.py 2014-10-30 01:02:33 +0000
+++ tests/helpers/__init__.py 1970-01-01 00:00:00 +0000
@@ -1,64 +0,0 @@
1import amulet
2import json
3
4
5def check_response(response, expected_code=200):
6 if response.status_code != expected_code:
7 msg = (
8 "Elastic search did not respond as expected. \n"
9 "Expected status code: %{expected_code} \n"
10 "Status code: %{status_code} \n"
11 "Response text: %{response_text}".format(
12 expected_code=expected_code,
13 status_code=response.status_code,
14 response_text=response.text))
15
16 amulet.raise_status(amulet.FAIL, msg=msg)
17
18
19def curl_on_unit(curl_command, deployment, unit_number=0):
20 unit = "elasticsearch/{}".format(unit_number)
21
22 response = deployment.sentry.unit[unit].run(curl_command)
23 if response[1] != 0:
24 msg = (
25 "Elastic search didn't respond to the command \n"
26 "'{curl_command}' as expected.\n"
27 "Return code: {return_code}\n"
28 "Result: {result}".format(
29 curl_command=curl_command,
30 return_code=response[1],
31 result=response[0])
32 )
33 amulet.raise_status(amulet.FAIL, msg=msg)
34
35 return json.loads(response[0])
36
37
38def setup_deployment(deployment, timeout=900):
39 """Setup the deployment and wait until installed."""
40 try:
41 deployment.setup(timeout=timeout)
42 deployment.sentry.wait()
43 except amulet.helpers.TimeoutError:
44 amulet.raise_status(
45 amulet.SKIP, msg="Environment wasn't setup in time")
46
47
48def get_cluster_health(deployment, unit_number=0, wait_for_nodes=0,
49 timeout=180):
50 curl_command = "curl http://localhost:9200"
51 curl_command = curl_command + "/_cluster/health?timeout={}s".format(
52 timeout)
53 if wait_for_nodes > 0:
54 curl_command = curl_command + "&wait_for_nodes={}".format(
55 wait_for_nodes)
56
57 return curl_on_unit(curl_command, deployment, unit_number=unit_number)
58
59
60def get_index_health(deployment, index_name, unit_number=0):
61 curl_command = "curl http://localhost:9200"
62 curl_command = curl_command + "/_cluster/health/" + index_name
63
64 return curl_on_unit(curl_command, deployment)
650
=== added file 'tests/tests.yaml'
--- tests/tests.yaml 1970-01-01 00:00:00 +0000
+++ tests/tests.yaml 2016-05-04 19:49:57 +0000
@@ -0,0 +1,2 @@
1packages:
2 - python3-amulet

Subscribers

People subscribed via source and target branches