Path: blob/develop/tests/functional/ec2/test_bundle_instance.py
1567 views
#!/usr/bin/env python1# Copyright 2013 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 base6414import datetime1516import awscli.customizations.ec2.bundleinstance17from awscli.testutils import mock, BaseAWSCommandParamsTest18from awscli.compat import StringIO192021class TestBundleInstance(BaseAWSCommandParamsTest):2223prefix = 'ec2 bundle-instance'2425POLICY = (26'{"expiration": "2013-08-10T00:00:00.000000Z",'27'"conditions": [{"bucket": "mybucket"},{"acl": '28'"ec2-bundle-read"},["starts-with", "$key", "foobar"]]}')29POLICY_SIGNATURE = 'ynxybUMv9YuGbPl7HZ8AFJW/2t0='3031def setUp(self):32super(TestBundleInstance, self).setUp()33# This mocks out datetime.datetime.now() so that it always34# returns the same datetime object. This is because this value35# is embedded into the policy file that is generated and we36# don't what the policy or its signature to change each time37# we run the test.38self.datetime_patcher = mock.patch.object(39awscli.customizations.ec2.bundleinstance.datetime, 'datetime',40mock.Mock(wraps=datetime.datetime)41)42mocked_datetime = self.datetime_patcher.start()43mocked_datetime.now.return_value = datetime.datetime(2013, 8, 9)4445def tearDown(self):46super(TestBundleInstance, self).tearDown()47self.datetime_patcher.stop()4849def test_no_policy_provided(self):50args = ' --instance-id i-12345678 --owner-akid AKIAIOSFODNN7EXAMPLE'51args += ' --owner-sak wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'52args += ' --bucket mybucket --prefix foobar'53args_list = (self.prefix + args).split()54result = {'InstanceId': 'i-12345678',55'Storage': {56'S3': {57'Bucket': 'mybucket',58'Prefix': 'foobar',59'AWSAccessKeyId': 'AKIAIOSFODNN7EXAMPLE',60'UploadPolicy': self.POLICY,61'UploadPolicySignature': self.POLICY_SIGNATURE}62}63}64self.assert_params_for_cmd(args_list, result)656667def test_policy_provided(self):68policy = '{"notarealpolicy":true}'69base64policy = base64.encodebytes(policy.encode('latin-1')).strip().decode('utf-8')70policy_signature = 'a5SmoLOxoM0MHpOdC25nE7KIafg='71args = ' --instance-id i-12345678 --owner-akid AKIAIOSFODNN7EXAMPLE'72args += ' --owner-sak wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'73args += ' --bucket mybucket --prefix foobar --policy %s' % policy74args_list = (self.prefix + args).split()75result = {'InstanceId': 'i-12345678',76'Storage': {77'S3': {78'Bucket': 'mybucket',79'Prefix': 'foobar',80'AWSAccessKeyId': 'AKIAIOSFODNN7EXAMPLE',81'UploadPolicy': '{"notarealpolicy":true}',82'UploadPolicySignature': policy_signature}83}84}85self.assert_params_for_cmd(args_list, result)8687def test_both(self):88captured = StringIO()89json = """{"S3":{"Bucket":"foobar","Prefix":"fiebaz"}}"""90args = ' --instance-id i-12345678 --owner-aki blah --owner-sak blah --storage %s' % json91args_list = (self.prefix + args).split()92_, stderr, _ = self.assert_params_for_cmd(args_list, expected_rc=255)93expected_err_msg = (94'Mixing the --storage option with the simple, '95'scalar options is not recommended'96)97self.assertIn(expected_err_msg, stderr)9899100if __name__ == "__main__":101unittest.main()102103104