Add explicit os_collect_config exceptions.
This commit is contained in:
parent
98e6c25a39
commit
4a568e4456
@ -16,6 +16,8 @@
|
||||
import httplib2
|
||||
import json
|
||||
|
||||
from os_collect_config import exc
|
||||
|
||||
|
||||
EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data'
|
||||
|
||||
@ -24,7 +26,10 @@ h = httplib2.Http()
|
||||
|
||||
def _fetch_metadata(sub_url):
|
||||
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:
|
||||
raise Exception('Error fetching %s' % sub_url)
|
||||
if sub_url[-1] == '/':
|
||||
|
10
os_collect_config/exc.py
Normal file
10
os_collect_config/exc.py
Normal 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."""
|
@ -14,12 +14,14 @@
|
||||
# limitations under the License.
|
||||
|
||||
import fixtures
|
||||
import httplib2
|
||||
import testtools
|
||||
from testtools import matchers
|
||||
import urlparse
|
||||
import uuid
|
||||
|
||||
from os_collect_config import collect
|
||||
from os_collect_config import exc
|
||||
|
||||
|
||||
META_DATA = {'local-ipv4': '192.0.2.1',
|
||||
@ -67,6 +69,11 @@ class FakeHttp(object):
|
||||
return (FakeResponse(), META_DATA[path])
|
||||
|
||||
|
||||
class FakeFailHttp(object):
|
||||
def request(self, url):
|
||||
raise httplib2.socks.HTTPError(403, 'Forbidden')
|
||||
|
||||
|
||||
class TestCollect(testtools.TestCase):
|
||||
def test_collect_ec2(self):
|
||||
self.useFixture(
|
||||
@ -84,3 +91,10 @@ class TestCollect(testtools.TestCase):
|
||||
self.assertEquals(
|
||||
{'0': {'openssh-key': 'ssh-rsa AAAAAAAAABBBBBBBBCCCCCCCC'}},
|
||||
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)
|
||||
|
Loading…
Reference in New Issue
Block a user