From 6703ba193e5ea7455777ffdf2d2902593e4204be Mon Sep 17 00:00:00 2001 From: "jaypipes@gmail.com" <> Date: Tue, 12 Apr 2011 17:45:16 -0400 Subject: [PATCH] OK, fix docs to make it clear that only the string 'true' is allowed for boolean headers. Add False-hood unit tests as well. --- doc/source/glanceapi.rst | 3 +-- glance/utils.py | 10 ++++------ tests/unit/test_utils.py | 23 +++++++++++++++++++---- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/doc/source/glanceapi.rst b/doc/source/glanceapi.rst index b06e602fed..48e7c10aec 100644 --- a/doc/source/glanceapi.rst +++ b/doc/source/glanceapi.rst @@ -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. diff --git a/glance/utils.py b/glance/utils.py index e897055d0d..6c64d10f8e 100644 --- a/glance/utils.py +++ b/glance/utils.py @@ -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 diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index a7f7e6f9bc..02e010c263 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -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])