From 7f2546ff260d57a50c1ce1f4e90958d88271631d Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 27 Jun 2013 09:01:02 -0700 Subject: [PATCH] Use oslo.config. Also make sure __main__ is tested. --- os_collect_config/collect.py | 31 ++++++++++++++++++------- os_collect_config/tests/test_collect.py | 17 ++++++++++++-- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/os_collect_config/collect.py b/os_collect_config/collect.py index df8cd6c..a727e45 100644 --- a/os_collect_config/collect.py +++ b/os_collect_config/collect.py @@ -18,40 +18,55 @@ import json from openstack.common import log from os_collect_config import exc +from oslo.config import cfg EC2_METADATA_URL = 'http://169.254.169.254/latest/meta-data' h = httplib2.Http() +ec2_opts = [ + cfg.StrOpt('metadata_url', + default=EC2_METADATA_URL, + help='URL to query for EC2 Metadata') +] -def _fetch_metadata(sub_url): + +def _fetch_metadata(fetch_url): global h try: - (resp, content) = h.request('%s%s' % (EC2_METADATA_URL, sub_url)) + (resp, content) = h.request(fetch_url) except httplib2.socks.HTTPError as e: log.getLogger().warn(e) raise exc.Ec2MetadataNotAvailable - if sub_url[-1] == '/': + if fetch_url[-1] == '/': new_content = {} for subkey in content.split("\n"): if '=' in subkey: subkey = subkey[:subkey.index('=')] + '/' - sub_sub_url = sub_url + subkey + sub_fetch_url = fetch_url + subkey if subkey[-1] == '/': subkey = subkey[:-1] - new_content[subkey] = _fetch_metadata(sub_sub_url) + new_content[subkey] = _fetch_metadata(sub_fetch_url) content = new_content return content -def collect_ec2(): - return _fetch_metadata('/') +def collect_ec2(ec2_url): + root_url = '%s/' % (ec2_url) + return _fetch_metadata(root_url) def __main__(): + ec2_group = cfg.OptGroup(name='ec2', + title='EC2 Metadata options') + + conf = cfg.ConfigOpts() + conf.register_group(ec2_group) + conf.register_opts(ec2_opts, group='ec2') + log.setup("os-collect-config") - print json.dumps(collect_ec2(), indent=1) + print json.dumps(collect_ec2(conf.ec2.metadata_url), indent=1) if __name__ == '__main__': diff --git a/os_collect_config/tests/test_collect.py b/os_collect_config/tests/test_collect.py index eb476e2..40e4b59 100644 --- a/os_collect_config/tests/test_collect.py +++ b/os_collect_config/tests/test_collect.py @@ -15,6 +15,7 @@ import fixtures import httplib2 +import json import testtools from testtools import matchers import urlparse @@ -82,7 +83,7 @@ class TestCollect(testtools.TestCase): def test_collect_ec2(self): self.useFixture( fixtures.MonkeyPatch('os_collect_config.collect.h', FakeHttp())) - ec2 = collect.collect_ec2() + ec2 = collect.collect_ec2(collect.EC2_METADATA_URL) self.assertThat(ec2, matchers.IsInstance(dict)) for k in ('public-ipv4', 'instance-id', 'hostname'): @@ -102,5 +103,17 @@ class TestCollect(testtools.TestCase): fixtures.MonkeyPatch( 'os_collect_config.collect.h', FakeFailHttp())) self.assertRaises(exc.Ec2MetadataNotAvailable, - collect.collect_ec2) + collect.collect_ec2, collect.EC2_METADATA_URL) self.assertIn('Forbidden', self.log.output) + + +class TestMain(testtools.TestCase): + def test_main(self): + self.useFixture( + fixtures.MonkeyPatch('os_collect_config.collect.h', FakeHttp())) + out = self.useFixture(fixtures.ByteStream('stdout')) + self.useFixture(fixtures.MonkeyPatch('sys.stdout', out.stream)) + collect.__main__() + result = json.loads(out.stream.getvalue()) + self.assertIn("local-ipv4", result) + self.assertIn("reservation-id", result)