| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 # | |
| 3 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 4 # | 3 # |
| 5 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 # you may not use this file except in compliance with the License. | 5 # you may not use this file except in compliance with the License. |
| 7 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 8 # | 7 # |
| 9 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 # | 9 # |
| 11 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 12 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 15 # limitations under the License. | 14 # limitations under the License. |
| 15 """Tests for ls command.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 16 | 18 |
| 17 import posixpath | 19 import posixpath |
| 18 import re | 20 import re |
| 19 import subprocess | 21 import subprocess |
| 20 import sys | 22 import sys |
| 21 | 23 |
| 22 import gslib | 24 import gslib |
| 25 from gslib.cs_api_map import ApiSelector |
| 23 import gslib.tests.testcase as testcase | 26 import gslib.tests.testcase as testcase |
| 27 from gslib.tests.testcase.integration_testcase import SkipForS3 |
| 28 from gslib.tests.util import ObjectToURI as suri |
| 29 from gslib.tests.util import unittest |
| 30 from gslib.util import IS_WINDOWS |
| 24 from gslib.util import Retry | 31 from gslib.util import Retry |
| 25 from gslib.tests.util import ObjectToURI as suri | 32 from gslib.util import UTF8 |
| 26 | 33 |
| 27 | 34 |
| 28 class TestLs(testcase.GsUtilIntegrationTestCase): | 35 class TestLs(testcase.GsUtilIntegrationTestCase): |
| 29 """Integration tests for ls command.""" | 36 """Integration tests for ls command.""" |
| 30 | 37 |
| 31 def test_blank_ls(self): | 38 def test_blank_ls(self): |
| 32 self.RunGsUtil(['ls']) | 39 self.RunGsUtil(['ls']) |
| 33 | 40 |
| 34 def test_empty_bucket(self): | 41 def test_empty_bucket(self): |
| 35 bucket_uri = self.CreateBucket() | 42 bucket_uri = self.CreateBucket() |
| 36 # Use @Retry as hedge against bucket listing eventual consistency. | 43 self.AssertNObjectsInBucket(bucket_uri, 0) |
| 37 @Retry(AssertionError, tries=3, timeout_secs=1) | |
| 38 def _Check1(): | |
| 39 stdout = self.RunGsUtil(['ls', suri(bucket_uri)], return_stdout=True) | |
| 40 self.assertEqual('', stdout) | |
| 41 _Check1() | |
| 42 | 44 |
| 43 def test_empty_bucket_with_b(self): | 45 def test_empty_bucket_with_b(self): |
| 44 bucket_uri = self.CreateBucket() | 46 bucket_uri = self.CreateBucket() |
| 45 # Use @Retry as hedge against bucket listing eventual consistency. | 47 # Use @Retry as hedge against bucket listing eventual consistency. |
| 46 @Retry(AssertionError, tries=3, timeout_secs=1) | 48 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 47 def _Check1(): | 49 def _Check1(): |
| 48 stdout = self.RunGsUtil(['ls', '-b', suri(bucket_uri)], | 50 stdout = self.RunGsUtil(['ls', '-b', suri(bucket_uri)], |
| 49 return_stdout=True) | 51 return_stdout=True) |
| 50 self.assertEqual('%s/\n' % suri(bucket_uri), stdout) | 52 self.assertEqual('%s/\n' % suri(bucket_uri), stdout) |
| 51 _Check1() | 53 _Check1() |
| 52 | 54 |
| 53 def test_bucket_with_Lb(self): | 55 def test_bucket_with_Lb(self): |
| 56 """Tests ls -Lb.""" |
| 54 bucket_uri = self.CreateBucket() | 57 bucket_uri = self.CreateBucket() |
| 55 # Use @Retry as hedge against bucket listing eventual consistency. | 58 # Use @Retry as hedge against bucket listing eventual consistency. |
| 56 @Retry(AssertionError, tries=3, timeout_secs=1) | 59 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 57 def _Check1(): | 60 def _Check1(): |
| 58 stdout = self.RunGsUtil(['ls', '-Lb', suri(bucket_uri)], | 61 stdout = self.RunGsUtil(['ls', '-Lb', suri(bucket_uri)], |
| 59 return_stdout=True) | 62 return_stdout=True) |
| 60 self.assertIn(suri(bucket_uri), stdout) | 63 self.assertIn(suri(bucket_uri), stdout) |
| 61 self.assertNotIn('TOTAL:', stdout) | 64 self.assertNotIn('TOTAL:', stdout) |
| 62 _Check1() | 65 _Check1() |
| 63 | 66 |
| 64 def test_bucket_with_lb(self): | 67 def test_bucket_with_lb(self): |
| 68 """Tests ls -lb.""" |
| 65 bucket_uri = self.CreateBucket() | 69 bucket_uri = self.CreateBucket() |
| 66 # Use @Retry as hedge against bucket listing eventual consistency. | 70 # Use @Retry as hedge against bucket listing eventual consistency. |
| 67 @Retry(AssertionError, tries=3, timeout_secs=1) | 71 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 68 def _Check1(): | 72 def _Check1(): |
| 69 stdout = self.RunGsUtil(['ls', '-lb', suri(bucket_uri)], | 73 stdout = self.RunGsUtil(['ls', '-lb', suri(bucket_uri)], |
| 70 return_stdout=True) | 74 return_stdout=True) |
| 71 self.assertIn(suri(bucket_uri), stdout) | 75 self.assertIn(suri(bucket_uri), stdout) |
| 72 self.assertNotIn('TOTAL:', stdout) | 76 self.assertNotIn('TOTAL:', stdout) |
| 73 _Check1() | 77 _Check1() |
| 74 | 78 |
| 75 def test_bucket_list_wildcard(self): | 79 def test_bucket_list_wildcard(self): |
| 80 """Tests listing multiple buckets with a wildcard.""" |
| 76 random_prefix = self.MakeRandomTestString() | 81 random_prefix = self.MakeRandomTestString() |
| 77 bucket1_name = self.MakeTempName('bucket', prefix=random_prefix) | 82 bucket1_name = self.MakeTempName('bucket', prefix=random_prefix) |
| 78 bucket2_name = self.MakeTempName('bucket', prefix=random_prefix) | 83 bucket2_name = self.MakeTempName('bucket', prefix=random_prefix) |
| 79 bucket1_uri = self.CreateBucket(bucket_name=bucket1_name) | 84 bucket1_uri = self.CreateBucket(bucket_name=bucket1_name) |
| 80 bucket2_uri = self.CreateBucket(bucket_name=bucket2_name) | 85 bucket2_uri = self.CreateBucket(bucket_name=bucket2_name) |
| 81 # This just double checks that the common prefix of the two buckets is what | 86 # This just double checks that the common prefix of the two buckets is what |
| 82 # we think it should be (based on implementation detail of CreateBucket). | 87 # we think it should be (based on implementation detail of CreateBucket). |
| 83 # We want to be careful when setting a wildcard on buckets to make sure we | 88 # We want to be careful when setting a wildcard on buckets to make sure we |
| 84 # don't step outside the test buckets to affect other buckets. | 89 # don't step outside the test buckets to affect other buckets. |
| 85 common_prefix = posixpath.commonprefix([suri(bucket1_uri), | 90 common_prefix = posixpath.commonprefix([suri(bucket1_uri), |
| 86 suri(bucket2_uri)]) | 91 suri(bucket2_uri)]) |
| 87 self.assertTrue(common_prefix.startswith( | 92 self.assertTrue(common_prefix.startswith( |
| 88 'gs://%sgsutil-test-test_bucket_list_wildcard-bucket-' % random_prefix)) | 93 '%s://%sgsutil-test-test_bucket_list_wildcard-bucket-' % |
| 94 (self.default_provider, random_prefix))) |
| 89 wildcard = '%s*' % common_prefix | 95 wildcard = '%s*' % common_prefix |
| 90 | 96 |
| 91 # Use @Retry as hedge against bucket listing eventual consistency. | 97 # Use @Retry as hedge against bucket listing eventual consistency. |
| 92 @Retry(AssertionError, tries=3, timeout_secs=1) | 98 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 93 def _Check1(): | 99 def _Check1(): |
| 94 stdout = self.RunGsUtil(['ls', '-b', wildcard], return_stdout=True) | 100 stdout = self.RunGsUtil(['ls', '-b', wildcard], return_stdout=True) |
| 95 expected = set([suri(bucket1_uri) + '/', suri(bucket2_uri) + '/']) | 101 expected = set([suri(bucket1_uri) + '/', suri(bucket2_uri) + '/']) |
| 96 actual = set(stdout.split()) | 102 actual = set(stdout.split()) |
| 97 self.assertEqual(expected, actual) | 103 self.assertEqual(expected, actual) |
| 98 _Check1() | 104 _Check1() |
| 99 | 105 |
| 100 def test_nonexistent_bucket_with_ls(self): | 106 def test_nonexistent_bucket_with_ls(self): |
| 107 """Tests a bucket that is known not to exist.""" |
| 101 stderr = self.RunGsUtil( | 108 stderr = self.RunGsUtil( |
| 102 ['ls', '-lb', 'gs://%s' % self.NONEXISTENT_BUCKET_NAME], | 109 ['ls', '-lb', 'gs://%s' % self.nonexistent_bucket_name], |
| 103 return_stderr=True, expected_status=1) | 110 return_stderr=True, expected_status=1) |
| 104 self.assertIn('404', stderr) | 111 self.assertIn('404', stderr) |
| 105 | 112 |
| 106 stderr = self.RunGsUtil( | 113 stderr = self.RunGsUtil( |
| 107 ['ls', '-Lb', 'gs://%s' % self.NONEXISTENT_BUCKET_NAME], | 114 ['ls', '-Lb', 'gs://%s' % self.nonexistent_bucket_name], |
| 108 return_stderr=True, expected_status=1) | 115 return_stderr=True, expected_status=1) |
| 109 self.assertIn('404', stderr) | 116 self.assertIn('404', stderr) |
| 110 | 117 |
| 111 stderr = self.RunGsUtil( | 118 stderr = self.RunGsUtil( |
| 112 ['ls', '-b', 'gs://%s' % self.NONEXISTENT_BUCKET_NAME], | 119 ['ls', '-b', 'gs://%s' % self.nonexistent_bucket_name], |
| 113 return_stderr=True, expected_status=1) | 120 return_stderr=True, expected_status=1) |
| 114 self.assertIn('404', stderr) | 121 self.assertIn('404', stderr) |
| 115 | 122 |
| 123 def test_list_missing_object(self): |
| 124 """Tests listing a non-existent object.""" |
| 125 bucket_uri = self.CreateBucket() |
| 126 stderr = self.RunGsUtil(['ls', suri(bucket_uri, 'missing')], |
| 127 return_stderr=True, expected_status=1) |
| 128 self.assertIn('matched no objects', stderr) |
| 129 |
| 116 def test_with_one_object(self): | 130 def test_with_one_object(self): |
| 117 bucket_uri = self.CreateBucket(test_objects=1) | 131 bucket_uri = self.CreateBucket() |
| 118 objuri = [suri(bucket_uri.clone_replace_name(key.name)) | 132 obj_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 119 for key in bucket_uri.list_bucket()][0] | |
| 120 # Use @Retry as hedge against bucket listing eventual consistency. | 133 # Use @Retry as hedge against bucket listing eventual consistency. |
| 121 @Retry(AssertionError, tries=3, timeout_secs=1) | 134 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 122 def _Check1(): | 135 def _Check1(): |
| 123 stdout = self.RunGsUtil(['ls', suri(bucket_uri)], return_stdout=True) | 136 stdout = self.RunGsUtil(['ls', suri(bucket_uri)], return_stdout=True) |
| 124 self.assertEqual('%s\n' % objuri, stdout) | 137 self.assertEqual('%s\n' % obj_uri, stdout) |
| 125 _Check1() | 138 _Check1() |
| 126 | 139 |
| 127 def test_subdir(self): | 140 def test_subdir(self): |
| 141 """Tests listing a bucket subdirectory.""" |
| 128 bucket_uri = self.CreateBucket(test_objects=1) | 142 bucket_uri = self.CreateBucket(test_objects=1) |
| 129 k1_uri = bucket_uri.clone_replace_name('foo') | 143 k1_uri = bucket_uri.clone_replace_name('foo') |
| 130 k1_uri.set_contents_from_string('baz') | 144 k1_uri.set_contents_from_string('baz') |
| 131 k2_uri = bucket_uri.clone_replace_name('dir/foo') | 145 k2_uri = bucket_uri.clone_replace_name('dir/foo') |
| 132 k2_uri.set_contents_from_string('bar') | 146 k2_uri.set_contents_from_string('bar') |
| 133 # Use @Retry as hedge against bucket listing eventual consistency. | 147 # Use @Retry as hedge against bucket listing eventual consistency. |
| 134 @Retry(AssertionError, tries=3, timeout_secs=1) | 148 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 135 def _Check1(): | 149 def _Check1(): |
| 136 stdout = self.RunGsUtil(['ls', '%s/dir' % suri(bucket_uri)], | 150 stdout = self.RunGsUtil(['ls', '%s/dir' % suri(bucket_uri)], |
| 137 return_stdout=True) | 151 return_stdout=True) |
| 138 self.assertEqual('%s\n' % suri(k2_uri), stdout) | 152 self.assertEqual('%s\n' % suri(k2_uri), stdout) |
| 139 stdout = self.RunGsUtil(['ls', suri(k1_uri)], return_stdout=True) | 153 stdout = self.RunGsUtil(['ls', suri(k1_uri)], return_stdout=True) |
| 140 self.assertEqual('%s\n' % suri(k1_uri), stdout) | 154 self.assertEqual('%s\n' % suri(k1_uri), stdout) |
| 141 _Check1() | 155 _Check1() |
| 142 | 156 |
| 143 def test_versioning(self): | 157 def test_versioning(self): |
| 158 """Tests listing a versioned bucket.""" |
| 144 bucket1_uri = self.CreateBucket(test_objects=1) | 159 bucket1_uri = self.CreateBucket(test_objects=1) |
| 145 bucket2_uri = self.CreateVersionedBucket(test_objects=1) | 160 bucket2_uri = self.CreateVersionedBucket(test_objects=1) |
| 161 self.AssertNObjectsInBucket(bucket1_uri, 1, versioned=True) |
| 146 bucket_list = list(bucket1_uri.list_bucket()) | 162 bucket_list = list(bucket1_uri.list_bucket()) |
| 163 |
| 147 objuri = [bucket1_uri.clone_replace_key(key).versionless_uri | 164 objuri = [bucket1_uri.clone_replace_key(key).versionless_uri |
| 148 for key in bucket_list][0] | 165 for key in bucket_list][0] |
| 149 self.RunGsUtil(['cp', objuri, suri(bucket2_uri)]) | 166 self.RunGsUtil(['cp', objuri, suri(bucket2_uri)]) |
| 150 self.RunGsUtil(['cp', objuri, suri(bucket2_uri)]) | 167 self.RunGsUtil(['cp', objuri, suri(bucket2_uri)]) |
| 151 # Use @Retry as hedge against bucket listing eventual consistency. | 168 # Use @Retry as hedge against bucket listing eventual consistency. |
| 152 @Retry(AssertionError, tries=3, timeout_secs=1) | 169 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 153 def _Check1(): | 170 def _Check2(): |
| 154 stdout = self.RunGsUtil(['ls', '-a', suri(bucket2_uri)], | 171 stdout = self.RunGsUtil(['ls', '-a', suri(bucket2_uri)], |
| 155 return_stdout=True) | 172 return_stdout=True) |
| 156 self.assertNumLines(stdout, 3) | 173 self.assertNumLines(stdout, 3) |
| 157 stdout = self.RunGsUtil(['ls', '-la', suri(bucket2_uri)], | 174 stdout = self.RunGsUtil(['ls', '-la', suri(bucket2_uri)], |
| 158 return_stdout=True) | 175 return_stdout=True) |
| 159 self.assertIn('%s#' % bucket2_uri.clone_replace_name(bucket_list[0].name), | 176 self.assertIn('%s#' % bucket2_uri.clone_replace_name(bucket_list[0].name), |
| 160 stdout) | 177 stdout) |
| 161 self.assertIn('metageneration=', stdout) | 178 self.assertIn('metageneration=', stdout) |
| 162 _Check1() | 179 _Check2() |
| 163 | 180 |
| 164 def test_etag(self): | 181 def test_etag(self): |
| 182 """Tests that listing an object with an etag.""" |
| 165 bucket_uri = self.CreateBucket() | 183 bucket_uri = self.CreateBucket() |
| 166 obj_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo') | 184 obj_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 167 etag = obj_uri.get_key().etag | 185 # TODO: When testcase setup can use JSON, match against the exact JSON |
| 186 # etag. |
| 187 etag = obj_uri.get_key().etag.strip('"\'') |
| 168 # Use @Retry as hedge against bucket listing eventual consistency. | 188 # Use @Retry as hedge against bucket listing eventual consistency. |
| 169 @Retry(AssertionError, tries=3, timeout_secs=1) | 189 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 170 def _Check1(): | 190 def _Check1(): |
| 171 stdout = self.RunGsUtil(['ls', '-l', suri(bucket_uri)], | 191 stdout = self.RunGsUtil(['ls', '-l', suri(bucket_uri)], |
| 172 return_stdout=True) | 192 return_stdout=True) |
| 173 self.assertNotIn(etag, stdout) | 193 if self.test_api == ApiSelector.XML: |
| 194 self.assertNotIn(etag, stdout) |
| 195 else: |
| 196 self.assertNotIn('etag=', stdout) |
| 197 _Check1() |
| 174 | 198 |
| 199 def _Check2(): |
| 175 stdout = self.RunGsUtil(['ls', '-le', suri(bucket_uri)], | 200 stdout = self.RunGsUtil(['ls', '-le', suri(bucket_uri)], |
| 176 return_stdout=True) | 201 return_stdout=True) |
| 177 self.assertIn(etag, stdout) | 202 if self.test_api == ApiSelector.XML: |
| 203 self.assertIn(etag, stdout) |
| 204 else: |
| 205 self.assertIn('etag=', stdout) |
| 206 _Check2() |
| 178 | 207 |
| 208 def _Check3(): |
| 179 stdout = self.RunGsUtil(['ls', '-ale', suri(bucket_uri)], | 209 stdout = self.RunGsUtil(['ls', '-ale', suri(bucket_uri)], |
| 180 return_stdout=True) | 210 return_stdout=True) |
| 181 self.assertIn(etag, stdout) | 211 if self.test_api == ApiSelector.XML: |
| 182 | 212 self.assertIn(etag, stdout) |
| 183 _Check1() | 213 else: |
| 214 self.assertIn('etag=', stdout) |
| 215 _Check3() |
| 184 | 216 |
| 185 def test_list_sizes(self): | 217 def test_list_sizes(self): |
| 218 """Tests various size listing options.""" |
| 186 bucket_uri = self.CreateBucket() | 219 bucket_uri = self.CreateBucket() |
| 187 self.CreateObject(bucket_uri=bucket_uri, contents='x' * 2048) | 220 self.CreateObject(bucket_uri=bucket_uri, contents='x' * 2048) |
| 188 | 221 |
| 189 # Use @Retry as hedge against bucket listing eventual consistency. | 222 # Use @Retry as hedge against bucket listing eventual consistency. |
| 190 @Retry(AssertionError, tries=3, timeout_secs=1) | 223 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 191 def _Check1(): | 224 def _Check1(): |
| 192 stdout = self.RunGsUtil(['ls', '-l', suri(bucket_uri)], | 225 stdout = self.RunGsUtil(['ls', '-l', suri(bucket_uri)], |
| 193 return_stdout=True) | 226 return_stdout=True) |
| 194 self.assertIn('2048', stdout) | 227 self.assertIn('2048', stdout) |
| 195 _Check1() | 228 _Check1() |
| (...skipping 23 matching lines...) Expand all Loading... |
| 219 _Check4() | 252 _Check4() |
| 220 | 253 |
| 221 # Use @Retry as hedge against bucket listing eventual consistency. | 254 # Use @Retry as hedge against bucket listing eventual consistency. |
| 222 @Retry(AssertionError, tries=3, timeout_secs=1) | 255 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 223 def _Check5(): | 256 def _Check5(): |
| 224 stdout = self.RunGsUtil(['ls', '-alh', suri(bucket_uri)], | 257 stdout = self.RunGsUtil(['ls', '-alh', suri(bucket_uri)], |
| 225 return_stdout=True) | 258 return_stdout=True) |
| 226 self.assertIn('2 KB', stdout) | 259 self.assertIn('2 KB', stdout) |
| 227 _Check5() | 260 _Check5() |
| 228 | 261 |
| 262 @unittest.skipIf(IS_WINDOWS, |
| 263 'Unicode handling on Windows requires mods to site-packages') |
| 229 def test_list_unicode_filename(self): | 264 def test_list_unicode_filename(self): |
| 265 """Tests listing an object with a unicode filename.""" |
| 266 # Note: This test fails on Windows (command.exe). I was able to get ls to |
| 267 # output Unicode filenames correctly by hacking the UniStream class code |
| 268 # shown at |
| 269 # http://stackoverflow.com/questions/878972/windows-cmd-encoding-change-caus
es-python-crash/3259271 |
| 270 # into the start of gslib/commands/ls.py, along with no-op flush and |
| 271 # isastream functions (as an experiment). However, even with that change, |
| 272 # the current test still fails, since it also needs to run that |
| 273 # stdout/stderr-replacement code. That UniStream class replacement really |
| 274 # needs to be added to the site-packages on Windows python. |
| 230 object_name = u'Аудиоархив' | 275 object_name = u'Аудиоархив' |
| 231 object_name_bytes = object_name.encode('utf-8') | 276 object_name_bytes = object_name.encode(UTF8) |
| 232 bucket_uri = self.CreateVersionedBucket() | 277 bucket_uri = self.CreateVersionedBucket() |
| 233 key_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo', | 278 key_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo', |
| 234 object_name=object_name) | 279 object_name=object_name) |
| 280 self.AssertNObjectsInBucket(bucket_uri, 1, versioned=True) |
| 235 stdout = self.RunGsUtil(['ls', '-ael', suri(key_uri)], | 281 stdout = self.RunGsUtil(['ls', '-ael', suri(key_uri)], |
| 236 return_stdout=True) | 282 return_stdout=True) |
| 237 self.assertIn(object_name_bytes, stdout) | 283 self.assertIn(object_name_bytes, stdout) |
| 238 self.assertIn(key_uri.generation, stdout) | 284 if self.default_provider == 'gs': |
| 239 self.assertIn( | 285 self.assertIn(key_uri.generation, stdout) |
| 240 'metageneration=%s' % key_uri.get_key().metageneration, stdout) | 286 self.assertIn( |
| 241 self.assertIn( | 287 'metageneration=%s' % key_uri.get_key().metageneration, stdout) |
| 242 'etag=%s' % key_uri.get_key().etag, stdout) | 288 if self.test_api == ApiSelector.XML: |
| 289 self.assertIn(key_uri.get_key().etag.strip('"\''), stdout) |
| 290 else: |
| 291 # TODO: When testcase setup can use JSON, match against the exact JSON |
| 292 # etag. |
| 293 self.assertIn('etag=', stdout) |
| 294 elif self.default_provider == 's3': |
| 295 self.assertIn(key_uri.version_id, stdout) |
| 296 self.assertIn(key_uri.get_key().etag.strip('"\''), stdout) |
| 243 | 297 |
| 244 def test_list_gzip_content_length(self): | 298 def test_list_gzip_content_length(self): |
| 299 """Tests listing a gzipped object.""" |
| 245 file_size = 10000 | 300 file_size = 10000 |
| 246 file_contents = 'x' * file_size | 301 file_contents = 'x' * file_size |
| 247 fpath = self.CreateTempFile(contents=file_contents, file_name='foo.txt') | 302 fpath = self.CreateTempFile(contents=file_contents, file_name='foo.txt') |
| 248 key_uri = self.CreateObject() | 303 key_uri = self.CreateObject() |
| 249 self.RunGsUtil(['cp', '-z', 'txt', suri(fpath), suri(key_uri)]) | 304 self.RunGsUtil(['cp', '-z', 'txt', suri(fpath), suri(key_uri)]) |
| 250 | 305 |
| 251 # Use @Retry as hedge against bucket listing eventual consistency. | 306 # Use @Retry as hedge against bucket listing eventual consistency. |
| 252 @Retry(AssertionError, tries=3, timeout_secs=1) | 307 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 253 def _Check1(): | 308 def _Check1(): |
| 254 stdout = self.RunGsUtil(['ls', '-L', suri(key_uri)], return_stdout=True) | 309 stdout = self.RunGsUtil(['ls', '-L', suri(key_uri)], return_stdout=True) |
| 255 self.assertRegexpMatches(stdout, r'Content-Encoding:\s+gzip') | 310 self.assertRegexpMatches(stdout, r'Content-Encoding:\s+gzip') |
| 256 find_content_length_re = r'Content-Length:\s+(?P<num>\d)' | 311 find_content_length_re = r'Content-Length:\s+(?P<num>\d)' |
| 257 self.assertRegexpMatches(stdout, find_content_length_re) | 312 self.assertRegexpMatches(stdout, find_content_length_re) |
| 258 m = re.search(find_content_length_re, stdout) | 313 m = re.search(find_content_length_re, stdout) |
| 259 content_length = int(m.group('num')) | 314 content_length = int(m.group('num')) |
| 260 self.assertGreater(content_length, 0) | 315 self.assertGreater(content_length, 0) |
| 261 self.assertLess(content_length, file_size) | 316 self.assertLess(content_length, file_size) |
| 262 _Check1() | 317 _Check1() |
| 263 | 318 |
| 264 def test_output_chopped(self): | 319 def test_output_chopped(self): |
| 320 """Tests that gsutil still succeeds with a truncated stdout.""" |
| 265 bucket_uri = self.CreateBucket(test_objects=2) | 321 bucket_uri = self.CreateBucket(test_objects=2) |
| 266 | 322 |
| 267 # Run Python with the -u flag so output is not buffered. | 323 # Run Python with the -u flag so output is not buffered. |
| 268 gsutil_cmd = [ | 324 gsutil_cmd = [ |
| 269 sys.executable, '-u', gslib.GSUTIL_PATH, 'ls', suri(bucket_uri)] | 325 sys.executable, '-u', gslib.GSUTIL_PATH, 'ls', suri(bucket_uri)] |
| 270 # Set bufsize to 0 to make sure output is not buffered. | 326 # Set bufsize to 0 to make sure output is not buffered. |
| 271 p = subprocess.Popen(gsutil_cmd, stdout=subprocess.PIPE, bufsize=0) | 327 p = subprocess.Popen(gsutil_cmd, stdout=subprocess.PIPE, bufsize=0) |
| 272 # Immediately close the stdout pipe so that gsutil gets a broken pipe error. | 328 # Immediately close the stdout pipe so that gsutil gets a broken pipe error. |
| 273 p.stdout.close() | 329 p.stdout.close() |
| 274 p.wait() | 330 p.wait() |
| 275 # Make sure it still exited cleanly. | 331 # Make sure it still exited cleanly. |
| 276 self.assertEqual(p.returncode, 0) | 332 self.assertEqual(p.returncode, 0) |
| 333 |
| 334 def test_recursive_list_trailing_slash(self): |
| 335 """Tests listing an object with a trailing slash.""" |
| 336 bucket_uri = self.CreateBucket() |
| 337 self.CreateObject(bucket_uri=bucket_uri, object_name='/', contents='foo') |
| 338 self.AssertNObjectsInBucket(bucket_uri, 1) |
| 339 stdout = self.RunGsUtil(['ls', '-R', suri(bucket_uri)], return_stdout=True) |
| 340 # Note: The suri function normalizes the URI, so the double slash gets |
| 341 # removed. |
| 342 self.assertIn(suri(bucket_uri) + '/', stdout) |
| 343 |
| 344 def test_recursive_list_trailing_two_slash(self): |
| 345 """Tests listing an object with two trailing slashes.""" |
| 346 bucket_uri = self.CreateBucket() |
| 347 self.CreateObject(bucket_uri=bucket_uri, object_name='//', contents='foo') |
| 348 self.AssertNObjectsInBucket(bucket_uri, 1) |
| 349 stdout = self.RunGsUtil(['ls', '-R', suri(bucket_uri)], return_stdout=True) |
| 350 # Note: The suri function normalizes the URI, so the double slash gets |
| 351 # removed. |
| 352 self.assertIn(suri(bucket_uri) + '//', stdout) |
| 353 |
| 354 @SkipForS3('S3 anonymous access is not supported.') |
| 355 def test_get_object_without_list_bucket_permission(self): |
| 356 # Bucket is not publicly readable by default. |
| 357 bucket_uri = self.CreateBucket() |
| 358 object_uri = self.CreateObject(bucket_uri=bucket_uri, |
| 359 object_name='permitted', contents='foo') |
| 360 # Set this object to be publicly readable. |
| 361 self.RunGsUtil(['acl', 'set', 'public-read', suri(object_uri)]) |
| 362 # Drop credentials. |
| 363 with self.SetAnonymousBotoCreds(): |
| 364 stdout = self.RunGsUtil(['ls', '-L', suri(object_uri)], |
| 365 return_stdout=True) |
| 366 self.assertIn(suri(object_uri), stdout) |
| OLD | NEW |