Skip to content

Commit

Permalink
Merge d51a9f0 into 9e354c0
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhuang committed Jun 5, 2015
2 parents 9e354c0 + d51a9f0 commit a1f62c1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
34 changes: 34 additions & 0 deletions tests/test_zeus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ def test_initialization_no_http(self):
z = client.ZeusClient(FAKE_TOKEN, "zeus.rocks")
assert z.server == "http://zeus.rocks"

def test_validate_dates(self):
# normal
from_date = 12345
to_date = 12346
self.z._validateDates(from_date, to_date)
# None value
from_date = None
to_date = 12346
self.z._validateDates(from_date, to_date)
# invalid value
from_date = 'wrongvalue'
to_date = 12346
self.assertRaises(
client.ZeusException, self.z._validateDates, from_date, to_date)
# inversed order
from_date = 12346
to_date = 12345
self.assertRaises(
client.ZeusException, self.z._validateDates, from_date, to_date)

@mock.patch('zeus.client.requests')
def test_post_empty_log(self, mock_requests):
logs = []
Expand Down Expand Up @@ -69,6 +89,13 @@ def test_post_single_log_wrong_name(self, mock_requests):
client.ZeusException, self.z.sendLog, 'W#?rongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, 'W-rongName', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, None, logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, '', logs)
self.assertRaises(
client.ZeusException, self.z.sendLog, '0123456789ABCDEF' * 16,
logs)

@mock.patch('zeus.client.requests')
def test_post_multiple_logs(self, mock_requests):
Expand Down Expand Up @@ -125,6 +152,13 @@ def test_post_single_metric_wrong_name(self, mock_requests):
client.ZeusException, self.z.sendMetric, '_WrongName', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, 'W#?rongName', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, None, metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, '', metrics)
self.assertRaises(
client.ZeusException, self.z.sendMetric, '0123456789ABCDEF' * 16,
metrics)

@mock.patch('zeus.client.requests')
def test_post_multiple_metrics(self, mock_requests):
Expand Down
49 changes: 36 additions & 13 deletions zeus/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,39 @@ def _sendRequest(self, method, path, data):
return r.status_code, r.json()

def _validateLogName(self, name):
return True if re.match(r"^[a-zA-Z0-9]*$", name) else False
if name is None:
raise ZeusException("Invalid input. Log name cannot be None.")
name_length = len(name)
if name_length < 1 or name_length > 255:
raise ZeusException("Invalid log name. It must be longer than 1 "
"character and shorter than 255 charaters.")
if not re.match(r"^[a-zA-Z0-9]*$", name):
raise ZeusException("Invalid log name. It can only contain "
"letters or numbers.")

def _validateMetricName(self, name):
return True if re.match(r"^[^_.-][.\w-]*$", name) else False
if name is None:
raise ZeusException("Invalid input. Metric name cannot be None.")
name_length = len(name)
if name_length < 1 or name_length > 255:
raise ZeusException("Invalid metric name. It must be longer than "
"1 character and shorter than 255 charaters.")
if not re.match(r"^[^_.-][.\w-]*$", name):
raise ZeusException("Invalid metric name. The name needs to start "
"with a letter or number and can contain "
"_ - or .")

def _validateDates(self, from_date, to_date):
try:
from_date_value = float(from_date) if from_date else None
to_date_value = float(to_date) if to_date else None
except ValueError as e:
raise ZeusException("Invalid date. %s" % str(e))
if (from_date_value is None) or (to_date_value is None):
return
elif from_date_value > to_date_value:
raise ZeusException("Invalid date. The from_date should not be "
"after to_date.")

def sendLog(self, log_name, logs):
"""Return ``dict`` saying how many *logs* were successfully inserted
Expand All @@ -58,9 +87,7 @@ def sendLog(self, log_name, logs):
:rtype: dict
"""
if not self._validateLogName(log_name):
raise ZeusException("Invalid input name. It can only contain" +
" letters or numbers")
self._validateLogName(log_name)
data = {'logs': json.dumps(logs)}
return self._sendRequest('POST', '/logs/' + self.token +
'/' + log_name + '/', data)
Expand All @@ -75,10 +102,7 @@ def sendMetric(self, metric_name, metrics):
:rtype: dict
"""
if not self._validateMetricName(metric_name):
raise ZeusException("Invalid input name. The name needs to start" +
" with a letter or number and can contain" +
" _ - or .")
self._validateMetricName(metric_name)
data = {'metrics': json.dumps(metrics)}
return self._sendRequest('POST', '/metrics/' + self.token +
'/' + metric_name + '/', data)
Expand All @@ -104,6 +128,7 @@ def getLog(self,
:rtype: array
"""
self._validateDates(from_date, to_date)
data = {"log_name": log_name}
if attribute_name:
data['attribute_name'] = attribute_name
Expand Down Expand Up @@ -145,6 +170,7 @@ def getMetric(self, metric_name,
:rtype: array
"""
self._validateDates(from_date, to_date)
data = {"metric_name": metric_name}
if from_date:
data['from'] = from_date
Expand Down Expand Up @@ -199,10 +225,7 @@ def deleteMetric(self, metric_name):
:rtype: boolean
"""
if not self._validateMetricName(metric_name):
raise ZeusException("Invalid input name. The name needs to start" +
" with a letter or number and can contain" +
" _ - or .")
self._validateMetricName(metric_name)
return self._sendRequest('DELETE', '/metrics/' + self.token +
'/' + metric_name + '/', None)

Expand Down

0 comments on commit a1f62c1

Please sign in to comment.