Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2011 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | """Validate a working Glance deployment""" |
| 18 | |
| 19 | import httplib2 |
| 20 | import json |
| 21 | import os |
| 22 | from pprint import pprint |
| 23 | |
Soren Hansen | ec3f709 | 2011-09-08 13:03:42 +0200 | [diff] [blame^] | 24 | from kong import tests |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 25 | |
| 26 | |
| 27 | class TestGlanceAPI(tests.FunctionalTest): |
| 28 | def test_001_connect_to_glance_api(self): |
| 29 | """ |
| 30 | Verifies ability to connect to glance api, |
| 31 | expects glance to return an empty set |
| 32 | """ |
| 33 | if 'apiver' in self.glance: |
| 34 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 35 | self.glance['port'], self.glance['apiver']) |
| 36 | else: |
| 37 | path = "http://%s:%s/images" % (self.glance['host'], |
| 38 | self.glance['port']) |
| 39 | http = httplib2.Http() |
| 40 | response, content = http.request(path, 'GET') |
| 41 | self.assertEqual(200, response.status) |
| 42 | data = json.loads(content) |
| 43 | self.assertTrue('images' in data) |
| 44 | test_001_connect_to_glance_api.tags = ['glance'] |
| 45 | |
| 46 | def test_002_upload_kernel_to_glance(self): |
| 47 | """ |
| 48 | Uploads a test kernal to glance api |
| 49 | """ |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 50 | kernel = self.config['environment']['kernel'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 51 | if 'apiver' in self.glance: |
| 52 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 53 | self.glance['port'], self.glance['apiver']) |
| 54 | else: |
| 55 | path = "http://%s:%s/images" % (self.glance['host'], |
| 56 | self.glance['port']) |
| 57 | headers = {'x-image-meta-is-public': 'true', |
| 58 | 'x-image-meta-name': 'test-kernel', |
| 59 | 'x-image-meta-disk-format': 'aki', |
| 60 | 'x-image-meta-container-format': 'aki', |
| 61 | 'Content-Length': '%d' % os.path.getsize(kernel), |
| 62 | 'Content-Type': 'application/octet-stream'} |
| 63 | image_file = open(kernel, "rb") |
| 64 | http = httplib2.Http() |
| 65 | response, content = http.request(path, 'POST', |
| 66 | headers=headers, |
| 67 | body=image_file) |
| 68 | image_file.close() |
| 69 | self.assertEqual(201, response.status) |
| 70 | data = json.loads(content) |
| 71 | self.glance['kernel_id'] = data['image']['id'] |
| 72 | self.assertEqual(data['image']['name'], "test-kernel") |
| 73 | self.assertEqual(data['image']['checksum'], self._md5sum_file(kernel)) |
| 74 | test_002_upload_kernel_to_glance.tags = ['glance', 'nova'] |
| 75 | |
| 76 | def test_003_upload_initrd_to_glance(self): |
| 77 | """ |
| 78 | Uploads a test initrd to glance api |
| 79 | """ |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 80 | initrd = self.config['environment']['initrd'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 81 | if 'apiver' in self.glance: |
| 82 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 83 | self.glance['port'], self.glance['apiver']) |
| 84 | else: |
| 85 | path = "http://%s:%s/images" % (self.glance['host'], |
| 86 | self.glance['port']) |
| 87 | headers = {'x-image-meta-is-public': 'true', |
| 88 | 'x-image-meta-name': 'test-ramdisk', |
| 89 | 'x-image-meta-disk-format': 'ari', |
| 90 | 'x-image-meta-container-format': 'ari', |
| 91 | 'Content-Length': '%d' % os.path.getsize(initrd), |
| 92 | 'Content-Type': 'application/octet-stream'} |
| 93 | image_file = open(initrd, "rb") |
| 94 | http = httplib2.Http() |
| 95 | response, content = http.request(path, |
| 96 | 'POST', |
| 97 | headers=headers, |
| 98 | body=image_file) |
| 99 | image_file.close() |
| 100 | self.assertEqual(201, response.status) |
| 101 | data = json.loads(content) |
| 102 | self.glance['ramdisk_id'] = data['image']['id'] |
| 103 | self.assertEqual(data['image']['name'], "test-ramdisk") |
| 104 | self.assertEqual(data['image']['checksum'], self._md5sum_file(initrd)) |
| 105 | test_003_upload_initrd_to_glance.tags = ['glance', 'nova'] |
| 106 | |
| 107 | def test_004_upload_image_to_glance(self): |
| 108 | """ |
| 109 | Uploads a test image to glance api, and |
| 110 | links it to the initrd and kernel uploaded |
| 111 | earlier |
| 112 | """ |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 113 | image = self.config['environment']['image'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 114 | upload_data = "" |
| 115 | for chunk in self._read_in_chunks(image): |
| 116 | upload_data += chunk |
| 117 | if 'apiver' in self.glance: |
| 118 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 119 | self.glance['port'], self.glance['apiver']) |
| 120 | else: |
| 121 | path = "http://%s:%s/images" % (self.glance['host'], |
| 122 | self.glance['port']) |
| 123 | headers = {'x-image-meta-is-public': 'true', |
| 124 | 'x-image-meta-name': 'test-image', |
| 125 | 'x-image-meta-disk-format': 'ami', |
| 126 | 'x-image-meta-container-format': 'ami', |
| 127 | 'x-image-meta-property-Kernel_id': '%s' % \ |
| 128 | self.glance['kernel_id'], |
| 129 | 'x-image-meta-property-Ramdisk_id': '%s' % \ |
| 130 | self.glance['ramdisk_id'], |
| 131 | 'Content-Length': '%d' % os.path.getsize(image), |
| 132 | 'Content-Type': 'application/octet-stream'} |
| 133 | http = httplib2.Http() |
| 134 | response, content = http.request(path, 'POST', |
| 135 | headers=headers, |
| 136 | body=upload_data) |
| 137 | self.assertEqual(201, response.status) |
| 138 | data = json.loads(content) |
| 139 | self.glance['image_id'] = data['image']['id'] |
| 140 | self.assertEqual(data['image']['name'], "test-image") |
| 141 | self.assertEqual(data['image']['checksum'], self._md5sum_file(image)) |
| 142 | test_004_upload_image_to_glance.tags = ['glance', 'nova'] |
| 143 | |
| 144 | def test_005_set_image_meta_property(self): |
| 145 | if 'apiver' in self.glance: |
| 146 | path = "http://%s:%s/%s/images/%s" % (self.glance['host'], |
| 147 | self.glance['port'], self.glance['apiver'], |
| 148 | self.glance['image_id']) |
| 149 | else: |
| 150 | path = "http://%s:%s/images/%s" % (self.glance['host'], |
| 151 | self.glance['port'], self.glance['image_id']) |
| 152 | headers = {'X-Image-Meta-Property-Distro': 'Ubuntu', |
| 153 | 'X-Image-Meta-Property-Arch': 'x86_64', |
| 154 | 'X-Image-Meta-Property-Kernel_id': '%s' % \ |
| 155 | self.glance['kernel_id'], |
| 156 | 'X-Image-Meta-Property-Ramdisk_id': '%s' % \ |
| 157 | self.glance['ramdisk_id']} |
| 158 | http = httplib2.Http() |
| 159 | response, content = http.request(path, 'PUT', headers=headers) |
| 160 | self.assertEqual(response.status, 200) |
| 161 | data = json.loads(content) |
| 162 | self.assertEqual(data['image']['properties']['arch'], "x86_64") |
| 163 | self.assertEqual(data['image']['properties']['distro'], "Ubuntu") |
| 164 | self.assertEqual(data['image']['properties']['kernel_id'], |
| 165 | str(self.glance['kernel_id'])) |
| 166 | self.assertEqual(data['image']['properties']['ramdisk_id'], |
| 167 | str(self.glance['ramdisk_id'])) |
| 168 | test_005_set_image_meta_property.tags = ['glance'] |
| 169 | |
| 170 | def test_006_list_image_metadata(self): |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 171 | image = self.config['environment']['image'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 172 | if 'apiver' in self.glance: |
| 173 | path = "http://%s:%s/%s/images/%s" % (self.glance['host'], |
| 174 | self.glance['port'], self.glance['apiver'], |
| 175 | self.glance['image_id']) |
| 176 | else: |
| 177 | path = "http://%s:%s/images/%s" % (self.glance['host'], |
| 178 | self.glance['port'], self.glance['image_id']) |
| 179 | http = httplib2.Http() |
| 180 | response, content = http.request(path, 'HEAD') |
| 181 | self.assertEqual(response.status, 200) |
| 182 | self.assertEqual(response['x-image-meta-name'], "test-image") |
| 183 | self.assertEqual(response['x-image-meta-checksum'], |
| 184 | self._md5sum_file(image)) |
| 185 | self.assertEqual(response['x-image-meta-container_format'], "ami") |
| 186 | self.assertEqual(response['x-image-meta-disk_format'], "ami") |
| 187 | self.assertEqual(response['x-image-meta-property-arch'], "x86_64") |
| 188 | self.assertEqual(response['x-image-meta-property-distro'], "Ubuntu") |
| 189 | self.assertEqual(response['x-image-meta-property-kernel_id'], |
| 190 | str(self.glance['kernel_id'])) |
| 191 | self.assertEqual(response['x-image-meta-property-ramdisk_id'], |
| 192 | str(self.glance['ramdisk_id'])) |
| 193 | test_006_list_image_metadata.tags = ['glance'] |