forked from aws/aws-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
99 lines (82 loc) · 3.77 KB
/
test_utils.py
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
# 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.
from awscli.testutils import unittest
from awscli.testutils import BaseAWSHelpOutputTest
import mock
from botocore.exceptions import ClientError
from awscli.customizations import utils
class FakeParsedArgs(object):
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 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(ValueError):
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(ValueError):
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))