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