blob: eb3535f353d4127acf634a25b498159d9255a93c [file] [log] [blame]
Matthew Treinisha62347f2013-03-01 16:37:30 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2013 OpenStack Foundation
Matthew Treinisha62347f2013-03-01 16:37:30 -05004# All Rights Reserved.
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04005# Copyright 2013 IBM Corp.
Matthew Treinisha62347f2013-03-01 16:37:30 -05006#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18
19import cStringIO as StringIO
20import random
21
Sean Dague1937d092013-05-17 16:36:38 -040022from tempest.api.image import base
Matthew Treinisha62347f2013-03-01 16:37:30 -050023from tempest import exceptions
Matthew Treinisha62347f2013-03-01 16:37:30 -050024from tempest.test import attr
25
26
Matthew Treinishce3ef922013-03-11 14:02:46 -040027class CreateRegisterImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050028
29 """
30 Here we test the registration and creation of images
31 """
32
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -040033 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -050034 def test_register_with_invalid_container_format(self):
35 # Negative tests for invalid data supplied to POST /images
36 self.assertRaises(exceptions.BadRequest, self.client.create_image,
37 'test', 'wrong', 'vhd')
38
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -040039 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -050040 def test_register_with_invalid_disk_format(self):
41 self.assertRaises(exceptions.BadRequest, self.client.create_image,
42 'test', 'bare', 'wrong')
43
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -040044 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -050045 def test_register_then_upload(self):
46 # Register, then upload an image
Matthew Treinishce3ef922013-03-11 14:02:46 -040047 resp, body = self.create_image(name='New Name',
48 container_format='bare',
49 disk_format='raw',
50 visibility='public')
Attila Fazekase191cb12013-07-29 06:41:52 +020051 self.assertIn('id', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050052 image_id = body.get('id')
Attila Fazekase191cb12013-07-29 06:41:52 +020053 self.assertIn('name', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050054 self.assertEqual('New Name', body.get('name'))
Attila Fazekase191cb12013-07-29 06:41:52 +020055 self.assertIn('visibility', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050056 self.assertTrue(body.get('visibility') == 'public')
Attila Fazekase191cb12013-07-29 06:41:52 +020057 self.assertIn('status', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050058 self.assertEqual('queued', body.get('status'))
59
60 # Now try uploading an image file
61 image_file = StringIO.StringIO(('*' * 1024))
62 resp, body = self.client.store_image(image_id, image_file)
63 self.assertEqual(resp.status, 204)
64 resp, body = self.client.get_image_metadata(image_id)
Attila Fazekase191cb12013-07-29 06:41:52 +020065 self.assertIn('size', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050066 self.assertEqual(1024, body.get('size'))
67
68
Matthew Treinishce3ef922013-03-11 14:02:46 -040069class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050070
71 """
72 Here we test the listing of image information
73 """
74
75 @classmethod
76 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -040077 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -050078 # We add a few images here to test the listing functionality of
79 # the images API
80 for x in xrange(0, 10):
Matthew Treinish390ce112013-06-04 16:23:38 -040081 cls._create_standard_image(x)
Matthew Treinisha62347f2013-03-01 16:37:30 -050082
83 @classmethod
Matthew Treinisha62347f2013-03-01 16:37:30 -050084 def _create_standard_image(cls, number):
85 """
86 Create a new standard image and return the ID of the newly-registered
87 image. Note that the size of the new image is a random number between
88 1024 and 4096
89 """
90 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
91 name = 'New Standard Image %s' % number
Matthew Treinishce3ef922013-03-11 14:02:46 -040092 resp, body = cls.create_image(name=name, container_format='bare',
93 disk_format='raw',
94 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -050095 image_id = body['id']
96 resp, body = cls.client.store_image(image_id, data=image_file)
97
98 return image_id
99
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -0400100 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500101 def test_index_no_params(self):
102 # Simple test to see all fixture images returned
103 resp, images_list = self.client.image_list()
104 self.assertEqual(resp['status'], '200')
105 image_list = map(lambda x: x['id'], images_list)
106 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200107 self.assertIn(image, image_list)
Zhi kun Liu682c7da2013-07-30 13:21:14 +0800108
109 @attr(type=['negative', 'gate'])
110 def test_get_image_meta_by_null_id(self):
111 self.assertRaises(exceptions.NotFound,
112 self.client.get_image_metadata, '')