Skip to content

Commit 75feb15

Browse files
authored
Merge pull request #22 from rubenvp8510/travis
Setting up Travis CI
2 parents 632c419 + 94aacfc commit 75feb15

9 files changed

Lines changed: 151 additions & 34 deletions

File tree

.travis.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sudo: required
2+
3+
language: python
4+
python:
5+
- "2.7"
6+
- "3.4"
7+
8+
services:
9+
- docker
10+
11+
before_install:
12+
- sudo apt-get install jq
13+
- ./.travis/run_hawkular.sh
14+
env:
15+
global:
16+
- PYTHONPATH=.
17+
matrix:
18+
- HAWKULAR_VERSION=latest
19+
- HAWKULAR_VERSION=0.15
20+
21+
script: pytest

.travis/run_hawkular.sh

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
3+
WAIT_STEP=3
4+
MAX_STEPS=120
5+
LOWER_VERSION="0.15"
6+
7+
if [[ -z ${HAWKULAR_VERSION+x} ]]; then
8+
HAWKULAR_VERSION='latest'
9+
fi
10+
11+
HAWKULAR_IMAGE=rubensvp/hawkular-metrics:${HAWKULAR_VERSION}
12+
13+
function metrics_status {
14+
curl -s http://localhost:8080/hawkular/metrics/status | jq -r '.MetricsService' 2> /dev/null
15+
}
16+
17+
function alerts_status {
18+
curl -s http://localhost:8080/hawkular/alerts/status | jq -r '.status' 2> /dev/null
19+
}
20+
21+
function cassandra_status {
22+
docker exec hawkular-cassandra nodetool statusbinary | tr -dc '[[:print:]]' 2> /dev/null
23+
}
24+
25+
function wait_hawkular {
26+
METRICS_STATUS=$(metrics_status)
27+
ALERTS_STATUS=$(alerts_status)
28+
TOTAL_WAIT=0
29+
echo "Starting hawkular metrics $HAWKULAR_VERSION ..."
30+
while ([ "$METRICS_STATUS" != "STARTED" ] || ( [ "$ALERTS_STATUS" != "STARTED" ] && [ "$HAWKULAR_VERSION" != "$LOWER_VERSION" ]) ) && [ ${TOTAL_WAIT} -lt ${MAX_STEPS} ]; do
31+
METRICS_STATUS=$(metrics_status)
32+
ALERTS_STATUS=$(alerts_status)
33+
sleep ${WAIT_STEP}
34+
if [[ "$HAWKULAR_VERSION" == "$LOWER_VERSION" ]]; then
35+
echo "Hawkular server status, metrics: $METRICS_STATUS"
36+
else
37+
echo "Hawkular server status, metrics: $METRICS_STATUS, alerts: $ALERTS_STATUS"
38+
fi
39+
TOTAL_WAIT=$((TOTAL_WAIT+WAIT_STEP))
40+
echo "Waited $TOTAL_WAIT seconds for Hawkular metrics to start."
41+
done
42+
}
43+
44+
function launch_hawkular {
45+
docker run --name hawkular-metrics -p 8080:8080 --link hawkular-cassandra -d ${HAWKULAR_IMAGE}
46+
}
47+
48+
function launch_cassandra {
49+
docker run --name hawkular-cassandra -e CASSANDRA_START_RPC=true -d cassandra:3.7
50+
}
51+
52+
function wait_cassandra {
53+
CASSANDRA_STATUS=$(cassandra_status)
54+
TOTAL_WAIT=0;
55+
while [ "$CASSANDRA_STATUS" != "running" ] && [ ${TOTAL_WAIT} -lt ${MAX_STEPS} ]; do
56+
CASSANDRA_STATUS=$(cassandra_status)
57+
echo "Cassandra server status: $CASSANDRA_STATUS."
58+
sleep ${WAIT_STEP}
59+
TOTAL_WAIT=$((TOTAL_WAIT+WAIT_STEP))
60+
echo "Waited $TOTAL_WAIT seconds for Cassandra to start."
61+
done
62+
}
63+
64+
launch_cassandra
65+
wait_cassandra
66+
launch_hawkular
67+
wait_hawkular

hawkular/alerts.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
17-
18-
from client import ApiOject, HawkularBaseClient
19-
17+
from hawkular.client import ApiOject, HawkularBaseClient
2018

2119
class Trigger(ApiOject):
2220
__slots__ = [

hawkular/client.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ def __init__(self, dictionary=dict()):
5656
setattr(self, k, udict.get(k,self.defaults.get(k)))
5757

5858
def to_json_object(self):
59-
return ApiOject.transform_dict_to_camelcase(self.__dict__)
59+
dictionary = {}
60+
for attribute in self.__slots__:
61+
if hasattr(self,attribute):
62+
dictionary[attribute] = getattr(self,attribute)
63+
return ApiOject.transform_dict_to_camelcase(dictionary)
6064

6165
@staticmethod
6266
def _to_camelcase(word):
@@ -71,13 +75,13 @@ def _to_underscore(word):
7175
def transform_dict_to_camelcase(dictionary):
7276
if dictionary is None:
7377
return dict()
74-
return dict((ApiOject._to_camelcase(k), v) for k, v in dictionary.iteritems() if v is not None)
78+
return dict((ApiOject._to_camelcase(k), v) for k, v in dictionary.items() if v is not None)
7579

7680
@staticmethod
7781
def transform_dict_to_underscore(dictionary):
7882
if dictionary is None:
7983
return dict()
80-
return dict((ApiOject._to_underscore(k), v) for k, v in dictionary.iteritems() if v is not None)
84+
return dict((ApiOject._to_underscore(k), v) for k, v in dictionary.items() if v is not None)
8185

8286
@classmethod
8387
def list_to_object_list(cls, o):
@@ -171,8 +175,9 @@ def _http(self, url, method, data=None, decoder=None):
171175
if self.token is not None:
172176
req.add_header('Authorization', 'Bearer {0}'.format(self.token))
173177
elif self.username is not None:
178+
b64 = base64.b64encode(bytes(self.username + ':' + self.password, encoding='utf-8'))
174179
req.add_header('Authorization',
175-
'Basic {0}'.format(base64.b64encode(self.username + b':' + self.password)))
180+
'Basic {0}'.format(b64))
176181

177182
if not isinstance(data, str):
178183
data = json.dumps(data, indent=2)

hawkular/metrics.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def __init__(self,
9898
token=None,
9999
username=None,
100100
password=None,
101-
auto_set_legacy_api=True):
101+
auto_set_legacy_api=True,
102+
authtoken=None):
102103
"""
103104
A new instance of HawkularMetricsClient is created with the following defaults:
104105
@@ -123,6 +124,7 @@ def __init__(self,
123124
self.username = username
124125
self.password = password
125126
self.legacy_api = False
127+
self.authtoken = authtoken
126128

127129
opener = build_opener(HawkularHTTPErrorProcessor())
128130
install_opener(opener)
@@ -185,6 +187,9 @@ def _http(self, url, method, data=None):
185187
elif self.username is not None:
186188
req.add_header('Authorization', 'Basic {0}'.format(base64.b64encode(self.username + b':' + self.password)))
187189

190+
if self.authtoken is not None:
191+
req.add_header('Hawkular-Admin-Token', self.authtoken)
192+
188193
if not isinstance(data, str):
189194
data = json.dumps(data, indent=2)
190195

tests/__init__.py

Whitespace-only changes.

tests/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import os
2+
3+
version = os.environ.get('HAWKULAR_VERSION','latest')
4+
5+
if version != 'latest':
6+
major_version, minor_version = version.split('.')
7+
major_version = int(major_version)
8+
minor_version = int(minor_version)
9+
else:
10+
major_version = minor_version = 0
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@
1818

1919
import unittest
2020
import uuid
21-
from alerts import *
21+
from hawkular.alerts import *
22+
import os
23+
from tests import base
2224

2325

2426
class TestAlertsFunctionsBase(unittest.TestCase):
2527
def setUp(self):
2628
self.maxDiff = None
2729
self.test_tenant = str(uuid.uuid4())
2830
self.client = HawkularAlertsClient(tenant_id=self.test_tenant,
29-
port=8080,
30-
username='jdoe',
31-
password='password')
31+
port=8080)
3232

3333

34-
class MetricsTestCase(TestAlertsFunctionsBase):
34+
@unittest.skipIf(base.version != 'latest' and base.major_version == 0 and base.minor_version <= 15,
35+
'Not supported in ' + base.version + ' version')
36+
class AlertsTestCase(TestAlertsFunctionsBase):
3537
def test_trigger_creation(self):
3638
trigger = Trigger()
3739
trigger.id = 'id_1'
Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,42 @@
1818

1919
import unittest
2020
import uuid
21-
from metrics import *
21+
from hawkular.metrics import *
22+
import os
23+
24+
from tests import base
2225

23-
class TestMetricFunctionsBase(unittest.TestCase):
2426

27+
class TestMetricFunctionsBase(unittest.TestCase):
2528
def setUp(self):
2629
self.maxDiff = None
2730
self.test_tenant = str(uuid.uuid4())
28-
self.client = HawkularMetricsClient(tenant_id=self.test_tenant, port=8080)
29-
31+
self.client = HawkularMetricsClient(tenant_id=self.test_tenant, port=8080, authtoken='secret')
32+
33+
3034
class TenantTestCase(TestMetricFunctionsBase):
3135
"""
3236
Test creating and fetching tenants. Each creation test should also
3337
fetch the tenants to test that functionality also
3438
"""
35-
39+
3640
def test_tenant_creation(self):
3741
tenant = str(uuid.uuid4())
3842
self.client.create_tenant(tenant)
3943
tenants = self.client.query_tenants()
4044

41-
expect = { 'id': tenant }
45+
expect = {'id': tenant}
4246
self.assertIn(expect, tenants)
4347

4448
def test_tenant_creation_with_retentions(self):
4549
tenant = str(uuid.uuid4())
46-
retentions = { 'gauge': 8, 'availability': 30 }
50+
retentions = {'gauge': 8, 'availability': 30}
4751
self.client.create_tenant(tenant, retentions)
4852

49-
expect = { 'id': tenant, 'retentions': { 'gauge': 8, 'availability': 30 } }
53+
expect = {'id': tenant, 'retentions': {'gauge': 8, 'availability': 30}}
5054
self.assertIn(expect, self.client.query_tenants())
51-
55+
56+
5257
class MetricsTestCase(TestMetricFunctionsBase):
5358
"""
5459
Test metric functionality, both adding definition and querying for definition,
@@ -62,7 +67,7 @@ def find_metric(self, name, definitions):
6267
for defin in definitions:
6368
if defin['id'] == name:
6469
return defin
65-
70+
6671
def test_gauge_creation(self):
6772
"""
6873
Test creating gauge metric definitions with different tags and definition.
@@ -72,7 +77,8 @@ def test_gauge_creation(self):
7277

7378
md1 = self.client.create_metric_definition(MetricType.Gauge, id_name.format('1'))
7479
md2 = self.client.create_metric_definition(MetricType.Gauge, id_name.format('2'), dataRetention=90)
75-
md3 = self.client.create_metric_definition(MetricType.Gauge, id_name.format('3'), dataRetention=90, units='bytes', env='qa')
80+
md3 = self.client.create_metric_definition(MetricType.Gauge, id_name.format('3'), dataRetention=90,
81+
units='bytes', env='qa')
7682
self.assertTrue(md1)
7783
self.assertTrue(md2)
7884
self.assertTrue(md3)
@@ -88,8 +94,8 @@ def test_gauge_creation(self):
8894

8995
# This is what the returned dict should look like
9096
expect = [
91-
{ 'dataRetention': 7, 'type': 'gauge', 'id': 'test.create.gauge.1',
92-
'tenantId': self.test_tenant },
97+
{'dataRetention': 7, 'type': 'gauge', 'id': 'test.create.gauge.1',
98+
'tenantId': self.test_tenant},
9399
{'dataRetention': 90, 'type': 'gauge', 'id': 'test.create.gauge.2', 'tenantId': self.test_tenant},
94100
{'tags': {'units': 'bytes', 'env': 'qa'},
95101
'id': 'test.create.gauge.3', 'dataRetention': 90, 'type': 'gauge', 'tenantId': self.test_tenant}]
@@ -109,7 +115,7 @@ def test_availability_creation(self):
109115
self.client.create_metric_definition(MetricType.Availability, id_name.format('2'), dataRetention=90)
110116
self.client.create_metric_definition(MetricType.Availability, id_name.format('3'), dataRetention=94, env='qa')
111117
# Fetch metrics and check that it did appear
112-
m = self.client.query_metric_definitions(MetricType.Availability)
118+
m = self.client.query_metric_definitions(MetricType.Availability)
113119
self.assertEqual(3, len(m))
114120

115121
avail_3 = self.find_metric(id_name.format('3'), m)
@@ -135,7 +141,7 @@ def test_tags_modifications(self):
135141
self.assertEqual(0, len(tags_2))
136142

137143
def test_tags_queries(self):
138-
for i in range(1,9):
144+
for i in range(1, 9):
139145
m_id = 'test.query.tags.{}'.format(i)
140146
hostname = 'host{}'.format(i)
141147
self.client.create_metric_definition(MetricType.Counter, m_id, hostname=hostname, env='qa')
@@ -170,22 +176,24 @@ def test_add_availability_single(self):
170176

171177
up = self.client.query_metric(MetricType.Availability, 'test.avail.1')
172178
self.assertEqual(up[0]['value'], 'up')
173-
179+
174180
down = self.client.query_metric(MetricType.Availability, 'test.avail.2')
175181
self.assertEqual(down[0]['value'], Availability.Down)
176182

183+
@unittest.skipIf(base.version != 'latest' and base.major_version == 0 and base.minor_version <= 15,
184+
'Not supported in ' + base.version + ' version')
177185
def test_add_string_single(self):
178186
self.client.push(MetricType.String, 'test.string.1', "foo")
179187
data = self.client.query_metric(MetricType.String, 'test.string.1')
180188
self.assertEqual(data[0]["value"], 'foo')
181-
189+
182190
def test_add_gauge_multi_datapoint(self):
183191
metric_1v = create_datapoint(float(1.45))
184192
metric_2v = create_datapoint(float(2.00), (time_millis() - 2000))
185193

186194
metric = create_metric(MetricType.Gauge, 'test.gauge.multi', [metric_1v, metric_2v])
187195
self.client.put(metric)
188-
196+
189197
data = self.client.query_metric(MetricType.Gauge, 'test.gauge.multi')
190198
self.assertEqual(len(data), 2)
191199
self.assertEqual(data[0]['value'], float(1.45))
@@ -197,7 +205,7 @@ def test_add_availability_multi_datapoint(self):
197205
down = create_datapoint('down', t)
198206

199207
m = create_metric(MetricType.Availability, 'test.avail.multi', [up, down])
200-
208+
201209
self.client.put(m)
202210
data = self.client.query_metric(MetricType.Availability, 'test.avail.multi')
203211

@@ -212,7 +220,7 @@ def test_add_mixed_metrics_and_datapoints(self):
212220
metric_multi = create_metric(MetricType.Gauge, 'test.multi.gauge.1', [metric1, metric1_2])
213221

214222
metric2 = create_datapoint(Availability.Up)
215-
metric2_multi = create_metric(MetricType.Availability,'test.multi.gauge.2', [metric2])
223+
metric2_multi = create_metric(MetricType.Availability, 'test.multi.gauge.2', [metric2])
216224

217225
self.client.put([metric_multi, metric2_multi])
218226

@@ -237,7 +245,7 @@ def test_query_options(self):
237245
self.assertEqual(2, len(d))
238246

239247
# Query for data which has start time limitation
240-
d = self.client.query_metric(MetricType.Gauge, 'test.query.gauge.1', start=(t-1000))
248+
d = self.client.query_metric(MetricType.Gauge, 'test.query.gauge.1', start=(t - 1000))
241249
self.assertEqual(1, len(d))
242250

243251
def test_tenant_changing(self):
@@ -278,5 +286,6 @@ def test_get_metrics_stats_url(self):
278286
url = self.client._get_metrics_stats_url('some.key')
279287
self.assertEqual('some.key/stats', url)
280288

289+
281290
if __name__ == '__main__':
282291
unittest.main()

0 commit comments

Comments
 (0)