| OLD | NEW |
| 1 # -*- coding: utf-8 -*- |
| 1 # Copyright 2013 Google Inc. All Rights Reserved. | 2 # Copyright 2013 Google Inc. All Rights Reserved. |
| 2 # | 3 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # 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. |
| 5 # You may obtain a copy of the License at | 6 # You may obtain a copy of the License at |
| 6 # | 7 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 9 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 10 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 13 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 14 # limitations under the License. |
| 15 """Tests for du command.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 14 | 18 |
| 15 import gslib.tests.testcase as testcase | 19 import gslib.tests.testcase as testcase |
| 20 from gslib.tests.testcase.integration_testcase import SkipForS3 |
| 21 from gslib.tests.util import ObjectToURI as suri |
| 16 from gslib.util import Retry | 22 from gslib.util import Retry |
| 17 from gslib.tests.util import ObjectToURI as suri | |
| 18 | 23 |
| 19 | 24 |
| 20 class TestDu(testcase.GsUtilIntegrationTestCase): | 25 class TestDu(testcase.GsUtilIntegrationTestCase): |
| 21 """Integration tests for du command.""" | 26 """Integration tests for du command.""" |
| 22 | 27 |
| 23 def _create_nested_subdir(self): | 28 def _create_nested_subdir(self): |
| 29 """Creates a nested subdirectory for use by tests in this module.""" |
| 24 bucket_uri = self.CreateBucket() | 30 bucket_uri = self.CreateBucket() |
| 25 obj_uris = [] | 31 obj_uris = [] |
| 26 obj_uris.append(self.CreateObject( | 32 obj_uris.append(self.CreateObject( |
| 27 bucket_uri=bucket_uri, object_name='sub1/five', contents='5five')) | 33 bucket_uri=bucket_uri, object_name='sub1/five', contents='5five')) |
| 28 obj_uris.append(self.CreateObject( | 34 obj_uris.append(self.CreateObject( |
| 29 bucket_uri=bucket_uri, object_name='sub1/four', contents='four')) | 35 bucket_uri=bucket_uri, object_name='sub1/four', contents='four')) |
| 30 obj_uris.append(self.CreateObject( | 36 obj_uris.append(self.CreateObject( |
| 31 bucket_uri=bucket_uri, object_name='sub1/sub2/five', contents='5five')) | 37 bucket_uri=bucket_uri, object_name='sub1/sub2/five', contents='5five')) |
| 32 obj_uris.append(self.CreateObject( | 38 obj_uris.append(self.CreateObject( |
| 33 bucket_uri=bucket_uri, object_name='sub1/sub2/four', contents='four')) | 39 bucket_uri=bucket_uri, object_name='sub1/sub2/four', contents='four')) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 46 bucket_uri = self.CreateBucket() | 52 bucket_uri = self.CreateBucket() |
| 47 obj_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo') | 53 obj_uri = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 48 # Use @Retry as hedge against bucket listing eventual consistency. | 54 # Use @Retry as hedge against bucket listing eventual consistency. |
| 49 @Retry(AssertionError, tries=3, timeout_secs=1) | 55 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 50 def _Check(): | 56 def _Check(): |
| 51 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) | 57 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) |
| 52 self.assertEqual(stdout, '%-10s %s\n' % (3, suri(obj_uri))) | 58 self.assertEqual(stdout, '%-10s %s\n' % (3, suri(obj_uri))) |
| 53 _Check() | 59 _Check() |
| 54 | 60 |
| 55 def test_subdirs(self): | 61 def test_subdirs(self): |
| 62 """Tests that subdirectory sizes are correctly calculated and listed.""" |
| 56 bucket_uri, obj_uris = self._create_nested_subdir() | 63 bucket_uri, obj_uris = self._create_nested_subdir() |
| 57 | 64 |
| 58 # Use @Retry as hedge against bucket listing eventual consistency. | 65 # Use @Retry as hedge against bucket listing eventual consistency. |
| 59 @Retry(AssertionError, tries=3, timeout_secs=1) | 66 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 60 def _Check(): | 67 def _Check(): |
| 61 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) | 68 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) |
| 62 self.assertSetEqual(set(stdout.splitlines()), set([ | 69 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 63 '%-10s %s' % (5, suri(obj_uris[0])), | 70 '%-10s %s' % (5, suri(obj_uris[0])), |
| 64 '%-10s %s' % (4, suri(obj_uris[1])), | 71 '%-10s %s' % (4, suri(obj_uris[1])), |
| 65 '%-10s %s' % (5, suri(obj_uris[2])), | 72 '%-10s %s' % (5, suri(obj_uris[2])), |
| 66 '%-10s %s' % (4, suri(obj_uris[3])), | 73 '%-10s %s' % (4, suri(obj_uris[3])), |
| 67 '%-10s %s/sub1/sub2/' % (9, suri(bucket_uri)), | 74 '%-10s %s/sub1/sub2/' % (9, suri(bucket_uri)), |
| 68 '%-10s %s/sub1/' % (18, suri(bucket_uri)), | 75 '%-10s %s/sub1/' % (18, suri(bucket_uri)), |
| 69 ])) | 76 ])) |
| 70 _Check() | 77 _Check() |
| 71 | 78 |
| 72 def test_multi_args(self): | 79 def test_multi_args(self): |
| 80 """Tests running du with multiple command line arguments.""" |
| 73 bucket_uri = self.CreateBucket() | 81 bucket_uri = self.CreateBucket() |
| 74 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') | 82 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 75 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='foo2') | 83 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='foo2') |
| 76 # Use @Retry as hedge against bucket listing eventual consistency. | 84 # Use @Retry as hedge against bucket listing eventual consistency. |
| 77 @Retry(AssertionError, tries=3, timeout_secs=1) | 85 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 78 def _Check(): | 86 def _Check(): |
| 79 stdout = self.RunGsUtil(['du', suri(obj_uri1), suri(obj_uri2)], | 87 stdout = self.RunGsUtil(['du', suri(obj_uri1), suri(obj_uri2)], |
| 80 return_stdout=True) | 88 return_stdout=True) |
| 81 self.assertSetEqual(set(stdout.splitlines()), set([ | 89 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 82 '%-10s %s' % (3, suri(obj_uri1)), | 90 '%-10s %s' % (3, suri(obj_uri1)), |
| 83 '%-10s %s' % (4, suri(obj_uri2)), | 91 '%-10s %s' % (4, suri(obj_uri2)), |
| 84 ])) | 92 ])) |
| 85 _Check() | 93 _Check() |
| 86 | 94 |
| 87 def test_total(self): | 95 def test_total(self): |
| 96 """Tests total size listing via the -c flag.""" |
| 88 bucket_uri = self.CreateBucket() | 97 bucket_uri = self.CreateBucket() |
| 89 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') | 98 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 90 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='zebra') | 99 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='zebra') |
| 91 # Use @Retry as hedge against bucket listing eventual consistency. | 100 # Use @Retry as hedge against bucket listing eventual consistency. |
| 92 @Retry(AssertionError, tries=3, timeout_secs=1) | 101 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 93 def _Check(): | 102 def _Check(): |
| 94 stdout = self.RunGsUtil(['du', '-c', suri(bucket_uri)], | 103 stdout = self.RunGsUtil(['du', '-c', suri(bucket_uri)], |
| 95 return_stdout=True) | 104 return_stdout=True) |
| 96 self.assertSetEqual(set(stdout.splitlines()), set([ | 105 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 97 '%-10s %s' % (3, suri(obj_uri1)), | 106 '%-10s %s' % (3, suri(obj_uri1)), |
| 98 '%-10s %s' % (5, suri(obj_uri2)), | 107 '%-10s %s' % (5, suri(obj_uri2)), |
| 99 '%-10s total' % 8, | 108 '%-10s total' % 8, |
| 100 ])) | 109 ])) |
| 101 _Check() | 110 _Check() |
| 102 | 111 |
| 103 def test_human_readable(self): | 112 def test_human_readable(self): |
| 104 obj_uri = self.CreateObject(contents='x' * 2048) | 113 obj_uri = self.CreateObject(contents='x' * 2048) |
| 105 # Use @Retry as hedge against bucket listing eventual consistency. | 114 # Use @Retry as hedge against bucket listing eventual consistency. |
| 106 @Retry(AssertionError, tries=3, timeout_secs=1) | 115 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 107 def _Check(): | 116 def _Check(): |
| 108 stdout = self.RunGsUtil(['du', '-h', suri(obj_uri)], return_stdout=True) | 117 stdout = self.RunGsUtil(['du', '-h', suri(obj_uri)], return_stdout=True) |
| 109 self.assertEqual(stdout, '%-10s %s\n' % ('2 KB', suri(obj_uri))) | 118 self.assertEqual(stdout, '%-10s %s\n' % ('2 KB', suri(obj_uri))) |
| 110 _Check() | 119 _Check() |
| 111 | 120 |
| 112 def test_summary(self): | 121 def test_summary(self): |
| 122 """Tests summary listing with the -s flag.""" |
| 113 bucket_uri1, _ = self._create_nested_subdir() | 123 bucket_uri1, _ = self._create_nested_subdir() |
| 114 bucket_uri2, _ = self._create_nested_subdir() | 124 bucket_uri2, _ = self._create_nested_subdir() |
| 115 | 125 |
| 116 # Use @Retry as hedge against bucket listing eventual consistency. | 126 # Use @Retry as hedge against bucket listing eventual consistency. |
| 117 @Retry(AssertionError, tries=3, timeout_secs=1) | 127 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 118 def _Check(): | 128 def _Check(): |
| 119 stdout = self.RunGsUtil([ | 129 stdout = self.RunGsUtil([ |
| 120 'du', '-s', suri(bucket_uri1), suri(bucket_uri2)], return_stdout=True) | 130 'du', '-s', suri(bucket_uri1), suri(bucket_uri2)], return_stdout=True) |
| 121 self.assertSetEqual(set(stdout.splitlines()), set([ | 131 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 122 '%-10s %s' % (18, suri(bucket_uri1)), | 132 '%-10s %s' % (18, suri(bucket_uri1)), |
| 123 '%-10s %s' % (18, suri(bucket_uri2)), | 133 '%-10s %s' % (18, suri(bucket_uri2)), |
| 124 ])) | 134 ])) |
| 125 _Check() | 135 _Check() |
| 126 | 136 |
| 137 def test_subdir_summary(self): |
| 138 """Tests summary listing with the -s flag on a subdirectory.""" |
| 139 bucket_uri1, _ = self._create_nested_subdir() |
| 140 bucket_uri2, _ = self._create_nested_subdir() |
| 141 subdir1 = suri(bucket_uri1, 'sub1') |
| 142 subdir2 = suri(bucket_uri2, 'sub1') |
| 143 |
| 144 # Use @Retry as hedge against bucket listing eventual consistency. |
| 145 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 146 def _Check(): |
| 147 stdout = self.RunGsUtil( |
| 148 ['du', '-s', subdir1, subdir2], return_stdout=True) |
| 149 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 150 '%-10s %s' % (18, subdir1), |
| 151 '%-10s %s' % (18, subdir2), |
| 152 ])) |
| 153 _Check() |
| 154 |
| 155 @SkipForS3('S3 lists versions in reverse order.') |
| 127 def test_versioned(self): | 156 def test_versioned(self): |
| 157 """Tests listing all versions with the -a flag.""" |
| 128 bucket_uri = self.CreateVersionedBucket() | 158 bucket_uri = self.CreateVersionedBucket() |
| 129 object_uri1 = self.CreateObject( | 159 object_uri1 = self.CreateObject( |
| 130 bucket_uri=bucket_uri, object_name='foo', contents='foo') | 160 bucket_uri=bucket_uri, object_name='foo', contents='foo') |
| 131 object_uri2 = self.CreateObject( | 161 object_uri2 = self.CreateObject( |
| 132 bucket_uri=bucket_uri, object_name='foo', contents='foo2') | 162 bucket_uri=bucket_uri, object_name='foo', contents='foo2') |
| 133 | 163 |
| 134 # Use @Retry as hedge against bucket listing eventual consistency. | 164 # Use @Retry as hedge against bucket listing eventual consistency. |
| 135 @Retry(AssertionError, tries=3, timeout_secs=1) | 165 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 136 def _Check1(): | 166 def _Check1(): |
| 137 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) | 167 stdout = self.RunGsUtil(['du', suri(bucket_uri)], return_stdout=True) |
| 138 self.assertEqual(stdout, '%-10s %s\n' % (4, suri(object_uri2))) | 168 self.assertEqual(stdout, '%-10s %s\n' % (4, suri(object_uri2))) |
| 139 _Check1() | 169 _Check1() |
| 140 | 170 |
| 141 # Use @Retry as hedge against bucket listing eventual consistency. | 171 # Use @Retry as hedge against bucket listing eventual consistency. |
| 142 @Retry(AssertionError, tries=3, timeout_secs=1) | 172 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 143 def _Check2(): | 173 def _Check2(): |
| 144 stdout = self.RunGsUtil(['du', '-a', suri(bucket_uri)], | 174 stdout = self.RunGsUtil(['du', '-a', suri(bucket_uri)], |
| 145 return_stdout=True) | 175 return_stdout=True) |
| 146 self.assertSetEqual(set(stdout.splitlines()), set([ | 176 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 147 '%-10s %s#%s' % ( | 177 '%-10s %s#%s' % ( |
| 148 3, suri(object_uri1), object_uri1.generation), | 178 3, suri(object_uri1), object_uri1.generation), |
| 149 '%-10s %s#%s' % ( | 179 '%-10s %s#%s' % ( |
| 150 4, suri(object_uri2), object_uri2.generation), | 180 4, suri(object_uri2), object_uri2.generation), |
| 151 ])) | 181 ])) |
| 152 _Check2() | 182 _Check2() |
| 153 | 183 |
| 154 def test_null_endings(self): | 184 def test_null_endings(self): |
| 185 """Tests outputting 0-endings with the -0 flag.""" |
| 155 bucket_uri = self.CreateBucket() | 186 bucket_uri = self.CreateBucket() |
| 156 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') | 187 obj_uri1 = self.CreateObject(bucket_uri=bucket_uri, contents='foo') |
| 157 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='zebra') | 188 obj_uri2 = self.CreateObject(bucket_uri=bucket_uri, contents='zebra') |
| 158 # Use @Retry as hedge against bucket listing eventual consistency. | 189 # Use @Retry as hedge against bucket listing eventual consistency. |
| 159 @Retry(AssertionError, tries=3, timeout_secs=1) | 190 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 160 def _Check(): | 191 def _Check(): |
| 161 stdout = self.RunGsUtil(['du', '-0c', suri(bucket_uri)], | 192 stdout = self.RunGsUtil(['du', '-0c', suri(bucket_uri)], |
| 162 return_stdout=True) | 193 return_stdout=True) |
| 163 self.assertSetEqual(set(stdout.split('\0')), set([ | 194 self.assertSetEqual(set(stdout.split('\0')), set([ |
| 164 '%-10s %s' % (3, suri(obj_uri1)), | 195 '%-10s %s' % (3, suri(obj_uri1)), |
| 165 '%-10s %s' % (5, suri(obj_uri2)), | 196 '%-10s %s' % (5, suri(obj_uri2)), |
| 166 '%-10s total' % 8, | 197 '%-10s total' % 8, |
| 167 '' | 198 '' |
| 168 ])) | 199 ])) |
| 169 _Check() | 200 _Check() |
| 170 | 201 |
| 171 def test_excludes(self): | 202 def test_excludes(self): |
| 203 """Tests exclude pattern excluding certain file paths.""" |
| 172 bucket_uri, obj_uris = self._create_nested_subdir() | 204 bucket_uri, obj_uris = self._create_nested_subdir() |
| 173 | 205 |
| 174 # Use @Retry as hedge against bucket listing eventual consistency. | 206 # Use @Retry as hedge against bucket listing eventual consistency. |
| 175 @Retry(AssertionError, tries=3, timeout_secs=1) | 207 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 176 def _Check(): | 208 def _Check(): |
| 177 stdout = self.RunGsUtil([ | 209 stdout = self.RunGsUtil([ |
| 178 'du', '-e', '*sub2/five*', '-e', '*sub1/four', | 210 'du', '-e', '*sub2/five*', '-e', '*sub1/four', |
| 179 suri(bucket_uri)], return_stdout=True) | 211 suri(bucket_uri)], return_stdout=True) |
| 180 self.assertSetEqual(set(stdout.splitlines()), set([ | 212 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 181 '%-10s %s' % (5, suri(obj_uris[0])), | 213 '%-10s %s' % (5, suri(obj_uris[0])), |
| 182 '%-10s %s' % (4, suri(obj_uris[3])), | 214 '%-10s %s' % (4, suri(obj_uris[3])), |
| 183 '%-10s %s/sub1/sub2/' % (4, suri(bucket_uri)), | 215 '%-10s %s/sub1/sub2/' % (4, suri(bucket_uri)), |
| 184 '%-10s %s/sub1/' % (9, suri(bucket_uri)), | 216 '%-10s %s/sub1/' % (9, suri(bucket_uri)), |
| 185 ])) | 217 ])) |
| 186 _Check() | 218 _Check() |
| 187 | 219 |
| 188 def test_excludes_file(self): | 220 def test_excludes_file(self): |
| 221 """Tests file exclusion with the -X flag.""" |
| 189 bucket_uri, obj_uris = self._create_nested_subdir() | 222 bucket_uri, obj_uris = self._create_nested_subdir() |
| 190 fpath = self.CreateTempFile(contents='*sub2/five*\n*sub1/four') | 223 fpath = self.CreateTempFile(contents='*sub2/five*\n*sub1/four') |
| 191 | 224 |
| 192 # Use @Retry as hedge against bucket listing eventual consistency. | 225 # Use @Retry as hedge against bucket listing eventual consistency. |
| 193 @Retry(AssertionError, tries=3, timeout_secs=1) | 226 @Retry(AssertionError, tries=3, timeout_secs=1) |
| 194 def _Check(): | 227 def _Check(): |
| 195 stdout = self.RunGsUtil([ | 228 stdout = self.RunGsUtil([ |
| 196 'du', '-X', fpath, suri(bucket_uri)], return_stdout=True) | 229 'du', '-X', fpath, suri(bucket_uri)], return_stdout=True) |
| 197 self.assertSetEqual(set(stdout.splitlines()), set([ | 230 self.assertSetEqual(set(stdout.splitlines()), set([ |
| 198 '%-10s %s' % (5, suri(obj_uris[0])), | 231 '%-10s %s' % (5, suri(obj_uris[0])), |
| 199 '%-10s %s' % (4, suri(obj_uris[3])), | 232 '%-10s %s' % (4, suri(obj_uris[3])), |
| 200 '%-10s %s/sub1/sub2/' % (4, suri(bucket_uri)), | 233 '%-10s %s/sub1/sub2/' % (4, suri(bucket_uri)), |
| 201 '%-10s %s/sub1/' % (9, suri(bucket_uri)), | 234 '%-10s %s/sub1/' % (9, suri(bucket_uri)), |
| 202 ])) | 235 ])) |
| 203 _Check() | 236 _Check() |
| OLD | NEW |