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 | """ |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 80 | if not 'initrd' in self.config['environment']: |
| 81 | self.glance['ramdisk_id'] = None |
| 82 | return |
| 83 | |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 84 | initrd = self.config['environment']['initrd'] |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 85 | |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 86 | if 'apiver' in self.glance: |
| 87 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 88 | self.glance['port'], self.glance['apiver']) |
| 89 | else: |
| 90 | path = "http://%s:%s/images" % (self.glance['host'], |
| 91 | self.glance['port']) |
| 92 | headers = {'x-image-meta-is-public': 'true', |
| 93 | 'x-image-meta-name': 'test-ramdisk', |
| 94 | 'x-image-meta-disk-format': 'ari', |
| 95 | 'x-image-meta-container-format': 'ari', |
| 96 | 'Content-Length': '%d' % os.path.getsize(initrd), |
| 97 | 'Content-Type': 'application/octet-stream'} |
| 98 | image_file = open(initrd, "rb") |
| 99 | http = httplib2.Http() |
| 100 | response, content = http.request(path, |
| 101 | 'POST', |
| 102 | headers=headers, |
| 103 | body=image_file) |
| 104 | image_file.close() |
| 105 | self.assertEqual(201, response.status) |
| 106 | data = json.loads(content) |
| 107 | self.glance['ramdisk_id'] = data['image']['id'] |
| 108 | self.assertEqual(data['image']['name'], "test-ramdisk") |
| 109 | self.assertEqual(data['image']['checksum'], self._md5sum_file(initrd)) |
| 110 | test_003_upload_initrd_to_glance.tags = ['glance', 'nova'] |
| 111 | |
| 112 | def test_004_upload_image_to_glance(self): |
| 113 | """ |
| 114 | Uploads a test image to glance api, and |
| 115 | links it to the initrd and kernel uploaded |
| 116 | earlier |
| 117 | """ |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 118 | image = self.config['environment']['image'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 119 | upload_data = "" |
| 120 | for chunk in self._read_in_chunks(image): |
| 121 | upload_data += chunk |
| 122 | if 'apiver' in self.glance: |
| 123 | path = "http://%s:%s/%s/images" % (self.glance['host'], |
| 124 | self.glance['port'], self.glance['apiver']) |
| 125 | else: |
| 126 | path = "http://%s:%s/images" % (self.glance['host'], |
| 127 | self.glance['port']) |
| 128 | headers = {'x-image-meta-is-public': 'true', |
| 129 | 'x-image-meta-name': 'test-image', |
| 130 | 'x-image-meta-disk-format': 'ami', |
| 131 | 'x-image-meta-container-format': 'ami', |
| 132 | 'x-image-meta-property-Kernel_id': '%s' % \ |
| 133 | self.glance['kernel_id'], |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 134 | 'Content-Length': '%d' % os.path.getsize(image), |
| 135 | 'Content-Type': 'application/octet-stream'} |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 136 | |
| 137 | if self.glance['ramdisk_id']: |
| 138 | ramdisk_id = '%s' % self.glance['ramdisk_id'] |
| 139 | headers['x-image-meta-property-Ramdisk_id'] = ramdisk_id |
| 140 | |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 141 | http = httplib2.Http() |
| 142 | response, content = http.request(path, 'POST', |
| 143 | headers=headers, |
| 144 | body=upload_data) |
| 145 | self.assertEqual(201, response.status) |
| 146 | data = json.loads(content) |
| 147 | self.glance['image_id'] = data['image']['id'] |
| 148 | self.assertEqual(data['image']['name'], "test-image") |
| 149 | self.assertEqual(data['image']['checksum'], self._md5sum_file(image)) |
| 150 | test_004_upload_image_to_glance.tags = ['glance', 'nova'] |
| 151 | |
| 152 | def test_005_set_image_meta_property(self): |
| 153 | if 'apiver' in self.glance: |
| 154 | path = "http://%s:%s/%s/images/%s" % (self.glance['host'], |
| 155 | self.glance['port'], self.glance['apiver'], |
| 156 | self.glance['image_id']) |
| 157 | else: |
| 158 | path = "http://%s:%s/images/%s" % (self.glance['host'], |
| 159 | self.glance['port'], self.glance['image_id']) |
| 160 | headers = {'X-Image-Meta-Property-Distro': 'Ubuntu', |
| 161 | 'X-Image-Meta-Property-Arch': 'x86_64', |
| 162 | 'X-Image-Meta-Property-Kernel_id': '%s' % \ |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 163 | self.glance['kernel_id']} |
| 164 | |
| 165 | if self.glance['ramdisk_id']: |
| 166 | ramdisk_id = '%s' % self.glance['ramdisk_id'] |
| 167 | headers['X-Image-Meta-Property-Ramdisk_id'] = ramdisk_id |
| 168 | |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 169 | http = httplib2.Http() |
| 170 | response, content = http.request(path, 'PUT', headers=headers) |
| 171 | self.assertEqual(response.status, 200) |
| 172 | data = json.loads(content) |
| 173 | self.assertEqual(data['image']['properties']['arch'], "x86_64") |
| 174 | self.assertEqual(data['image']['properties']['distro'], "Ubuntu") |
| 175 | self.assertEqual(data['image']['properties']['kernel_id'], |
| 176 | str(self.glance['kernel_id'])) |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 177 | if self.glance['ramdisk_id']: |
| 178 | self.assertEqual(data['image']['properties']['ramdisk_id'], |
| 179 | str(self.glance['ramdisk_id'])) |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 180 | test_005_set_image_meta_property.tags = ['glance'] |
| 181 | |
| 182 | def test_006_list_image_metadata(self): |
Justin Shepherd | 66c861a | 2011-08-18 11:02:53 -0500 | [diff] [blame] | 183 | image = self.config['environment']['image'] |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 184 | if 'apiver' in self.glance: |
| 185 | path = "http://%s:%s/%s/images/%s" % (self.glance['host'], |
| 186 | self.glance['port'], self.glance['apiver'], |
| 187 | self.glance['image_id']) |
| 188 | else: |
| 189 | path = "http://%s:%s/images/%s" % (self.glance['host'], |
| 190 | self.glance['port'], self.glance['image_id']) |
| 191 | http = httplib2.Http() |
| 192 | response, content = http.request(path, 'HEAD') |
| 193 | self.assertEqual(response.status, 200) |
| 194 | self.assertEqual(response['x-image-meta-name'], "test-image") |
| 195 | self.assertEqual(response['x-image-meta-checksum'], |
| 196 | self._md5sum_file(image)) |
| 197 | self.assertEqual(response['x-image-meta-container_format'], "ami") |
| 198 | self.assertEqual(response['x-image-meta-disk_format'], "ami") |
| 199 | self.assertEqual(response['x-image-meta-property-arch'], "x86_64") |
| 200 | self.assertEqual(response['x-image-meta-property-distro'], "Ubuntu") |
| 201 | self.assertEqual(response['x-image-meta-property-kernel_id'], |
| 202 | str(self.glance['kernel_id'])) |
Soren Hansen | 1e9110f | 2011-09-09 14:15:09 +0200 | [diff] [blame] | 203 | if self.glance['ramdisk_id']: |
| 204 | self.assertEqual(response['x-image-meta-property-ramdisk_id'], |
| 205 | str(self.glance['ramdisk_id'])) |
Justin Shepherd | 0d9bbd1 | 2011-08-11 12:57:44 -0500 | [diff] [blame] | 206 | test_006_list_image_metadata.tags = ['glance'] |