Index: test/test_gogrid.py
===================================================================
--- test/test_gogrid.py (revision 982615)
+++ test/test_gogrid.py (working copy)
@@ -20,6 +20,7 @@
except ImportError:
import simplejson as json
+from libcloud.types import LibCloudException
from libcloud.drivers.gogrid import GoGridNodeDriver
from libcloud.base import Node, NodeImage, NodeSize
@@ -32,6 +33,7 @@
def setUp(self):
GoGridNodeDriver.connectionCls.conn_classes = (None, GoGridMockHttp)
+ GoGridMockHttp.type = None
self.driver = GoGridNodeDriver("foo", "bar")
def test_create_node(self):
@@ -66,6 +68,15 @@
self.assertEqual(image.name, 'CentOS 5.3 (32-bit) w/ None')
self.assertEqual(image.id, 1531)
+ def test_malformed_reply(self):
+ GoGridMockHttp.type = 'FAIL'
+ try:
+ images = self.driver.list_images()
+ except LibCloudException, e:
+ self.assertEqual(True, isinstance(e, LibCloudException))
+ else:
+ self.fail("test should have thrown")
+
class GoGridMockHttp(MockHttp):
fixtures = FileFixtures('gogrid')
@@ -74,6 +85,10 @@
body = self.fixtures.load('image_list.json')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
+ def _api_grid_image_list_FAIL(self, method, url, body, headers):
+ body = "
some non valid json here
"
+ return (httplib.SERVICE_UNAVAILABLE, body, {}, httplib.responses[httplib.SERVICE_UNAVAILABLE])
+
def _api_grid_server_list(self, method, url, body, headers):
body = self.fixtures.load('server_list.json')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])
Index: libcloud/types.py
===================================================================
--- libcloud/types.py (revision 982615)
+++ libcloud/types.py (working copy)
@@ -73,14 +73,16 @@
PENDING = 3
UNKNOWN = 4
-class InvalidCredsException(Exception):
+class LibCloudException(Exception): pass
+
+class InvalidCredsException(LibCloudException):
"""Exception used when invalid credentials are used on a provider."""
def __init__(self, value='Invalid credentials with the provider'):
self.value = value
def __str__(self):
return repr(self.value)
-class DeploymentException(Exception):
+class DeploymentException(LibCloudException):
"""
Exception used when a Deployment Task failed.
Index: libcloud/drivers/gogrid.py
===================================================================
--- libcloud/drivers/gogrid.py (revision 982615)
+++ libcloud/drivers/gogrid.py (working copy)
@@ -16,7 +16,7 @@
GoGrid driver
"""
from libcloud.providers import Provider
-from libcloud.types import NodeState, InvalidCredsException
+from libcloud.types import NodeState, LibCloudException, InvalidCredsException
from libcloud.base import Node, ConnectionUserAndKey, Response, NodeDriver
from libcloud.base import NodeSize, NodeImage, NodeLocation
import time
@@ -78,7 +78,10 @@
raise InvalidCredsException()
if not self.body:
return None
- return json.loads(self.body)['status'] == 'success'
+ try:
+ return json.loads(self.body)['status'] == 'success'
+ except ValueError:
+ raise LibCloudException('Malformed reply')
def parse_body(self):
if not self.body: