blob: 9abf28dbb22ebee6e83b130e869e266ae91fb8ed [file] [log] [blame]
Matthew Treinisha62347f2013-03-01 16:37:30 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 OpenStack, LLC
4# All Rights Reserved.
5# Copyright 2013 IBM Corp
6#
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
22from tempest import clients
23from tempest import exceptions
24import tempest.test
25from tempest.test import attr
26
27
28class CreateRegisterImagesTest(tempest.test.BaseTestCase):
29
30 """
31 Here we test the registration and creation of images
32 """
33
34 @classmethod
35 def setUpClass(cls):
36 cls.os = clients.Manager()
37 cls.client = cls.os.image_client_v2
38 cls.created_images = []
39
40 @classmethod
41 def tearDownClass(cls):
42 for image_id in cls.created_images:
43 cls.client.delete(image_id)
44
45 @attr(type='negative')
46 def test_register_with_invalid_container_format(self):
47 # Negative tests for invalid data supplied to POST /images
48 self.assertRaises(exceptions.BadRequest, self.client.create_image,
49 'test', 'wrong', 'vhd')
50
51 @attr(type='negative')
52 def test_register_with_invalid_disk_format(self):
53 self.assertRaises(exceptions.BadRequest, self.client.create_image,
54 'test', 'bare', 'wrong')
55
56 @attr(type='image')
57 def test_register_then_upload(self):
58 # Register, then upload an image
59 resp, body = self.client.create_image('New Name', 'bare', 'raw',
60 is_public=True)
61 self.assertTrue('id' in body)
62 image_id = body.get('id')
63 self.created_images.append(image_id)
64 self.assertTrue('name' in body)
65 self.assertEqual('New Name', body.get('name'))
66 self.assertTrue('visibility' in body)
67 self.assertTrue(body.get('visibility') == 'public')
68 self.assertTrue('status' in body)
69 self.assertEqual('queued', body.get('status'))
70
71 # Now try uploading an image file
72 image_file = StringIO.StringIO(('*' * 1024))
73 resp, body = self.client.store_image(image_id, image_file)
74 self.assertEqual(resp.status, 204)
75 resp, body = self.client.get_image_metadata(image_id)
76 self.assertTrue('size' in body)
77 self.assertEqual(1024, body.get('size'))
78
79
80class ListImagesTest(tempest.test.BaseTestCase):
81
82 """
83 Here we test the listing of image information
84 """
85
86 @classmethod
87 def setUpClass(cls):
88 cls.os = clients.Manager()
89 cls.client = cls.os.image_client_v2
90 cls.created_images = []
91
92 # We add a few images here to test the listing functionality of
93 # the images API
94 for x in xrange(0, 10):
95 cls.created_images.append(cls._create_standard_image(x))
96
97 @classmethod
98 def tearDownClass(cls):
99 for image_id in cls.created_images:
100 cls.client.delete_image(image_id)
101 cls.client.wait_for_resource_deletion(image_id)
102
103 @classmethod
104 def _create_standard_image(cls, number):
105 """
106 Create a new standard image and return the ID of the newly-registered
107 image. Note that the size of the new image is a random number between
108 1024 and 4096
109 """
110 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
111 name = 'New Standard Image %s' % number
112 resp, body = cls.client.create_image(name, 'bare', 'raw',
113 is_public=True)
114 image_id = body['id']
115 resp, body = cls.client.store_image(image_id, data=image_file)
116
117 return image_id
118
119 @attr(type='image')
120 def test_index_no_params(self):
121 # Simple test to see all fixture images returned
122 resp, images_list = self.client.image_list()
123 self.assertEqual(resp['status'], '200')
124 image_list = map(lambda x: x['id'], images_list)
125 for image in self.created_images:
126 self.assertTrue(image in image_list)