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