summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Pasquier <pasquier.simon@gmail.com>2013-05-24 17:10:04 +0200
committerSimon Pasquier <pasquier.simon@gmail.com>2013-06-03 14:21:40 +0200
commit2d00ede1e65139197f70471325b213f8241146b6 (patch)
tree7cd3f2c02c5c77ddabcf31f55ecb8cec4d5c7a68
parent16452ffedf2bf692101d186b99b6f77b559b05ec (diff)
downloadheat-cfntools-2d00ede1e65139197f70471325b213f8241146b6.tar.gz
cfn-get-metadata: log metadata to standard output
Add display() method to the Metadata class that prints the metadata to standard output if the metdata has been successfully retrieved (either from local cache or from the remote server). Change-Id: Idf6c1aecf2a5204d7cf7fbf3c8d826f750a72785 Fixes: bug #1183298
-rwxr-xr-xbin/cfn-get-metadata1
-rw-r--r--heat_cfntools/cfntools/cfn_helper.py14
-rw-r--r--heat_cfntools/tests/test_cfn_helper.py51
3 files changed, 49 insertions, 17 deletions
diff --git a/bin/cfn-get-metadata b/bin/cfn-get-metadata
index 5d27b6b..6aa9251 100755
--- a/bin/cfn-get-metadata
+++ b/bin/cfn-get-metadata
@@ -86,3 +86,4 @@ metadata = Metadata(args.stack_name,
credentials_file=args.credential_file)
metadata.retrieve()
LOG.debug(str(metadata))
+metadata.display()
diff --git a/heat_cfntools/cfntools/cfn_helper.py b/heat_cfntools/cfntools/cfn_helper.py
index 3d1e38f..5fa7b5a 100644
--- a/heat_cfntools/cfntools/cfn_helper.py
+++ b/heat_cfntools/cfntools/cfn_helper.py
@@ -1041,7 +1041,12 @@ class Metadata(object):
meta_str=None,
default_path='/var/lib/heat-cfntools/cfn-init-data',
last_path='/var/cache/heat-cfntools/last_metadata'):
- """Read the metadata from the given filename."""
+ """Read the metadata from the given filename or from the remote server.
+
+ Returns:
+ True -- success
+ False -- error
+ """
if meta_str:
self._data = meta_str
else:
@@ -1116,9 +1121,16 @@ class Metadata(object):
cf.write(json.dumps(self._metadata))
os.rename(cf.name, last_path)
+ return True
+
def __str__(self):
return json.dumps(self._metadata)
+ def display(self):
+ if self._metadata is not None:
+ print str(self)
+ return
+
def _is_valid_metadata(self):
"""Should find the AWS::CloudFormation::Init json key."""
is_valid = self._metadata and \
diff --git a/heat_cfntools/tests/test_cfn_helper.py b/heat_cfntools/tests/test_cfn_helper.py
index a02f594..843f9e4 100644
--- a/heat_cfntools/tests/test_cfn_helper.py
+++ b/heat_cfntools/tests/test_cfn_helper.py
@@ -528,17 +528,16 @@ class TestMetadataRetrieve(testtools.TestCase):
default_file.flush()
self.assertThat(default_file.name, ttm.FileContains(md_str))
- md.retrieve(
- default_path=default_file.name,
- last_path=self.last_file)
+ self.assertTrue(
+ md.retrieve(default_path=default_file.name,
+ last_path=self.last_file))
self.assertThat(self.last_file, ttm.FileContains(md_str))
self.assertThat(md_data, ttm.Equals(md._metadata))
md = cfn_helper.Metadata('teststack', None)
- md.retrieve(
- default_path=default_file.name,
- last_path=self.last_file)
+ self.assertTrue(md.retrieve(default_path=default_file.name,
+ last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
def test_metadata_retrieve_none(self):
@@ -546,11 +545,17 @@ class TestMetadataRetrieve(testtools.TestCase):
md = cfn_helper.Metadata('teststack', None)
default_file = os.path.join(self.tdir.path, 'default_file')
- md.retrieve(
- default_path=default_file,
- last_path=self.last_file)
+ self.assertFalse(md.retrieve(default_path=default_file,
+ last_path=self.last_file))
self.assertIsNone(md._metadata)
+ displayed = self.useFixture(fixtures.StringStream('stdout'))
+ fake_stdout = displayed.stream
+ self.useFixture(fixtures.MonkeyPatch('sys.stdout', fake_stdout))
+ md.display()
+ fake_stdout.flush()
+ self.assertEqual(displayed.getDetails()['stdout'].as_text(), "")
+
def test_metadata_retrieve_passed(self):
md_data = {"AWS::CloudFormation::Init": {"config": {"files": {
@@ -558,14 +563,26 @@ class TestMetadataRetrieve(testtools.TestCase):
md_str = json.dumps(md_data)
md = cfn_helper.Metadata('teststack', None)
- md.retrieve(meta_str=md_str, last_path=self.last_file)
+ self.assertTrue(md.retrieve(meta_str=md_str,
+ last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
md = cfn_helper.Metadata('teststack', None)
- md.retrieve(meta_str=md_data, last_path=self.last_file)
+ self.assertTrue(md.retrieve(meta_str=md_data,
+ last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
self.assertEqual(md_str, str(md))
+ displayed = self.useFixture(fixtures.StringStream('stdout'))
+ fake_stdout = displayed.stream
+ self.useFixture(fixtures.MonkeyPatch('sys.stdout', fake_stdout))
+ md.display()
+ fake_stdout.flush()
+ self.assertEqual(displayed.getDetails()['stdout'].as_text(),
+ "{\"AWS::CloudFormation::Init\": {\"config\": {"
+ "\"files\": {\"/tmp/foo\": {\"content\": \"bar\"}"
+ "}}}}\n")
+
def test_metadata_creates_cache(self):
temp_home = tempfile.mkdtemp()
@@ -584,7 +601,7 @@ class TestMetadataRetrieve(testtools.TestCase):
self.assertFalse(os.path.exists(last_path),
"last_metadata file already exists")
- md.retrieve(meta_str=md_str, last_path=last_path)
+ self.assertTrue(md.retrieve(meta_str=md_str, last_path=last_path))
self.assertTrue(os.path.exists(last_path),
"last_metadata file should exist")
# Ensure created dirs and file have right perms
@@ -597,7 +614,8 @@ class TestMetadataRetrieve(testtools.TestCase):
"/tmp/foo": {"content": "bar"}}}}}
md = cfn_helper.Metadata('teststack', None)
- md.retrieve(meta_str=md_data, last_path=self.last_file)
+ self.assertTrue(
+ md.retrieve(meta_str=md_data, last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
self.assertTrue(md._is_valid_metadata())
@@ -627,7 +645,7 @@ class TestMetadataRetrieve(testtools.TestCase):
None,
access_key='foo',
secret_key='bar')
- md.retrieve(last_path=self.last_file)
+ self.assertTrue(md.retrieve(last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
with tempfile.NamedTemporaryFile(mode='w') as fcreds:
@@ -635,7 +653,7 @@ class TestMetadataRetrieve(testtools.TestCase):
fcreds.flush()
md = cfn_helper.Metadata(
'teststack', None, credentials_file=fcreds.name)
- md.retrieve(last_path=self.last_file)
+ self.assertTrue(md.retrieve(last_path=self.last_file))
self.assertThat(md_data, ttm.Equals(md._metadata))
m.VerifyAll()
@@ -649,7 +667,8 @@ class TestMetadataRetrieve(testtools.TestCase):
foo_file.name: {"content": "bar"}}}}}
md = cfn_helper.Metadata('teststack', None)
- md.retrieve(meta_str=md_data, last_path=self.last_file)
+ self.assertTrue(
+ md.retrieve(meta_str=md_data, last_path=self.last_file))
md.cfn_init()
self.assertThat(foo_file.name, ttm.FileContains('bar'))