unittest image register (basic functionality)
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 30 Oct 2012 14:23:25 +0000 (16:23 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 30 Oct 2012 14:23:25 +0000 (16:23 +0200)
kamaki/clients/__init__.py
kamaki/clients/image.py
kamaki/clients/tests.py

index d487353..a48560e 100644 (file)
@@ -58,12 +58,13 @@ class Client(object):
         self.http_client = http_client
 
     def _raise_for_status(self, r):
-        message = "%d %s" % (r.status_code, r.status)
+        message = "%s" % r.status
         try:
             details = r.text
         except:
             details = ''
-        raise ClientError(message, r.status_code, details)
+        print('RAISE?')
+        raise ClientError(message=message, status=r.status_code, details=details)
 
     def set_header(self, name, value, iff=True):
         """Set a header 'name':'value' provided value is not None and iff is True"""
index 84b3cd1..94606ee 100644 (file)
@@ -97,7 +97,17 @@ class ImageClient(Client):
         for key, val in properties.items():
             self.set_header('X-Image-Meta-Property-'+key, val)
 
-        self.post(path, success=200)
+        try:
+            r = self.post(path, success=200)
+        except ClientError as err:
+            try:
+                prefix, suffix = err.details.split('File not found')
+                details = '%s Location %s not found %s'%(prefix, location, suffix)
+                raise ClientError(err.message, err.status, details)
+            except ValueError:
+                pass
+            raise
+        r.release()
 
     def list_members(self, image_id):
         path = path4url('images',image_id,'members')
index f5dfb3d..1b6252e 100644 (file)
@@ -68,13 +68,29 @@ class testAstakos(unittest.TestCase):
 
 class testImage(unittest.TestCase):
        def setUp(self):
+               cyclades_url = 'https://cyclades.okeanos.grnet.gr/api/v1.1'
                url = 'https://cyclades.okeanos.grnet.gr/plankton'
                token = 'Kn+G9dfmlPLR2WFnhfBOow=='
                self.imgid = 'b2dffe52-64a4-48c3-8a4c-8214cc3165cf'
+               self.now = time.mktime(time.gmtime())
+               self.imgname = 'img_%s'%self.now
+               self.imglocation = 'pithos://saxtouri@grnet.gr/pithos/my.img'
                self.client = image(url, token)
+               self.cyclades = cyclades(cyclades_url, token)
+
+               self._imglist={}
 
        def tearDown(self):
-               pass
+               for img in self._imglist.values():
+                       print('\tDelete img %s (%s)'%(img['name'], img['id']))
+                       self.cyclades.delete_image(img['id'])
+
+       def _get_img_by_name(self, name):
+               r = self.cyclades.list_images()
+               for img in r:
+                       if img['name'] == name:
+                               return img
+               return None
 
        def assert_dicts_are_deeply_equal(self, d1, d2):
                for k,v in d1.items():
@@ -160,6 +176,14 @@ class testImage(unittest.TestCase):
                                'description'):
                                self.assertTrue(r['properties'].has_key(interm))
 
+       def test_register(self):
+               """Test register"""
+               print('// register %s %s --public'%(self.imgname, self.imglocation))
+               self.client.register(self.imgname, self.imglocation, params=dict(is_public=True))
+               img = self._get_img_by_name(self.imgname)
+               self.assertTrue(img != None)
+               self._imglist[self.imgname]=img
+
 class testCyclades(unittest.TestCase):
        """Set up a Cyclades thorough test"""
        def setUp(self):