OK, fix docs to make it clear that only the string 'true' is allowed for boolean headers. Add False-hood unit tests as well.

This commit is contained in:
jaypipes@gmail.com 2011-04-12 17:45:16 -04:00
parent 788296100d
commit 6703ba193e
3 changed files with 24 additions and 12 deletions

View File

@ -308,8 +308,7 @@ The list of metadata headers that Glance accepts are listed below.
This header is optional.
When present, Glance converts the value of the header to a boolean value,
so "on, 1, true" are all true values. When true, the image is marked as
When Glance finds the string "true" (case-insensitive), the image is marked as
a public image, meaning that any user may view its metadata and may read
the disk image from Glance.

View File

@ -99,16 +99,14 @@ def get_image_meta_from_headers(response):
def bool_from_header_value(value):
"""
Returns True if value is any of ('True', 'On', '1'),
case-insensitive
Returns True if value is a boolean True or the
string 'true', case-insensitive, False otherwise
"""
if type(value) == type(bool):
if isinstance(value, bool):
return value
elif isinstance(value, (basestring, unicode)):
if str(value).lower() in ('true', 'on', '1'):
if str(value).lower() == 'true':
return True
elif type(value) == type(int):
return bool(value)
return False

View File

@ -73,10 +73,7 @@ class TestUtils(unittest.TestCase):
"""
fixtures = [{'is_public': True},
{'is_public': 'True'},
{'is_public': 'true'},
{'is_public': 'On'},
{'is_public': 1},
{'is_public': '1'}]
{'is_public': 'true'}]
expected = {'is_public': True}
@ -91,3 +88,21 @@ class TestUtils(unittest.TestCase):
result = utils.get_image_meta_from_headers(response)
for k, v in expected.items():
self.assertEqual(v, result[k])
# Ensure False for other values...
fixtures = [{'is_public': False},
{'is_public': 'Off'},
{'is_public': 'on'},
{'is_public': '1'},
{'is_public': 'False'}]
expected = {'is_public': False}
for fixture in fixtures:
headers = utils.image_meta_to_http_headers(fixture)
response = FakeResponse()
response.headers = headers
result = utils.get_image_meta_from_headers(response)
for k, v in expected.items():
self.assertEqual(v, result[k])