File: test_utils.py

package info (click to toggle)
awscli 2.31.35-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 156,692 kB
  • sloc: python: 213,816; xml: 14,082; makefile: 189; sh: 178; javascript: 8
file content (321 lines) | stat: -rw-r--r-- 12,270 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# 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.
import argparse
import io

from botocore.exceptions import ClientError
from botocore.model import Shape

from awscli.customizations import utils
from awscli.customizations.exceptions import ParamValidationError
from awscli.testutils import BaseAWSHelpOutputTest, mock, unittest


class FakeParsedArgs:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)


class TestCommandTableRenames(BaseAWSHelpOutputTest):
    def test_rename_command_table(self):
        handler = lambda command_table, **kwargs: utils.rename_command(
            command_table, 'ec2', 'fooec2'
        )
        # Verify that we can rename a top level command.
        self.session.register('building-command-table.main', handler)
        self.driver.main(['fooec2', 'help'])
        self.assert_contains('fooec2')

        # We can also see subcommands help as well.
        self.driver.main(['fooec2', 'run-instances', 'help'])
        self.assert_contains('run-instances')


class TestCommandTableAlias(BaseAWSHelpOutputTest):
    def test_alias_command_table(self):
        old_name = 'cloudhsmv2'
        new_name = 'nopossiblewaythisisalreadythere'

        def handler(command_table, **kwargs):
            utils.alias_command(command_table, old_name, new_name)

        self._assert_command_exists(old_name, handler)
        self._assert_command_exists(new_name, handler)

        # Verify that the new name is documented
        self.driver.main(['help'])
        self.assert_contains(new_name)
        self.assert_not_contains(old_name)

    def _assert_command_exists(self, command_name, handler):
        # Verify that we can alias a top level command.
        self.session.register('building-command-table.main', handler)
        self.driver.main([command_name, 'help'])
        self.assert_contains(command_name)

        # We can also see subcommands help as well.
        self.driver.main([command_name, 'describe-clusters', 'help'])
        self.assert_contains('describe-clusters')


class TestHiddenAlias(unittest.TestCase):
    def test_not_marked_as_required_if_not_needed(self):
        original_arg_required = mock.Mock()
        original_arg_required.required = False
        arg_table = {'original': original_arg_required}
        utils.make_hidden_alias(arg_table, 'original', 'new-name')
        self.assertIn('new-name', arg_table)
        # Note: the _DOCUMENT_AS_REQUIRED is tested as a functional
        # test because it only affects how the doc synopsis is
        # rendered.
        self.assertFalse(arg_table['original'].required)
        self.assertFalse(arg_table['new-name'].required)

    def test_hidden_alias_marks_as_not_required(self):
        original_arg_required = mock.Mock()
        original_arg_required.required = True
        arg_table = {'original': original_arg_required}
        utils.make_hidden_alias(arg_table, 'original', 'new-name')
        self.assertIn('new-name', arg_table)
        self.assertFalse(arg_table['original'].required)
        self.assertFalse(arg_table['new-name'].required)


class TestValidateMututuallyExclusiveGroups(unittest.TestCase):
    def test_two_single_groups(self):
        # The most basic example of mutually exclusive args.
        # If foo is specified, but bar is not, then we're fine.
        parsed = FakeParsedArgs(foo='one', bar=None)
        utils.validate_mutually_exclusive(parsed, ['foo'], ['bar'])
        # If bar is specified and foo is not, then we're fine.
        parsed = FakeParsedArgs(foo=None, bar='one')
        utils.validate_mutually_exclusive(parsed, ['foo'], ['bar'])
        # But if we specify both foo and bar then we get an error.
        parsed = FakeParsedArgs(foo='one', bar='two')
        with self.assertRaises(ParamValidationError):
            utils.validate_mutually_exclusive(parsed, ['foo'], ['bar'])

    def test_multiple_groups(self):
        groups = (
            ['one', 'two', 'three'],
            ['foo', 'bar', 'baz'],
            ['qux', 'bad', 'morebad'],
        )
        # This is fine.
        parsed = FakeParsedArgs(foo='foo', bar='bar', baz='baz')
        utils.validate_mutually_exclusive(parsed, *groups)
        # But this is bad.
        parsed = FakeParsedArgs(foo='one', bar=None, qux='three')
        with self.assertRaises(ParamValidationError):
            utils.validate_mutually_exclusive(parsed, *groups)


class TestS3BucketExists(unittest.TestCase):
    def setUp(self):
        self.s3_client = mock.Mock()
        self.bucket_name = 'mybucket'
        self.error_response = {
            'Error': {'Code': '404', 'Message': 'Not Found'}
        }
        self.bucket_no_exists_error = ClientError(
            self.error_response, 'HeadBucket'
        )

    def test_bucket_exists(self):
        self.assertTrue(
            utils.s3_bucket_exists(self.s3_client, self.bucket_name)
        )

    def test_bucket_not_exists(self):
        self.s3_client.head_bucket.side_effect = self.bucket_no_exists_error
        self.assertFalse(
            utils.s3_bucket_exists(self.s3_client, self.bucket_name)
        )

    def test_bucket_exists_with_non_404(self):
        self.error_response['Error']['Code'] = '403'
        self.error_response['Error']['Message'] = 'Forbidden'
        forbidden_error = ClientError(self.error_response, 'HeadBucket')
        self.s3_client.head_bucket.side_effect = forbidden_error
        self.assertTrue(
            utils.s3_bucket_exists(self.s3_client, self.bucket_name)
        )


class TestClientCreationFromGlobals(unittest.TestCase):
    def setUp(self):
        self.fake_client = {}
        self.session = mock.Mock()
        self.session.create_client.return_value = self.fake_client
        self.parsed_globals = argparse.Namespace()
        self.parsed_globals.region = 'us-west-2'
        self.parsed_globals.endpoint_url = 'https://foo.bar.com'
        self.parsed_globals.verify_ssl = False

    def test_creates_clients_with_no_overrides(self):
        client = utils.create_client_from_parsed_globals(
            self.session, 'ec2', self.parsed_globals
        )
        self.assertEqual(self.fake_client, client)
        self.session.create_client.assert_called_once_with(
            'ec2',
            region_name='us-west-2',
            verify=False,
            endpoint_url='https://foo.bar.com',
        )

    def test_creates_clients_with_overrides(self):
        overrides = {
            'region_name': 'custom',
            'verify': True,
            'other_thing': 'more custom',
        }
        client = utils.create_client_from_parsed_globals(
            self.session, 'ec2', self.parsed_globals, overrides
        )
        self.assertEqual(self.fake_client, client)
        self.session.create_client.assert_called_once_with(
            'ec2',
            region_name='custom',
            verify=True,
            other_thing='more custom',
            endpoint_url='https://foo.bar.com',
        )

    def test_creates_clients_with_no_parsed_globals(self):
        client = utils.create_client_from_parsed_globals(
            self.session, 'ec2', argparse.Namespace()
        )
        self.assertEqual(self.fake_client, client)
        self.session.create_client.assert_called_once_with('ec2')


class MockPipedStdout(io.BytesIO):
    """Mocks `sys.stdout`.

    We can't use `TextIOWrapper` because calling
    `TextIOWrapper(.., encoding=None)` sets the ``encoding`` attribute to
    `UTF-8`. The attribute is also `readonly` in `TextIOWrapper` and
    `TextIOBase` so it cannot be overwritten in subclasses.
    """

    def __init__(self):
        self.encoding = None

        super(MockPipedStdout, self).__init__()

    def write(self, data):
        # sys.stdout.write() will default to encoding to ascii, when its
        # `encoding` is `None`.
        if self.encoding is None:
            data = data.encode('ascii')
        else:
            data = data.encode(self.encoding)
        super(MockPipedStdout, self).write(data)


class TestUniPrint(unittest.TestCase):
    def test_out_file_with_encoding_attribute(self):
        buf = io.BytesIO()
        out = io.TextIOWrapper(buf, encoding='utf-8')
        utils.uni_print('\u2713', out)
        self.assertEqual(buf.getvalue(), '\u2713'.encode())

    def test_encoding_with_encoding_none(self):
        '''When the output of the aws command is being piped,
        the `encoding` attribute of `sys.stdout` is `None`.'''
        out = MockPipedStdout()
        utils.uni_print('SomeChars\u2713\u2714OtherChars', out)
        self.assertEqual(out.getvalue(), b'SomeChars??OtherChars')

    def test_encoding_statement_fails_are_replaced(self):
        buf = io.BytesIO()
        out = io.TextIOWrapper(buf, encoding='ascii')
        utils.uni_print('SomeChars\u2713\u2714OtherChars', out)
        # We replace the characters that can't be encoded
        # with '?'.
        self.assertEqual(buf.getvalue(), b'SomeChars??OtherChars')


class TestGetPolicyARNSuffix(unittest.TestCase):
    def test_get_policy_arn_suffix(self):
        self.assertEqual(
            "aws-cn", utils.get_policy_arn_suffix("cn-northwest-1")
        )
        self.assertEqual(
            "aws-cn", utils.get_policy_arn_suffix("cn-northwest-2")
        )
        self.assertEqual("aws-cn", utils.get_policy_arn_suffix("cn-north-1"))
        self.assertEqual(
            "aws-us-gov", utils.get_policy_arn_suffix("us-gov-west-1")
        )
        self.assertEqual("aws", utils.get_policy_arn_suffix("ca-central-1"))
        self.assertEqual("aws", utils.get_policy_arn_suffix("us-east-1"))
        self.assertEqual("aws", utils.get_policy_arn_suffix("sa-east-1"))
        self.assertEqual("aws", utils.get_policy_arn_suffix("ap-south-1"))


class TestGetShapeDocOverview(unittest.TestCase):
    def assert_expected_shape_overview(self, shape_docs, expected_overview):
        shape = mock.Mock(Shape)
        shape.documentation = shape_docs
        actual_overview = utils.get_shape_doc_overview(shape)
        self.assertEqual(actual_overview, expected_overview)

    def test_get_shape_doc_overview(self):
        self.assert_expected_shape_overview(
            shape_docs='Shape documentation',
            expected_overview='Shape documentation.',
        )

    def test_uses_content_before_first_period(self):
        self.assert_expected_shape_overview(
            shape_docs='First sentence. Second Sentence.',
            expected_overview='First sentence.',
        )

    def test_uses_content_before_first_colon(self):
        self.assert_expected_shape_overview(
            shape_docs='<p>Broken XML docs',
            expected_overview='<p>Broken XML docs.',
        )

    def test_removes_xml_tags(self):
        self.assert_expected_shape_overview(
            shape_docs='<p>Shape documentation</p>',
            expected_overview='Shape documentation.',
        )

    def test_removes_nested_xml_tags(self):
        self.assert_expected_shape_overview(
            shape_docs='<p>Shape <code>documentation</code></p>',
            expected_overview='Shape documentation.',
        )

    def test_can_handle_broken_xml(self):
        self.assert_expected_shape_overview(
            shape_docs='<p>Broken XML docs',
            expected_overview='<p>Broken XML docs.',
        )

    def test_ignores_newlines(self):
        self.assert_expected_shape_overview(
            shape_docs='First line\nSecond line',
            expected_overview='First line Second line.',
        )

    def test_ignores_line_separator_char(self):
        self.assert_expected_shape_overview(
            shape_docs='First line\u2028Second line',
            expected_overview='First line Second line.',
        )