| 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 compose command.""" |
| 14 | 16 |
| 15 import gslib.tests.testcase as testcase | 17 from __future__ import absolute_import |
| 16 | 18 |
| 17 from gslib.commands.compose import MAX_COMPOSE_ARITY | 19 from gslib.commands.compose import MAX_COMPOSE_ARITY |
| 18 from gslib.tests.util import HAS_S3_CREDS | 20 import gslib.tests.testcase as testcase |
| 21 from gslib.tests.testcase.integration_testcase import SkipForS3 |
| 19 from gslib.tests.util import ObjectToURI as suri | 22 from gslib.tests.util import ObjectToURI as suri |
| 20 from gslib.tests.util import unittest | |
| 21 | 23 |
| 22 | 24 |
| 25 @SkipForS3('S3 does not support object composition.') |
| 23 class TestCompose(testcase.GsUtilIntegrationTestCase): | 26 class TestCompose(testcase.GsUtilIntegrationTestCase): |
| 24 """Integration tests for compose command.""" | 27 """Integration tests for compose command.""" |
| 25 | 28 |
| 26 def check_n_ary_compose(self, num_components): | 29 def check_n_ary_compose(self, num_components): |
| 27 num_components = 2 | 30 """Tests composing num_components object.""" |
| 28 bucket_uri = self.CreateBucket() | 31 bucket_uri = self.CreateBucket() |
| 29 | 32 |
| 30 data_list = ['data-%d,' % i for i in xrange(num_components)] | 33 data_list = ['data-%d,' % i for i in xrange(num_components)] |
| 31 components = [self.CreateObject(bucket_uri=bucket_uri, contents=data).uri | 34 components = [self.CreateObject(bucket_uri=bucket_uri, contents=data).uri |
| 32 for data in data_list] | 35 for data in data_list] |
| 33 | 36 |
| 34 composite = bucket_uri.clone_replace_name(self.MakeTempName('obj')) | 37 composite = bucket_uri.clone_replace_name(self.MakeTempName('obj')) |
| 35 | 38 |
| 36 self.RunGsUtil(['compose'] + components + [composite.uri]) | 39 self.RunGsUtil(['compose'] + components + [composite.uri]) |
| 37 self.assertEqual(composite.get_contents_as_string(), ''.join(data_list)) | 40 self.assertEqual(composite.get_contents_as_string(), ''.join(data_list)) |
| 38 | 41 |
| 39 def test_compose_too_many_fails(self): | 42 def test_compose_too_many_fails(self): |
| 40 components = ['gs://b/component-obj'] * (MAX_COMPOSE_ARITY + 1) | 43 components = ['gs://b/component-obj'] * (MAX_COMPOSE_ARITY + 1) |
| 41 stderr = self.RunGsUtil(['compose'] + components + ['gs://b/composite-obj'], | 44 stderr = self.RunGsUtil(['compose'] + components + ['gs://b/composite-obj'], |
| 42 expected_status=1, return_stderr=True) | 45 expected_status=1, return_stderr=True) |
| 43 self.assertIn('command accepts at most', stderr) | 46 self.assertIn('command accepts at most', stderr) |
| 44 | 47 |
| 45 def test_compose_too_few_fails(self): | 48 def test_compose_too_few_fails(self): |
| 46 stderr = self.RunGsUtil( | 49 stderr = self.RunGsUtil( |
| 47 ['compose', 'gs://b/component-obj', 'gs://b/composite-obj'], | 50 ['compose', 'gs://b/component-obj', 'gs://b/composite-obj'], |
| 48 expected_status=1, return_stderr=True) | 51 expected_status=1, return_stderr=True) |
| 49 self.assertEquals( | 52 self.assertIn( |
| 50 'CommandException: "compose" requires at least 2 component objects.\n', | 53 'CommandException: "compose" requires at least 2 component objects.\n', |
| 51 stderr) | 54 stderr) |
| 52 | 55 |
| 53 def test_compose_between_buckets_fails(self): | 56 def test_compose_between_buckets_fails(self): |
| 54 target = 'gs://b/composite-obj' | 57 target = 'gs://b/composite-obj' |
| 55 offending_obj = 'gs://alt-b/obj2' | 58 offending_obj = 'gs://alt-b/obj2' |
| 56 components = ['gs://b/obj1', offending_obj] | 59 components = ['gs://b/obj1', offending_obj] |
| 57 stderr = self.RunGsUtil(['compose'] + components + [target], | 60 stderr = self.RunGsUtil(['compose'] + components + [target], |
| 58 expected_status=1, return_stderr=True) | 61 expected_status=1, return_stderr=True) |
| 59 expected_msg = ( | 62 expected_msg = ( |
| 60 'Composing gs://b/composite-obj from 2 component objects.\n' | 63 'CommandException: GCS does ' |
| 61 'BotoClientError: GCS does not support inter-bucket composing.\n') | 64 'not support inter-bucket composing.\n') |
| 62 self.assertEquals(expected_msg, stderr) | 65 self.assertIn(expected_msg, stderr) |
| 63 | |
| 64 @unittest.skipUnless(HAS_S3_CREDS, 'Test requires S3 credentials.') | |
| 65 def test_compose_non_gcs_target(self): | |
| 66 stderr = self.RunGsUtil(['compose', 'gs://b/o1', 'gs://b/o2', 's3://b/o3'], | |
| 67 expected_status=1, return_stderr=True) | |
| 68 expected_msg = ('CommandException: "compose" called on URI with ' | |
| 69 'unsupported provider (%s).\n' % 's3://b/o3') | |
| 70 self.assertEquals(expected_msg, stderr) | |
| 71 | |
| 72 @unittest.skipUnless(HAS_S3_CREDS, 'Test requires S3 credentials.') | |
| 73 def test_compose_non_gcs_component(self): | |
| 74 stderr = self.RunGsUtil(['compose', 'gs://b/o1', 's3://b/o2', 'gs://b/o3'], | |
| 75 expected_status=1, return_stderr=True) | |
| 76 expected_msg = ('CommandException: "compose" called on URI with ' | |
| 77 'unsupported provider (%s).\n' % 's3://b/o2') | |
| 78 self.assertEquals(expected_msg, stderr) | |
| 79 | 66 |
| 80 def test_versioned_target_disallowed(self): | 67 def test_versioned_target_disallowed(self): |
| 81 stderr = self.RunGsUtil( | 68 stderr = self.RunGsUtil( |
| 82 ['compose', 'gs://b/o1', 'gs://b/o2', 'gs://b/o3#1234'], | 69 ['compose', 'gs://b/o1', 'gs://b/o2', 'gs://b/o3#1234'], |
| 83 expected_status=1, return_stderr=True) | 70 expected_status=1, return_stderr=True) |
| 84 expected_msg = ('CommandException: A version-specific URI\n(%s)\n' | 71 expected_msg = ('CommandException: A version-specific URL (%s) ' |
| 85 'cannot be the destination for gsutil compose - abort.\n' | 72 'cannot be the destination for gsutil compose - abort.' |
| 86 % 'gs://b/o3#1234') | 73 % 'gs://b/o3#1234') |
| 87 self.assertEquals(expected_msg, stderr) | 74 self.assertIn(expected_msg, stderr) |
| 88 | |
| 89 | 75 |
| 90 def test_simple_compose(self): | 76 def test_simple_compose(self): |
| 91 self.check_n_ary_compose(2) | 77 self.check_n_ary_compose(2) |
| 92 | 78 |
| 93 def test_maximal_compose(self): | 79 def test_maximal_compose(self): |
| 94 self.check_n_ary_compose(MAX_COMPOSE_ARITY) | 80 self.check_n_ary_compose(MAX_COMPOSE_ARITY) |
| 95 | 81 |
| 96 def test_compose_with_wildcard(self): | 82 def test_compose_with_wildcard(self): |
| 83 """Tests composing objects with a wildcarded URI.""" |
| 97 bucket_uri = self.CreateBucket() | 84 bucket_uri = self.CreateBucket() |
| 98 | 85 |
| 99 component1 = self.CreateObject( | 86 component1 = self.CreateObject( |
| 100 bucket_uri=bucket_uri, contents='hello ', object_name='component1') | 87 bucket_uri=bucket_uri, contents='hello ', object_name='component1') |
| 101 component2 = self.CreateObject( | 88 component2 = self.CreateObject( |
| 102 bucket_uri=bucket_uri, contents='world!', object_name='component2') | 89 bucket_uri=bucket_uri, contents='world!', object_name='component2') |
| 103 | 90 |
| 104 composite = bucket_uri.clone_replace_name(self.MakeTempName('obj')) | 91 composite = bucket_uri.clone_replace_name(self.MakeTempName('obj')) |
| 105 | 92 |
| 106 self.RunGsUtil(['compose', component1.uri, component2.uri, composite.uri]) | 93 self.RunGsUtil(['compose', component1.uri, component2.uri, composite.uri]) |
| 107 self.assertEqual(composite.get_contents_as_string(), 'hello world!') | 94 self.assertEqual(composite.get_contents_as_string(), 'hello world!') |
| 95 |
| 96 def test_compose_with_precondition(self): |
| 97 """Tests composing objects with a destination precondition.""" |
| 98 # Tests that cp -v option handles the if-generation-match header correctly. |
| 99 bucket_uri = self.CreateVersionedBucket() |
| 100 k1_uri = self.CreateObject(bucket_uri=bucket_uri, contents='data1') |
| 101 k2_uri = self.CreateObject(bucket_uri=bucket_uri, contents='data2') |
| 102 g1 = k1_uri.generation |
| 103 |
| 104 gen_match_header = 'x-goog-if-generation-match:%s' % g1 |
| 105 # Append object 1 and 2 |
| 106 self.RunGsUtil(['-h', gen_match_header, 'compose', suri(k1_uri), |
| 107 suri(k2_uri), suri(k1_uri)]) |
| 108 |
| 109 # Second compose should fail the precondition. |
| 110 stderr = self.RunGsUtil(['-h', gen_match_header, 'compose', suri(k1_uri), |
| 111 suri(k2_uri), suri(k1_uri)], |
| 112 return_stderr=True, expected_status=1) |
| 113 |
| 114 self.assertIn('PreconditionException', stderr) |
| 115 |
| 116 |
| 117 class TestCompatibleCompose(testcase.GsUtilIntegrationTestCase): |
| 118 def test_compose_non_gcs_target(self): |
| 119 stderr = self.RunGsUtil(['compose', 'gs://b/o1', 'gs://b/o2', 's3://b/o3'], |
| 120 expected_status=1, return_stderr=True) |
| 121 expected_msg = ('CommandException: "compose" called on URL with ' |
| 122 'unsupported provider (%s).\n' % 's3://b/o3') |
| 123 self.assertIn(expected_msg, stderr) |
| 124 |
| 125 def test_compose_non_gcs_component(self): |
| 126 stderr = self.RunGsUtil(['compose', 'gs://b/o1', 's3://b/o2', 'gs://b/o3'], |
| 127 expected_status=1, return_stderr=True) |
| 128 expected_msg = ('CommandException: "compose" called on URL with ' |
| 129 'unsupported provider (%s).\n' % 's3://b/o2') |
| 130 self.assertIn(expected_msg, stderr) |
| 131 |
| OLD | NEW |