| 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 """Integration tests for mv command.""" |
| 16 |
| 17 from __future__ import absolute_import |
| 14 | 18 |
| 15 import gslib.tests.testcase as testcase | 19 import gslib.tests.testcase as testcase |
| 16 from gslib.util import Retry | |
| 17 from gslib.tests.util import ObjectToURI as suri | 20 from gslib.tests.util import ObjectToURI as suri |
| 18 | 21 |
| 19 | 22 |
| 20 class TestMv(testcase.GsUtilIntegrationTestCase): | 23 class TestMv(testcase.GsUtilIntegrationTestCase): |
| 21 """Integration tests for mv command.""" | 24 """Integration tests for mv command.""" |
| 22 | 25 |
| 23 def test_moving(self): | 26 def test_moving(self): |
| 24 # Create two buckets, one with 2 objects and one with 0 objects, and verify. | 27 """Tests moving two buckets, one with 2 objects and one with 0 objects.""" |
| 25 bucket1_uri = self.CreateBucket(test_objects=2) | 28 bucket1_uri = self.CreateBucket(test_objects=2) |
| 26 # Use @Retry as hedge against bucket listing eventual consistency. | 29 self.AssertNObjectsInBucket(bucket1_uri, 2) |
| 27 @Retry(AssertionError, tries=3, timeout_secs=1) | |
| 28 def _Check1(): | |
| 29 stdout = self.RunGsUtil(['ls', suri(bucket1_uri)], return_stdout=True) | |
| 30 self.assertNumLines(stdout, 2) | |
| 31 _Check1() | |
| 32 bucket2_uri = self.CreateBucket() | 30 bucket2_uri = self.CreateBucket() |
| 33 # Use @Retry as hedge against bucket listing eventual consistency. | 31 self.AssertNObjectsInBucket(bucket2_uri, 0) |
| 34 @Retry(AssertionError, tries=3, timeout_secs=1) | |
| 35 def _Check2(): | |
| 36 stdout = self.RunGsUtil(['ls', suri(bucket2_uri)], return_stdout=True) | |
| 37 self.assertNumLines(stdout, 0) | |
| 38 _Check2() | |
| 39 | 32 |
| 40 # Move two objects from bucket1 to bucket2. | 33 # Move two objects from bucket1 to bucket2. |
| 41 objs = [bucket1_uri.clone_replace_key(key).versionless_uri | 34 objs = [bucket1_uri.clone_replace_key(key).versionless_uri |
| 42 for key in bucket1_uri.list_bucket()] | 35 for key in bucket1_uri.list_bucket()] |
| 43 cmd = (['-m', 'mv'] + objs + [suri(bucket2_uri)]) | 36 cmd = (['-m', 'mv'] + objs + [suri(bucket2_uri)]) |
| 44 stderr = self.RunGsUtil(cmd, return_stderr=True) | 37 stderr = self.RunGsUtil(cmd, return_stderr=True) |
| 45 self.assertEqual(stderr.count('Copying'), 2) | 38 self.assertEqual(stderr.count('Copying'), 2) |
| 46 self.assertEqual(stderr.count('Removing'), 2) | 39 self.assertEqual(stderr.count('Removing'), 2) |
| 47 | 40 |
| 48 # Verify objects were moved. | 41 self.AssertNObjectsInBucket(bucket1_uri, 0) |
| 49 # Use @Retry as hedge against bucket listing eventual consistency. | 42 self.AssertNObjectsInBucket(bucket2_uri, 2) |
| 50 @Retry(AssertionError, tries=3, timeout_secs=1) | |
| 51 def _Check3(): | |
| 52 stdout = self.RunGsUtil(['ls', suri(bucket1_uri)], return_stdout=True) | |
| 53 self.assertNumLines(stdout, 0) | |
| 54 stdout = self.RunGsUtil(['ls', suri(bucket2_uri)], return_stdout=True) | |
| 55 self.assertNumLines(stdout, 2) | |
| 56 _Check3() | |
| 57 | 43 |
| 58 # Remove one of the objects. | 44 # Remove one of the objects. |
| 59 objs = [bucket2_uri.clone_replace_key(key).versionless_uri | 45 objs = [bucket2_uri.clone_replace_key(key).versionless_uri |
| 60 for key in bucket2_uri.list_bucket()] | 46 for key in bucket2_uri.list_bucket()] |
| 61 obj1 = objs[0] | 47 obj1 = objs[0] |
| 62 self.RunGsUtil(['rm', obj1]) | 48 self.RunGsUtil(['rm', obj1]) |
| 63 | 49 |
| 64 # Verify there are now 1 and 0 objects. | 50 self.AssertNObjectsInBucket(bucket1_uri, 0) |
| 65 # Use @Retry as hedge against bucket listing eventual consistency. | 51 self.AssertNObjectsInBucket(bucket2_uri, 1) |
| 66 @Retry(AssertionError, tries=3, timeout_secs=1) | |
| 67 def _Check4(): | |
| 68 stdout = self.RunGsUtil(['ls', suri(bucket1_uri)], return_stdout=True) | |
| 69 self.assertNumLines(stdout, 0) | |
| 70 stdout = self.RunGsUtil(['ls', suri(bucket2_uri)], return_stdout=True) | |
| 71 self.assertNumLines(stdout, 1) | |
| 72 _Check4() | |
| 73 | 52 |
| 74 # Move the 1 remaining object back. | 53 # Move the 1 remaining object back. |
| 75 objs = [suri(bucket2_uri.clone_replace_key(key)) | 54 objs = [suri(bucket2_uri.clone_replace_key(key)) |
| 76 for key in bucket2_uri.list_bucket()] | 55 for key in bucket2_uri.list_bucket()] |
| 77 cmd = (['-m', 'mv'] + objs + [suri(bucket1_uri)]) | 56 cmd = (['-m', 'mv'] + objs + [suri(bucket1_uri)]) |
| 78 stderr = self.RunGsUtil(cmd, return_stderr=True) | 57 stderr = self.RunGsUtil(cmd, return_stderr=True) |
| 79 self.assertEqual(stderr.count('Copying'), 1) | 58 self.assertEqual(stderr.count('Copying'), 1) |
| 80 self.assertEqual(stderr.count('Removing'), 1) | 59 self.assertEqual(stderr.count('Removing'), 1) |
| 81 | 60 |
| 82 # Verify object moved. | 61 self.AssertNObjectsInBucket(bucket1_uri, 1) |
| 83 # Use @Retry as hedge against bucket listing eventual consistency. | 62 self.AssertNObjectsInBucket(bucket2_uri, 0) |
| 84 @Retry(AssertionError, tries=3, timeout_secs=1) | 63 |
| 85 def _Check5(): | 64 def test_move_dir_to_bucket(self): |
| 86 stdout = self.RunGsUtil(['ls', suri(bucket1_uri)], return_stdout=True) | 65 """Tests moving a local directory to a bucket.""" |
| 87 self.assertNumLines(stdout, 1) | 66 bucket_uri = self.CreateBucket() |
| 88 stdout = self.RunGsUtil(['ls', suri(bucket2_uri)], return_stdout=True) | 67 dir_to_move = self.CreateTempDir(test_files=2) |
| 89 self.assertNumLines(stdout, 0) | 68 self.RunGsUtil(['mv', dir_to_move, suri(bucket_uri)]) |
| 90 _Check5() | 69 self.AssertNObjectsInBucket(bucket_uri, 2) |
| 70 |
| 71 |
| 72 |
| OLD | NEW |