Add explicit os_collect_config exceptions.

This commit is contained in:
Clint Byrum 2013-06-26 16:35:46 -07:00
parent 98e6c25a39
commit 4a568e4456
3 changed files with 30 additions and 1 deletions

View File

@ -16,6 +16,8 @@
import httplib2 import httplib2
import json import json
from os_collect_config import exc
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data' EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
@ -24,7 +26,10 @@ h = httplib2.Http()
def _fetch_metadata(sub_url): def _fetch_metadata(sub_url):
global h global h
(resp, content) = h.request('%s%s' % (EC2_METADATA_URL, sub_url)) try:
(resp, content) = h.request('%s%s' % (EC2_METADATA_URL, sub_url))
except httplib2.socks.HTTPError:
raise exc.Ec2MetadataNotAvailable
if resp.status != 200: if resp.status != 200:
raise Exception('Error fetching %s' % sub_url) raise Exception('Error fetching %s' % sub_url)
if sub_url[-1] == '/': if sub_url[-1] == '/':

10
os_collect_config/exc.py Normal file
View File

@ -0,0 +1,10 @@
class SourceNotAvailable(RuntimeError):
"""The requested data source is unavailable."""
class Ec2MetadataNotAvailable(SourceNotAvailable):
"""The EC2 metadata service is not available."""
class CfnMetadataNotAvailable(SourceNotAvailable):
"""The cfn metadata service is not available."""

View File

@ -14,12 +14,14 @@
# limitations under the License. # limitations under the License.
import fixtures import fixtures
import httplib2
import testtools import testtools
from testtools import matchers from testtools import matchers
import urlparse import urlparse
import uuid import uuid
from os_collect_config import collect from os_collect_config import collect
from os_collect_config import exc
META_DATA = {'local-ipv4': '192.0.2.1', META_DATA = {'local-ipv4': '192.0.2.1',
@ -67,6 +69,11 @@ class FakeHttp(object):
return (FakeResponse(), META_DATA[path]) return (FakeResponse(), META_DATA[path])
class FakeFailHttp(object):
def request(self, url):
raise httplib2.socks.HTTPError(403, 'Forbidden')
class TestCollect(testtools.TestCase): class TestCollect(testtools.TestCase):
def test_collect_ec2(self): def test_collect_ec2(self):
self.useFixture( self.useFixture(
@ -84,3 +91,10 @@ class TestCollect(testtools.TestCase):
self.assertEquals( self.assertEquals(
{'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}}, {'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}},
ec2['public-keys']) ec2['public-keys'])
def test_collect_ec2_fail(self):
self.useFixture(
fixtures.MonkeyPatch(
'os_collect_config.collect.h', FakeFailHttp()))
self.assertRaises(exc.Ec2MetadataNotAvailable,
collect.collect_ec2)