OLD | NEW |
(Empty) | |
| 1 # -*- coding: utf-8 -*- |
| 2 # Copyright (c) 2013, Google, Inc. |
| 3 # All rights reserved. |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 |
| 24 """Base TestCase class for gs integration tests.""" |
| 25 |
| 26 import shutil |
| 27 import tempfile |
| 28 import time |
| 29 |
| 30 from boto.exception import GSResponseError |
| 31 from boto.gs.connection import GSConnection |
| 32 from tests.integration.gs.util import has_google_credentials |
| 33 from tests.integration.gs.util import retry |
| 34 from tests.unit import unittest |
| 35 |
| 36 @unittest.skipUnless(has_google_credentials(), |
| 37 "Google credentials are required to run the Google " |
| 38 "Cloud Storage tests. Update your boto.cfg to run " |
| 39 "these tests.") |
| 40 class GSTestCase(unittest.TestCase): |
| 41 gs = True |
| 42 |
| 43 def setUp(self): |
| 44 self._conn = GSConnection() |
| 45 self._buckets = [] |
| 46 self._tempdirs = [] |
| 47 |
| 48 # Retry with an exponential backoff if a server error is received. This |
| 49 # ensures that we try *really* hard to clean up after ourselves. |
| 50 @retry(GSResponseError) |
| 51 def tearDown(self): |
| 52 while len(self._tempdirs): |
| 53 tmpdir = self._tempdirs.pop() |
| 54 shutil.rmtree(tmpdir, ignore_errors=True) |
| 55 |
| 56 while(len(self._buckets)): |
| 57 b = self._buckets[-1] |
| 58 bucket = self._conn.get_bucket(b) |
| 59 while len(list(bucket.list_versions())) > 0: |
| 60 for k in bucket.list_versions(): |
| 61 bucket.delete_key(k.name, generation=k.generation) |
| 62 bucket.delete() |
| 63 self._buckets.pop() |
| 64 |
| 65 def _GetConnection(self): |
| 66 """Returns the GSConnection object used to connect to GCS.""" |
| 67 return self._conn |
| 68 |
| 69 def _MakeTempName(self): |
| 70 """Creates and returns a temporary name for testing that is likely to be |
| 71 unique.""" |
| 72 return "boto-gs-test-%s" % repr(time.time()).replace(".", "-") |
| 73 |
| 74 def _MakeBucketName(self): |
| 75 """Creates and returns a temporary bucket name for testing that is |
| 76 likely to be unique.""" |
| 77 b = self._MakeTempName() |
| 78 self._buckets.append(b) |
| 79 return b |
| 80 |
| 81 def _MakeBucket(self): |
| 82 """Creates and returns temporary bucket for testing. After the test, the |
| 83 contents of the bucket and the bucket itself will be deleted.""" |
| 84 b = self._conn.create_bucket(self._MakeBucketName()) |
| 85 return b |
| 86 |
| 87 def _MakeKey(self, data='', bucket=None, set_contents=True): |
| 88 """Creates and returns a Key with provided data. If no bucket is given, |
| 89 a temporary bucket is created.""" |
| 90 if data and not set_contents: |
| 91 # The data and set_contents parameters are mutually exclusive. |
| 92 raise ValueError('MakeKey called with a non-empty data parameter ' |
| 93 'but set_contents was set to False.') |
| 94 if not bucket: |
| 95 bucket = self._MakeBucket() |
| 96 key_name = self._MakeTempName() |
| 97 k = bucket.new_key(key_name) |
| 98 if set_contents: |
| 99 k.set_contents_from_string(data) |
| 100 return k |
| 101 |
| 102 def _MakeVersionedBucket(self): |
| 103 """Creates and returns temporary versioned bucket for testing. After the |
| 104 test, the contents of the bucket and the bucket itself will be |
| 105 deleted.""" |
| 106 b = self._MakeBucket() |
| 107 b.configure_versioning(True) |
| 108 return b |
| 109 |
| 110 def _MakeTempDir(self): |
| 111 """Creates and returns a temporary directory on disk. After the test, |
| 112 the contents of the directory and the directory itself will be |
| 113 deleted.""" |
| 114 tmpdir = tempfile.mkdtemp(prefix=self._MakeTempName()) |
| 115 self._tempdirs.append(tmpdir) |
| 116 return tmpdir |
OLD | NEW |