Explorar o código

Fixes for GCE object store tests

Enis Afgan %!s(int64=7) %!d(string=hai) anos
pai
achega
6a9bea5d98

+ 4 - 4
cloudbridge/cloud/interfaces/resources.py

@@ -2336,11 +2336,11 @@ class BucketObject(CloudResource):
     @abstractmethod
     def generate_url(self, expires_in):
         """
-        Generate a URL to this object.
+        Generate a signed URL to this object.
 
-        If the object is public, `expires_in` argument is not necessary, but if
-        the object is private, the lifetime of URL is set using `expires_in`
-        argument.
+        A signed URL associated with an object gives time-limited read access
+        to that specific object. Anyone in possession of the URL has the access
+        granted by the URL.
 
         :type expires_in: ``int``
         :param expires_in: Time to live of the generated URL in seconds.

+ 9 - 7
cloudbridge/cloud/providers/gce/resources.py

@@ -2301,6 +2301,8 @@ class GCSObject(BaseBucketObject):
         """
         Set the contents of this object to the given text.
         """
+        if type(data) is str:
+            data = data.encode()
         media_body = googleapiclient.http.MediaIoBaseUpload(
                 io.BytesIO(data), mimetype='plain/text')
         response = self._bucket.create_object_with_media_body(self.name,
@@ -2327,15 +2329,15 @@ class GCSObject(BaseBucketObject):
              .delete(bucket=self._obj['bucket'], object=self.name)
              .execute())
 
-    def generate_url(self, expires_in=0):
+    def generate_url(self, expires_in):
         """
         Generates a signed URL accessible to everyone.
         """
-        expiration = calendar.timegm(time.gmtime()) + 2 * 24 * 60 * 60
-        signature = self._provider.sign_blob(
-                'GET\n\n\n%d\n/%s/%s' %
-                (expiration, self._obj['bucket'], self.name))
-        encoded_signature = base64.b64encode(signature)
+        expiration = calendar.timegm(time.gmtime()) + expires_in
+        signed_signature = self._provider.sign_blob(
+            'GET\n\n\n%d\n/%s/%s' %
+            (expiration, self._obj['bucket'], self.name))
+        encoded_signature = base64.b64encode(signed_signature).decode("utf-8")
         url_encoded_signature = (encoded_signature.replace('+', '%2B')
                                                   .replace('/', '%2F'))
         return ('https://storage.googleapis.com/%s/%s?GoogleAccessId=%s'
@@ -2439,7 +2441,7 @@ class GCSBucket(BaseBucket):
         response = self.create_object_with_media_body(
             name,
             googleapiclient.http.MediaIoBaseUpload(
-                    io.BytesIO(''), mimetype='plain/text'))
+                    io.BytesIO(b''), mimetype='plain/text'))
         return GCSObject(self._provider, self, response) if response else None
 
     def create_object_with_media_body(self, name, media_body):

+ 0 - 3
cloudbridge/cloud/providers/gce/services.py

@@ -1120,9 +1120,6 @@ class GCSBucketService(BaseBucketService):
                                      False, data=buckets)
 
     def create(self, name, location=None):
-        """
-        Create a new bucket and returns it. Returns None if creation fails.
-        """
         GCSBucket.assert_valid_resource_name(name)
         body = {'name': name}
         if location:

+ 1 - 1
test/test_object_store_service.py

@@ -189,7 +189,7 @@ class CloudObjectStoreServiceTestCase(ProviderTestBase):
                 with open(test_file, 'rb') as f:
                     self.assertEqual(target_stream.getvalue(), f.read())
 
-    @skip("Skip unless you want to test swift objects bigger than 5 Gig")
+    @skip("Skip unless you want to test objects bigger than 5GB")
     @helpers.skipIfNoService(['storage.buckets'])
     def test_upload_download_bucket_content_with_large_file(self):
         # Creates a 6 Gig file in the temp directory, then uploads it to