Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/customizations/cloudtrail/test_utils.py
1569 views
1
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from awscli.customizations.cloudtrail import utils
14
from awscli.testutils import mock, unittest
15
16
17
class TestCloudTrailUtils(unittest.TestCase):
18
def test_gets_sts_account_id(self):
19
mock_sts_client = mock.Mock()
20
user_info = {'Account': '1234'}
21
mock_sts_client.get_caller_identity.return_value = user_info
22
account_id = utils.get_account_id(mock_sts_client)
23
self.assertEqual(account_id, '1234')
24
25
def test_gets_account_id_from_arn(self):
26
arn = 'foo:bar:baz:qux:1234'
27
self.assertEqual('1234', utils.get_account_id_from_arn(arn))
28
29
def test_gets_trail_by_arn(self):
30
cloudtrail_client = mock.Mock()
31
cloudtrail_client.describe_trails.return_value = {'trailList': [
32
{'TrailARN': 'a', 'Foo': 'Baz'},
33
{'TrailARN': 'b', 'Foo': 'Bar'}
34
]}
35
result = utils.get_trail_by_arn(cloudtrail_client, 'b')
36
self.assertEqual('Bar', result['Foo'])
37
38
def test_throws_when_unable_to_get_trail_by_arn(self):
39
cloudtrail_client = mock.Mock()
40
cloudtrail_client.describe_trails.return_value = {'trailList': []}
41
self.assertRaises(
42
ValueError, utils.get_trail_by_arn, cloudtrail_client, 'b')
43
44