Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/functional/history/__init__.py
1567 views
1
# Copyright 2017 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
import uuid
14
15
from botocore.history import HistoryRecorder
16
17
from awscli.testutils import mock, create_clidriver, FileCreator
18
from awscli.testutils import BaseAWSCommandParamsTest
19
from awscli.compat import BytesIO
20
21
22
class BaseHistoryCommandParamsTest(BaseAWSCommandParamsTest):
23
def setUp(self):
24
history_recorder = self._make_clean_history_recorder()
25
super(BaseHistoryCommandParamsTest, self).setUp()
26
self.history_recorder = history_recorder
27
self.files = FileCreator()
28
config_contents = (
29
'[default]\n'
30
'cli_history = enabled'
31
)
32
self.environ['AWS_CONFIG_FILE'] = self.files.create_file(
33
'config', config_contents)
34
self.environ['AWS_CLI_HISTORY_FILE'] = self.files.create_file(
35
'history.db', '')
36
self.driver = create_clidriver()
37
# The run_cmd patches stdout with a StringIO object (similar to what
38
# nose does). Therefore it will run into issues when
39
# get_binary_stdout is called because it returns sys.stdout.buffer
40
# for Py3 and StringIO does not have a buffer
41
self.binary_stdout_patch = mock.patch(
42
'awscli.utils.get_binary_stdout')
43
mock_get_binary_stdout = self.binary_stdout_patch.start()
44
self.binary_stdout = BytesIO()
45
mock_get_binary_stdout.return_value = self.binary_stdout
46
47
def _make_clean_history_recorder(self):
48
# This is to ensure that for each new test run the CLI is using
49
# a brand new HistoryRecorder as this is global so previous test
50
# runs could have injected handlers onto it as all of the tests
51
# are ran in the same process.
52
history_recorder = HistoryRecorder()
53
54
# The HISTORY_RECORDER is instantiated on module import before we
55
# doing any patching which means we cannot simply patch
56
# botocore.get_global_history_recorder as the objects are already
57
# instantiated as so we have to individually patch each one of these...
58
self._apply_history_recorder_patch(
59
'awscli.clidriver', history_recorder)
60
self._apply_history_recorder_patch(
61
'awscli.customizations.history', history_recorder)
62
return history_recorder
63
64
def _apply_history_recorder_patch(self, module, history_recorder):
65
patch_history_recorder = mock.patch(
66
module + '.HISTORY_RECORDER', history_recorder)
67
patch_history_recorder.start()
68
self.addCleanup(patch_history_recorder.stop)
69
70
def _cleanup_db_connections(self):
71
# Reaching into private data to close out the database connection.
72
# Windows won't let us delete the tempdir until these connections are
73
# closed in the tearDown step and we have no other way of forcing
74
# them to close.
75
handlers = self.history_recorder._handlers
76
for handler in handlers:
77
handler._writer.close()
78
79
def tearDown(self):
80
super(BaseHistoryCommandParamsTest, self).tearDown()
81
self._cleanup_db_connections()
82
self.files.remove_all()
83
self.binary_stdout_patch.stop()
84
85