Skip to content

Commit d83a680

Browse files
committed
Setup travis CI.
Fix some metrics test in order to pass Travis. Fix alerts for work with python 3.4.x Support two versions of hawkular metrics for testing
1 parent 632c419 commit d83a680

7 files changed

Lines changed: 92 additions & 13 deletions

File tree

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
PYTHONPATH=.
16+
17+
script: pytest

.travis/run_hawkular.sh

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818

1919
import unittest
2020
import uuid
21-
from alerts import *
21+
from hawkular.alerts import *
2222

2323

2424
class TestAlertsFunctionsBase(unittest.TestCase):
2525
def setUp(self):
2626
self.maxDiff = None
2727
self.test_tenant = str(uuid.uuid4())
2828
self.client = HawkularAlertsClient(tenant_id=self.test_tenant,
29-
port=8080,
30-
username='jdoe',
31-
password='password')
29+
port=8080)
3230

3331

3432
class MetricsTestCase(TestAlertsFunctionsBase):
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import unittest
2020
import uuid
21-
from metrics import *
21+
from hawkular.metrics import *
2222

2323
class TestMetricFunctionsBase(unittest.TestCase):
2424

0 commit comments

Comments
 (0)