Updating images tests

- Consolidating glance/images test modules.
- Consolidating all glance tests into a single ami-specific test
- Test now cleans up images it pushes into glance
- Updating links for sample ami-style image
- Updating config variables for ami-style image pieces

Change-Id: Ie0aaec36eca8425b687740b289fce6964b13fdbd
diff --git a/kong/tests/test_images.py b/kong/tests/test_images.py
index d8a4721..1163723 100644
--- a/kong/tests/test_images.py
+++ b/kong/tests/test_images.py
@@ -1,3 +1,4 @@
+import httplib2
 import json
 import os
 import re
@@ -6,10 +7,10 @@
 from kong import tests
 
 
-class ImagesTest(tests.FunctionalTest):
+class TestImagesThroughCompute(tests.FunctionalTest):
 
     def setUp(self):
-        super(ImagesTest, self).setUp()
+        super(TestImagesThroughCompute, self).setUp()
         self.os = openstack.Manager(self.nova)
 
     def _assert_image_links(self, image):
@@ -82,3 +83,125 @@
         for image in resp_body['images']:
             self._assert_image_entity_detailed(image)
     test_detail.tags = ['nova', 'glance']
+
+
+class TestGlanceAPI(tests.FunctionalTest):
+
+    def setUp(self):
+        super(TestGlanceAPI, self).setUp()
+        self.base_url = "http://%s:%s/%s/images" % (self.glance['host'],
+		                                    self.glance['port'],
+                                                    self.glance['apiver'])
+
+    def test_upload_ami_style_image(self):
+        """Uploads a three-part ami-style image"""
+        aki_location = self.config['environment']['aki_location']
+        headers = {'x-image-meta-is-public': 'true',
+                   'x-image-meta-name': 'test-kernel',
+                   'x-image-meta-disk-format': 'aki',
+                   'x-image-meta-container-format': 'aki',
+                   'Content-Length': '%d' % os.path.getsize(aki_location),
+                   'Content-Type': 'application/octet-stream'}
+        image_file = open(aki_location, "rb")
+        http = httplib2.Http()
+        response, content = http.request(self.base_url, 'POST',
+                                         headers=headers,body=image_file)
+        image_file.close()
+        self.assertEqual(201, response.status)
+        data = json.loads(content)
+        self.assertEqual(data['image']['name'], "test-kernel")
+        self.assertEqual(data['image']['checksum'],
+                         self._md5sum_file(aki_location))
+        kernel_id = data['image']['id']
+
+        ari_location = self.config['environment'].get('ari_location')
+        if ari_location:
+            headers = {'x-image-meta-is-public': 'true',
+                       'x-image-meta-name': 'test-ramdisk',
+                       'x-image-meta-disk-format': 'ari',
+                       'x-image-meta-container-format': 'ari',
+                       'Content-Length': '%d' % os.path.getsize(ari_location),
+                       'Content-Type': 'application/octet-stream'}
+            image_file = open(ari_location, "rb")
+            http = httplib2.Http()
+            response, content = http.request(self.base_url, 'POST',
+                                             headers=headers, body=image_file)
+            image_file.close()
+            self.assertEqual(201, response.status)
+            data = json.loads(content)
+            self.assertEqual(data['image']['name'], "test-ramdisk")
+            self.assertEqual(data['image']['checksum'],
+                             self._md5sum_file(ari_location))
+            ramdisk_id = data['image']['id']
+        else:
+            ramdisk_id = None
+
+        ami_location = self.config['environment']['ami_location']
+        upload_data = ""
+        for chunk in self._read_in_chunks(ami_location):
+            upload_data += chunk
+        headers = {'x-image-meta-is-public': 'true',
+                   'x-image-meta-name': 'test-image',
+                   'x-image-meta-disk-format': 'ami',
+                   'x-image-meta-container-format': 'ami',
+                   'x-image-meta-property-kernel_id': kernel_id,
+                   'Content-Length': '%d' % os.path.getsize(ami_location),
+                   'Content-Type': 'application/octet-stream'}
+
+        if ari_location:
+            headers['x-image-meta-property-ramdisk_id'] = ramdisk_id
+
+        http = httplib2.Http()
+        response, content = http.request(self.base_url, 'POST',
+                                         headers=headers, body=upload_data)
+        self.assertEqual(201, response.status)
+        data = json.loads(content)
+        self.assertEqual(data['image']['name'], "test-image")
+        self.assertEqual(data['image']['checksum'], 
+                         self._md5sum_file(ami_location))
+        machine_id = data['image']['id']
+
+        # now ensure we can modify the image properties
+        headers = {'X-Image-Meta-Property-distro': 'Ubuntu',
+                   'X-Image-Meta-Property-arch': 'x86_64',
+                   'X-Image-Meta-Property-kernel_id': kernel_id}
+        if ari_location:
+            headers['X-Image-Meta-Property-ramdisk_id'] = ramdisk_id
+
+        http = httplib2.Http()
+        url = '%s/%s' % (self.base_url, machine_id)
+        response, content = http.request(url, 'PUT', headers=headers)
+        self.assertEqual(response.status, 200)
+        data = json.loads(content)
+        properties = data['image']['properties']
+        self.assertEqual(properties['arch'], "x86_64")
+        self.assertEqual(properties['distro'], "Ubuntu")
+        self.assertEqual(properties['kernel_id'], kernel_id)
+        if ari_location:
+            self.assertEqual(properties['ramdisk_id'], ramdisk_id)
+
+        # list the metadata to ensure the new values stuck
+        http = httplib2.Http()
+        response, content = http.request(url, 'HEAD')
+        self.assertEqual(response.status, 200)
+        self.assertEqual(response['x-image-meta-name'], "test-image")
+        self.assertEqual(response['x-image-meta-checksum'],
+                         self._md5sum_file(ami_location))
+        self.assertEqual(response['x-image-meta-container_format'], "ami")
+        self.assertEqual(response['x-image-meta-disk_format'], "ami")
+        self.assertEqual(response['x-image-meta-property-arch'], "x86_64")
+        self.assertEqual(response['x-image-meta-property-distro'], "Ubuntu")
+        self.assertEqual(response['x-image-meta-property-kernel_id'],
+                         kernel_id)
+        if ari_location:
+            self.assertEqual(response['x-image-meta-property-ramdisk_id'],
+                             ramdisk_id)
+
+        # delete images for which we have non-None ids
+        delete_ids = filter(lambda x: x, (kernel_id, ramdisk_id, machine_id))
+        for image_id in delete_ids:
+            http = httplib2.Http()
+            url = '%s/%s' % (self.base_url, image_id)
+            response, content = http.request(url, 'DELETE')
+
+    test_upload_ami_style_image.tags = ['glance']