Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/iot/test_outfile.py
1567 views
1
#!/usr/bin/env python
2
# Copyright 2015 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
from awscli.testutils import BaseAWSCommandParamsTest, FileCreator
15
import os
16
17
18
class TestOutFileQueryArguments(BaseAWSCommandParamsTest):
19
def setUp(self):
20
self.files = FileCreator()
21
super(TestOutFileQueryArguments, self).setUp()
22
23
def tearDown(self):
24
self.files.remove_all()
25
super(TestOutFileQueryArguments, self).tearDown()
26
27
def test_saves_cert_to_file_for_create_certificate_from_csr(self):
28
self.parsed_response = {
29
'certificatePem': 'cert...',
30
'ResponseMetadata': {
31
'HTTPStatusCode': 200,
32
'RequestId': 'request-id'
33
}
34
}
35
outfile = self.files.full_path('cert.pem')
36
cmdline = 'iot create-certificate-from-csr'
37
cmdline += ' --certificate-signing-request "abc"'
38
cmdline += ' --certificate-pem-outfile ' + outfile
39
self.run_cmd(cmdline, 0)
40
self.assertTrue(os.path.exists(outfile))
41
with open(outfile) as fp:
42
self.assertEqual('cert...', fp.read())
43
44
def test_saves_files_for_create_keys_and_cert(self):
45
self.parsed_response = {
46
'certificatePem': 'cert...',
47
'keyPair': {
48
'PublicKey': 'public',
49
'PrivateKey': 'private'
50
},
51
'ResponseMetadata': {
52
'HTTPStatusCode': 200,
53
'RequestId': 'request-id'
54
}
55
}
56
out_cert = self.files.full_path('cert.pem')
57
out_pub = self.files.full_path('key_rsa.pub')
58
out_priv = self.files.full_path('key_rsa')
59
cmdline = 'iot create-keys-and-certificate'
60
cmdline += ' --certificate-pem-outfile ' + out_cert
61
cmdline += ' --public-key-outfile ' + out_pub
62
cmdline += ' --private-key-outfile ' + out_priv
63
self.run_cmd(cmdline, 0)
64
self.assertTrue(os.path.exists(out_cert))
65
self.assertTrue(os.path.exists(out_pub))
66
self.assertTrue(os.path.exists(out_priv))
67
with open(out_cert) as fp:
68
self.assertEqual('cert...', fp.read())
69
with open(out_pub) as fp:
70
self.assertEqual('public', fp.read())
71
with open(out_priv) as fp:
72
self.assertEqual('private', fp.read())
73
74
def test_bad_response(self):
75
outfile = self.files.full_path('cert.pem')
76
self.parsed_response = {
77
'Error': {'Code': 'v1', 'Message': 'v2', 'Type': 'v3'},
78
'ResponseMetadata': {
79
'HTTPStatusCode': 403,
80
'RequestId': 'request-id'
81
}
82
}
83
self.http_response.status_code = 403
84
cmdline = 'iot create-certificate-from-csr'
85
cmdline += ' --certificate-signing-request "abc"'
86
cmdline += ' --certificate-pem-outfile ' + outfile
87
# The error message should be in the stderr.
88
self.assert_params_for_cmd(
89
cmdline,
90
stderr_contains=self.parsed_response['Error']['Message'],
91
expected_rc=255)
92
93
def test_ensures_file_is_writable_before_sending(self):
94
outfile = os.sep.join(['', 'does', 'not', 'exist_', 'file.txt'])
95
self.parsed_response = {}
96
cmdline = 'iot create-certificate-from-csr'
97
cmdline += ' --certificate-signing-request "abc"'
98
cmdline += ' --certificate-pem-outfile ' + outfile
99
self.assert_params_for_cmd(
100
cmdline,
101
stderr_contains='Unable to write to file: ',
102
expected_rc=255)
103
104