Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/ec2/test_bundle_instance.py
1567 views
1
#!/usr/bin/env python
2
# Copyright 2013 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 base64
15
import datetime
16
17
import awscli.customizations.ec2.bundleinstance
18
from awscli.testutils import mock, BaseAWSCommandParamsTest
19
from awscli.compat import StringIO
20
21
22
class TestBundleInstance(BaseAWSCommandParamsTest):
23
24
prefix = 'ec2 bundle-instance'
25
26
POLICY = (
27
'{"expiration": "2013-08-10T00:00:00.000000Z",'
28
'"conditions": [{"bucket": "mybucket"},{"acl": '
29
'"ec2-bundle-read"},["starts-with", "$key", "foobar"]]}')
30
POLICY_SIGNATURE = 'ynxybUMv9YuGbPl7HZ8AFJW/2t0='
31
32
def setUp(self):
33
super(TestBundleInstance, self).setUp()
34
# This mocks out datetime.datetime.now() so that it always
35
# returns the same datetime object. This is because this value
36
# is embedded into the policy file that is generated and we
37
# don't what the policy or its signature to change each time
38
# we run the test.
39
self.datetime_patcher = mock.patch.object(
40
awscli.customizations.ec2.bundleinstance.datetime, 'datetime',
41
mock.Mock(wraps=datetime.datetime)
42
)
43
mocked_datetime = self.datetime_patcher.start()
44
mocked_datetime.now.return_value = datetime.datetime(2013, 8, 9)
45
46
def tearDown(self):
47
super(TestBundleInstance, self).tearDown()
48
self.datetime_patcher.stop()
49
50
def test_no_policy_provided(self):
51
args = ' --instance-id i-12345678 --owner-akid AKIAIOSFODNN7EXAMPLE'
52
args += ' --owner-sak wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
53
args += ' --bucket mybucket --prefix foobar'
54
args_list = (self.prefix + args).split()
55
result = {'InstanceId': 'i-12345678',
56
'Storage': {
57
'S3': {
58
'Bucket': 'mybucket',
59
'Prefix': 'foobar',
60
'AWSAccessKeyId': 'AKIAIOSFODNN7EXAMPLE',
61
'UploadPolicy': self.POLICY,
62
'UploadPolicySignature': self.POLICY_SIGNATURE}
63
}
64
}
65
self.assert_params_for_cmd(args_list, result)
66
67
68
def test_policy_provided(self):
69
policy = '{"notarealpolicy":true}'
70
base64policy = base64.encodebytes(policy.encode('latin-1')).strip().decode('utf-8')
71
policy_signature = 'a5SmoLOxoM0MHpOdC25nE7KIafg='
72
args = ' --instance-id i-12345678 --owner-akid AKIAIOSFODNN7EXAMPLE'
73
args += ' --owner-sak wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
74
args += ' --bucket mybucket --prefix foobar --policy %s' % policy
75
args_list = (self.prefix + args).split()
76
result = {'InstanceId': 'i-12345678',
77
'Storage': {
78
'S3': {
79
'Bucket': 'mybucket',
80
'Prefix': 'foobar',
81
'AWSAccessKeyId': 'AKIAIOSFODNN7EXAMPLE',
82
'UploadPolicy': '{"notarealpolicy":true}',
83
'UploadPolicySignature': policy_signature}
84
}
85
}
86
self.assert_params_for_cmd(args_list, result)
87
88
def test_both(self):
89
captured = StringIO()
90
json = """{"S3":{"Bucket":"foobar","Prefix":"fiebaz"}}"""
91
args = ' --instance-id i-12345678 --owner-aki blah --owner-sak blah --storage %s' % json
92
args_list = (self.prefix + args).split()
93
_, stderr, _ = self.assert_params_for_cmd(args_list, expected_rc=255)
94
expected_err_msg = (
95
'Mixing the --storage option with the simple, '
96
'scalar options is not recommended'
97
)
98
self.assertIn(expected_err_msg, stderr)
99
100
101
if __name__ == "__main__":
102
unittest.main()
103
104