1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#!/usr/bin/env python
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import os
import shutil
import tempfile
from awscli.testutils import BaseAWSCommandParamsTest, BaseAWSHelpOutputTest
class TestGetObject(BaseAWSCommandParamsTest):
prefix = ['s3api', 'select-object-content']
def setUp(self):
super(TestGetObject, self).setUp()
self.parsed_response = {'Payload': self.create_fake_payload()}
self._tempdir = tempfile.mkdtemp()
def tearDown(self):
super(TestGetObject, self).tearDown()
shutil.rmtree(self._tempdir)
def create_fake_payload(self):
yield {'Records': {'Payload': b'a,b,c,d\n'}}
# These next two events are ignored because they aren't
# "Records".
yield {
'Progress': {
'Details': {
'BytesScanned': 1048576,
'BytesProcessed': 37748736,
}
}
}
yield {'Records': {'Payload': b'e,f,g,h\n'}}
yield {
'Stats': {
'Details': {
'BytesProcessed': 62605400,
'BytesScanned': 1662276,
}
}
}
yield {'End': {}}
def test_can_stream_to_file(self):
filename = os.path.join(self._tempdir, 'outfile')
cmdline = self.prefix[::]
cmdline.extend(['--bucket', 'mybucket'])
cmdline.extend(['--key', 'mykey'])
cmdline.extend(['--expression', 'SELECT * FROM S3Object'])
cmdline.extend(['--expression-type', 'SQL'])
cmdline.extend(['--request-progress', 'Enabled=True'])
cmdline.extend(
['--input-serialization', '{"CSV": {}, "CompressionType": "GZIP"}']
)
cmdline.extend(['--output-serialization', '{"CSV": {}}'])
cmdline.extend([filename])
expected_params = {
'Bucket': 'mybucket',
'Key': 'mykey',
'Expression': 'SELECT * FROM S3Object',
'ExpressionType': 'SQL',
'InputSerialization': {'CSV': {}, 'CompressionType': 'GZIP'},
'OutputSerialization': {'CSV': {}},
'RequestProgress': {'Enabled': True},
}
stdout = self.assert_params_for_cmd(cmdline, expected_params)[0]
self.assertEqual(stdout, '')
with open(filename) as f:
contents = f.read()
self.assertEqual(contents, ('a,b,c,d\n' 'e,f,g,h\n'))
def test_errors_are_propagated(self):
self.http_response.status_code = 400
self.parsed_response = {
'Error': {
'Code': 'CastFailed',
'Message': 'Attempt to convert from one data type to another',
}
}
cmdline = self.prefix + [
'--bucket',
'mybucket',
'--key',
'mykey',
'--expression',
'SELECT * FROM S3Object',
'--expression-type',
'SQL',
'--request-progress',
'Enabled=True',
'--input-serialization',
'{"CSV": {}, "CompressionType": "GZIP"}',
'--output-serialization',
'{"CSV": {}}',
os.path.join(self._tempdir, 'outfile'),
]
expected_params = {
'Bucket': 'mybucket',
'Key': 'mykey',
'Expression': 'SELECT * FROM S3Object',
'ExpressionType': 'SQL',
'InputSerialization': {'CSV': {}, 'CompressionType': 'GZIP'},
'OutputSerialization': {'CSV': {}},
'RequestProgress': {'Enabled': True},
}
self.assert_params_for_cmd(
cmd=cmdline,
params=expected_params,
expected_rc=254,
stderr_contains=(
'An error occurred (CastFailed) when '
'calling the SelectObjectContent operation'
),
)
class TestHelpOutput(BaseAWSHelpOutputTest):
def test_output(self):
self.driver.main(['s3api', 'select-object-content', 'help'])
# We don't want to be super picky because the wording may change
# We just want to verify the Output section was customized.
self.assert_contains(
'Output\n======\n' 'This command generates no output'
)
self.assert_not_contains('[outfile')
self.assert_contains('outfile')
|