Skip to content

Adding new exception:ArgusObjectNotFoundException #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion argusclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
#

from .client import ArgusServiceClient, ArgusException, ArgusAuthException, MetricQuery, AnnotationQuery
from .client import ArgusServiceClient, ArgusException, ArgusAuthException, ArgusObjectNotFoundException, MetricQuery, AnnotationQuery
from .model import Namespace, Metric, Annotation, Dashboard, Alert, Trigger, Notification, User, AddListResult
9 changes: 7 additions & 2 deletions argusclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class ArgusAuthException(ArgusException):
"""
pass

class ArgusObjectNotFoundException(ArgusException):
"""
An exception type that is thrown for Argus object not found errors.
"""
pass

class BaseQuery(object):
def __init__(self, baseExpr, *tailParams, **kwargs):
Expand Down Expand Up @@ -741,9 +746,9 @@ def check_success(resp, decCls):
raise ArgusException(resp.text)
return res
elif resp.status_code == httplib.NOT_FOUND:
raise ArgusException("Object not found at endpoint: %s message: %s" % (resp.request.url, resp.text))
raise ArgusObjectNotFoundException("Object not found at endpoint: %s message: %s" % (resp.url, resp.text))
elif resp.status_code == httplib.UNAUTHORIZED:
raise ArgusAuthException("Failed to authenticate at endpoint: %s message: %s" % (resp.request.url, resp.text))
raise ArgusAuthException("Failed to authenticate at endpoint: %s message: %s" % (resp.url, resp.text))
else:
# TODO handle this differently, as this is typically a more severe exception (see W-2830904)
raise ArgusException(resp.text)
16 changes: 10 additions & 6 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ def __init__(self, url):


class MockResponse(object):
def __init__(self, json_text, status_code, request=None):
def __init__(self, json_text, status_code, request=None,url=None):
self.text = json_text
self.status_code = status_code
self.cookies = cookies
self.request = request
self.url = url

def json(self, **kwargs):
return json.loads(self.text, **kwargs)
Expand All @@ -47,8 +48,11 @@ def testFailure(self):
def testError(self):
self.failUnlessRaises(ArgusException, lambda: check_success(MockResponse("", 500), decCls=JsonDecoder))

def testUnauthorized(self):
self.failUnlessRaises(ArgusAuthException, lambda: check_success(MockResponse("", 401), decCls=JsonDecoder))

def testUnexpectedEndpoint(self):
self.failUnlessRaises(Exception, lambda: check_success(MockResponse("HTTP 404 Not Found", 404), decCls=JsonDecoder))
self.failUnlessRaises(ArgusObjectNotFoundException, lambda: check_success(MockResponse("HTTP 404 Not Found", 404), decCls=JsonDecoder))


class TestServiceBase(unittest.TestCase):
Expand Down Expand Up @@ -92,7 +96,7 @@ def testAuthImplicit(self):

@mock.patch('requests.Session.post', return_value=MockResponse("""{ "status": 401, "message": "Unauthorized" }""", 401, request=MockRequest("v2/auth/login")))
def testUnauthorized(self, mockPost):
"""A stright-forward login failure with invalid username/password"""
"""A straight-forward login failure with invalid username/password"""
self.failUnlessRaises(ArgusAuthException, lambda: self.argus.login())

def testAuthWithDirectRefreshToken(self):
Expand Down Expand Up @@ -423,18 +427,18 @@ def testGetUserAlert(self, mockGet):
res = self.argus.alerts.get_user_alert(testId, testId)
self.assertTrue(isinstance(res, Alert))
self.assertEquals(res.to_dict(), alert_D)
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this. This must have been me forgetting to run the unittests with my last change.


@mock.patch('requests.Session.get', return_value=MockResponse(json.dumps([]), 200))
def testGetUserAlertNoMatch(self, mockGet):
res = self.argus.alerts.get_user_alert(testId, testId)
self.assertEquals(res, None)
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))

@mock.patch('requests.Session.get', return_value=MockResponse(json.dumps([alert_D, alert_D]), 200))
def testGetUserAlertUnexpectedMultiple(self, mockGet):
self.failUnlessRaises(AssertionError, lambda: self.argus.alerts.get_user_alert(testId, testId))
self.assertIn((os.path.join(endpoint, "alerts"),), tuple(mockGet.call_args))
self.assertIn((os.path.join(endpoint, "alerts/meta"),), tuple(mockGet.call_args))


class TestAlertTrigger(TestServiceBase):
Expand Down