diff --git a/tests/stubs.py b/tests/stubs.py index 6bb8670b9a..efe024664f 100644 --- a/tests/stubs.py +++ b/tests/stubs.py @@ -267,7 +267,7 @@ def stub_out_registry_db_image_api(stubs): The "datastore" always starts with this set of image fixtures. :param stubs: Set of stubout stubs - + :return: count of items in the "datastore" """ class FakeDatastore(object): @@ -301,11 +301,25 @@ def stub_out_registry_db_image_api(stubs): 'checksum': None, 'size': 19, 'location': "file:///tmp/glance-tests/2", - 'properties': []}] + 'properties': []}, + {'id': 3, + 'name': 'fake iso image', + 'status': 'active', + 'disk_format': 'iso', + 'container_format': 'bare', + 'is_public': False, + 'created_at': datetime.datetime.utcnow(), + 'updated_at': datetime.datetime.utcnow(), + 'deleted_at': None, + 'deleted': False, + 'checksum': None, + 'size': 19, + 'location': "file:///tmp/glance-tests/3", + 'properties': {}}] def __init__(self): self.images = FakeDatastore.FIXTURES - self.next_id = 3 + self.next_id = 4 def image_create(self, _context, values): @@ -401,3 +415,4 @@ def stub_out_registry_db_image_api(stubs): fake_datastore.image_get) stubs.Set(glance.registry.db.api, 'image_get_all_public', fake_datastore.image_get_all_public) + return fake_datastore.next_id diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py index 3f605b609b..0c51af7cba 100644 --- a/tests/unit/test_api.py +++ b/tests/unit/test_api.py @@ -37,7 +37,7 @@ class TestRegistryAPI(unittest.TestCase): """Establish a clean test environment""" self.stubs = stubout.StubOutForTesting() stubs.stub_out_registry_and_store_server(self.stubs) - stubs.stub_out_registry_db_image_api(self.stubs) + self.next_image_id = stubs.stub_out_registry_db_image_api(self.stubs) stubs.stub_out_filesystem_backend() self.api = rserver.API({'verbose': VERBOSE, 'debug': DEBUG}) @@ -134,14 +134,14 @@ class TestRegistryAPI(unittest.TestCase): self.assertEquals(v, res_dict['image'][k]) # Test ID auto-assigned properly - self.assertEquals(3, res_dict['image']['id']) + self.assertEquals(self.next_image_id, res_dict['image']['id']) # Test status was updated properly self.assertEquals('active', res_dict['image']['status']) def test_create_image_with_bad_container_format(self): """Tests proper exception is raised if a bad disk_format is set""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'vhd', @@ -158,7 +158,7 @@ class TestRegistryAPI(unittest.TestCase): def test_create_image_with_bad_disk_format(self): """Tests proper exception is raised if a bad disk_format is set""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'invalid', @@ -192,7 +192,7 @@ class TestRegistryAPI(unittest.TestCase): def test_create_image_with_bad_status(self): """Tests proper exception is raised if a bad status is set""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'vhd', @@ -232,7 +232,7 @@ class TestRegistryAPI(unittest.TestCase): image""" fixture = {'status': 'killed'} - req = webob.Request.blank('/images/3') + req = webob.Request.blank('/images/' + str(self.next_image_id)) req.method = 'PUT' req.body = json.dumps(dict(image=fixture)) @@ -328,7 +328,7 @@ class TestRegistryAPI(unittest.TestCase): """Tests proper exception is raised if attempt to delete non-existing image""" - req = webob.Request.blank('/images/3') + req = webob.Request.blank('/images/' + str(self.next_image_id)) req.method = 'DELETE' @@ -342,7 +342,7 @@ class TestGlanceAPI(unittest.TestCase): """Establish a clean test environment""" self.stubs = stubout.StubOutForTesting() stubs.stub_out_registry_and_store_server(self.stubs) - stubs.stub_out_registry_db_image_api(self.stubs) + self.next_image_id = stubs.stub_out_registry_db_image_api(self.stubs) stubs.stub_out_filesystem_backend() sql_connection = os.environ.get('GLANCE_SQL_CONNECTION', "sqlite://") options = {'verbose': VERBOSE, @@ -442,7 +442,7 @@ class TestGlanceAPI(unittest.TestCase): res_body = json.loads(res.body)['image'] self.assertEquals(res_body['location'], - 'file:///tmp/glance-tests/3') + 'file:///tmp/glance-tests/' + str(self.next_image_id)) # Test that the Location: header is set to the URI to # edit the newly-created image, as required by APP. @@ -450,7 +450,8 @@ class TestGlanceAPI(unittest.TestCase): self.assertTrue('location' in res.headers, "'location' not in response headers.\n" "res.headerlist = %r" % res.headerlist) - self.assertTrue('/images/3' in res.headers['location']) + self.assertTrue('/images/' + str(self.next_image_id) + in res.headers['location']) def test_image_is_checksummed(self): """Test that the image contents are checksummed properly""" @@ -473,9 +474,9 @@ class TestGlanceAPI(unittest.TestCase): res_body = json.loads(res.body)['image'] self.assertEquals(res_body['location'], - 'file:///tmp/glance-tests/3') + 'file:///tmp/glance-tests/' + str(self.next_image_id)) self.assertEquals(image_checksum, res_body['checksum'], - "Mismatched checksum. Expected %s, got %s" % + "Mismatched checksum. Expected %s, got %s" % (image_checksum, res_body['checksum'])) def test_etag_equals_checksum_header(self): @@ -500,7 +501,7 @@ class TestGlanceAPI(unittest.TestCase): # HEAD the image and check the ETag equals the checksum header... expected_headers = {'x-image-meta-checksum': image_checksum, 'etag': image_checksum} - req = webob.Request.blank("/images/3") + req = webob.Request.blank("/images/" + str(self.next_image_id)) req.method = 'HEAD' res = req.get_response(self.api) self.assertEquals(res.status_int, 200) @@ -533,9 +534,9 @@ class TestGlanceAPI(unittest.TestCase): self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code) # Test the image was killed... - expected_headers = {'x-image-meta-id': '3', + expected_headers = {'x-image-meta-id': str(self.next_image_id), 'x-image-meta-status': 'killed'} - req = webob.Request.blank("/images/3") + req = webob.Request.blank("/images/" + str(self.next_image_id)) req.method = 'HEAD' res = req.get_response(self.api) self.assertEquals(res.status_int, 200) diff --git a/tests/unit/test_clients.py b/tests/unit/test_clients.py index b01d77a615..1c82d13906 100644 --- a/tests/unit/test_clients.py +++ b/tests/unit/test_clients.py @@ -51,7 +51,7 @@ class TestRegistryClient(unittest.TestCase): def setUp(self): """Establish a clean test environment""" self.stubs = stubout.StubOutForTesting() - stubs.stub_out_registry_db_image_api(self.stubs) + self.next_image_id = stubs.stub_out_registry_db_image_api(self.stubs) stubs.stub_out_registry_and_store_server(self.stubs) self.client = rclient.RegistryClient("0.0.0.0") @@ -127,10 +127,10 @@ class TestRegistryClient(unittest.TestCase): new_image = self.client.add_image(fixture) # Test ID auto-assigned properly - self.assertEquals(3, new_image['id']) + self.assertEquals(self.next_image_id, new_image['id']) # Test all other attributes set - data = self.client.get_image(3) + data = self.client.get_image(self.next_image_id) for k, v in fixture.items(): self.assertEquals(v, data[k]) @@ -152,7 +152,7 @@ class TestRegistryClient(unittest.TestCase): new_image = self.client.add_image(fixture) # Test ID auto-assigned properly - self.assertEquals(3, new_image['id']) + self.assertEquals(self.next_image_id, new_image['id']) for k, v in fixture.items(): self.assertEquals(v, new_image[k]) @@ -179,7 +179,7 @@ class TestRegistryClient(unittest.TestCase): def test_add_image_with_bad_status(self): """Tests proper exception is raised if a bad status is set""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'vmdk', @@ -208,7 +208,7 @@ class TestRegistryClient(unittest.TestCase): def test_update_image_not_existing(self): """Tests non existing image update doesn't work""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'vmdk', @@ -218,7 +218,7 @@ class TestRegistryClient(unittest.TestCase): self.assertRaises(exception.NotFound, self.client.update_image, - 3, + self.next_image_id, fixture) def test_delete_image(self): @@ -240,7 +240,7 @@ class TestRegistryClient(unittest.TestCase): self.assertRaises(exception.NotFound, self.client.delete_image, - 3) + self.next_image_id) class TestClient(unittest.TestCase): @@ -253,7 +253,7 @@ class TestClient(unittest.TestCase): def setUp(self): """Establish a clean test environment""" self.stubs = stubout.StubOutForTesting() - stubs.stub_out_registry_db_image_api(self.stubs) + self.next_image_id = stubs.stub_out_registry_db_image_api(self.stubs) stubs.stub_out_registry_and_store_server(self.stubs) stubs.stub_out_filesystem_backend() self.client = client.Client("0.0.0.0", doc_root="") @@ -290,7 +290,7 @@ class TestClient(unittest.TestCase): self.assertRaises(exception.NotFound, self.client.get_image, - 3) + self.next_image_id) def test_get_image_index(self): """Test correct set of public image returned""" @@ -347,6 +347,23 @@ class TestClient(unittest.TestCase): for k, v in fixture.items(): self.assertEquals(v, data[k]) + def test_get_image_iso_meta(self): + """Tests that the detailed info about an iso image is returned""" + fixture = {'id': 3, + 'name': 'fake iso image', + 'is_public': False, + 'disk_format': 'iso', + 'container_format': 'bare', + 'status': 'active', + 'size': 19, + 'location': "file:///tmp/glance-tests/3", + 'properties': {}} + + data = self.client.get_image_meta(3) + + for k, v in fixture.items(): + self.assertEquals(v, data[k]) + def test_get_image_non_existing(self): """Tests that NotFound is raised when getting a non-existing image""" @@ -378,10 +395,10 @@ class TestClient(unittest.TestCase): new_image_id = new_image['id'] # Test ID auto-assigned properly - self.assertEquals(3, new_image_id) + self.assertEquals(self.next_image_id, new_image_id) # Test all other attributes set - data = self.client.get_image_meta(3) + data = self.client.get_image_meta(self.next_image_id) for k, v in fixture.items(): self.assertEquals(v, data[k]) @@ -404,10 +421,10 @@ class TestClient(unittest.TestCase): new_image_id = new_image['id'] # Test ID auto-assigned properly - self.assertEquals(3, new_image_id) + self.assertEquals(self.next_image_id, new_image_id) # Test all other attributes set - data = self.client.get_image_meta(3) + data = self.client.get_image_meta(self.next_image_id) for k, v in fixture.items(): self.assertEquals(v, data[k]) @@ -416,6 +433,48 @@ class TestClient(unittest.TestCase): self.assertTrue('status' in data) self.assertEquals('active', data['status']) + def test_add_image_with_iso_properties(self): + """Tests that we can add image metadata with iso disk format""" + fixture = {'name': 'fake public iso', + 'is_public': True, + 'disk_format': 'iso', + 'container_format': 'bare', + 'size': 19, + 'location': "file:///tmp/glance-tests/2", + 'properties': {'install': 'Bindows Heaven'}, + } + new_image = self.client.add_image(fixture) + new_image_id = new_image['id'] + + # Test ID auto-assigned properly + self.assertEquals(self.next_image_id, new_image_id) + + # Test all other attributes set + data = self.client.get_image_meta(self.next_image_id) + + for k, v in fixture.items(): + self.assertEquals(v, data[k]) + + # Test status was updated properly + self.assertTrue('status' in data) + self.assertEquals('active', data['status']) + + def test_add_image_with_bad_iso_properties(self): + """Tests that we can add image metadata with iso disk format""" + fixture = {'name': 'fake public iso', + 'is_public': True, + 'disk_format': 'iso', + 'container_format': 'vhd', + 'size': 19, + 'location': "file:///tmp/glance-tests/" + + str(self.next_image_id), + 'properties': {'install': 'Bindows Heaven'}, + } + + self.assertRaises(exception.Invalid, + self.client.add_image, + fixture) + def test_add_image_already_exists(self): """Tests proper exception is raised if image with ID already exists""" fixture = {'id': 2, @@ -460,9 +519,9 @@ class TestClient(unittest.TestCase): new_image = self.client.add_image(fixture, image_data_fixture) new_image_id = new_image['id'] - self.assertEquals(3, new_image_id) + self.assertEquals(self.next_image_id, new_image_id) - new_meta, new_image_chunks = self.client.get_image(3) + new_meta, new_image_chunks = self.client.get_image(self.next_image_id) new_image_data = "" for image_chunk in new_image_chunks: @@ -495,12 +554,12 @@ class TestClient(unittest.TestCase): new_image = self.client.add_image(fixture, open(tmp_image_filepath)) new_image_id = new_image['id'] - self.assertEquals(3, new_image_id) + self.assertEquals(self.next_image_id, new_image_id) if os.path.exists(tmp_image_filepath): os.unlink(tmp_image_filepath) - new_meta, new_image_chunks = self.client.get_image(3) + new_meta, new_image_chunks = self.client.get_image(self.next_image_id) new_image_data = "" for image_chunk in new_image_chunks: @@ -523,9 +582,9 @@ class TestClient(unittest.TestCase): new_image = self.client.add_image(fixture, image_data_fixture) new_image_id = new_image['id'] - self.assertEquals(3, new_image_id) + self.assertEquals(self.next_image_id, new_image_id) - new_meta, new_image_chunks = self.client.get_image(3) + new_meta, new_image_chunks = self.client.get_image(self.next_image_id) new_image_data = "" for image_chunk in new_image_chunks: @@ -570,7 +629,7 @@ class TestClient(unittest.TestCase): def test_update_image_not_existing(self): """Tests non existing image update doesn't work""" - fixture = {'id': 3, + fixture = {'id': self.next_image_id, 'name': 'fake public image', 'is_public': True, 'disk_format': 'vhd', @@ -580,7 +639,7 @@ class TestClient(unittest.TestCase): self.assertRaises(exception.NotFound, self.client.update_image, - 3, + self.next_image_id, fixture) def test_delete_image(self): @@ -602,4 +661,4 @@ class TestClient(unittest.TestCase): self.assertRaises(exception.NotFound, self.client.delete_image, - 3) + self.next_image_id)