Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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.
from awscli.testutils import BaseAWSCommandParamsTest
from awscli.compat import six
from six.moves import cStringIO
import mock
class TestSendEmail(BaseAWSCommandParamsTest):
prefix = 'ses send-email'
def test_plain_text(self):
args = (' --subject This_is_a_test --from foo@bar.com'
' --to fie@baz.com --text This_is_the_message')
args_list = (self.prefix + args).split()
result = {
'Source': 'foo@bar.com',
'Destination': {'ToAddresses': ['fie@baz.com']},
'Message': {'Body': {'Text': {'Data': 'This_is_the_message'}},
'Subject': {'Data': 'This_is_a_test'}}}
self.assert_params_for_cmd(args_list, result)
def test_plain_text_multiple_to(self):
args = (' --subject This_is_a_test --from foo@bar.com'
def test_can_submit(self):
fileinfo = FileInfo(
src=self.bucket+'/'+self.key, dest=self.filename,
operation_name='download')
self.assertTrue(
self.transfer_request_submitter.can_submit(fileinfo))
fileinfo.operation_name = 'foo'
self.assertFalse(
self.transfer_request_submitter.can_submit(fileinfo))
def test_dry_run(self):
self.cli_params['dryrun'] = True
self.transfer_request_submitter = UploadRequestSubmitter(
self.transfer_manager, self.result_queue, self.cli_params)
fileinfo = FileInfo(
src=self.filename, src_type='local', operation_name='upload',
dest=self.bucket + '/' + self.key, dest_type='s3')
self.transfer_request_submitter.submit(fileinfo)
result = self.result_queue.get()
self.assertIsInstance(result, DryRunResult)
self.assertEqual(result.transfer_type, 'upload')
self.assertTrue(result.src.endswith(self.filename))
self.assertEqual(result.dest, 's3://' + self.bucket + '/' + self.key)
def test_submit_with_expected_size_provided(self):
provided_size = 100
self.cli_params['expected_size'] = provided_size
fileinfo = FileInfo(
src=self.filename, dest=self.bucket+'/'+self.key)
self.transfer_request_submitter.submit(fileinfo)
upload_call_kwargs = self.transfer_manager.upload.call_args[1]
ref_subscribers = [
ProvideSizeSubscriber,
UploadStreamResultSubscriber
]
actual_subscribers = upload_call_kwargs['subscribers']
self.assertEqual(len(ref_subscribers), len(actual_subscribers))
for i, actual_subscriber in enumerate(actual_subscribers):
self.assertIsInstance(actual_subscriber, ref_subscribers[i])
# The ProvideSizeSubscriber should be providing the correct size
self.assertEqual(actual_subscribers[0].size, provided_size)
def create_clidriver():
driver = awscli.clidriver.create_clidriver()
session = driver.session
data_path = session.get_config_variable('data_path').split(os.pathsep)
if not data_path:
data_path = []
_LOADER.search_paths.extend(data_path)
session.register_component('data_loader', _LOADER)
return driver
def test_aws_configure_in_error_message_no_credentials(self):
driver = create_clidriver()
def raise_exception(*args, **kwargs):
raise NoCredentialsError()
driver.session.register(
'building-command-table',
lambda command_table, **kwargs: \
command_table.__setitem__('ec2', raise_exception))
with mock.patch('sys.stderr') as f:
driver.main('ec2 describe-instances'.split())
self.assertEqual(
f.write.call_args_list[0][0][0],
'Unable to locate credentials. '
'You can configure credentials by running "aws configure".')
def test_event_emission_for_top_level_params(self):
driver = create_clidriver()
# --unknown-foo is an known arg, so we expect a 255 rc.
rc = driver.main('ec2 describe-instances --unknown-arg foo'.split())
self.assertEqual(rc, 255)
self.assertIn('Unknown options: --unknown-arg', self.stderr.getvalue())
# The argument table is memoized in the CLIDriver object. So
# when we call main() above, it will get created and cached
# and the argument table won't get created again (and therefore
# the building-top-level-params event will not get generated again).
# So, for this test we need to create a new driver object.
driver = create_clidriver()
driver.session.register(
'building-top-level-params', self.inject_new_param)
driver.session.register(
'top-level-args-parsed',
lambda parsed_args, **kwargs: args_seen.append(parsed_args))
#
# 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.
from awscli.testutils import unittest
from botocore.compat import OrderedDict, json
from awscli.customizations.datapipeline import translator
# Thoughout these tests, 'df' refers to the condensed JSON definition format
# that the user provides and API refers to the format expected by the API.
class TestTranslatePipelineDefinitions(unittest.TestCase):
maxDiff = None
def load_def(self, json_string):
return json.loads(json_string, object_pairs_hook=OrderedDict)
def test_convert_schedule_df_to_api(self):
definition = self.load_def("""{"objects": [
{
"id" : "S3ToS3Copy",
"type" : "CopyActivity",
"schedule" : { "ref" : "CopyPeriod" },
"input" : { "ref" : "InputData" },
"output" : { "ref" : "OutputData" }
}
]}""")
actual = translator.definition_to_api(definition)
# 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 datetime
from awscli.customizations.s3.filegenerator import FileStat
from awscli.customizations.s3.syncstrategy.exacttimestamps import \
ExactTimestampsSync
from awscli.testutils import unittest
class TestExactTimestampsSync(unittest.TestCase):
def setUp(self):
self.sync_strategy = ExactTimestampsSync()
def test_compare_exact_timestamps_dest_older(self):
"""
Confirm that same-sized files are synced when
the destination is older than the source and
`exact_timestamps` is set.
"""
time_src = datetime.datetime.now()
time_dst = time_src - datetime.timedelta(days=1)
src_file = FileStat(src='', dest='',
compare_key='test.py', size=10,
last_update=time_src, src_type='s3',
dest_type='local', operation_name='download')
'type': 's3'},
'dir_op': True, 'use_src_name': True}
file_stats = FileGenerator(self.client, '', True).call(input_local_dir)
all_filenames = self.filenames + self.symlink_files
all_filenames.sort()
result_list = []
for file_stat in file_stats:
result_list.append(getattr(file_stat, 'src'))
self.assertEqual(len(result_list), len(all_filenames))
# Just check to make sure the right local files are generated.
for i in range(len(result_list)):
filename = six.text_type(os.path.abspath(all_filenames[i]))
self.assertEqual(result_list[i], filename)
class TestListFilesLocally(unittest.TestCase):
maxDiff = None
def setUp(self):
self.directory = six.text_type(tempfile.mkdtemp())
def tearDown(self):
shutil.rmtree(self.directory)
@mock.patch('os.listdir')
def test_error_raised_on_decoding_error(self, listdir_mock):
# On Python3, sys.getdefaultencoding
file_generator = FileGenerator(None, None, None)
# utf-8 encoding for U+2713.
listdir_mock.return_value = [b'\xe2\x9c\x93']
list(file_generator.list_files(self.directory, dir_op=True))
# Ensure the message was added to the result queue and is